Skip to main content

Posts

Showing posts from 2009

Some thoughts on redesigning education

Some time back I read a blog post on redesigning education. It asked some very good questions. Stuff which I had been thinking of myself. I left my thoughts on the blog, but I would also like to start a conversation around these ideas with those who read this blog as well. I would like to know what other people think of the issue of redesigning (college) education. I have often thought about how college education can be improved. To answer this question, we first have to ask a very basic question. What is the purpose of education? To me, we need education for 3 things: To learn more about the world around us To lead positive constructive lives To earn a good living / fulfill our ambitions I think education has to a large extent evolved to fulfill #3 (with a bias towards earning a comfortable living). The semester system, along with multiple choice tests, and grading, has made our education system into an assembly line. Students are pushed into the assembly line, given classes, adminis

Fixing a mistake after commiting in Git

So, I ran into an interesting issue while coding today. I made a lot of changes and committed code to my Git repository. Just after committing, I realized that I still had to make some changes to a file and those changes should also have gone in with the previous commit. Fixing a commit mistake can be done in two ways. Revert the commit, make the changes and then recommit them. This is the preferred way if we have already made the changes public (and thus someone may already have pulled them). However, in my case I am the only one working on this project right now and I had not pushed the changes to GitHub, so I could try the other method. So here's what I did. I made changes to the file, added it to the index, and committed it thus: $git commit --amend Committing code with the --amend switch will cause the index to be committed as part of the previous commit. Git also brings up the editor window, in case we want to change the commit message. This method can also be used to change

Lack of design patterns in Python

While searching for PyCon videos, I came across Joe Gregorio's very good video on (lack of) design patterns in Python. I have also added the video timeline along with some notes I made for myself and my takeaway. Enjoy the video. Timeline: [00:00] - Start [00:15] - People pick tools based on a mythology and not necessarily facts [02:35] - Python isn't just Java without the compiler [03:34] - Design patterns are also a sign of weakness in a language [04:06] - Lack of design patterns in Python (proof of lack) [06:10] - Patterns are built into Python [07:00] - Strategy pattern in Python the wrong and right way [07:36] - The strategy pattern is invisible in languages with first-class functions [08:07] - Some other language features in Python (first class functions, metaprogramming, iterators, closures) [09:17] - The iterator pattern (iterators) is also built into Python [09:36] - The observer pattern is also built into Python [10:17] - Factory method pattern in Python ( [10:34]

iAccelerator

iAccelerator I took a sabbatical from my consulting work this summer to participate in the iAccelerator program. This is a program for early stage software startups, similar to YCombinator , but the first of it's kind (I believe) in India. I enjoy teaching programming, I have taught programming classes at a college , done corporate workshops, and more recently have been working on a website for participatory learning . This time I was looking for a different kind of mentoring experience, something which would be free form and fun. After a discussion with Freeman , I decided to go spend the summer at iAccelerator. In the first few days of the program we had introductions by all the teams where they spoke about their product and vision. This was followed by a session on team building and thinking out of the box, a few legal sessions, sessions on accounting and company law, and several other mentoring sessions. All this is very useful for early stage software stratups, especially whe

Custom JSON Encoder in Django

I have been messing around with how to display date formats (in questions and answers) for my web based learning site from the past couple days. After trying various things, I settled for what seems to be a web 2.0 standard for displaying dates - displaying dates as '2 days 4 hours ago' instead of the actual date and time '4th August, 2009 5:50 PM' . I like this because I do not have to deal with any browser localization issues. Everything is a delta between the UTC time a question was asked and the current UTC time (btw if you are working with datetime in Python, do read this blog post ). In my Django based application, I use JQuery to get questions and answers for forums on every topic page. The application sends back JSON to the Javascript functions which display the questions and answers from the JSON objects. The DjangoJSONEncoder (scroll to the end of this module) provided by Django serializes dates in a specific format. I wanted to change this so that a d

Git supports commiting specific parts of a file

While working on my web based learning project, I made a lot of changes to a Django view file without committing them. When I actually decided to commit, I realized that the changes in my view file should go into separate commits. I knew it was possible to commit parts of a file in Git, but I was not sure exactly how it could be done. I asked the question on StackOverflow and got an answer within minutes :-) Anyways here's how I did it. I had 3 files in my index: urls.py app/courses/templates/course/show.html app/courses/view.py The first 2 files were fine, but I did not want to commit the entire view file with the first two, I wanted only one change in that file to go in this commit. Since I had already added the view file to the index, I first had to unstage it with: git reset app/courses/view.py Then I did an interactive add to add only one hunk from the view file: git add --patch apps/courses/view.py Running this command showed me the first hunk from the file and an option ask

