Languages


I’ve had cause to transfer a whole stack of data from an old sqlite database to a PostgreSQL database, and since on the new database I am using SQL Alchemy, I used SA to do the transfer.

Because of some of the relations, I have had to keep IDs constant across some of the columns. This is fine, but because they have Sequence objects associated with them, these are not kept up to date with the ‘custom’ created IDs. Thus, when attempting to create a new row in the table, it often fails, since it is trying to add a primary key that already exists.

After a little bit of research, I discovered it is possible to force the sequence object to increment, using the command:

nextID = engine.execute(Sequence('sequence_name'))

Using a little bit of magic, we can find out the largest index currently in use:

maxID = db.query(Object).order_by('-id').first().id

And a simple while loop will keep incrementing until we have reached the correct id.

View Comments (0)   RSS Feed for Comments on this Post

Having to program in Java is a real eye-opener. It’s really not something I enjoy, mainly because I know there are better ways to do just about everything other than “the Java way”.

Take visibility, for instance. You can declare attributes and methods as private, protected or public. But if you have two objects of a particular class, they can access each others private variables. How fucking crazy is that? I mean, something is hardly private is a different object can still access it!

Another place where visibility is fucked up is in terms of methods and availability of these. If I have a class that is not defined as a public class, and I have an instance of this class, I can find out what it’s declared methods are. But I cannot invoke them, even if they are public!

I’ve got a project where I have to build a parser, and evaluate expressions. Sometimes there is a statement that can affect the state of the afore-mentioned object (which is in turn a sub-class of JFrame).

One of the statements is to resize the JFrame, which I can do by using the .setSize() method on this object. However, I cannot declare my own methods, even as public ones, and then call then from the client class.

Seriously, this is shit. All I want to do is basically pass a message to this object, and there’s no way for me to do so without changing the visibility of the class. I have a reference to the fucking object, I just can’t really do much with it.

View Comments (0)   RSS Feed for Comments on this Post

One program I tend to get around to writing in any new language I learn is a Sudoku solver. I originally wrote one in python, that solved every puzzle I could throw at it (although it had to guess and backtrack on failure in some cases).

Recently, I wrote one in Java. It was three classes plus a Driver/Main class. It took hours to write (at least the whole length of Phar Lap), and eventually it worked. I spent quite a while fixing up all of the syntactic errors I make in Java after programming in better languages again.

I then wrote one in Prolog. It was amazingly simple - it just defines a sudoku puzzle as a list of lists (each of which is 9 elements long), states that each element in each of the lists must be unique, and that each element in the first position of each list (columns) must be unique. Next, it states that each 3×3 grid must be unique, and that each element must be an integer of range 1-9.

That’s it. Solved.

My unique/1 predicate takes a list of items, and succeeds only if each item in the list is unique. This is simple:

  1. An empty list is unique.
  2. A list of one item is unique.
  3. A list of the form [A,B|X] is unique if A and B are different, and [A|X] and [B|X] are also unique-lists.

I also use a predicate that indicates a variable should be of the value 1-9, it’s possible to use advanced techniques and something called “Domains”, but I couldn’t get this figured out.

The only trick then is in the definitions. The simplest to understand, but messiest is to define the elements in the sudoku puzzle as each having unique names ([[A1,A2,...,A9],[B1,...]] and so on), and then passing some lists to unique, ie all ‘Ax’ elements, all ‘X1′ elements, and so on.

The coolest thing about Prolog is that you don’t need to say how things will happen, but just what the desired outcomes are. This can be dangerous - it’s rather easy to write something that will be executed in a ridiculous amount of time, so you often need to think about how to narrow the search space as quickly as possible. In Java, I needed to have methods that would remove elements from a possibles list, and then check to see if there was only one element, in which case it’s the actual value that belongs there, and so on. This is all error-prone code, wheras Prolog’s backtracking does all of this for you.

The biggest benefit is that in a puzzle that is fiendishly hard, Prolog will still come up with a solution, wheras some other languages, using the algorithm I defined above may not. And, in Prolog, you will automatically get multiple solutions to a problem if they exist.

Can you tell I love Prolog?

View Comments (3)   RSS Feed for Comments on this Post

I got tripped up by how C represents Boolean values today.

