Filipe De Sousa

Messing around with PyGame

Programming, Python, Personal, Games, PyGame

I recently came to the realisation that I had never written a graphical game. Text-driven, or simple question-and-answer types, I have written, mostly just for practice and understanding of certain ideas. When it comes to something with graphics drawn onto the screen, whether 2D sprites or 3D models, I’ve never ventured into that.
Having become so accustomed to Python, and using it so often as-of late, this made a logical choice as the language to use. It helped that I had read up on PyGame, and thought it made for an ideal starting point. I was right, too. Within an hour, I had all the dependencies checked off the list and PyGame was running with just a one-colour screen up to confirm it was working.

So far, I’ve managed to write up a few bits for an RPG, using some basic sprites. It has some animations, map-building and character movement. It has a long way to go before it’s an enjoyable game, but it’s a base to start something bigger on.
Currently working on some form of viewport that follows the player’s sprite, allowing for large maps that go off-screen. This is much less fun than the rest of the project thus far, not to mention requiring more work too, but once I’m done, I can start adding more interesting features, like transition points between maps, which should begin to make it more of a real game.

So far, PyGame has just made it really easy to put things together. It’s a great framework that just gets out of your way when you don’t need it, and provides easy access to everything when you do. This just makes it very easy to learn, both the framework and about making games.
I’m more excited about writing code for this game than for most of my previous projects, mostly because I can enjoy it afterwards, and who doesn’t like that?

Learning with the Raspberry-Pi

Programming, Operating Systems, Raspberry-Pi

Today I learned that Cambridge University has a free course, Baking Pi: Operating Systems Development. My Pi is just sitting around, doing little or nothing, and this is probably the best way to get around that. I’ve always wondered about Operating Systems and developing them, even having taken the module at University that turned out to not have much detail, and this is going to be a brilliant way to learn. Now I just need to get a working setup for my Pi, or it’ll be the same as not having one, really.

Anyone struggling for ideas, with a Pi lying about, and with an avid interest in programming and operating systems, should really check it out. It won’t be easy, but nothing interesting in computer science ever is, or should be.

Another Day, Another Idea

Programming, Facebook, Social, App Idea

I suck as a friend, because I forget birthdays all the time. I barely remember my own, let alone remembering everybody else’s, so how about a social app that uses information from Facebook (I was going to also state “and other social sites,” but there are few popular ones that have personal information) about your friends — think name, birthday and gender — to personalise one of a number of random messages, to make things seem more personal. Though it sounds like something that only horrible friends would use, it could really be a nice idea overall, depending on the options the app has.

Feature ideas:

  • Name fill-in
  • Message categories to choose from
  • Random choice of message
  • Discernment of gender for choosing suitable messages
    (no woman wants to read, “How’s it hanging?” Actually, nobody wants to read that…)
  • Ability to choose which friends to send messages to
  • Personalisation of messages (category, specific messages, nicknames, etc.) for each friend

Simple set of ideas, and an app that barely gives functionality to people. That seems to me to be exactly what people want, much of the time.

Idea for a Translation Site

Idea, Programming, Translation

Before I get started, let me state outright that this is just an idea, one that might not work as expected. My understanding of how Android handles translations is quite simple, and where I base some of my, possibly wrong, assumptions are made. Again, this is just an idea and nothing more.

So let me get started. A website that allows voting on translations to various languages for a given word or phrase. This is not the last part of the suggestion, as this would just allow choosing the best translations. The real magic would be in putting together a list of translations, for various chosen languages, into the correct format for the application. For Android, this would be multiple strings.xml files with the translations labelled with IDs (suitable IDs for each translation being used), split into various folders, labelled with the various regions and dialects that the translations are in. This would take most of the hassle out of translating an application, as long as the developer is willing to put in a little work to read strings in, rather than using literals everywhere. A little extra magic would be taking in a suitable file, for the system to figure out and automate the translation.

This idea might work, it might revolutionise the translation of applications, and it may well help lazy developers (guilty, right here) in getting their app seen and used by more people around the world. If you think this idea is useful, in any way, shape or form, run with it. I’m not sure I could do much with this idea, but my opinion is that something similar to this would be particularly useful, it just needs someone to work on it and implement it.
I hate being seen as conceited, especially for such a stupidly simplistic idea, so I leave it to someone wiser and better at developing ideas into products than I am, as I still think it’s a cool idea, with a lot of uses.