JQuery selectors and future elements

I am using JQuery for the AJAX and dynamic aspects of my web based learning platform. I like the fact that JQuery allows us to use CSS type selectors to select elements on the page to either manipulate them, add event handlers, or a host of other things. So, the code below intercepts clicks on all links and dynamically adds a new link when a link is clicked. <html> <head> <link rel="stylesheet" type="text/css" href="/site-media/al/style.css" /> <script type="text/javascript" src="./jquery-1.3.2.min.js"></script> </head> <body> <script type="text/javascript"> $(document).ready(function() { $("a").click(function() { $("ul#questions").append("<li><a href='#'>click me</a></li>"); }); }); </script> <div> <

Refreshing iptables

The last time I played around with IPTables was about 6 - 7 years back. I have been working with a Linux box again from the last few months and I am absolutely thrilled about it. Nevertheless, I have also been meaning to set up a simple IPTables firewall on my machine. Since I had forgotten all my IPTables concepts, I decided to hunt the Internet for some good articles. I found some really nice resources to refresh my memory as well as learn new things. Here is a nice 3 part video series from Linux Journal . iptables part 1 iptables part 2 iptables part 3 The entire series takes less than a half hour and is a good refresher or introduction to IPTables. Here is a nice picture which explains how a packet is routed through various chains in the ip tables. If you are looking for a quick refresher, this might help you out. If you want more details, here is a tutoria l, and yet another tutorial , and Netfilter's excellent documentation on IPTables. So now there is no excuse for not se

Inheritance vs. composition depending on how much is same and how much differs

I am reading the excellent Django book right now. In the 4th chapter on Django templates , there is an example of includes and inheritance in Django templates. Without going into details about Django templates, the include is very similar to composition where we can include the text of another template for evaluation. Inheritance in Django templates works in a way similar to object inheritance. Django templates can specify certain blocks which can be redefined in subtemplates. The subtemplates use the rest of the parent template as is. Now we have all learned that inheritance is used when we have a is-a relationship between classes, and composition is used when we have a contains-a relationship. This is absolutely right, but while reading about Django templates, I just realized another pattern in these relationships. This is really simple and perhaps many of you may have already have had this insight... We use inheritance when we want to allow reuse of the bulk of one object in other

Slashy strings in Groovy

Groovy has done a great job of enhancing Java Strings. It offers a lot of features like String interpolation with GStrings, triple quoted Strings, multi-line Strings, and slashy Strings. In this post I will talk about slashy Strings in Groovy. But before doing that let us see how we represent a regular expression in Java. Let's say I have a list of fully qualified file names and I want to match all files in my 'c:\tmp' directory. I would create a String to represent my regex in Java like this: String exp = "C:\\\\tmp\\\\.*" The four '\' are needed because '\' is a meta character in regular expressions, so we need to represent a '\' as a '\\'. Because a '\' is used for escaping special characters in Java, we need to represent '\\' as '\\\\'. Wow doesn't this look cumbersome. Well, this is not just one case. Regular expressions make use of the '\' character for special classes. Everytime we want to

Testing Groovy domain classes

If you are trying to test Grails domain class constraints by putting your unit test cases in the 'test/unit' directory, then your tests will fail because the domain objects will not have the 'valdate' method. This can be resolved in two ways: Place the test cases inside test/integration (which will slow things down) Use the method 'mockForConstraintsTests(Trail)' to create mock method in your domain class and continue writing your test cases in 'test/unit' What follows is some example code around this finding. I am working on a Groovy on Grails project for a website to help programmers keep up and refresh their skills. I started with some domain classes and then moved on to write some unit tests. When we create a Grails project using grails create-app , it creates several directories, one of which is a directory called 'test' for holding unit tests. This directory contains two directories, 'unit', and 'integration' for unit and

Groovy on Grails

Grails is a web application framework build on top of well known languages, frameworks and libraries. It uses the Groovy programming language which gives it power because of it's dynamic nature. Under the hoods, Grails uses Spring framework (for MVC), Hibernate (for OR mapping), Quartz (for scheduling), Log4J (for logging), JUnit for unit testing, and Canoo Webtest for functional testing. We already have a Gazillion frameworks, do we need Grails ? Typically web development in Java is a very long and arduous process. When a web project is started, many configuration files have to be set up after which the project development begins. Even then we have to create domain objects, basic controller functionality, the model, stylesheets, views, and other aspects of business logic. Grails makes things easier by allowing us to do all of these things rapidly and in fewer lines of code. So how does Grails allow us to do all this with fewer lines of code? Grails uses convention over c

