SZC’s Agglomeration

{an agglomeration of laconic notes in relation to anything that sparks my interest}

Python :: Mapping a string to a dictionary

Posted by Stefan Camilleri on {2009.February.9}

/** Today at work I had to find a neat way of mapping a string of parameters into a usable dictionary in Python.  Searching on the web didn’t reveal any results, so I had to get my hands dirty and figure it out for myself  */ {

The data provided is in the form of a string “option1=1 option2=2 option3=3″

The solution is quite neat and simple too…

value = “option1=1 option2=2 option3=3″

dict( map( lambda x: x.split(“=”), value.split()) ) )

Essentially this produces a list of lists which is then fed to the dict function as follows:

[['option1','1'],['option2','2'],['option3','3']]

Simple :)

}

Posted in dev, python, tips, tutorial | Tagged: , , , | 2 Comments »

Java Performance Tuning

Posted by Stefan Camilleri on {2008.August.28}

/** The more we abstract, the more a performance hit we get.  Paraphrasing Mr Donald Knuth, having an efficient and elegant solution, is an oxymoronic claim */

So is it all lost?  I love code elegance, I find it exciting; and yes, I know that is a very geeky thing to say.  Even though Java is potentially one of the most elegant languages around nowadays, the myriad of frameworks and modular approaches available lead to levels of abstraction that definitely have a performance hit on the vm.

But not all is lost, Kirk Pepperdine, who as most Java people know, is a top Java Champion, wrote an interesting article about this on sun.com {

I invite you all to hop over and have a look.  The tips in here are not totally specific to Java, so you can learn a lot from this.

http://java.sun.com/developer/technicalArticles/Interviews/community/pepperdine_qa.html

}

Posted in dev, java, performance, tips | Tagged: , , , | Leave a Comment »

visual studio 2008 sp1

Posted by Stefan Camilleri on {2008.August.12}

/** Visual Studio 2008 Service Pack 1 is finally out */

More info on my friend’s blog {

http://marlongrech.wordpress.com/2008/08/11/visual-studio-2008-net-35-sp1-is-out-now/

}

Posted in .NET, dev, visual studio | Tagged: , , , | Leave a Comment »

regular expressions (a.k.a. regEx) tutorial #1

Posted by Stefan Camilleri on {2008.August.10}

/** Over the past few years, many developers have commented about their lack of understanding on how regular expressions (regEx) are written; yet all admit how helpful it would be to them if they knew how to create them */

I am therefore going to jot down a quick ‘teach yourself in 5 minutes’ for the basic regEx expressions.  I will not go into too much detail, since most advanced features are not used anyway on a day to day basis, yet the most simple of regEx can be really helpful in coding, even for simple search and replace operations in your IDE.  I assure you that once you get the hang of them, you will fall in love (as I have) {

The Basics

regEx is basically a pattern search, most would already know this, but I’m just stating it just in case.  As in the find menu, you would enter the word you are looking for, in actuality, that word is a pattern.

For this tutorial, I’ll use a mnemonic which I like -> ‘Californians Like Girls in Small Bikinis’ (well not only Californians maybe ;) )

I will be using Javascript notation for the regEx here, yet you can use this in any other engine by omitting the / from the start and end of the expression.

I’m going to try not to overload my tutorial with tech explanations like lexicons and tokens, or how we could break this down using BNF, since I’m pretty sure that anyone familiar with them can figure them out for themselves.

The structure I’m using is:    /Regular Expression/   ->   Matches highlighted in my mnemonic
Followed by a brief explanation.

Simple literal regEx match

  1. /al/ -> Californians Like Girls in Small Bikinis
  2. /ni/ -> Californians Like Girls in Small Bikinis

In this case, this is a simple match, returning the instances of the searched token.  The expression will match the first instance, the blue instance is returned on subsequent matches.

Simple wildcard regEx match

  1. /.al/ -> Californians Like Girls in Small Bikinis
  2. /ni./ -> Californians Like Girls in Small Bikinis

Here we introduce the . wildcard.  This means ‘any character’.   So the first term matches ‘any character followed by a and l.

Unbound repetition of patterns

  1. /.*al/ -> Californians Like Girls in Small Bikinis
  2. /ni.*/ -> Californians Like Girls in Small Bikinis
  3. /.*Cal/ -> Californians Like Girls in Small Bikinis