Word of the day: Petrichor
Scent of rain on dry earth.

What is wrong with the 40-hour work week?

Work

Everything I have heard about working at American companies seems to boils down to long hours, and working while at home. I can’t stand the idea of not having a work-life separation (even if I have no life to speak of, I have a right to protect it). I do over 40 hours a week, every week, sitting in a relatively uncomfortable chair with monitors too bright, air conditioning beating down on my eyes, and lights that do more burning than brightening. I’ve done longer days of 11 hours, finding that as my upper limit, where I haven’t found much of a productivity gain past the 8 hour mark. In fact, it just puts me into a bit of a mood by the time I get home, and leaves me tired. Considering I rarely sleep that well that can carry over to the next day, and I just end up losing so much time and productivity. So I just keep thinking, what is the point? What is the American obsession with working themselves silly?

For me, there is no real advantage to working stupidly long hours. I don’t get paid past the 40-hour mark as it stands, so I can’t claim that the compensation is good enough. In terms of learning, I do it more effectively when I’m happy, calm and well-rested. There are plenty of successful people in all industries that work a more reasonable amount of hours than the seemingly expected minimum of 60. After all it should come down to the quality of the work done, not quantity of the hours worked. If working long hours lowers the quality of the work, where are the advantages?

This post has mostly been a rant, but maybe it will stop me from responding to people on Twitter so questioningly. I have recently seen plenty of tweets along the lines of “If you don’t want to work more than 60 hours, work at McDonald’s,” which is an unfairly judging remark, as there are people I know working at fast-food places, hotels and bars that work 60 hours a week because they need the money, but cannot find better. Are they passionate about the job? Definitely not, but the money is a necessity.
The insinuation that working more than a certain number of hours implies passion for the work, and only “passionate” people need apply for the computing industry does not pan out. If everybody is different, then it stands to reason that everybody learns and works in different ways. This industry is fraught with personal preferences from monitor setups, programming language preferences, keyboard and mouse choices, even to where the curly braces should be placed on a line. Does it not stand to reason that everybody should make up their own mind over how many hours they work?

Again I must state, this post is ranting on. I tend to have strong views over people that issue statements in a one-size-fits-all style, especially one such as this. At times, I partially agree, but just keep remembering that people will never all fit into one box. Hell, try to fit all humans into a box, and suddenly you have the questions of gender, race and religion cropping up, because even if we are all the same species each and every one of us is different, and these differences should always be celebrated and never mocked.

Blog Test On MDEdit

Programming, Personal, Android

I’ve been working on MDEdit on-and-off, just as an excuse to keep up with some Android development and to play with Java. It has become a pretty nice, usable app, though I haven’t really given it a proper test just yet, so I’m writing this post on my Touchpad running CM9, and with the latest MDEdit installed on it. It’s not missing a beat, as I’d expect from such a simplistic app, and that makes me pretty happy!

There’s not much to add on this point, so instead, I’m going to try a little bit of markdown, and there is some more below.

Missing Features

There are many features still missing, mostly because of my experimenting rather than implementing. Let’s add a list of them below.

  • Settings activity
  • Changing editor colours
  • Changing viewer HTML
  • Portrait orientation
    Has landscape based on sensor rotation, but any other mode destroys the fragments and causes endless null values.
  • Spacing around edges of editor
    The padding hasn’t helped, and writing edge-to-edge is an annoyance
  • Changing the markdown parser
    Obviously having some issues with lists
  • Changing text size
    The text is far too small on my tablet

Possible Release

The real answer is eventually but in some part of my mind, I actually think never.
As far as markdown editors go, this is very barebones, and limited in appeal. The only real “feature” is the fact it converts markdown-formatted plain text to HTML, big whoop! Wanna fight about it?!

Wrap it up

This post was mainly an excuse to play with the app. The app itself just gets out of the way and lets me write, with the swiping to go between editor and viewer being the only break in concentration, though being better than split-screen view of editor and viewer in one.

The app has potential to at least be my favourite little editor on my tablet. It’s capable, and that’s the most important thing in reality.

Python, Python, Python

Programming, Work, Python