To save space, but more to make comparisons easy, I was storing a series of bits (representing hours in a day) as integers. I had built up some structure so that my sub-classed NSMatrix could use Cocoa Bindings and be connected to a Controller, so that changing the values in an NSArray of Integers in that controller would change the selected cells in the matrix, and changing the cell selection similarly affects the values stored in the array.

But, and it took me a while to figure this out, I was having problems getting the data back from the array properly. For some reason, numbers greater than 255 were not displaying properly. Basically, I seemed to be losing bits off the top. So I spent ages rewriting the code using longs, long longs, and unsigned long longs, but nothing seemed to work.

Eventually, I pinpointed the error to this: in C an integer with the lower 8 bits all set to 0 evaluates to FALSE.

It doesn’t seem to matter if the integer is a long, or whatever.

Thus, when I checked, using a bitwise AND (& operator), the stored value against a value representing the cell value, and stored it directly in a BOOL, and then used said BOOL to set the selection of that cell, it was evaluating to False for all values greater than 255.

All I needed was to change one line of code:

BOOL highlight = (value & bin);

becomes:

BOOL highlight = (value & bin) > 0;

All fixed.

View Comment (1)   RSS Feed for Comments on this Post

It’s not possible to directly hook up an NSSegmentedControl (or NSSegmentedCell) to a Core Data controlled NSArrayController (or any, for that instance). I wanted to be able to use one of these controllers to allow the user to select one, many or all items from the array.

To define the behaviour a little bit more: You can select the first item in the control (which will be called “All”), and it will select or deselect all of the other items, depending on what it’s state becomes. If you click on another segment, then it will toggle the selection of that segment. If all segments are ON after pressing a segment, then the first segment must also display ON, otherwise it will be OFF.

I did it by creating a new class: SegmentController. (I know, I should put on a prefix…)

SegmentController has two required IBOutlet variables, which refer to the NSSegmentedControl, the NSArrayController, and a property NSMutableIndexSet, which stores, sets and gets the selection indexes (I think this should be indices, but I’ll stick with Apple’s convention). I also have a sortDescriptors array, so that this object can sort the array controller.

There are a couple of limitations, which I might look at how I deal with - for instance, the NSSegmentedControl must have exactly one more segment than the NSArrayController has items. I’m using it to select one or more days, and this number doesn’t change. It should be possible to dynamically create the right number of segments, and populate them with values (I even have a readonly field called shortName set aside in my Day objects for this), but at awakeFromNib the NSArrayController is still empty, and I can’t figure out a nice neat way to force a fetch.

Hooking up the elements in Interface builder is easy. Create the NSArrayController, and set that up however you need to. Do the same with the NSSegmentedControl - at this stage you’ll need to put values in each of the segments. Now, create a new instance of SegmentController (you might need to add the class files to your XCode project first). Connect the two outlets up to the required objects.

Now, in the Bindings Inspector, connect up you’ll want to make the Selection Indexes parameter point to the selectionIndexes model key of your Segment Controller object.

Finally, make the selector of the NSSegmentedControl point to the selectSegment: action of the Segment Controller.

I did notice a slight delay between deselecting one segment and the “All” segment deselecting. A simple optimisation - moving the call to segmentCount out of the loop test and into a local variable made it much smoother.

That should probably do it. It might need a bit of tweaking. You should be able to have multiple instances of this in your project, if it is required. It’s been designed that you’ll need a seperate one for each Segmented Control, as it refers to the objects it is connected to, rather than the sender of the message.

My source code is here: SegmentController.zip

View Comments (0)   RSS Feed for Comments on this Post

I’m currently learning stacks (and maybe developing a useful application along the way) about Core Data and Cocoa Bindings.

I managed to hook up all of the “first level” Entities to UI elements, but was struggling a bit figurng out how to get the to-many relationships to work.

To make all elements of the type Person appear in an NSTableView, you can create an NSArrayController, set it to the Entity type, and put Person in the relevant field. Then all people will appear in the list.

If you have a manager, who might supervise zero or more people, then how do you get a sub-set of People. More specifically, how do you get a just the other side of a To-many relationship.

I thought I was going to have to resort to actually writing some code, and use a Predicate, or something like that. But there is an IB only method.

You can create an NSArrayController, and as well as putting in the Entity type (and hooking it up to the Managed Object Context), just put that it gets it’s Content Set from another object - in this case it would be People.selection.supervises (or whatever it is called).