The * symbol is what we call the ‘Kleene Closure’, this means ‘0 or more instances of’, so what we are searching for here is ‘0 or more instances of any character followed by al’.

Note that in the first instance, the second al is matched, not the first, since the regEx acts on the whole line.  Also note the third example, since we state ‘0 or more’, ‘Cal’ is still matched.

One-or-More repetition

  1. /.+al/ -> Californians Like Girls in Small Bikinis
  2. /ni.+/ -> Californians Like Girls in Small Bikinis
  3. /.+Cal/ -> Californians Like Girls in Small Bikinis

This is exactly like the previous example, with one difference, the + means ‘1 or more’, so in the third example, nothing is matched, since there is no instance of any character followed by ‘Cal’

Character classes

  1. /[Cm]al/ -> Californians Like Girls in Small Bikinis
  2. /ni[ai]/ -> Californians Like Girls in Small Bikinis

The [ ] denote a character class.  This means ‘any one of these characters’.  So in the first case, we are looking for a ‘C’ or an ‘m’ followed by ‘al’, which gives us two matches.  You can see why the second one only returns one match.

Negated character classes

  1. /[^Cx]al/ -> Californians Like Girls in Small Bikinis
  2. /ni[^ai]/ -> Californians Like Girls in Small Bikinis

This is the negation of the previous example.  The ^ at the start of character class means ‘anything that isn’t on of these characters’.

It is important that the ^ is within the [  ]

Start of sentence

  1. /^Californians/ -> Californians Like Girls in Small Bikinis
  2. /^Bikinis/ -> Californians Like Girls in Small Bikinis

The ^ at the start of a regEx means ’start of string’, so we are looking for the ’start of a string followed by a C, and a, etc…

End of sentence

  1. /Californians$/ -> Californians Like Girls in Small Bikinis
  2. /Bikinis$/ -> Californians Like Girls in Small Bikinis

The $ in this case matches the ‘end of a string’

}

That will be all for this tutorial.  There will be other tutorials to follow this, which I will add to this post as links once I create them.

Any feedback would be appreciated.

Tutorials ToC:

  1. The Basics (this tutorial)
  2. Shortcuts
  3. Search and Replace (matching groups)
  4. Saving memory, and shortening your regEx
  5. Advanced: Positive and Negative lookahead and lookbehind.
  6. A quick’n dirty way to test regEx, as well as examples in C#, Java, VB and JavaScript

p.s. for anyone curious as to what that mnemonic is for, it is basically used to remember the 5 layers of the epidermis, starting from the lower layer. i.e. corneum, licidum, granulosum, spinosum and basale.

Posted in dev, regex, tutorial | Tagged: , , | 8 Comments »

tutorial deployment in progress…

Posted by Stefan Camilleri on {2008.August.6}

/** I recently realised that many a time we tend to forget the most fundamental of tasks in the language we use on a daily basis */

In most cases, this is more due to the fact that our IDE’s tend to make life so easy for us, that we tend to lose touch with the most routine stuff, like create a WAR file manually, or creating mappings for our ORM of choice {

I shall therefore start jotting down notes on how to perform these most mundane of tasks, so as to keep a handy reference which I can come back to should I wake up one morning and not find any coffee available throughout the entire island :)

For starters this will be mostly focused on Java technologies, since this is my daily lingo, yet there will also be excerpts of other lingos such as C#, PHP and JavaScript… amongst others (though I seriously doubt there will be any/much VB)

}

Posted in dev, tutorial | Tagged: , | 2 Comments »

date validation regular expression (improved formula catering for leap years and correct days in month)

Posted by Stefan Camilleri on {2008.August.6}

/** A friend of mine, João, very kindly pointed out a flaw in my date regex posted previously; the inability to validate the correct number of days in a month (30 or 31) */