I can’t get Python off my mind. It’s an addictive language, and I’ve been using it so much at work that it’s just my go-to language. I love the syntax, I love the standard library, I just love it. Probably one of the most English-like programming languages, Python has become my main language as of late. I’ve been tasked with writing various scripts, and Python just comes through every time.
I no longer worry about how to read all of the command-line arguments my script might need, or how the configuration file will be parsed, or even the best way to read in XML. The modules are already in the standard library, I just import and go.
It really also helps that the documentation is really clear and to-the-point without excessive examples to clutter the readability of the page. I usually read other people’s examples to get to grips with how to do something — Fragments and ViewPagers never clicked with me until updating the Android Developer Tools, Eclipse and downloading the official examples — but have recently only needed the documentation, and that’s becoming a rare occurrence now that I’m far more familiar with my favourite modules.

What is so special about Python?

It’s not the fastest language and is a slightly heavy install, but it abstracts away so much (of course, it’s not the only language to do so e.g. Ruby, JavaScript, Lua, and countless others) that you probably don’t care about when writing a fire-and-forget script.
For example; I have a function that takes in a sequence of objects, checks equality of each object in the sequence with a control object, then spits out a result that states whether any of the objects in the sequence were equal to the control. Having to worry about types becomes a problem, then the flexibility of the types to be handled also becomes problematic. To make things easier, let’s look at some sample code (mind the rusty Java example).

Java

// Omitting class declaration for simplicity
public <T> boolean isInCollection(T obj, Collection<T> col) {
    for (T item : col) {
        if (item != null && obj.equals(item))
            return true;
    }
    return false;
}

Python

# Purely a function in a module, no class declaration
def inseq(obj, seq):
    return obj in seq

This uses just the standard in to check whether an obj is in a sequence. Simple as. It’s a slight cheat considering what I stated, but it works just fine, and is just so simple to boot. It doesn’t really even need to be in a function.
To make things a little fairer, here’s an example that checks whether the object is None first.

# Omitting class declaration again
def inseq(obj, seq):
    return any(obj == item for item in seq if item is not None)

Simply put, that example loops through the sequence, assigning the next item to item, checks it’s not None with if item is not None, and then checks equality with obj == item. The built-in function any returns True if any item in the sequence evaluates to True, otherwise returns False.
The sheer simplicity with which that example can be achieved is amazing. The Java version has almost as much flexibility — though not allowing arrays (you’d need to overload the function for that) — through the use of generics, but requires the explicit null checks (though most equals functions check for null values, so this is a cheeky cheat on my end) and calling of the equals function on the object for comparison.

The power of simplicity is what really stands out in ‘scripting’ languages. That isn’t to say that compiled languages are not useful. Their power and performance benefits are what keep us coming back for more, along with making them the only truly viable solution for large systems. I have only gotten my hands dirty with C++ a small number of times since starting at BNP Paribas, but am preparing to do more of it soon, with C# being proposed for new development, and Java having been introduced in other departments. The speed that everything needs to happen at on the internal systems is so great that interpreted languages are never considered except as a joke. My own experiences with Python in the bank are just for fire-and-forget scripts, where I don’t care about the object’s type, I just find out whether it quacks or moos when I poke it into action. If the script takes five minutes to process a 100MB XML file, or a 250MB log file, the user waiting won’t really care once he or she realises that it didn’t require a lengthy login process to then use a heavyweight app just to get a few lines of results.

Who cares then? Big woop, wanna fight about it?

The power of simplicity is a wonderful thing, and would make an interpreted/scripting language a great candidate for teaching. Many of the newer generation interpreted languages have good object-orientation, but without bogging down a first-timer in objects when the first lesson is to print ‘Hello, world!’
This of course doesn’t mean you should learn to code, but for teaching kids, you don’t want to introduce them to memory handling, type-casting and object-orientation so early on. It will just scare them off with thoughts of complexity.

The simple nature of the language in question is great even for pros. You can prototype the logic of an application in a few minutes to a few hours, test your formulas, and make sure it runs exactly as you need it to. It can be used to fill the gaps, providing advanced users the chance to make their own functionality using extensions of the main application. Hell, use it as just another scripting language if you must. These higher-level languages have advantages in abstracting not only syntax, but the tedious low-level intricacies of the computer’s equipment, so that moving a file across discs and transferring data between machines is a far simpler operation.

I guess what I’m getting at is

