2008-12-08

How to restore grub after Windows installation

There's nothing magical about it, you can easily Google it up, but I always forget the "find /boot/grub..." part and there are many other ways people explain this procedure, so I'm adding a screenshot as I'm doing it yet another time.

This is a best and easiest way to fix MBR after Windows f*ck it up.



In short:
1. boot from Ubuntu live CD (or any other live cd)
2. start a terminal
3.

sudo grub
find /boot/grub/stage1
root (hdX,Y)
setup (hdX)
quit


If you've got several drives you should check if your Windows partition is bootable and the other ones are not, otherwise if you install grub into linux partition, Windows will "fix" itself by changing bootable flag. To view and change bootable flags in Ubuntu Live go to System > Administration > Partition Editor (or run sudo gparted in terminal). The rest is illustrated below.






If grub loads but boot fails, it means grub detected your partitions incorrectly. Hit 'e' on linux entry, change the "root (hd1,0)" to "root (hd0,0)" or vice versa and press 'b' to attempt a modified boot. Repeat the "sudo grub" procedure when booted, but choose "setup (hdX)" target the oposite of your "root", i.e., if you did "root (hd1,0)", do "setup (hd0)" instead of "setup (hd1)". You should also edit /boot/grub/menu.lst afterwards and change all (hd0 to (hd1 and (hd1 to (hd0 to fix the boot menu.

Does all software have to suck?..

2008-11-25

Python MUD Game Example

I've found a small MUD-like single player game that I did with Python several years ago. Thought it would be nice to share the source. So here it is.

First of all, a live demo (type help. gone if my internet connection or home server is down.)

server down


And a "screenshot" (sorry for my bad sense of humor):


> help
welcome to mud. available commands are:
go, move, help, exit, look, say, take, drop, inventory, use
>
Unknown Command
> look
spajus sees deep green woods. There also seems to be: flower, bird
> touch bird
looks like a rainbow
> use bird
you do not have bird
> take bird
spajus puts bird in his inventory
> use bird
cuckarekoo! motherfucka!?!
> drop bird
bird was dropped..
> kill bird
Unknown Command
> look north
spajus sees shallow river. There also seems to be: object
> go north
spajus moves to shallow river
> look object
spajus sees a stinky one
> touch object
ewww...
> take object
spajus puts shit in his inventory
> use shit
you are sick, you know that?


Now, the engine.py

class MudObject:
def __init__(self, name, sight, collide = 'nothing happens', usability = 'unusable'):
self.name = name
self.sight = sight
self.collide = collide
self.usability = usability
def view(self):
return self.sight
def touch(self):
return self.collide
def use(self):
return self.usability
class MudPlayer:
def __init__(self, name):
self.inventory = {}
self.name = name
self.health = 100
def move(self, area):
return self.name + ' moves to ' + area.sight
def take(self, obj):
self.inventory[obj.name] = obj
return self.name + ' puts ' + obj.name + ' in his inventory'
def drop(self, name):
if self.inventory.has_key(name):
return self.inventory.pop(name)
def say(self, what):
return self.name + ' says: ' + what
def use(self, what):
if self.inventory.has_key(what):
return self.inventory[what].use()
else:
return 'you do not have ' + what

class MudArea:
def __init__(self, sight):
self.objects = {}
self.panorama = {}
self.sight = sight
self.inverted_directions = {'north':'south', 'south':'north', 'east':'west', 'west':'east'}
def addArea(self, direction, area):
area.panorama[self.inverted_directions[direction]] = self
self.panorama[direction] = area

def relocate(self, args):
try:
return self.panorama[args]
except KeyError:
return None
def addObject(self, name, obj):
if obj != None:
self.objects[name] = obj
return name + ' was dropped..'
def getObject(self, name):
if self.objects.has_key(name):
return self.objects.pop(name)
else:
return 'there is no ' + name + ' arround!'
def touchObject(self, name):
if self.objects.has_key(name):
return self.objects[name].touch()
else:
return 'there is no ' + name + ' arround!'
def view(self, args = 'arround'):
if (args != '' and args != 'arround'):
try:
return self.panorama[args].view()
except KeyError:
try:
return self.objects[args].view()
except KeyError:
return 'nothing.'
else:
objects = ', '.join([k for k, v in self.objects.items()])
if (objects != ''):
obsight = '. There also seems to be: ' + objects
else:
obsight = ''
return self.sight + obsight
import sys

class MudCommand:
""" welcome to mud. available commands are:
go, move, help, exit, look, touch, say, take, drop, inventory, use """
def __init__(self, char, area):
self.char = char
self.area = area

def go(self, args):
""" alias of move """
return self.move(args)

def use(self, args):
""" uses item from inventory """
return self.char.use(args)

def inventory(self, args):
""" displays inventory """
return self.char.name + ' has: ' + ', '.join(self.char.inventory)

def help(self, args):
""" gives you help on a topic"""
if args == '':
return self.__doc__
else:
try:
return getattr(self, args).__doc__
except AttributeError:
return 'help topic not found'

def exit(self, args):
""" exits game """
print 'bye bye!'
sys.exit()

def look(self, args):
""" lets you look arround """
return self.char.name + ' sees ' + self.area.view(args)

def take(self, args):
""" takes item from the ground """
try:
return self.char.take(self.area.getObject(args))
except AttributeError:
return 'you cannot take ' + args

def touch(self, args):
""" touches item from the ground """
return self.area.touchObject(args)

def drop(self, args):
""" drops item from inventory to current area """
return self.area.addObject(args, self.char.drop(args))

def move(self, args):
""" moves arround """
area = self.area.relocate(args)
if area != None:
self.area = area
return self.char.move(self.area)
else:
return 'There seems to be nothing that way.'

def say(self, args):
""" makes character talk """
return self.char.say(args)
class MudGame:
def __init__(self, char, area):
self.cmd = MudCommand(char, area)

def run(self):
while True:
command = raw_input('> ');
self.parse(command)

def parse(self, command):
comm = command.lower().split(' ')
try:
cmd = comm[0]
except IndexError:
cmd = 'help'
try:
args = comm[1:]
except IndexError:
args = []
try:
result = getattr(self.cmd, cmd)(' '.join(args).strip())
except AttributeError:
result = 'Unknown Command'
print result


And the main game script - mud.py:

from engine import *

#objects (name, description, on touch, on use)
rose = MudObject('rose', 'a red blossom with spikes', 'bites fingers!', 'wanna eat it or what?')
shit = MudObject('shit', 'a stinky one', 'ewww...', 'you are sick, you know that?')
gaidys = MudObject('bird', 'oh, a cock!', 'looks like a rainbow', 'cuckarekoo! motherfucka!?!')

#areas
woods = MudArea('deep green woods')
river = MudArea('shallow river')
hills = MudArea('orc hills')
house = MudArea('house of all gay')
meadow = MudArea('a green smelly meadow')

#attaching interactive stuff to areas
river.addObject('object', shit)
woods.addObject('flower', rose)
woods.addObject('bird', gaidys)
meadow.addObject('animal', gaidys)

#link all areas with bidirectional references
river.addArea('south', hills)
woods.addArea('north', river)
woods.addArea('west', house)
hills.addArea('east', meadow)
meadow.addArea('north', woods)

#create a player
char = MudPlayer('spajus')

#create a game with player and starting area
game = MudGame(char, woods)

#lets go!
game.run()


OK, and of course, unit tests:

import unittest
from engine import *

class TestMudObject(unittest.TestCase):
def setUp(self):
self.o = MudObject('object1', 'sight1', 'collision1', 'usage1')
self.o2 = MudObject('object2', 'sight2')

def test_view(self):
self.assertEqual(self.o.view(), 'sight1')
self.assertEqual(self.o2.view(), 'sight2')
self.assertNotEqual(self.o.view(), 'c')
self.assertNotEqual(self.o.view(), self.o2.view())

def test_touch(self):
self.assertEqual(self.o.touch(), 'collision1')
self.assertEqual(self.o2.touch(), 'nothing happens')
self.assertNotEqual(self.o.touch(), 'sight1')
self.assertNotEqual(self.o.touch(), self.o2.touch())

def test_use(self):
self.assertEqual(self.o.use(), 'usage1')
self.assertEqual(self.o2.use(), 'unusable')
self.assertNotEqual(self.o.use(), 'unsuable')
self.assertNotEqual(self.o.use(), self.o2.use())

class TestMudPlayer(unittest.TestCase):
def setUp(self):
self.p1 = MudPlayer('player1')
self.p2 = MudPlayer('player2')
self.area1 = MudArea('area1')
self.area2 = MudArea('area2')
self.o = MudObject('object1', 'sight1', 'collision1', 'usage1')
self.o2 = MudObject('object2', 'sight2')

def test_move(self):
f1 = self.p1.move
f2 = self.p2.move
a1 = self.area1
a2 = self.area2
self.assertEqual(f1(a1), 'player1 moves to area1')
self.assertEqual(f2(a1), 'player2 moves to area1')
self.assertEqual(f1(a2), 'player1 moves to area2')
self.assertEqual(f2(a2), 'player2 moves to area2')
self.assertNotEqual(f1(a1), 'player1 moves to area2')

def test_take_drop(self):
take = self.p1.take
use = self.p1.use
drop = self.p1.drop
inven = self.p1.inventory

o1 = self.o
o2 = self.o2

self.assertEqual(inven, {})
self.assertEqual(take(o1), 'player1 puts object1 in his inventory')
self.assertEqual(inven, {'object1':o1})
self.assertEqual(take(o2), 'player1 puts object2 in his inventory')
self.assertEqual(inven, {'object1':o1, 'object2':o2})

self.assertEqual(drop('object1'), o1)
#neesamo objekto dropint neina
self.assertRaises(TypeError, drop('object1'))
self.assertEqual(inven, {'object2':o2})
self.assertEqual(drop('object2'), o2)
self.assertEqual(inven, {})

def test_use(self):
p1 = self.p1
o1 = self.o
self.assertNotEqual(p1.use('object1'), 'usage1')
self.assertEqual(p1.use('object1'), 'you do not have object1')
p1.take(o1)
self.assertEqual(p1.use('object1'), 'usage1')

class TestMudArea(unittest.TestCase):
def setUp(self):
self.a1 = MudArea('area1')
self.a2 = MudArea('area2')
self.o1 = MudObject('obj1', 'sight1', 'collide1', 'use1')
self.o2 = MudObject('obj2', 'sight2')

def test_addArea(self):
#assignmentas turi buti veidrodinis
self.a1.addArea('north', self.a2)
self.assertEqual(self.a1.panorama, {'north':self.a2})
self.assertEqual(self.a2.panorama, {'south':self.a1})

def test_relocate(self):
self.a1.addArea('north', self.a2)
self.assertEqual(self.a1.relocate('north'), self.a2)
self.assertEqual(self.a2.relocate('north'), None)
self.assertEqual(self.a2.relocate('south'), self.a1)

def test_addObject(self):
self.assertEqual(self.a1.objects, {})
#dropped returninama todel, kad paprastai objectas zaidimo metu addinamas tada, kai playeris dropina ji.
#kreivai biski, bet ka padarysi :)
self.assertEqual(self.a1.addObject('something', self.o1), 'something was dropped..')
self.assertEqual(self.a1.objects, {'something':self.o1})
self.a1.addObject('other', self.o2)
self.assertEqual(self.a1.objects, {'something':self.o1, 'other':self.o2})
self.assertEqual(self.a1.addObject('something_clone', self.o1), 'something_clone was dropped..')
self.assertEqual(self.a1.objects, {'something':self.o1, 'other':self.o2, 'something_clone':self.o1})

def test_getObject(self):
self.assertEqual(self.a1.objects, {})
self.a1.addObject('something', self.o1)
self.a1.addObject('other', self.o2)
self.assertEqual(self.a1.getObject('something'), self.o1)
self.assertEqual(self.a1.objects, {'other':self.o2})
self.assertEqual(self.a1.getObject('something'), 'there is no something arround!')

def test_touchObject(self):
self.assertEqual(self.a1.objects, {})
self.a1.addObject('something', self.o1)
self.a1.addObject('other', self.o2)
self.assertEqual(self.a1.touchObject('something'), self.o1.touch())
self.assertNotEqual(self.a1.touchObject('obj2'), self.o2.touch())
self.assertEqual(self.a1.touchObject('ass'), 'there is no ass arround!')

def test_view(self):
view = self.a1.view
self.assertEqual(view(), 'area1')
#unindentified object/panorama
self.assertEqual(view('my brain'), 'nothing.')

self.a1.addObject('my brain', self.o1)
self.assertEqual(view('my brain'), self.o1.view())
#padarom dar idomiau. kadangi pridejom my brain, reikia parodyti ir tai..
self.assertNotEqual(view(), 'area1')
self.assertEqual(view(), 'area1. There also seems to be: my brain')
self.a1.addObject('duck', self.o2)
self.assertEqual(view(), 'area1. There also seems to be: my brain, duck') #', '.join(self.a1.objects)

self.assertEqual(view('north'), 'nothing.')
self.a1.addArea('north', self.a2)
self.assertEqual(view('north'), 'area2')
#kadangi priskyrem area2, turejo atsispindeti ir is ten paziurejus i priesinga north krypti (south), turi matytis area1 viewas
self.assertEqual(self.a2.view('south'), view())
#akurat.. matosi :)

class MudCommandTest(unittest.TestCase):
def setUp(self):
self.p1 = MudPlayer('player1')
self.a1 = MudArea('area1')
self.a2 = MudArea('area2')
self.o1 = MudObject('obj1', 'sight1', 'collide1', 'use1')
self.o2 = MudObject('obj2', 'sight2', 'collide2', 'use2')
self.a2.addObject('bread', self.o1)
self.a2.addObject('pig', self.o2)
self.a1.addArea('east', self.a2)
self.c = MudCommand(self.p1, self.a1)

def test_go_move(self):
#MudArea.go === MudArea.move
#test wrong way
self.assertEqual(self.c.go('somewhere'), 'There seems to be nothing that way.')
#test walk arround
self.assertEqual(self.c.go('east'), 'player1 moves to area2')
self.assertEqual(self.c.go('west'), 'player1 moves to area1')

def test_use(self):
self.assertEqual(self.c.use('bla'), 'you do not have bla')
#lets go east and take something to test using
self.c.go('east')
self.c.take('bread')
#as bread was only the looks, we know it's actually obj1, so lets use it
self.assertEqual(self.c.use('obj1'), self.o1.use())

def test_inventory(self):
self.c.go('east')
self.c.take('bread')
self.assertEqual(self.c.inventory(None), 'player1 has: obj1')

def test_help(self):
self.assertEqual(self.c.help(''), self.c.__doc__)
self.assertEqual(self.c.help('move'), self.c.move.__doc__)
self.assertEqual(self.c.help('blabla'), 'help topic not found')

def test_look(self):
self.assertEqual(self.c.look(''), 'player1 sees ' + self.a1.view())
self.assertEqual(self.c.look('at my balls'), 'player1 sees nothing.')
self.assertEqual(self.c.look('east'), 'player1 sees ' + self.a2.view())

def test_take(self):
self.c.go('east')
self.assertEqual(self.c.take('bread'), 'player1 puts obj1 in his inventory')
self.assertEqual(self.p1.inventory, {'obj1':self.o1})
#already taken!
self.assertEqual(self.c.take('bread'), 'you cannot take bread')

def test_touch(self): #perv test.. :)
self.assertEqual(self.c.touch('self'), 'there is no self arround!')
self.assertNotEqual(self.c.touch('bread'), self.o1.touch())
self.c.go('east')
#kad paliesti reik pirma nueiti
self.assertEqual(self.c.touch('bread'), self.o1.touch())

def test_drop(self):
self.assertEqual(self.c.drop('smelly thing'), None)
self.c.go('east')
self.c.take('bread')
self.c.go('west')
self.assertEqual(self.c.drop('obj1'), 'obj1 was dropped..')
self.assertEqual(self.a1.objects, {'obj1':self.o1})

def test_say(self):
self.assertEqual(self.c.say('i love this game'), 'player1 says: i love this game')
self.assertNotEqual(self.c.say('python sucks'), 'player1 says: that\'s true!')

if __name__ == '__main__':
unittest.main()


To run the game:
python mud.py


To run the tests:
python testengine.py


Damn, I really miss such coding activities :)

2008-11-11

InputStreamChain

If you have several Java InputStreams that you want to queue up into a single InputStream object, you can use this:

package com.varaneckas;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

/**
* {@link InputStream} implementation that allows chaining of various
* streams for seamless sequential reading
*
* @author Tomas Varaneckas <tomas.varaneckas@gmail.com>
*/
public class InputStreamChain extends InputStream {

/**
* Input stream chain
*/
private final LinkedList<InputStream> streams = new LinkedList<InputStream>();

/**
* Currently active stream
*/
private InputStream current;

/**
* Default constructor
*/
public InputStreamChain() {
//nothing to do
}

/**
* Constructor with an initial stream
*
* @param first Initial InputStream
*/
public InputStreamChain(final InputStream first) {
addInputStream(first);
}

/**
* Constructor with an array of initial streams
*
* @param streams Array of initial InputStreams
*/
public InputStreamChain(final InputStream[] streams) {
for (InputStream stream : streams) {
addInputStream(stream);
}
}

/**
* Vararg constructor
*
* @param streams initial input streams
*/
public InputStreamChain(final InputStream ... streams) {
for (InputStream stream : streams) {
addInputStream(stream);
}
}

/**
* Adds input stream to the end of chain
*
* @param stream InputStream to add to chain
* @return instance of self (for fluent calls)
*/
public InputStreamChain addInputStream(final InputStream stream) {
streams.addLast(stream);
if (current == null) {
current = streams.removeFirst();
}
return this;
}

@Override
public int read() throws IOException {
int bit = current.read();
if (bit == -1 && streams.size() > 0) {
try {
current.close();
} catch (final IOException e) {
//replace this with a call to logging facility
e.printStackTrace();
}
current = streams.removeFirst();
bit = read();
}
return bit;
}

@Override
public int available() throws IOException {
int available = current.available();
for (InputStream stream : streams) {
available += stream.available();
}
return available;
}

@Override
public void close() throws IOException {
current.close();
}

@Override
public boolean markSupported() {
return current.markSupported();
}

@Override
public synchronized void mark(int i) {
current.mark(i);
}

@Override
public synchronized void reset() throws IOException {
current.reset();
}

@Override
public long skip(long l) throws IOException {
return current.skip(l);
}

}


Example code:
InputStream chuck = new ByteArrayInputStream("Chuck ".getBytes());
InputStream norris = new ByteArrayInputStream("Norris".getBytes());
InputStream chuckNorris = new InputStreamChain()
.addInputStream(chuck)
.addInputStream(norris);
//will print "Chuck Norris"
System.out.println(new BufferedReader(
new InputStreamReader(chuckNorris)).readLine());


Have fun!

2008-11-06

Software design tips from the creator of C++ programming language

[1] Know what you are trying to achieve
[2] Keep in mind that software development is a human activity
[3] Proof by analogy is fraud
[4] Have specific and tangible aims
[5] Don’t try technological fixes for sociological problems
[6] Consider the longer term in design and in the treatment of people
[7] There is no lower limit to the size of programs for which it is sensible to design before starting to code
[8] Design processes to encourage feedback
[9] Don’t confuse activity for progress
[10] Don’t generalize beyond what is needed, what you have direct experience with, and what can be tested
[11] Represent concepts as classes
[12] There are properties of a system that should not be represented as a class
[13] Represent hierarchical relationships between concepts as class hierarchies
[14] Actively search for commonality in the concepts of the application and implementation and represent the resulting more general concepts as base classes
[15] Classifications in other domains are not necessarily useful classifications in an inheritance model for an application
[16] Design class hierarchies based on behaviour and invariants
[17] Consider use cases
[18] Consider using CRC cards
[19] Use existing systems as models, as inspiration, and as starting points
[20] Beware of viewgraph engineering
[21] Throw a prototype away before it becomes a burden
[22] Design for change, focusing on flexibility, extensibility, portability, and reuse
[23] Focus on component design
[24] Let each interface represent a concept at a single level of abstraction
[25] Design for stability in the face of change
[26] Make designs stable by making heavily used interfaces minimal, general, and abstract
[27] Keep it small. Don’t add features "just in case"
[28] Always consider alternative representations for a class. If no alternative representation is plausible, the class is probably not representing a clean concept
[29] Repeatedly review and refine both the design and the implementation
[30] Use the best tools available for testing and for analysing the problem, the design, and the implementation
[31] Experiment, analyse, and test as early as possible and as often as possible
[32] Don’t forget about efficiency
[33] Keep the level of formality appropriate to the scale of the project
[34] Make sure that someone is in charge of the overall design
[35] Document, market, and support reusable components
[36] Document aims and principles as well as details
[37] Provide tutorials for new developers as part of the documentation
[38] Reward and encourage reuse of designs, libraries, and classes

I found these great tips in a classic programming book: The C++ Programming Language Third Edition by Bjarne Stroustrup, the creator of C++. If you want to learn C++ or deepen your knowledge, this is The Book.

2008-10-28

Stuck on games

I'm not much of a gamer, but this autumn is something terrible - there are at least three new time wasters for PC that I must get my hands on.

Bully: Scholarship Edition



Fallout 3



and finally, Grand Theft Auto IV



Time to disconnect...

2008-10-07

Hawkscope 0.2.0

This little peace of software has lately become my passion that reminds me of those days when I was programming 16 hours a day 7 days a week. Anyway, here are the highlights for the new Hawkscope 0.2.0:

  • Size dropped down from 740Kb to 122Kb.

  • Fixed bugs that prevented writing the configuration file in Windows and reading it in all OS.

  • Added Quick Access List menu that allows custom folders to be listed on top of all partitions. User home is there by default. This list can contain dynamic variables like Java properties (${user.home}/Desktop) or environmental variables (${$JAVA_HOME}). Read the User Guide to find out how to configure your own Quick Access List.

  • Floppy drives are ignored by default to avoid annoying device buzz whenever mouse travels over floppy disk entry. This can be turned back on by changing display.floppy property to "1". By the way, same thing can be done with hidden files. They are hidden by default, but there is display.hidden property in [user_home]/.hawkscope.properties.

  • Improvements in About and Error dialogs. You can now copy nicely formatted bug reports to Clipboard for easy submission. If you use Hawkscope and find anything suspicious, please, copy the report and add new issue, it takes just 10-20 seconds.

  • You can find more changes in Hawkscope Changelog.

    Here's how the new version looks on Windows Vista:



    Stay tuned for future improvements that include a full blown plugin system, GUI driven configuration and more.

    2008-09-28

    Hawkscope: System Tray File Browser

    I'm happy to announce the first usable release of my new weekend project. It's Hawkscope - a simple productivity tool that allows you to find and open any file or folder in seconds by single-clicking a tray icon and navigating through dynamically generated menus that reflect the contents of your available file systems.

    Hawkscope is open source, it's built with Java 1.6, therefore it doesn't run on Mac OS X (for now...), even with SoyLatte JDK. I tested it on Windows (XP and Vista) and Linux (Ubuntu + Gnome). Should work perfectly where Java 6 System Tray and Desktop API are supported.

    Here's how it looks on Windows Vista (running inside VirtualBox):


    And on my Ubuntu:


    You can always download latest release here. Enjoy!

    2008-09-26

    Java 6 on 32-bit Intel Mac

    Dying to have Java 6 on an older, non Core 2 Duo Intel Mac? You should get SoyLatte.

    SoyLatte is a functional, X11-based port of the FreeBSD Java 1.6 patchset to Mac OS X Intel machines. SoyLatte is initially focused on supporting Java 6 development; however, the long-term view far more captivating: open development of Java 7 for Mac OS X, with a release available in concert with the official Sun release, supported on all recent versions of Mac OS X.

    It lacks some features like system tray support, but overall these folks are doing a way better job than the official Apple Java team.

    2008-09-18

    Eclipse Template: Singleton Pattern

    Are you writing your Java classes as Singletons quite often? Use this Eclipse template and make any class a singleton in two seconds:

    Setup

    Go to Window -> Preferences -> Java -> Editor -> Templates. Create New:



    Code for copy paste:
    private static final ${enclosing_type} instance = new ${enclosing_type}();
    private ${enclosing_type}() {}
    public static ${enclosing_type} getInstance() {
        return instance;
    }


    Action

    Type "single" or "singleton", hit Content Assist shortcut key (ctrl+space by default), then enter:



    And there you go - a singleton in two seconds (actual time may vary on your typing and CPU speed).



    You may want to use a different Singleton implementation. Check out Java Singleton: The Proper Way for a good example.

    2008-09-01

    Add loggers to your Java code in seconds using Eclipse Templates

    Custom Eclipse Templates can greatly increase your productivity by automating daily development. For instance, instead of creating Apache Commons Logging loggers by hand, you can do this:

    Setup

    Go to Window -> Preferences -> Java -> Editor -> Templates. Create New:



    Action

    Type "log", hit Content Assist shortcut key (ctrl+space by default), then enter:



    Hit Ctrl+Shift+O to import Log and LogFactory:



    Saves hell of a time in the long run.

    2008-07-25

    Slap your Java code hard with Maven and PMD

    How good you think your code is? How can you be sure it's optimal, bug and bullet proof? Unit Tests? In case the coverage is good, they will tell if your code works, but will it tell if and where your code sucks in general? Let's get down to business.

    As Maven is used de facto for Java builds, I assume you're using it. You may have heard of PMD, but have you tried it? If you have, did you know that it has a nice Maven Plugin? A quick way to integrate it:

    Add the following to your pom.xml. Sadly, default check rulesets are too friendly, so you may want to try my configuration (even the "too hardcore" block):







    org.apache.maven.plugins
    maven-jxr-plugin


    org.apache.maven.plugins
    maven-pmd-plugin


    rulesets/basic.xml
    rulesets/braces.xml
    rulesets/clone.xml
    rulesets/codesize.xml
    rulesets/coupling.xml
    rulesets/favorites.xml
    rulesets/finalizers.xml
    rulesets/imports.xml
    rulesets/junit.xml
    rulesets/migrating_to_15.xml
    rulesets/optimizations.xml
    rulesets/typeresolution.xml
    rulesets/unusedcode.xml
    rulesets/strings.xml


    true
    true
    utf-8
    5
    20
    1.5








    To generate a report, simply run:

    mvn pmd:pmd

    You may want to build Java cross reference for links to source code to work:

    mvn jxr:jxr

    Finally, open the report and see how naughty the code is:

    target/site/pmd.html

    Watch out for Cyclomatic Complexity!

    2008-07-18

    Best of Jargon File

    My favorite definitions from Jargon File (AKA, The New Hacker's Dictionary).


    brute force and ignorance
    : n.

    A popular design technique at many software houses — brute force coding unrelieved by any knowledge of how problems have been previously solved in elegant ways. Dogmatic adherence to design methodologies tends to encourage this sort of thing. Characteristic of early larval stage programming; unfortunately, many never outgrow it. Often abbreviated BFI: “Gak, they used a bubble sort! That's strictly from BFI.” Compare bogosity. A very similar usage is said to be mainstream in Great Britain.


    BOFH: //, n.

    [common] Acronym, Bastard Operator From Hell. A system administrator with absolutely no tolerance for lusers. “You say you need more filespace? Seems to me you have plenty left...” Many BOFHs (and others who would be BOFHs if they could get away with it) hang out in the newsgroup alt.sysadmin.recovery, although there has also been created a top-level newsgroup hierarchy (bofh.*) of their own.

    Several people have written stories about BOFHs. The set usually considered canonical is by Simon Travaglia and may be found at the Bastard Home Page. BOFHs and BOFH wannabes hang out on scary devil monastery and wield LARTs.


    buried treasure: n.

    A surprising piece of code found in some program. While usually not wrong, it tends to vary from crufty to bletcherous, and has lain undiscovered only because it was functionally correct, however horrible it is. Used sarcastically, because what is found is anything but treasure. Buried treasure almost always needs to be dug up and removed. “I just found that the scheduler sorts its queue using bubble sort! Buried treasure!


    code police: n.

    [by analogy with George Orwell's ‘thought police’] A mythical team of Gestapo-like storm troopers that might burst into one's office and arrest one for violating programming style rules. May be used either seriously, to underline a claim that a particular style violation is dangerous, or ironically, to suggest that the practice under discussion is condemned mainly by anal-retentive weenies. “Dike out that goto or the code police will get you!” The ironic usage is perhaps more common.


    cow orker: n.

    [Usenet] n. fortuitous typo for co-worker, widely used in Usenet, with perhaps a hint that orking cows is illegal. This term was popularized by Scott Adams (the creator of Dilbert) but already appears in the January 1996 version of the scary devil monastery FAQ, and has been traced back to a 1989 sig block. Compare hing, grilf, filk, newsfroup.


    To look for something in a mass of code or data with one's own native optical sensors, as opposed to using some sort of pattern matching software like grep or any other automated search tool. Also called a vgrep; compare vdiff.


    gang bang: n.

    The use of large numbers of loosely coupled programmers in an attempt to wedge a great many features into a product in a short time. Though there have been memorable gang bangs (e.g., that over-the-weekend assembler port mentioned in Steven Levy's Hackers), and large numbers of loosely-coupled programmers operating in bazaar mode can do very useful work when they're not on a deadline, most are perpetrated by large companies trying to meet unrealistic deadlines; the inevitable result is enormous buggy masses of code entirely lacking in orthogonality. When market-driven managers make a list of all the features the competition has and assign one programmer to implement each, the probability of maintaining a coherent (or even functional) design goes to epsilon. See also firefighting, Mongolian Hordes technique, Conway's Law.


    Guido: /gwee´do/, /khwee´do/

    Without qualification, Guido van Rossum (author of Python). Note that Guido answers to English /gwee´do/ but in Dutch it's /khwee´do/. Mythically, Guido's most important attribute besides Python itself is Guido's time machine, a device he is reputed to possess because of the unnerving frequency with which user requests for new features have been met with the response “I just implemented that last night...”. See BDFL.


    guiltware: /gilt´weir/, n.

    1. A piece of freeware decorated with a message telling one how long and hard the author worked on it and intimating that one is a no-good freeloader if one does not immediately send the poor suffering martyr gobs of money.

    2. A piece of shareware that works.


    hired gun: n.

    A contract programmer, as opposed to a full-time staff member. All the connotations of this term suggested by innumerable spaghetti Westerns are intentional.


    hyperspace: /hi:´per·spays/, n.

    A memory location that is far away from where the program counter should be pointing, especially a place that is inaccessible because it is not even mapped in by the virtual-memory system. “Another core dump — looks like the program jumped off to hyperspace somehow.” (Compare jump off into never-never land.) This usage is from the SF notion of a spaceship jumping into hyperspace, that is, taking a shortcut through higher-dimensional space — in other words, bypassing this universe. The variant east hyperspace is recorded among CMU and Bliss hackers.


    I didn't change anything!: interj.

    An aggrieved cry often heard as bugs manifest during a regression test. The canonical reply to this assertion is “Then it works just the same as it did before, doesn't it?” See also one-line fix. This is also heard from applications programmers trying to blame an obvious applications problem on an unrelated systems software change, for example a divide-by-0 fault after terminals were added to a network. Usually, their statement is found to be false. Upon close questioning, they will admit some major restructuring of the program that shouldn't have broken anything, in their opinion, but which actually hosed the code completely.

    A contract programmer, as opposed to a full-time staff member. All the connotations of this term suggested by innumerable spaghetti Westerns are intentional.


    KIBO: /ki:´boh/<

    1. [acronym] Knowledge In, Bullshit Out. A summary of what happens whenever valid data is passed through an organization (or person) that deliberately or accidentally disregards or ignores its significance. Consider, for example, what an advertising campaign can do with a product's actual specifications. Compare GIGO; see also SNAFU principle.


    Macintrash: /mak´in·trash`/, n.

    The Apple Macintosh, as described by a hacker who doesn't appreciate being kept away from the real computer by the interface. The term maggotbox has been reported in regular use in the Research Triangle area of North Carolina. Compare Macintoy. See also beige toaster, WIMP environment, point-and-drool interface, drool-proof paper, user-friendly.


    meatspace: /meet´spays/, n.

    The physical world, where the meat lives — as opposed to cyberspace. Hackers are actually more willing to use this term than ‘cyberspace’, because it's not speculative — we already have a running meatspace implementation (the universe). Compare RL.


    Mongolian Hordes technique: n.

    [poss. from the Sixties counterculture expression Mongolian clusterfuck for a public orgy] Development by gang bang. Implies that large numbers of inexperienced programmers are being put on a job better performed by a few skilled ones (but see bazaar). Also called Chinese Army technique; see also Brooks's Law.


    nipple mouse: n.

    Var. clit mouse, clitoris Common term for the pointing device used on IBM ThinkPads and a few other laptop computers. The device, which sits between the ‘g’ and ‘h’ keys on the keyboard, indeed resembles a rubber nipple intended to be tweaked by a forefinger. Many hackers consider these superior to the glide pads found on most laptops, which are harder to control precisely.


    optimism: n.

    What a programmer is full of after fixing the last bug and before discovering the next last bug. Fred Brooks's book The Mythical Man-Month (See Brooks's Law) contains the following paragraph that describes this extremely well:

    All programmers are optimists. Perhaps this modern sorcery especially attracts those who believe in happy endings and fairy godmothers. Perhaps the hundreds of nitty frustrations drive away all but those who habitually focus on the end goal. Perhaps it is merely that computers are young, programmers are younger, and the young are always optimists. But however the selection process works, the result is indisputable: “This time it will surely run,” or “I just found the last bug.”.

    See also Lubarsky's Law of Cybernetic Entomology.


    pseudosuit: /soo´doh·s[y]oot`/, n.

    A suit wannabee; a hacker who has decided that he wants to be in management or administration and begins wearing ties, sport coats, and (shudder!) suits voluntarily. It's his funeral. See also lobotomy.


    RTFS: /R·T·F·S/

    [Unix] 1. imp. Abbreviation for ‘Read The Fucking Source’. Variant form of RTFM, used when the problem at hand is not necessarily obvious and not answerable from the manuals — or the manuals are not yet written and maybe never will be. For even trickier situations, see RTFB. Unlike RTFM, the anger inherent in RTFS is not usually directed at the person asking the question, but rather at the people who failed to provide adequate documentation.


    Saturday-night special: n.

    [from police slang for a cheap handgun] A quick-and-dirty program or feature kluged together during off hours, under a deadline, and in response to pressure from a salescritter. Such hacks are dangerously unreliable, but all too often sneak into a production release after insufficient review.


    shotgun debugging: n.

    The software equivalent of Easter egging; the making of relatively undirected changes to software in the hope that a bug will be perturbed out of existence. This almost never works, and usually introduces more bugs.


    space-cadet keyboard: n.

    Long one, but a must-see: http://www.catb.org/~esr/jargon/html/S/space-cadet-keyboard.html

    Spinning Pizza of Death: n.

    [OS X; common] The quartered-circle busy indicator on Mac OS X versions before 10.2, after which it was replaced by a sort of rainbow pinwheel thingy. It was analogous to the Microsoft Windows hourglass, but OS X 10.0's legendary slowness under the Aqua toolkit made this term rather more evocative. See Death, X of.


    stealth manager: n.

    [Corporate DP] A manager that appears out of nowhere, promises undeliverable software to unknown end users, and vanishes before the programming staff realizes what has happened. See smoke and mirrors.


    STFW: imp., /S·T·F·W/

    [Usenet] Common abbreviation for “Search The Fucking Web”, a suggestion that what you're asking for is a query better handled by a search engine than a human being. Usage is common and exactly parallel to both senses of RTFM. A politer equivalent is GIYF.


    suit: n.

    1. Ugly and uncomfortable ‘business clothing’ often worn by non-hackers. Invariably worn with a ‘tie’, a strangulation device that partially cuts off the blood supply to the brain. It is thought that this explains much about the behavior of suit-wearers. Compare droid.

    2. A person who habitually wears suits, as distinct from a techie or hacker. See pointy-haired, burble, management, Stupids, SNAFU principle, PHB, and brain-damaged.


    sysape: /sys´ayp/, n.

    A rather derogatory term for a computer operator; a play on sysop common at sites that use the banana hierarchy of problem complexity (see one-banana problem).


    top-post: n., v.

    [common] To put the newly-added portion of an email or Usenet response before the quoted part, as opposed to the more logical sequence of quoted portion first with original following. The problem with this practice is neatly summed up by the following FAQ entry:

    A: No.
    Q: Should I include quotations after my reply?

    This term is generally used pejoratively with the implication that the offending person is a newbie, a Microsoft addict (Microsoft mail tools produce a similar format by default), or simply a common-and-garden-variety idiot.


    voodoo programming: n.

    [from George Bush Sr.'s “voodoo economics”] 1. The use by guess or cookbook of an obscure or hairy system, feature, or algorithm that one does not truly understand. The implication is that the technique may not work, and if it doesn't, one will never know why. Almost synonymous with black magic, except that black magic typically isn't documented and nobody understands it. Compare magic, deep magic, heavy wizardry, rain dance, cargo cult programming, wave a dead chicken, SCSI voodoo.

    2. Things programmers do that they know shouldn't work but they try anyway, and which sometimes actually work, such as recompiling everything.


    Wintendo: /win·ten´doh/, n.

    [Play on “Nintendo”] A PC running the Windows operating system kept primarily for the purpose of viewing multimedia and playing games. The implication is that the speaker uses a Linux or *BSD box for everything else.


    zipperhead: n.

    [IBM] A person with a closed mind.

    2008-07-16

    What is Eclipse Mylyn anyway?

    Since the release of Eclipse Europa, there was one new big feature - Mylyn. I was sceptic about it, besides it made the IDE slower, so I've kept removing it without getting to know what the hell is Mylyn anyway? After seeing this video I'm about to give it another try. If you're using Eclipse without Mylyn, you must see the video below:



    Update
    Mylin still sucks big time. Could not get used to it. It's most probably for people with ADD. Read the discussion in comments.

    2008-07-10

    Oracle Exception Handling - Stack Trace


    Oracle PL/SQL is definitely the worst programming language I've ever encountered. Some time ago I thought that PHP was the worst, but well, things change.

    I've been searching for the source of a weird CLOB related bug in a big pile of PL/SQL sh.. mess for a couple of days till I got fed up and decided to find a way to get the stack trace or at least the last line of code where the error was triggered from. Would you believe that before Oracle 10g there was no normal way to get the trace? Here's some Daily WTF material from the official PL/SQL User's Guide and Reference.

    ----- WTF EXCERPT START -----

    Using Locator Variables to Identify Exception Locations

    Using one exception handler for a sequence of statements can mask the statement that caused an error:

    BEGIN
    SELECT ...
    SELECT ...
    EXCEPTION
    WHEN NO_DATA_FOUND THEN ...
    -- Which SELECT statement caused the error?
    END;
    Normally, this is not a problem. But, if the need arises, you can use a locator variable to track statement execution, as follows:
    DECLARE
    stmt INTEGER := 1; -- designates 1st SELECT statement
    BEGIN
    SELECT ...
    stmt := 2; -- designates 2nd SELECT statement
    SELECT ...
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    INSERT INTO errors VALUES ('Error in statement ' || stmt);
    END;
    ----- WTF EXCERPT END -----

    Yes, they even have a name for this. Locator Variables. Damn. I can't decide whether to laugh or to cry...

    On a good note, since Oracle 10g you can use DBMS_UTILITY.FORMAT_ERROR_BACKTRACE function to get a string representation of stack trace with procedure names and code line numbers. They still forgot to add this into the "Handling PL/SQL Errors" section of their manual...

    So, here's how you get the stack trace:
    declare
    x number;
    begin
    x := 1 / 0;
    dbms_output.put_line(x);
    exception
    when others then
    dbms_output.put_line(SQLERRM);
    dbms_output.put_line(dbms_utility.format_error_backtrace);
    end;
    Output:
    ORA-01476: divisor is equal to zero
    ORA-06512: at line 5

    And it took them only 10 versions to implement.

    2008-07-09

    EuroPython 2008 - Day 3



    The third and final day of EuroPython 2008 started with a little bit of rocket science by Michael Meinel from German Aerospace Center. He talked about FlowSimulator, which is a Python-controlled framework to unify massive parallel CFD workflows. Interesting point was that they used SWIG to allow Python to control code written in C/C++, so basically Python was a glue code.



    Second talk I attended was called "Small team, big demands? - Use the batteries included" by Jussi Rasinmäki. It was a tale of a software project which started with C, failed miserably and finally ended with Python. The outcome was twice as fast as C code with tenfold smaller code base. C can be slow if your code is really really bad.



    I went there for the "Batteries Included" line, which really was as simple as this:



    There was one thing that I believe you should avoid in your code, and hell, in public presentations too. I mean a function named Age_pine_hemib_h_KalliovirtaTokola. Seriously, WTF?



    Next, Raymond D. Hettinger gave a good talk on "Core Python Containers - Under The Hood". Well, before that he managed to amuse the audience with the usual behavior of Windows in his laptop. Why the hell anyone would use Windows here anyway?



    However, Raymond revealed some useful Python internals and told us how to make optimal use of the collections.



    Here's the moral of his story:



    Last talk I've attended in this year's EuroPython was called "Functional Programming with Python, or Why It's Good To Be Lazy?" by Adam Byrtek. It was a great talk that covered the concept of functional programming, which went down to Pythonic functional programming features - map/filter/reduce and lambda functions.



    Sadly I had some errands to run, so I couldn't make it to the Lightning Talks...



    Hopefully I'll see you all in EuroPython 2009, which is going to be held in Birmingham UK.

    2008-07-08

    EuroPython 2008 - Day 2


    I took way too much coffee during breaks on Day 1, that kept me awake till 3AM, so it was a tough day. Nevertheless, today was better than Day 1. Let's see where I've been.

    Designing Large-Scale Applications in Python by Marc-André Lemburg basically was a beginner oriented tutorial on how to do enterprisey stuff properly. It got more serious later on when he started mentioning concrete patterns and techniques. It's good that this conference has a (so so, but still) working WI-FI that kept me entertained till the next session. It's good to chew on some RSS feeds in the morning.



    Then Steve Alexander from Canonical gave an inspiring talk on how they developed a very large python web application LEAN style. Launchpad to be exact. One of the greatest points was the use of pre-commit continuous integration, which ensures the developers that trunk is never broken. Also, he mentioned that canonical is hiring ~20 python developers (see their ad in my post about EuroPython 2008 Day 1).



    Cool stuff with Jython by Frank Wierzbicki (from Sun) and Jim Baker was somehow boring and Jim's words "we write Java so you don't have to broke my heart, so I got back to reading RSS feeds". Frank was bragging about new hot NetBeans refactoring, which Eclipse has since.. uh.. forever? Read more on this topic in this great post. Dear pythonists, throw away your NetBeans CDs and visit eclipse.org instead.



    There were a few Lightning Talks worth noticing.



    Here's the full list.



    I liked Jure Vrščaj's talk on Remote Module Importing where he showed us how to implement custom Python path resolvers that can seek modules from internet or source control repositories.



    Mikhail Kashkin introduced a better style of Python programming - The Drunken Monkey style. Thumbs up for this one. :)



    It was amazing to see how Holger Krekel unit-tested JavaScript with pytest.



    Geoffrey French deserves a medal for his very alpha code editor gSym Prototype, which visualizes Python code with as AST and adds a Lisp View (with lots and lots of braces that all pythonists just love).



    Here's a screenshot of gSym visualizing some mathematical functions.



    Finally, a charismatic professor from Sweden, Hans Rosling, made a totally mind blowing keynote called "Code that make sense of the world". It can be basically rephrased as "Instead of letting people generate images from raw statistics in their head we should generate images in front of their heads".



    It was one of the best talks I've seen in my life. This 60 year old professor knows people like Larry Page and Bill Gates, he knew Fidel Castro he's also good at GTA 2. Possibly this has something to do with Fidel Castro :).

    Some more pictures from his talk...







    Here's a great point on how data should be represented to the public.



    And the following symptom that technical people tend to have made the audience laugh and applaud.



    Most of what he presented can be seen in the following video. You HAVE to see it.



    One day to go. See you tomorrow!