Well, consider it done, this regex will now correctly validate days in a month as it should be (next I’ll try to cater for leap years) {

Date format is dd/mm/yy or dd/mm/yyyy (if you need US support let me know).  Also note, that this regex does not use any lookbehind, so it should also work perfectly in JavaScript (which is probably where regex is used most anyway)

^(?:(?:0?[1-9])|(?:[12]\d)|(?:3(?:(?:0)|(?:1(?![\\.-/](?:(?:0?[2469])|(?:11))))))(?!(?:[\\.-/]\d{1,2}[\\.-/])(?:(?:[02468][048]00)|(?:(?:\d{2})?(?:(?:[2468][048])|(?:[13579][26])|(?:0[48]))))))[\\.-/](?:(?:0?[1-9])|(?:1[0-2]))[\\.-/](?:(?:\d{2})|(?:1\d{3})|(?:\d1\d{2})|(?:\d{2}1\d)|(?:\d{3}1))$

}

p.s. inevitably there will be bugs… so if you find any, let me know!

Posted in dev, regex | Tagged: , , | 10 Comments »

cheat sheets

Posted by Stefan Camilleri on {2008.July.30}

/** All developers are polyglots, but we are not gods */

That’s when cheat sheets come in handy {

My Portuguese friend João posted this very handy link on his blog, and I thought it worthy of sharing

Cheat Sheets

Cheat Sheets

}

Posted in dev, shortcuts | Tagged: , | 2 Comments »

date validation regular expression

Posted by Stefan Camilleri on {2008.July.30}

/** Here is some handy regex to validate a date using british locale (dd/mm/yy or dd/mm/yyyy) */

Note, this allows / \ . or – as delimiters {

^(?:(?:0?[1-9])|(?:[1-2]\d)|(?:3[0-1]))[-.\\/](?:(?:0?[1-9])|(?:1[0-2]))[-.\\/](?:(?:\d{3}[1-9])|(?:\d{2}[1-9]\d)|(?:\d[1-9]\d{2})|(?:[1-9]\d{3})|(?:\d{2}))$

}

p.s. this regex has now been superseded with an improved version here

Posted in dev, regex | Tagged: | 2 Comments »

force browser to download a file

Posted by Stefan Camilleri on {2008.July.30}

/** A common problem which I have come across multiple times in the past is that of forcing a browser (any browser) to download a file rather than try to open it. */

There’s a myriad of solutions out there on the web discussing this very issue, and I never gave much thought to it until yesterday. {

I came across an excrept of code at my client which was trying to send a csv file to the client (i.e. the client pc), yet the browser (all flavours) insisted on trying to render it, rather than download it.

The solution turned out to be particularly simple; something we all learnt how to do very well when we wanted some sweets as kids… i.e. LIE (to the browser this time)

All you have to do is set the content type to application/binary and the browser will simply not bother trying to do anything with it.

Here’s the magic expression in Java, yet of course this also applies to any other lingo (and if you have a problem, ask me, tell me your language, and if I can, I’ll show you how).

response.setContentType("application/binary");

NOTE: IE works slightly different from other browsers (what's new?), in that if you cancel the download, it will close the socket throwing an exception on the server. You of course will need to cater for this.

}

Posted in dev, java | Tagged: | 1 Comment »

ridiculous()

Posted by Stefan Camilleri on {2008.July.16}

/** When I just realise that no matter what, I can never cease to be amazed */

I’m working on some code right now, fixing up some system changes, and I have come across lots of crap, but this one tops it all IMHO {

400 lines of undocumented code in one method!

Go figure… and here I was thinking I was a crappy programmer when I was 20 since I used to commonly put around 50 to a 100 lines of code in my methods.

}

Oh well… just one of those nuisances you know.

Posted in aesthetics, architecture, dev | Leave a Comment »

posting code in wordpress

Posted by Stefan Camilleri on {2008.July.13}

/** Cruising the sea of blogs by the coding monkeys here on WordPress, I cannot fail to realise that there is a good bunch of people out there who have not yet discovered how to use the wordpress way of pasting code into their blogs */

So, in the hope that some will actually read this and take advantage of it, here goes {

Well, it is no rocket science really.  All you need to do is to enclose your code in a set of

put your code here

where ‘lingo’ can be any one of {

  • cpp
  • csharp
  • css
  • delphi
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • sql
  • vb
  • xml

}

}

Hope that helps :)

<h2>Happy blogging</h2>

Posted in aesthetics, blog | 2 Comments »

programmingFonts()

Posted by Stefan Camilleri on {2008.July.12}

/** So, the never-ending debate as to which is the best typeface to use for programming has also started brewing in my brain. */