Don’t hate, appreciate. I’ve talked to some programmers that think these languages are just for fun, that they’re just esoteric in nature, but haven’t given them a second thought and noticed them hidden power. Everything in the world of computers has its place and its uses, so stating that something is ‘completely and utterly useless’ does not generally bode well (even those fart apps have their place and use, after all).

Anything else?

Of course, as per usual, I have forgotten my original reason for even writing this post, and instead descended into opinion and drivel. I really just wanted to write about Python, add something to my completely ignored blog, and just get some writing practice in too.
On another note, approaching four months at BNP Paribas as a developer, and I have to say I like it. Things are rather relaxed and laid-back in my department, allowing me to work and learn at the same time without difficulty, and without the pain of whips at my back. I arrive early and leave late every day of my own volition, under nobody else’s will (I do try to stay later, but there are only so many hours I can sit at a computer for).

Since I’ve lost my train of thought, and it’s getting late for my to be typing away, this post is left, as always, feeling unfinished.

Long Break

Personal, Programming, Work, WheresMyTrain

It has been a pretty long while since I last wrote a blog post, and since then a lot has happened, but little has changed.
FDM came through with their spiel and found me a job. I have now been at BNP Paribas (can’t say which department as my head would explode with the number of acronyms) for just over a week, and am fitting in nicely.
That is about it. Little has changed. Instead of going to an office in Monument, I’m now heading off to Marylebone every morning. The work is different and motivating, rather than disheartening and self-assigned as at FDM after training finished. I’m still just as confused and lost as ever, though now slightly more so since I’m in a new place and still adjusting a bit.
The important bit is that all is well now. It feels good having some motivation back.

So my longish break has come from those changes, which, though few and far between, have taken up a large portion of my time. During this break, I’ve also barely changed anything in the Where’s My Train app. This is rather annoying, because I had been making preparations, and there was some coding in spare-time that has been forgotten, or almost forgotten, about.

During one weekend I managed to add a rudimentary and backwards-compatible Action Bar that has become a major pattern of the Android 4 UI. It looks rather nice, without breaking up the rest of the design of the application. It was extended to the helper activities too, so as to have a universal look-and-feel.

Over the course of a few nights, I managed to get a long Python script working that would convert between Ordnance Survey (OS) Northings and Eastings (OSGB36 standard), OS Grid References of Northing and Easting and the worldwide WGS84 standard. Though it sounds like something only an insane person would do/think about/say or write, it’s actually highly useful. Transport for London (TfL) kindly gives a lot of GPS co-ordinates for their services, such as train station locations, bus stop locations and Boris bike locations.
The average mobile device uses the WGS84 standard for its advantage of being country-agnostic. TfL uses the OSGB36 standard to have higher accuracy within the British Isles (considering their name specifies the name of a city in the UK, this shouldn’t be surprising). The conversion between the two can require some long functions, and complex mathematics. For me, it mostly involved actually finding out how to convert between them, then realising that OSGB36 is not used anywhere near as much as OS grid refs, and is definitely not as easy to find information about.
What this all breaks down to, is that I now have some type of way to convert between co-ordinates systems, and should soon be able to add some map data to Where’s My Train. I’ll likely then have to change the name after I add bus stop locations, routes, and estimations of arrival times. Hopefully that won’t take another month-and-a-half to implement.

In short, I haven’t done much, but I have something to show for it. The new job has been keeping me busy most recently, I’ve been hacking away at things on weekends, but not so much during the evenings. I’m planning to complete a great many tasks in the mean time, hoping to add some brilliant features to WMT.
Hopefully I’ll actually get these plans accomplished within a reasonable timescale, so I can get back to planning out the future of WMT.

First Release on Android Market

Programming, Android, Market, WheresMyTrain

Yesterday I, at long last, released an application on the Android Market. It’s very exciting for me, to see something I’ve created being placed into an open marketplace like this.
The app itself is free, and indeed open-source, so I’m not making any money from it, and in fact am giving back more than I’m receiving. I hope people will appreciate this, and enjoy using the application. It’s uncomplicated, is not designed to get in the user’s way, and is simple to use.