This isn’t quite flawless - if you just hook up a button to the “add:” outlet, then it doesn’t quite add properly - the reverse of the attribute is not set. I had to make up an IBOutlet in my window controller that creates a new component, and sets the relationship up.

Which meant I ended up having to write two lines of code. For each relationship.

View Comment (1)   RSS Feed for Comments on this Post

Three languages. Three different Object Relational Mapping systems. One operating system.

Over the past couple of days, I’ve been madly learning how to create programs in Core Data, using Objective C and Cocoa. It’s given me plenty of food for thought, and made me perhaps think that it’s not that SQL Alchemy rocks my world, it’s just that J2EE/EJB is just ORM done wrong.

I actually got exposure to SQL Alchemy in detail before J2EE - I first came across them at the same time, but using SQL Alchemy was at work, and I basically had to learn in 2 days what a full semester worth of J2EE taught me. Perhaps that was just because learning the python stuff was so damn easy.

Now that I understand just how cool an Object Relational Mapping is, getting into Core Data was easy. Creating the same schema in SQL Alchemy (actually, using Elixir, so it was just object creation), and then in a Core Data xcdatamodel - basically the same process, was even simpler in Core Data: simply because it is a GUI tool, and you can see the whole model in one go, instead of having to scroll through a text file examining classes.

But doing GUI programming using the ORM is where Core Data really shines. Using Cocoa Bindings, you can just plonk down an NSTableView, and tell the object where it gets it’s data from. If you have two GUI widgets using the same data model, and you change selection in one, it even changes the selection in the other!

Core Data also helps look after Undo, Saving and all other sorts of goodness I haven’t even come across yet.

The only thing that Core Data isn’t good for is a multi-user system - or more precisely, a system where multiple users are accessing the data at the same time. I’ve used SOAP as the messenger format, where I had a rich client accessing services provided by my server, but this is cumbersome. I’ll just use Pylons, or perhaps Django to do a web application - where the interface is largely a Web Browser. In this instance, it will probably be best to just stick to a python-based approach. I’m tempted by WebObjects, but that would still require me to use Java.

If only Apple would release a distributed Core Data. I might be able to do something kind of cool with Distributed Objects, for a rich client, at least, but for Web, I may as well do the whole thing in python.

View Comments (0)   RSS Feed for Comments on this Post

Categories are pretty cool in Objective C. Say you have some special methods, which you’d like to have another class to have, or wish to override a method for every instance of a class.

You can, with every OO language, just create a sub-class. However, this would mean having to use the sub-class in all of the locations throughout your program.

With categories, you can just (re)define a method, and use the same class. All of your code will know that this new method is available, or will automatically use the overriden version.

This to me obviates the need which is apparent in most OO languages to sub-class the hell out of everything. This in fact is what I like most about Cocoa/Objective-C. Most of the time, you don’t need to sub-class the widget and other API classes. Instead, you have archived instances of them.

Much tidier.

View Comments (0)   RSS Feed for Comments on this Post

One of the criticisms of many languages is that they are so complex, that people do not use all of the features of them. Meaning that one person might write a program in a language that another, fluent reader/writer of that language may not be able to understand. I think this is just rubbish, at least to some respect. If you see some code in a language that doesn’t make sense, then hopefully you should be able to either figure it out, or look up in the documentation (what, your language doesn’t have complete online, searchable documentation?) what is going on.

A similar thing happened to me the other day - and I didn’t even need to look up in the Python documentation to see what the story was.

People often criticise python because it lacks the ability to mark a variable as private, or protected. All attributes of a class are automatically public, and it is only really a guideline that if you prefix a variable with one or two underscores, you are marking it as protected or private. Namespace mangling means that this becomes slightly more than just a guideline, IIRC, although I’m not really sure of how this all works. Basically, the theory is that if you prefix an attribute, that is a marker to users of your API that you really shouldn’t use this.

I’ve often been frustrated when writing Java programs that attributes aren’t accessible, but I can see the value in only allowing protected access, through getters and setters. You can do this to prevent user classes/objects from putting garbage data into your attributes. This becomes even more important in Python, as it’s dynamic (but still strong) typing means I could put a “GOO” in where you expected an integer.

I discovered yesterday, as I was implementing a database in SQL Alchemy, that python has the ability to only allow protected access, through getters and setters, using the property function.

