Re.Mark

My Life As A Blog

Archive for the ‘Tools’ Category

IronPython Console App

leave a comment »

Having installed the CTP of IronPython tooling for Visual Studio, I thought I’d better try it out and write a simple app.  A Console Application seemed like a good choice – and I haven’t done much with getting data over the internet in Python, so I combined the two.  Here’s the resulting simple (and, in its current form, not especially useful) application:


import urllib2
from System import Console

print "Starting..."
data = urllib2.urlopen("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml").read()
print data

Console.WriteLine()
Console.ReadKey()
Advertisements

Written by remark

May 28, 2010 at 4:33 pm

Posted in .NET, Python, Tools

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

Running Python from C#

with one comment

Having posted a number of times about Ruby over the last few months, I thought I should also write a bit about Python.  The first thing I decided to try and do was to run some IronPython from C#.  I downloaded version 2.0 beta 5, which includes the latest version of the DLR.  I created a Console Application and added references to Microsoft.Scripting.dll and Microsoft.Scripting.Core.dll – the assemblies that constitute the DLR.  I also added a reference to IronPython.dll and IronPython.Modules.dll.  I decided to try to output some text, set a variable in IronPython and retrieve the value in C#.  Here’s the code:

ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
setup.LanguageSetups.Add(IronPython.Hosting.Python.CreateLanguageSetup(null));
ScriptRuntime runtime = new ScriptRuntime(setup);
runtime.IO.RedirectToConsole();
ScriptEngine engine = runtime.GetEngine("IronPython");
ScriptScope scope = engine.CreateScope();
ScriptSource source = engine.CreateScriptSourceFromString("print 'Hello World'\r\na=1", SourceCodeKind.Statements);
source.Execute(scope);
Console.WriteLine(scope.GetVariable("a"));

I’m impressed that C# and IronPython can interact like this.  The possibilities for extending applications with runtime dynamic code are intriguing  – worth looking into.

Written by remark

September 23, 2008 at 10:04 pm

Posted in .NET, c#, Python, Software, Tools

Ruby Tuesday #16 Part 2 : IronRuby news

leave a comment »

At OSCON last week, John Lam made a number of announcements about IronRuby (and speaking of OSCON, you should check out the announcements Sam Ramji made, too.)  In summary:

There’s a binary release of IronRuby, which includes the standard Ruby libraries.

An ironruby-contrib project has been established – another way to get involved in the IronRuby community.

A set of changes to RubySpec have been submitted.

The full blog post on the IronRuby announcements is here.  Well worth reading in full.

Written by remark

July 29, 2008 at 5:45 pm

Ruby Tuesday #12 : Twitter on Shoes

with one comment

Last week I hoped I would have finished my Twitter client in Shoes by now.  Unfortunately, I ran into some issues with Shoes (such as redraws where every control ends up superimposed on top of itself but pushed out to the left and down.)  The most serious of these issues is its frequent reluctance to allow me to enter text into text boxes (or edit_lines in shoe parlance.)  I’ve found that if I click off the Shoes window and back in, it will let me enter text and click buttons.  Not ideal, but it seems to work – I even managed to tweet using my new client.  So, without further ado, here is the code:

require 'Twitter'

Shoes.app :title => "Twitter Shoes", :height => 750, :width => 750 do
    flow :margin => 10 do
         stack :width => 700, :height => 90, :top => 10, :left => 10 do
            flow :margin => 5 do
                para "Username"
                @username = edit_line :height => 30, :width => 120, :left => 100
            end
            flow :margin => 5 do

            end
            flow :margin => 5 do
                para "Password"
                @password = edit_line :secret => true, :height => 30, :width => 120, :left => 100
            end
        end
        stack :height => 90, :top => 100, :left =>10, :width => 700 do
            flow :margin => 5 do
                para "Status", :top => 10
                @status = edit_line :height => 30, :width => 270, :left => 100, :top => 10
                button "Update", :height => 30, :width => 100, :left => 400, :top => 10 do
                    username = @username.text
                    password = @password.text
                    status = @status.text
                    if username.empty?
                        alert "You must enter a username."
                    elsif password.empty?
                        alert "You must enter a password."
                    elsif status.empty?
                        alert "You must provide a status."
                    else
                        client.update(status, username, password)
                    end
                end
                button "Update Timeline", :height => 30, :width => 120, :left => 550, :top => 10 do
                    username = @username.text
                    password = @password.text
                    if username.empty?
                        alert "You must enter a username."
                    elsif password.empty?
                        alert "You must enter a password."
                    else
                        timeline = ""
                        translator.xml_to_tweets(client.download_friends_timeline(username, password)).each do |tweet|
                            timeline << "#{tweet.user.screen_name} says #{tweet.text}\n\n"
                        end
                        @tweets.replace timeline
                    end
                end
            end
        end
        stack :height => 550, :top => 200, :width => 700, :margin => 10, :left => 10 do
            background "#eee"
            @tweets = para ""
        end
    end
end

You can also download it here.  Most of the changes are in the layout.  I started the layout from scratch.  I’d suggest reading the manual that you can find here.  It’s not exactly comprehensive, but it points you in the right direction.  If you’re like me, you’ll find that changing one value has unexpected and seemingly unconnected consequences.  Still, by changing one thing at a time and sticking to a very basic design I got there.