Internet and new media for teaching and learning

Today, Freeman and I did a video talk for a faculty development program at SCIT on how teachers can use the Internet and New Media for teaching and learning. Internet And New Media For Teaching View more presentations from parag . Some useful links for those who are new to this medium. Google Groups is a good and simple way to start a mailing list. Google Reader is a good blog reader. For hosting a blog, I recommend either Blogger or Wordpress to start with. If you want more control, you can download the Wordpress software and install it in your own server. ITunes is a good client software for listening to podcasts. But ITunes works only on Windows and Mac. For those running Linux, the Miro Player is a good option for video podcasts, and Amarok or JPodder for audio podcasts. ITunes university has several podcasts hosted by universities worldwide. If you want to create a podcast, you can use Audacity for recording them and Odeo for free hosting. Those interested in Twitte

CompSci videos at Stanford

I came across some very interesting videos published by Stanford. These videos are recordings of some computer science courses offered at Stanford. I still have not had a chance to see all of them, but I am sure they will be very informative. Programming methodology series at Stanford (lectures 23/24 may be useful for developers who already know programming) - video Programming abstractions series at Stanford (looks like an interesting lectures. They focus on data structures, implications of using certain data structures, recursion, algorithm analysis like the Big O notation) - video Programming paradigms series at Stanford (Talks about imperative, OO, & functional programming. Also discusses many good low level things like pointers, big/little Endian, representation of data as bytes, and also touches on algorithms, assembly code, etc) - video Machine learning series at Stanford - video I hope you find these useful. I will share more links as I discover them.

Some more impressions of Python

List comprehensions: List comprehensions are pretty neat. Before studying Python I did not know what they were. This is a wonderful way to perform some operation on an entire list. Let me explain with an example. Let's say we have a list of numbers and want another list containing the double of all the numbers greater than 5 from the first list. In Java we would have to do something like this: private static List getDoubleOfMembers(List numbers) { List result = new ArrayList (); for(int num : numbers) { if(num > 5) { result.add(num*2); } } return result; } However, in Python we can perform this operation using list comprehension like this: [n*2 for n in numbers n > 5] Isn't this nice? Lambda expressions: Python supports Lambda expressions but it does not support closures like Groovy. I wish Python supported closures. Unchecked exceptions: The blogosphere has had more than it's share of discussions on checked vs. unchecked exceptions. I will not spam y

My first impressions of Python for the second time

I had worked a bit in Python many years back. Since then I have forgotten almost everything I learned back then. I think the phrase "Out of sight out of mind" applies perfectly to my mind. Since the last few days, I have started relearning Python, and this time I am recording my impressions of Python after having come to it from a Java background. Indentation: Python uses indentation to specify blocks of code, instead of curly braces. I like this, because we anyways indent code to increase readability, so why not achieve two tasks together. Code looks much cleaner without the curly braces. However there may be a little downside. Everyone in the team will have to set up their IDE's in the same way. Things might fall apart if some people use tabs and others use spaces for indentation. Access modifiers: Python does not have public, private, and protected keywords. Everything is public. However, private members can be specified with a leading single underscore. If we use do

String interpolations in different languages