So, for this project, I have a database, where Sheep objects are stored, along with a variety of attributes. Sheep can be either Rams or Ewes, but the data that is stored about a Ram is virtually identical as that stored about a Ewe. However, using polymorphic mapping, I can have Ram and Ewe objects both stored in the Sheep table, since they are both sub-classes. Furthermore, I can use the gender attribute to determine if the object retrieved from the database should be a Ram or a Ewe. This is much better than anything I was able to do with J2EE, at least as far as I could find.

Because I am using a composite primary key (again, not something that is easy to do with J2EE, without defining a Primary Key class, ugh), and a Sheep can have a .sire and a .dam, which internally is stored with three columns in the table each, and that a sire/dam must be older than the sheep in question (and of particular gender), then some form of restricting which data can be assigned to the sire and dam attributes (which, using SQL Alchemy are references to other objects in the table).

Thus, I can have the following lines in the class definition for Sheep:

    sire = property(_getSire, _setSire)
    dam = property(_getDam, _setDam)

The crux of this post is that now, if you attempt to set a sire, using: sheep.sire = otherSheep; then it will pass the contents of otherSheep to the _setSire method of the Sheep class.

This goes even further - it now allows me to either use a Ram object, or a Sheep object (and uses some checking to ensure that it is a Gender=M), or even just a string that is the string representation of a sheep - the value that I am using for the three parts of the primary key. It will then look up the sheep in question, or create a new one if it doesn’t exist in the database.

Thus, a sheep is identified to the real world by three things: it’s flock number, such as 160188, the year it was born, like 2004, and the tag number, such as 040001, which is not unique between flocks, or necessarily years. For the flock this system is designed for, the tag number is unique for sheep of a given century, which would generally suffice, but in a situation where there were more than 9999 sheep born every year in a flock, then you wouldn’t just be able to use the last two digits of the year for the start of the tag number.

Thus a sheep might be declared as 160188-2007-070001, which is a unique identifier of that sheep in the world (or at least Australia!), but having this as a primary key on it’s own means more jigglery would be required to get the flock number and year. By storing as three seperate parts it is possible to keep an easy reference to the flock, and the year. There is no chance of a data integrity error as there might be if I stored the id, then the year and flock number. It would be possible then to change one without the others being udpated.

I’ve also done something quite clever with the mapping of the sire/dam relationship. By having different classes for Ram and Ewe, and mapping sire/dam to these, it is possible to use the same backreference term, offspring, to generate a list of sheep who are offspring of the Ram or Ewe in question. If you don’t use subclasses, then it isn’t nearly as neat.

If you can’t tell, I’m loving SQL Alchemy. I had some issues figuring out what the hell was going wrong with my relations between sheep and sire, but it turned out that you have to use the remote_side argument to the relation function to do what I wanted. Funnily, it was working with dam, but not with sire, but now it’s much more solid. No more circular reference constraints either.

By the way, this project is going to be an online database for livestock information, allowing potential purchasers to research pedigrees online. If anyone else is interested in purchasing it, send me an email. At this stage, it is sheep only, but will be easy to change to, say cattle if they are your thing…

View Comments (0)   RSS Feed for Comments on this Post

I really don’t respect or like Java as a language. I’m not going to go into reasons why here, but I am going to bitch about J2EE and Enterprise Java app development under NetBeans.

Now, I’m not just a clueless student annoyed with stuff that doesn’t work because I’m a gumby. I write enterprise applications in python, apache and SQL Alchemy for my day job. It shouldn’t be as hard as it is to develop in NetBeans.

For starters, if I deploy my code and it fails, I shouldn’t be able to redeploy it again and it works. Same goes for building. I have found instances where I can build and it fails, and then an immediate re-build succeeds. And I’m not talking “Clean Build”, just a regular ordinary build.

More to the point, if I do an “Undeploy and Deploy”, and I get a whole load of exceptions, I kind of expect that the deployment has failed. But if I then do a build, it works.

And, it appears that if I build without a fresh redeployment, it fails.

This is just build and deployment issues. I’ve also had instances where code has failed, when I was pretty fucking sure it should have worked. I was throwing exceptions all over the joint (or, more correctly, the JVM or some other bit of technology was), and they were meaningless. A redeployment and the associated re-build, and it worked.

Developing a similar application in python+SQL Alchemy is faster, doesn’t appear to run much slower, and is much, much easier to read later. Yet, it is not taken seriously, because it isn’t Java.

Ugh.

View Comments (0)   RSS Feed for Comments on this Post

Next Page »