IronPython

posted Tue, 15 Nov 2005 23:05:00 GMT by Jonas Bengtsson

I just finished watching IronPython: Python on the .NET Framework which is a demo of IronPython. I remember when it first was announced, one or two years ago, and thought it was neat but didn’t see the need when there already are CPython and Jython. But it’s nice to see who well it integrates with the .NET framework as well as the Python libraries (does it support C extended Python libraries as well?). You can use the whole .NET framework and even extend it in your Python script (which shouldn’t come as a surprise since it uses the CLR), and you can embed Python into “normal” .NET applications. You can even debug using Visual Studio, set breakpoints and introspect variables (without there being any IronPython support in Visual Studio, again thanks to the CLR).

IronPython is currently at a pre-alpha stage suitable for experimentation but not for serious development work.

Comments Two comments

Small scripts

posted Tue, 11 Oct 2005 21:24:00 GMT by Jonas Bengtsson

First I wrote a Perl stdin-parsing oneliner yesterday for summing up the output from grep -c:

perl -e "my($sum) = 0; while(<STDIN>) { $sum += $1 if $_ =~ /^[^:]+:(\d+)$/ } print $sum;"

And while the syntax is still a bit weird to me I’m starting to like Perl a bit. Did I just say that? I blame the Perlisms in Ruby! At least it solves the problems it’s designed for in a good way.

I also needed a little script to replace all .html files on my old blog with a “I’ve moved” notice. So I wrote the little Python script below (I added some error checking today to make it easier to use). It’s super simple and hence a little embarrassing to post, but perhaps someone will need it some day (at least it will be easy for me to find). And that I wrote it in Python is a bit embarassing as well since I need to start using Ruby for these kinds of tasks.

Use at your own risk!

"""Replaces all files in a directory (and its subdirectories) with one file.
Usage: replacefiles.py SOURCEFILE DESTINATIONDIRECTORY"""

import sys, os.path, shutil

def replace_files_with_file(sourceFile, destinationDirectory, dryrun):
   os.path.walk(destinationDirectory, replace_directory, (dryrun, sourceFile))

def replace_directory(arg, dirname, names):
   dryrun, sourceFile = arg
   for name in names:
      name = os.path.join(dirname, name)
      if os.path.isdir(name):
         continue
      if dryrun:
         print "copy %(sourceFile)s %(name)s" % vars()
      else:
         shutil.copyfile(sourceFile, name)

def exit_print_help(help):
   print help
   sys.exit(-1)

if __name__ == '__main__':
   if len(sys.argv) != 3:
      exit_print_help(__doc__)
   script, source, destination = sys.argv
   if not (os.path.isfile(source) and os.path.isdir(destination)):
      exit_print_help(__doc__)
   print "First a dry run. The following would happen when replacing all files in '%(destination)s' with '%(source)s':" % vars()
   replace_files_with_file(source, destination, True)
   if raw_input("Are you sure you want to do the actions listed above? [y/n] ")[0].lower() == 'y':
      replace_files_with_file(source, destination, False)
      print "Done!"

Comments Zero comments

Picking up the axe

posted Mon, 29 Aug 2005 00:42:00 GMT by Jonas Bengtsson

It’s been a while since I’ve been coding Ruby. I learned the basics back in 2002 but since I’m much more fluent in Python I very rarely use Ruby when I need something done. But this time I have the motivation of Rails and a newly bought Programming Ruby 2nd edition (a.k.a. PickAxe).

Now to some random incoherent things I’ve learned about Ruby recently.

The most important and interesting aspect of the language, IMHO, is blocks, and I think I grok it now and am able to know when to apply them.

There were two conventions that made Rails-code hard for me to understand before: symbols and e.g. belongs_to. Kevin Clark taught me symbols and this piece of code describes how belongs_to et al. work:

def tinker_with_class
  class_eval do
    def hello
      puts "Hello world!"
    end
  end
end

class Hello
  tinker_with_class
end

h = Hello.new
h.hello

Output: Hello world!

One thing that I wasn’t aware of before was that Range supports custom classes (implementing the succ and <=> methods), which is neat and something that Python lacks.

Mixins seems to be a really good means of reuse, but I don’t know that much about them for a couple of chapters.

Private methods and variables can only be accessed by the own object, i.e. you can’t access private methods and variables of objects of the same class (as you can in Python, C++, Java and most other languages).

Many of the Perlisms seems to be less preferred now than the first edition of the PickAxe, which is a good thing. Who wants to remember the difference between $!, $&, $., $_, $~ etc?

The main gripe I have is that irb doesn’t work that well with Swedish keyboard layout on Windows, which is a shame since interactive Python was instrumental in my learning of Python.

Anyhow, I like most aspects of Ruby and I hope it will become as natural to me as Python.

Comments Zero comments

Daily links poster

posted Tue, 14 Dec 2004 22:38:00 GMT by Jonas Bengtsson

In my previous post you can see the result of my recent lack of sleep. It is something vaguely (understatement alert) inspired by Tesugen.

It is a Python script that gets the latest links I’ve posted to del.icio.us, and then post all the new links to this blog using the Atom API.

At first I thought I should just post the links that I’ve added during the same day, but that wouldn’t work since I don’t have any server that is running all the time. In some cases I might not be able to run the script for a couple of days. So now I keep a local history of which links I’ve blogged, and only blog unblogged links.

It was yesterday I started working on it. I was really tired at work, and my head was aching, so my plan was to go to bed early. Somehow I ended up starting with this and all of a sudden it was 4.30 a.m (and I had to be at work 10 a.m. today). I really enjoy hacking away for fun once in a while.

The main problem was to get the posting to Blogger to work. I used this example as a starting point, but there was something wrong with the login. After remembering reading about Blogger changing the authorization model, I tried using HTTP Basic Authorization over HTTPS. After a while I tried with another password, and it worked, go figure. So I spent a couple of hours trying to fix a problem that wasn’t there. That proves that it was a wise choice to go home early from work. But it was quite interesting learning a bit about WSSE authorization. Getting del.icio.us to work was really easy as soon as I got libxml2 installed and found this example.

There might be some changes to the format of my link blog posts…

Comments Zero comments

Modulus

posted Wed, 10 Nov 2004 22:59:00 GMT by Jonas Bengtsson

In Python I get this:
>>> 2 % 5
2
>>> -2 % 5
3
>>> 2 % -5
-3
>>> -2 % -5
-2
But in Java I get this:
System.out.println(2%5);  //2
System.out.println(-2%5);  //-2
System.out.println(2%-5);  //2
System.out.println(-2%-5);  //-2
Who’s right I don’t know. I guess that you might say that the way Java does it is more correct since no division is taking place. But the way I wanted it to work today at work (coding C++) is the Python way.

Comments Zero comments