I found tons of posts on-line debating what was the best font, and for one, I always felt that Microsoft’s Consolas was by far the best programming font around (and technically it is almost, but not quite, entirely like the best coding font around).

To summarize what sparks the requirement of such a font, it basically boils down to {

  1. Programmers like fonts that look good at SMALL point sizes.  Not because we are masochists (well some of us probably are), but because we want to see as much code as possible on one screen.  Which would also explain the phenomenon of why all programmers push their graphics cards to the highest resolutions possible (oh yes, and it also looks gorgeous on a hi-def lcd).
  2. We spend hours on end reading funny words that make sense solely to us and to lexical analyzers, so we need the font to be VERY legible, and to distinguish clearly among similar characters like 0,O,o, ilL1… among others (oh yes, and the myth is true, most developers, after a full day of code, go home to see…more…code!)
  3. We use words like ’sexy’, ‘perfect’ and ‘elegant’ to describe an algorithm, so we should also be able to use such terms to describe the font we use to write such algorithms in.

}

So, to cut it short, here are the contestants {

  1. Proggy Tiny 10pt

    Proggy Tiny 10pt

  2. ProFont 7pt

    ProFont 7pt

  3. Envy Code R 7pt

    Envy Code R 7pt

  4. Droid Sans Mono 7pt

    Droid Sans Mono 7pt

  5. Courier New 8pt

    Courier New 8pt

  6. Consolas 8pt

    Consolas 8pt

  7. Bit Stream Vera Sans Mono 8pt

    Bit Stream Vera Sans Mono 8pt

  8. Anonymous 7pt

}

And that is it really.  I have concluded that for now, after testing all these fonts, Consolas remains the one of choice for me.  ProFont was terribly inviting, yet the boldness of the font is just way too heavy in comparison to the normal formatting.

Now I’m just hoping to find a font which looks as good as Consolas does in 8pt, but at 7pt, since 8 is just a bit too big for my likes.

Posted in aesthetics | Tagged: | 12 Comments »

reBirth()

Posted by Stefan Camilleri on {2008.July.12}

/** This is the re-birth of my blog.  Supposedly this would be the fifth or sixth time that I am creating a blog, so I am just curious as to how long this one will last! */

Reasons I’ve had a blog in the past are as follows {

  1. Livejournal.com :: the reason for this blog was simply because my then girlfriend had one and I wanted to experience what she felt when writing in her blog.  This didn’t work and I ended up spamming my own blog with random quizzes and stuff :: Return -> False
  2. Greatestjournal.com :: also was born during my ‘blogging-girlfriend’ stint.  I wanted to feel like I was ‘contributing’ to her blogging life, so I recommended that everyone (all friends etc…) move to this blog.  Everyone – to my surprise – DID actually move to this blog, but I soon lost interest and I ended up spamming my own blog with random quizzes and stuff :: Return -> False
  3. Blogger.com :: I don’t totally recall why I even had this blog.  It was probably more of a ‘tracking news’ blog than anything private about my life; as were supposedly the previous two.  Likewise, a few months in, and I ended up spamming my own blog with random quizzes and stuff :: Return -> False
  4. WordPress.com :: this was possibly my most successful blog to date.  I persistently tracked news and hand-picked interesting stuff.  It was so successful, that once a journalist friend of mine even got prompted by one of my posts and published an article based on it via the public news.  Then again, this blog was born whilst I was studying at university, which would account for why it lasted the longest.  In the end, I ended up spamming my own blog with random quizzes and stuff :: Return -> False
  5. Livejournal.com :: back to livejournal.com it was.  This time, I wanted an anonymous blog, where I would track things of interest that I learnt during the day and any targets that I had set for myself.  Unfortunately, my ex figured out who I was based on my style of writing and general interests.  Wow, kudos to her for knowing me so well!  Ultimately though, I almost ended up spamming my own blog with random quizzes and stuff (but I didn’t :Þ) :: Return -> False
  6. This would be the sixth attempt.  Hopefully this one will last.  Over here I plan to track all my private projects and am keeping this blog specifically for development purposes.  Eventually I’ll create my own blog and transfer everything to it, I have some cool ideas (at least I think they’re cool), though until now this will have to suffice :: Return -> Variant

}

…and well that is it!

Posted in ramblings | Tagged: | Leave a Comment »