Posts

Showing posts with the label programming

Getting the command line arguments in Dart (vm)

Another hidden gem in the Dart library today, can you tell I am trying to write a command line thingy? Reading the command line options is really easy. void main() { Options opts = new Options(); for (String arg in opts.arguments) { print(arg); } } Let us play: $ dart opts.dart a a $ dart opts.dart a b c d a b c d Nothing to add, I think, except check the source . Comments on Google+ .

OAuth2.0 with Dart in a web browser

Image
I was trying to access a Google GData API using Dart in a browser. More for fun than profit, but it posed a few challenges, so I thought I should document them. The first step was to Authorize using OAuth2.0 which is the easiest way of Authorizing an application to use Google APIs. This is pretty trivial using server-side Dart, but I wanted to achieve the entire thing inside a web browser, and so this involves a different OAuth2.0 flow to the web-server flow that I am used to using, in this case the OAuth2.0 Client-side Application Flow . The main difference with the web-server flow is that the entire OAuth2.0 dance occurs in a single step, i.e., there is no step to exchange a code for a token. This single step is performed by asking for a token in the initial redirect step with a response_type parameter of token . Additionally, the client secret is not passed at any time, because this would not be safe information for the client application to give up to a web browser (sin...

Running a subprocess with Dart and reading its output

Starting a foreign program, and reading its standard output stream is useful, and here is an example of listing the root directory. void main() { Process p = new Process('/bin/ls', ['/']); p.start(); StringInputStream stdoutStream = new StringInputStream(p.stdout); print(stdoutStream.read()); p.close(); } Notes: Argument list does not include the executable name like it usually does Use a StringInputStream to read an InputStream as a String Close the process or the script will hang forever Comments on Google+ .

Reading a file as a String in Dart

It took me a minute to work out how to read a file in Dart (vm), so here it is for posterity: void main() { File f = new File('my_filename.txt'); FileInputStream fileStream = f.openInputStream(); StringInputStream stringStream = new StringInputStream(fileStream); print(stringStream.read()); fileStream.close(); } Notes: A StringInputStream reads strings from an InputStream A FileInputStream is an implementor of InputStream that is opened from files Remember to close the FileInputStream Comments on Google+

Things I love about Dart (so far)

I've only been using Dart  for a few hours in total, but already there are things to love about it. Forget the obvious "It's not JavaScript", because I don't hate JS, and if I did hate JS it would be because of the lack of decent structure (who decided prototypes were nice to use?). Certainly, I don't hate dynamically typed languages, and in fact, I quite like them, and have built my career on them. This article is about small things, I'll leave that huge stuff for the clever people. 1. I already know Dart without having learned a single thing: Ever used Java or C#? Dart is essentially  the same as those familiar languages that we learned at school. A class is defined like: class Banana {   String getColor() {       return "yellow";   } } See, this could be any language you already know, and to illustrate that nicely the syntax highlighter treats it as Java, and no one really cares. 2. Class members are public by default: If you define a...

Custom PyGTK Widgets in Glade3: Part 2: Custom widget adaptors

In the last post about custom glade widgets, we briefly discussed how to add your PyGTK custom widgets to glade-3 so that they can be used in a user interface designer. In this post, this shall be extended to include how you can create a custom adaptor for a PyGTK widget to define additional behaviour. We shall be using a custom widget as an example, which I have called a "Service View". We plan that this widget has a content section, which is the main part, and also contains a close button at the bottom. We will use this widget as a general dockable view, kind of like a dialog which is a widget rather than a top-level window. This may seem utterly pointless, but it should be a useful component in an application that generates many different views in notebooks, like an IDE: Debugger, Terminal, Documentation Browser (etc). These can all share the same basic layout, but just replace the single main part of the widget. First let's write a widget: import gtk class ServiceView...

Custom PyGTK Widgets in Glade3

Glade 3, GTK User Interface Designer is really quite nice since release 3.1, since it does away with the gimp-like multi-window view and looks like a normal application. Another nice feature it has is being able to support widgets created in non-C (in our case PyGTK). Components of a plugin: A catalog file A support module Some icon pixmaps (optional) We shall use, for our example, the Kiwi Hyperlink widget, (which I wrote). This widget is an EventBox subclass, so we shall illustrate turning off some of the additional properties that are not required in the user interface designer. The catalog file This is described in http://glade.gnome.org/docs/catalogintro.html and the subsequent pages of the documentation. Essentially there are two sections of the catalog: The Widget definitions The Catalog list The Widget Definitions are like so: <glade-widget-classes> <glade-widget-class title="Hyper Link" name="HyperLink" /> </glade-widget-classes> These ar...

Posting to Blogger.com from the command line

Well, you can tell I am procrastinating working today (but you can forgive me after the PIDA GSoc Application was rejected as a mentoring organisation), but I have hacked up an early version of a library (for Python) to manage blogger.com posting. I was becoming slightly upset with Blogger.com, since it does some insane things like puts BR tags in the middle of PRE elements, which meant the syntax highlighting script I wanted to use was going nuts. I also gradually realised that none of the blogging clients work with the "new Blogger.com" although for me (who has only been blogging a few months) it has been the "only Blogger.com". So, with a quick search around for documentation from code.google.com, I was able to discover that Blogger.com uses the google GData API with a bit of Atom mixed in. I have no real ideas about these specifications, but http://code.google.com/apis/blogger/gdata.html explains it. Note that another document I found: http://code.blogspot.co...

Using pexpect to control Django manage.py

In the early stages of developing a Django application, I was deleting my sqlite database, and rerunning: python manage.py syncdb Every few minutes as the database models were changing. You might consider this some kind of evil thing to do, because we should all have our database models in concrete before writing any code but I am not that organised. One thing that was really distressing me was the fact that each time I ran a syncdb, I had to enter details for my default admin user. Username Email address Password Repeart Password This was fine for the first 400 times, then I got bored. The solution was to use pexpect. (There are probably command line options that you can give manage.py, but finding them out would have been more effort than this hack) Pexpect, http://pexpect.sourceforge.net/ is a pure python module for running external commands and controlling them as if you were a user. It has good documentation, and is perfect for needs of this nature. A quick browse through the exc...

Unit Testing PyGTK

Unit testing a GUI has always been something that scares me, and scares other people too. Because of this there are about a gazillion tools that behave in different ways, for example in Python, just have a look at: http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy#GUITestingTools (Since the whole world is obsessed with Web Applications, and this is becoming perversely true even in Python circles, some of these GUI Testing tools are actually web testing tools, but never mind that.) How do I do it? Well, we may talk in another blog about one of those tools, the awesome hack that is kiwi.ui.test but it is slightly restrictive on what you want to do, and how you should do it. Specifically, it needs you to have every widget named (which is fine when testing Glade-created interfaces, but a pain when hand-building them). Enter this one function that I found in the kiwi source. Kiwi is LGPL (whatever that means, but you should read the license if you are going to use it). import time i...

PIDA 0.4.0 Released

After a slightly quiet spell (also known as an extended beta period) we have released PIDA 0.4.0. What kind of open source author would I be if I didn't push my own software on my blog! My favourite feature on this release is built in pyflakes. When you save a Python file it runs Pyflakes on it for you and prettifies the output into a GUI list for you to browse through, click on, and jump to the line number. All in Vim, as usual. Please try it, at http://pida.berlios.de/

Oracle from Python in Linux

Well, pretending to know everything can have its downsides. Like when the boss says "Could you hack up a script to enter custom data into our 3rd party invoicing software?". One choice is to admit that you would not know where to start. The other would be to attempt to muddle through. "Of course, let's have a look". So an hour later we have discovered that the software in question uses an Oracle back-end: Excellent, this is going to be easy, Oracle is an relational database (I read once somewhere) and we learned SQL at university, so it can't be that hard. So, step one: boot into windows, and use the command line client to do some stuff. Great! The SQL is coming back to me already. But this is no good. I can't use windows! The single desktop is beginning to upset me, and well we need it running on Linux so I can feel leeter than my coworkers. So what will we be needing? 1. SQLPLUS for Linux (this the the command line client for Oracle). Should be easy en...