The application is called ‘Where’s My Train?!’ (thinking of changing the name, since having seen some other applications with the same name, displaying information for national rail services), and gives you access to predictions of train arrival times for most of the London Underground lines and stations.
Transport for London (TfL) does not provide information of this kind for C2C, DLR, Overground, or Tramlink services, as they’re not entirely part of the network, even though they operate inside of London.
Stations that have yet to receive upgrades are bound to have little to no predictions due to their older systems. There isn’t much I can do on these two points.

For the moment, this application merely provides the afore-mentioned predictions, though it is planned to add far more features, to make it more useful above and below ground.

Where can I find out more about the application?

The source is hosted on GitHub.
General information is on the project page on my site.
The application itself on Android Market.

Hopeless development

Programming, Personal, Random

Writing an application without a well defined spec isn’t easy. A spec that has use cases, a few must-haves that make things more open-ended, and a list of requirements for implementation that are not always ideal, without even a paragraph to explain the problem that is attempting to be solved, just makes things tough. I’m sure the person that wrote the meagre requirements understands the issue, but considering I have no direct contact with them, there’s no easy route to get minor questions answered.
Now, I’m sure there are people that could use this as a decent enough starting point, could apply some techniques, make some personas, make a mental link with another system that fulfils the same, or similar, set of specifications, and make a decent system. I’m not stating it’s impossible, although I am saying I’m not as wise as many other developers on this Earth.

Is it such a difficult task to produce a useful set of specifications for training material? If you want me to learn something, then let me learn, don’t add obstacles.
This project has made me feel hopeless so far, and I’ve often wondered whether I’m good enough for a developer’s role. In speaking with some of the trainers, who were developers before, it seems the lack of guidance in the materials really is the issue here. An issue they have to deal with as well, since we need guidance, and they have to work to a marking scheme.

This project is still a massive pain in my arse, and it has projects that split off from it, adding features, moving it further, and meaning a lot of dead, thrown-away code. I’m hoping that it gets better, but really not sure of what is going to happen.
Looking ahead, I really hope that I never receive such a loosely-worded specification for a project, otherwise I’ll go nuts, rather than just looking at it blankly, and wondering what I’ve done to deserve such a terrible fate.

Testing server reachability on Android

Android, Programming

Getting into the thick of it for my first techie post on here, and very much hoping my code comes out well formatted. Time shall tell!

So let me jump straight into it. Android does not provide an easy method for all devices, that checks a server’s reachability. The fact is that many devices don’t have the ability to send a ping request, which hinders the use of InetAddress.getByName(host).isReachable(timeOut) as described here.
In all fairness, even if the devices DID all support ping, many servers do not.

So how does one get around this thoroughly annoying problem? Easy, really.
Open an HttpURLConnection via a URL object. D’uh!
It’s a really easy way of doing things, but it works well. If you need an actual equivalent of ping, then follow the linked-to answer and set up a System.currentTimeMillis() before and after that code, calculate the difference.

If you’re after a more complete implementation, which first checks whether a connection is even available, then we are really only adding a few more lines of code.
Here is the code I use, as an example:

public static boolean isReachable(Context context) {
    //  First, check we have any sort of connectivity
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnected()) {
        //  Some sort of connection is open, check if server is reachable
        try {
            URL url = new URL(“http://www.google.com”);
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("User-Agent", "Android Application");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(30 * 1000); // Thirty seconds timeout in milliseconds
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // Good response
                return true;
            } else { // Anything else is unwanted
                return false;
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return false;
        }
    } else {
        return false;
    }
}

This code obviously comes laden with the usual “This Works For Me™” hoo-hah attached to it, so check that it will work for you first off, and won’t cause any issues for you if you decide to use it.

The code itself is very simple to understand.

  1. The first two lines acquire a ConnectivityManager instance, then acquire NetworkInfo, which contains the information required for checking whether the device has some type of network connectivity available to it.
  2. Next, we check if NetworkInfo is null (for safety), and then if any connection is available
  3. If so, then set off trying to connect to our server
  4. Open a connection to the server, setting user-agent (if you like), setting connection (good idea to do so), and a sensible timeout (if it takes more than 30 seconds, it might be too slow for our purposes)
  5. Attempt the connection, and when finished, check the response code. A 200 response means we’re safe.
    Anything else means something is wrong with our connection/connectivity and it should be tested again before trying it.

This code worked out for me, fit into one nice static method, and proved a useful alternative to InetAddress.
I hope it helps you too.