Coding

You are currently browsing the archive for the Coding category.

I got some strange error today when I tried to deploy one of my rails projects to my slice at slicehost. The problem was that I had a newer version of Hpricot running on the slice (0.6.161) versus 0.6 on my local machine. The following line failed:

Hpricot::XChar::PREDEFINED_U.merge!({" " => 32})

Ahm, okay. I didn’t see any changes made to the overall layout of Hpricot so I suspect that line should work. Any ideas?

(If anyone is interested in the meaning of the above line, check this ticket).

A few days ago I experimented with some data files from the Open Street Map Project. Because I do most of my quick’n'dirty prototyping with Python, I looked for a Python module which would read osm files (which is, to be fair, really no problem because it’s clean XML).

I stumbled over Rory McCann’s python-osm lib over at Github. His version was missing the relation feature of the osm data structure but it was a one minute walk to implement it.

You can access the forked project @Github.

I’m currently working on some Rails project which makes use of the great attachment_fu plugin by Technoweenie (which led to my first bugfix for an Open Source project).

Yesterday I stumbled over some glitch with Lightbox (or Script.aculo.us, I’m not really sure) which results in a mal-sized picture. For the sample, go here. The fix is quite simple, you have to explicitly set the width of the image (that should be around line #260):

imgPreloader.onload = (function(){
    this.lightboxImage.src = this.imageArray[this.activeImage][0];
    // The following line will fix the glitch:
    this.lightboxImage.width = imgPreloader.width;
    this.resizeImageContainer(imgPreloader.width, imgPreloader.height);
}).bind(this);
imgPreloader.src = this.imageArray[this.activeImage][0];

Wow, nearly three weeks since my last blog post (but I do post some updates more frequently on Twitter).

[Warning: tech stuff follows] Anyway, I’m doing quite fine. My bachelor project is slowly taking shape. Currently I am modifying SharpMap for our needs (adding a dynamic layer for our data, adding some events to know what’s going on inside of SharpMap,…).

I’ve also touched the clipping function, which really sucked. I replaced it with Sutherland-Hodgman’s polygon-clipping algorithm which works like a charm. This move was necessary because GDI+ has some problems rendering invalid data (meaning data which is far outside of the viewport). The process doing the drawing will block all others and your system will come to a halt - no moving cursors, no task manager, no nothing.

Because SharpMap is released under the LGPL, I will post the .diff patches as soon as the major work is finished.

Ruby sum

def sum(enum, &block)
  sum = 0
  enum.each do |value|
    v = block.call(value)
    sum = v ? v + sum : sum
  end
  sum
end

I played around a little bit with script.aculo.us the last few days and came up with some small Flickr photo browser called Folaroids.

It’s based on Ruby and JavaScript (Prototype and script.aculo.us). It’s the first time that I’ve done some real word application with erb and script.aculo.us (whose effects and support is just awesome).

Check it out (and leave comments).

Love? Hate? Whatever…

// This won't work:
Process proc = new Process();
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
 
proc.StartInfo.Filename = System.IO.Path.Combine(path, "myApp.exe");
proc.Start();
proc.WaitForExit(); // <-- Will crash here
 
// This does work:
Process proc = new Process();
 
proc.StartInfo.Filename = "myApp.exe";
proc.Start();
proc.WaitForExit();

Both samples will start myApp.exe just fine, but the first one seems to have some problems with Windows. Cost me four precious hours of my life.

btw, can anyone tell me why “new” in the code snippet above links to a google search for new and msdn.microsoft.com? No? That figures.

Py-ISO8211

Does anybody know if the iso8211 library for Python have been updated to work with Python 2.5? I got them working rudimentary but I still found some issues.

The last few days I had some time to create a lifestream, as you can find some here and there.

It was actually pretty simple thanks to Mark Pilgrim’s Universal Feed Parser. I simply stuffed all my personal feeds into a dict, including the source description for different styles, text,…; did the feed parsing, collected all entries, sorted them and wrote them to an html file (I was a bit shy about an online solution due to some speed, performance and privacy concerns - at least not everybody can see my virtual-personal actions for every second of my life).

All in all the hardest part was getting that damn thing running on Dreamhost via a cronjob (mainly because I’m using my own version of Python, own modules,…).

There’s an article at codeproject.com which transform a .NET DataSet to an XML Spreadsheet document which can be opened in Microsoft Excel. Unfortunately the code doesn’t seem to be generate valid XML, at least not for Excel. I’ve modified it but didn’t use some parts of if (e.g. I don’t use the time formatting at the moment so I stripped out all the styles using it,…).

Here’s the new code:

[code lang="csharp"] ///

/// Exports data in dataset to excel. /// Original code: http://www.codeproject.com/dotnet/exporttoexcel.asp /// public class excelexport { public static void exporttoexcel(dataset source, string filename) {

system.io.streamwriter exceldoc;

exceldoc = new system.io.streamwriter(filename);
const string startexcelxml = 
  "<?xml version=\"1.0\"?>\r\n" +
  "<?mso-application progid=\"excel.sheet\"?>\r\n" +
  "<workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +
  " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n" +
  " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\n" +
  " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">\r\n" +
  " <styles>\r\n" +
  "  <style ss:id=\"default\" ss:name=\"normal\">\r\n" +
  "   <alignment ss:vertical=\"bottom\"/>\r\n" +
  "   <borders/>\r\n" +
  "   <font/>\r\n" +
  "   <interior/>\r\n" +
  "   <numberformat/>\r\n" +
  "   <protection/>\r\n" +
  "  </style>\r\n" +
  "  <style ss:id=\"bold\">\r\n" +
  "   <font x:family=\"swiss\" ss:bold=\"1\"/>\r\n" + 
  "  </style>\r\n" + 
  " </styles>\r\n";
const string endexcelxml = "</workbook>";

int rowcount = 0;
int sheetcount = 1;

exceldoc.write(startexcelxml);
exceldoc.write("<worksheet ss:name=\"sheet" + sheetcount + "\">\r\n");
exceldoc.write("<table>\r\n");

for (int x = 0; x < source.tables[0].columns.count; x++)
{
  int width = source.tables[0].columns[x].columnname.length * 8;
  exceldoc.write("<column ss:width=\"" + width + "\"/>\r\n");
}

exceldoc.write("<row ss:styleid=\"bold\">\r\n");

for (int x = 0; x < source.tables[0].columns.count; x++)
{
  exceldoc.write("<cell><data ss:type=\"string\">");
  exceldoc.write(source.tables[0].columns[x].columnname);
  exceldoc.write("</data></cell>\r\n");
}

exceldoc.write("</row>\r\n");

foreach (datarow x in source.tables[0].rows)
{
  rowcount++;
  //if the number of rows is > 64000 create a new page to continue output
  if (rowcount == 64000)
  {
    rowcount = 0;
    sheetcount++;
    exceldoc.write("</table>\r\n");
    exceldoc.write(" </worksheet>\r\n");
    exceldoc.write("<worksheet ss:name=\"sheet" + sheetcount + "\">\r\n");
    exceldoc.write("<table>\r\n");
  }

  exceldoc.write("<row>\r\n"); //id=" + rowcount + "

  for (int y = 0; y < source.tables[0].columns.count; y++)
  {
    system.type rowtype;
    rowtype = x[y].gettype();
    switch (rowtype.tostring())
    {
      case "system.string":
        string xmlstring = x[y].tostring();
        xmlstring = xmlstring.trim();
        xmlstring = xmlstring.replace("&", "&amp;");
        xmlstring = xmlstring.replace(">", "&gt;");
        xmlstring = xmlstring.replace("<", "&lt;");
        exceldoc.write("<cell><data ss:type=\"string\">");
        exceldoc.write(xmlstring);
        exceldoc.write("</data></cell>\r\n");
        break;
      case "system.datetime":
        //excel has a specific date format of yyyy-mm-dd followed by  
        //the letter 't' then hh:mm:sss.lll example 2005-01-31t24:01:21.000
        //the following code puts the date stored in xmldate 
        //to the format above
        datetime xmldate = (datetime)x[y];
        string xmldatetostring = ""; //excel converted date
        xmldatetostring = xmldate.year.tostring() +
          "-" +
          (xmldate.month < 10 ? "0" +
          xmldate.month.tostring() : xmldate.month.tostring()) +
          "-" +
          (xmldate.day < 10 ? "0" +
          xmldate.day.tostring() : xmldate.day.tostring()) +
          "t" +
          (xmldate.hour < 10 ? "0" +
          xmldate.hour.tostring() : xmldate.hour.tostring()) +
          ":" +
          (xmldate.minute < 10 ? "0" +
          xmldate.minute.tostring() : xmldate.minute.tostring()) +
          ":" +
          (xmldate.second < 10 ? "0" +
          xmldate.second.tostring() : xmldate.second.tostring()) +
          ".000";

        exceldoc.write("<cell><data ss:type=\"datetime\">");
        exceldoc.write(xmldatetostring);
        exceldoc.write("</data></cell>\r\n");
        break;
      case "system.boolean":
        exceldoc.write("<cell><data ss:type=\"string\">");
        exceldoc.write(x[y].tostring());
        exceldoc.write("</data></cell>\r\n");
        break;
      case "system.int16":
      case "system.int32":
      case "system.int64":
      case "system.byte":
        exceldoc.write("<cell><data ss:type=\"number\">");
        exceldoc.write(x[y].tostring());
        exceldoc.write("</data></cell>\r\n");
        break;
      case "system.decimal":
      case "system.double":
        exceldoc.write("<cell><data ss:type=\"number\">");
        exceldoc.write(x[y].tostring());
        exceldoc.write("</data></cell>\r\n");
        break;
      case "system.dbnull":
        exceldoc.write("<cell><data ss:type=\"string\">");
        exceldoc.write("");
        exceldoc.write("</data></cell>\r\n");
        break;
      default:
        xmlstring = x[y].tostring();
        xmlstring = xmlstring.trim();
        xmlstring = xmlstring.replace("&", "&amp;");
        xmlstring = xmlstring.replace(">", "&gt;");
        xmlstring = xmlstring.replace("<", "&lt;");
        xmlstring = xmlstring.replace("\r\n", " - ");
        exceldoc.write("<cell><data ss:type=\"string\">\r\n");
        exceldoc.write(xmlstring);
        exceldoc.write("</data></cell>\r\n");
        break;
      //throw (new exception(rowtype.tostring() + " not handled."));
      }
    }
  exceldoc.write("</row>\r\n");
}

exceldoc.write("</table>\r\n");
exceldoc.write(" </worksheet>\r\n");
exceldoc.write(endexcelxml);
exceldoc.close();

} } [/code]

« Older entries