I am learning Groovy and refreshing my Python skills as well. While working on some examples, I realized that the operation of concatenating string to produce a larger string is a very frequently used operation. Very often parts of the string we are creating are already held in variables. A crude way of creating the larger string, is to simply concatenate everything as shown in this simple toString() method of the Person object. public class Person { //showing only relevant code public String toString() { return "Name: " + name + " salary: " + salary + " address: " + address; } } However, this is not very readable. A better way is to use either string interpolation or templating. Java gives us a couple of ways to do templating. We can either write the toString() method like this to become much more readable: public String toString() { return String.format("name: %s salary: %d address: %s", name, salary, address); } Or we can use the M

Create mock objects with metaprogramming

We usually create mock objects in our test suites for those classes which communicate with some external entity that we cannot (or do not care to) setup in our test cases. However, mocking is possible only when we have used dependency injection properly. If we have classes that instantiate their own dependencies then we cannot mock the dependencies. Sometimes our mock solutions may also be time intensive at runtime. I will explain this later in the post. One solution (the correct one) is to refactor the code and use proper dependency injection to allow classes be be mocked. However, when we have a huge code base of non DI code, which cannot all be changed at a time, then we just have to make so with fewer unit tests. But all is not lost. If we use a dynamic language like Groovy to write the test cases then we can use metaprogramming to change a classes methods at runtime instead of creating mock classes. I am building a command line Twitter client in Groovy. I have a class called Frie

Eclipse's JUnit test runner forcibly terminates threads

Today I was writing a unit test which instantiated a class, which started a thread to do some stuff. I was hoping to test the side effect of that thread. However, to my surprise, I realized that Eclipse would forcibly terminate the test after the test method exited, even before the thread completed it's work. I wrote a simple test to show this. import com.sun.org.apache.xpath.internal.operations.And; import junit.framework.TestCase; import junit.textui.TestRunner; /** * This class shows that Eclipse's JUnit test case runner will forcibly terminate * the JVM and all running threads */ public class ThreadTest extends TestCase { static Runnable run = new Runnable() { public void run() { int cnt = 0; while(true) System.out.println(cnt++); } }; public void testThread() { Thread t = new Thread(run); t.start(); } public static void main(String args[]) { TestRunner runner = new TestRunner(); runner.run(ThreadTest.class); } } This cl

Injecting methods in Groovy with ExpandoMetaClass

I am writing a Twitter client in Groovy. One of the things I want to do is create a thread which will poll Twitter for new updates at regular intervals, and go to sleep when it is not doing anything. The way we write such code is to invoke the sleep method with the number of milliseconds to sleep. To sleep for 2 minutes, I would write: Thread.sleep(1000 * 60 * 2) This code is fine, but not very readable. I would have preferred to write something like this: Thread.sleep(2.minutes()) This is not possible in Java, but it is in Groovy. In Groovy we can inject methods into a class at runtime. This is done using the class' ExpandoMetaClass, as the example below shows. Integer.metaClass.seconds = {return delegate * 1000} Integer.metaClass.minutes = {return delegate * 1000 * 60} Integer.metaClass.hours = {return delegate * 1000 * 60 * 60} //test assert 3.seconds() == 1000 * 3 assert 5.minutes() == 1000 * 60 * 5 assert 4.hours() == 1000 * 60 * 60 * 4 The above code gets Integer's metacl

What is metaprogramming?

The word metaprogramming keeps coming up everywhere in stuff I read nowadays. Even though I sort of knew what it meant I wasn't really sure. So I decided to ask the question ' what exactly is metaprogramming ' on SO . I got some really nice answers and links to articles. I have written this blog post as an effort to summarize what I have learned and to be able to share it with others who might be interested. According to Wikipedia '' Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation." The compiler is the simplest example of a metaprogram. It takes as input, code in some lan

Closures in lieu of inner classes in Groovy

Groovy does not support inner classes, at least not in version 1.6 . Having done Java coding for many years, I have become accustomed to using inner classes to encapsulate small related functionality, especially when it is needed for callbacks or some sort of configuration. Now that I am learning Groovy, and writing a Twitter client, I needed to use inner classes, only to find out that Groovy does not support them. However, we can use closures and maps to simulate inner classes in Groovy. Before I explain this technique, I must say that it is simply a workaround, and at least in my opinion a not-so-good solution. For this example I will show how I implemented a Jetty Handler using closures instead of an anonymous inner class. Jetty is a Java web server which supports Servlets and JSP's. I often use Jetty in my unit tests, because it can be started in-memory from an API call. Jetty can be configured in code with handlers, so that it will use these handlers to service a web request.

Sharpening the saw with online competitions

Some time back Jeff Atwood wrote about Sharpening the saw on his blog . He defines sharpening the saw as (which is rightfully attributed to Steven Covey): Sharpening the saw is shorthand for anything you do that isn't programming , necessarily, but (theoretically) makes you a better programmer. There are several things developers can do to sharpen their saw. One of them is participating in programming competitions. There are several websites which host online programming competitions, and I really like this concept. Participants can work on these competitions at a time of their convenience, and those who do not have "real world" competitions hosted in their cities and towns, can also participate. Code Chef publishes online competitions which can be solved in any one of 35 programming languages. I like the fact that they give developers freedom to code in their favorite language. Many other online competitions do not give this freedom, and impose a particular programming

Groovy beans

A typical Java Bean is a Java class which is serializable, has a public no-arg constructor, private attributes, and getter/setter methods. I'd say this is a lot of stuff for a simple pojo. Groovy beans did to pojo's what Java could not. They are actually simple. A typical Groovy bean needs to implement java.io.Serializable and add properties to it. We do not need to add any getters/setters, they are added automatically for us. Here is a simple Groovy bean. class Customer implements java.io.Serializable { String firstName //will be a private attribute in the bytecode String lastName //will be a private attribute def middleInitial //beans can even contain dynamic properties } See.... Groovy beans do not have any getters or setters, and you will also have noticed that the fields are not private. Even though the source does not contain getter/setter methods and private attributes, the bytecode does. When this bean is converted into bytecode all attributes which do not have an a

Control structures in Groovy

To control program flow in Groovy we can use all control structures available in Java, but with additional power. In Java, branching statements like 'if', and 'while' accept an expression which evaluates to a boolean value. We cannot use an object where a boolean is required. Groovy, on the other hand has the concept of Groovy truth, which coerces non boolean values into boolean values. Thus statement like these are perfectly valid in Groovy. List list = ['apples'] if(list) { println 'This list is not empty' } Here the list object evaluates to a boolean value. Any non empty list will evaluate to true. This article explains coercion rules for the Groovy truth. Groovy truth allows us to use non booleans inside conditionals. It evaluates objects to their (truth) boolean values. With Groovy truth we can use booleans, Strings, GStrings, List, Map, Numbers, and any arbitrary object where a boolean is expected. Groovy also gives greater power to switch statem

Groovy Maps

Using Maps in Groovy is much simpler than in Java. Some key differences are: Map declaration and instantiation are simpler Elements of a Map can be accessed either using the bean style or array style Iterating across Maps is far less verbose in Groovy than in Java The code sample below is self explanatory //Declaring and instantiating a Map def emptyMap = [:] //Populating a Map //Notice that the keys which are strings are not //enclosed within quotes def population = [Estonia:1299371, Finland:5250275, Honduras:7792854, HongKong:7055071] //Non string keys must be enclosed in () def numbers = [(Integer.valueOf("1")):"One", (Integer.valueOf("2")):"Two"] //Accessing Map elements //Maps can be accessed using bean style and //array style notations respectively println "Estonia population: " + population.Estonia println "Finland population: " + populatio

Groovy types

Java is a statically typed language , which means we have to declare the type of a variable at compile time. Python is dynamically typed. which means that references can never to bound to types at compile time. Groovy on the other hand has optional typing. What this means is Groovy references can be statically typed like in Java, or dynamically typed like in Python. String staticStr = new String("this is a statically typed String"); staticStr = 8; println """The type of staticStr is: ${staticStr.class.getName()} """ println """It's value is: $staticStr""" The output form running the above program is: String staticStr = new String("this is a statically typed String"); staticStr = 8; println """The type of staticStr is: ${staticStr.class.getName()} """ println """It's value is: $staticStr""" As you can see, staticStr remains a string even a

Groovy Ranges

Groovy Collections offer us a class in addition to Java Collections, called Range. Range is a subclass of java.util.List and is used for creating lists of sequential values. Some ranges can be: 1..3 a..d Notice that we use the .. operator to create a range. However ranges are not limited to numbers and characters. Any object which implements the methods compareTo(), next(), and previous() can be used in a Range. Ranges typically have a left and right border, and they can be either inclusive (containing border elements) or exclusive (sans the right border element). Ranges are usually used to extract a subset of elements from a list or used in switch statements to match a range of cases together. The code below is self explanatory: //A simple Groovy Range def inclusiveRange = 1..10 println 'The size of inclusiveRange is: ' + inclusiveRange.size() //This range does not include the right border element def exclusiveRange = 1..<10 exclusiverange1 =" 1<..10" negativep

Groovy closures

Java does not have closures , but till sometime back it was a highly debated topic on various Java forums. Groovy is a language which compiles to Java bytecode and does have closures. Closures are very prominent in many programming languages, such as Groovy, Python, Ruby, Javascript, and probably many more. So what are closures? A closure is a block of code which can be passed to a function. Closures in Groovy are normal objects, which do not have any visible state. Closures are most often used in iteration logic and resource handling. Whenever we iterate across a collection, we normally do so to perform some logic on all or some members of the collection. Closures make it easy to represent that logic in a very concise and readable way. Closures can also be used for resource handling. Very often we open a file resource do something with the file (read from it, write to it, or both), and then close the file. The first and last parts, that is opening and closing the file are really plumb