So, you can tweet from Shoes.  Shoes uses Ruby and has some intriguing ideas (like the HTMLish layout concepts), but there’s a lot to be said for a good visual designer with drag and drop.   Gets the same job done more quickly.  Add the ability to edit by hand and we’re all happy.  I’m still in search of my ideal Twitter client, though…

Written by remark

July 1, 2008 at 6:34 pm

Ruby Tuesday #11 Update : First Steps with Shoes

with one comment

When I left my last post, I had been encountering some crashes with Shoes.  So, I took the Ruby code I’d written, installed Shoes on a Mac (running Leopard) and, hey presto, it worked.  I haven’t tested another Vista PC to see if it’s Shoes or the PC I was using.  Here’s the code:

require 'Twitter'

translator = Translator.new
client = Client.new

Shoes.app :title => "Twitter Shoes", :height => 750, :width => 750 do
  background rgb(240, 250, 208 )
  stack :width => 700, :height => 1.0, :margin => 20 do
    background "#eee", :radius => 12
    border "#00D0FF", :strokewidth => 3, :radius => 12
    stack :margin => 20 do
      button "Get Timeline" do
        alert("Username is {#@username.text} and password is {#@password.text}")
        timeline = ""
        translator.xml_to_tweets(client.download_friends_timeline(@username.text, @password.text)).each do |tweet|
        timeline << "#{tweet.user.screen_name} says #{tweet.text}\n\n"
      end
      @label.replace timeline
      #alert "done"
    end
  end
  stack :height => 550, :margin => 20 do
    background "#eee"
    @label = para ""
  end
  stack :margin =>20 do
    @username = edit_line :height =>50, :width => 100
    @password = edit_line :secret => true, :height =>50, :width => 100
   end
 end
end

So, what needs to be done next now is to provide a way to tweet from the Shoes form.  And it could do with some tidying up.

To help if you’re following this series, I’ve posted the source files for Twitter.rb and App.rb.

Oh, and there’s a space between 208 and the closing bracket in the line beginning background because if I don’t put the space in, WordPress converts the sequence 8 followed by a bracket to a smiley face wearing sunglasses.  Like this 8) .  Which looks a bit weird in code.

Written by remark

June 24, 2008 at 10:11 pm

Ruby Tuesday #11 : First Steps with Shoes

leave a comment »

Last week, I decided that Shoes was the toolkit I’d use to build a GUI for the Twitter client I’ve built in Ruby. So, I downloaded Shoes and installed it on a machine running Vista. There’s a bunch of samples that come with Shoes – to run the Timer, for example, type Shoes samples\timer.rb.

Before I got any further, I checked my feedreader and noticed that Scott Hanselman had blogged about Shoes – and said that it’d be a grat idea to build a Twitter client in Shoes. Timing is everything. Let’s put our shoes on.

I like to start simple. In fact I pretty much like to end simple, too. Anyway, I thought I’d start out with a button that downloads the public timeline when you press it. Here’s the code:

require 'Twitter'

translator = Translator.new
client = Client.new 

Shoes.app :height => 150, :width => 250 do
   background rgb(240, 250, 208 )
   stack :margin => 10 do
     button "Get Public Timeline" do
       timeline = ""
       translator.xml_to_tweets(client.download_public_timeline).each do |tweet|
         timeline << "#{tweet.user.screen_name} says #{tweet.text\n"
       end
       @label.replace timeline
     end
     @label = para ""
   end
end

OK, a couple of confessions. The first is that it looks pretty awful. The second is that on my machine the require was causing an exception in Shoes, so I replaced it with the code from Twitter.rb. I’ve shown the code here with require because it should work and it’s much simpler to read.

Let’s see if we can tidy that up a bit. There’s a couple of issues. There’s no scrollbar for the tweets and all the tweets run into each other. By putting stacks inside a containing stack, we should be able to sort that out. Here goes:

require 'Twitter'

translator = Translator.new
client = Client.new

Shoes.app :title => "Twitter Shoes", :height => 750, :width => 750 do
   background rgb(240, 250, 208 )   stack :width => 700, :margin => 20 do
     background "#eee", :radius => 12
     border "#00D0FF", :strokewidth => 3, :radius => 12
     stack :margin => 20 do
       button "Get Public Timeline" do
         timeline = ""
         translator.xml_to_tweets(client.download_public_timeline).each do |tweet|
           timeline << "#{tweet.user.screen_name} says #{tweet.text\n\n"
         end
         @label.replace timeline
       end
     end
     stack :height => 650, :margin => 20 do
        background "#eee"
        @label = para ""
     end
   end
end

I borrowed the colours and border from the jot example in the samples that come with Shoes. I think that looks much better. What would be even better is if it downloaded your friends timeline (i.e. you and those you follow.) I put some text boxes (edit_box) in to enter the username and password and even figured out how to mask the password (:secret => true) but started to have some issues with Shoes crashing. So, next week’s task will be to see if I can overcome the crashing and get a more functional client.

Written by remark

June 24, 2008 at 8:10 pm