Posts Tagged ‘Java’

2011w43

Sunday, October 30th, 2011

Misc tools and other goodies

Another work week, another set of “discoveries”, like less -S, crontab -r and that when you issue a command which in turn uses $EDITOR to launch an appropriate text editor, and you instead of an editor window is greated with vim: no such command, well then perhaps in one of your profile- or config-files for the shell you have a line looking something like this:

EDITOR=`which vim`

Yes, this happened to me at work on a box which only had vi installed.

Pontus also showed me some SSH escape sequences which could come in handy. The first thing to know about them is how to “activate” them, which is done with the tilde-sign (~).

So on my setup, this would mean “AltGr+¨AltGr+¨” followed by a some sequence (? for help, . to close the connection (very good for when the remote server has rebooted, i.e. the ssh session has died, but the terminal never got wind of it, so it just sits there), or C^z to suspend it.)

cp importantFile{,.bak} is a pretty nice pattern as well.

Finally, I found a new (and totally inappropriate but functional) way of using mscgen: to generate staffing schedules.

In this case, being the “tech responsible” at FSCONS, this means scheduling my eight slave^H^H^H^H^Hcamera persons across the four tracks and two days.

Experiences from last year made me divide each day up into two pieces (AM and PM) which makes for sixteen blocks, divided evenly across the eight volunteers (who I am ever greatful to) for a total of two blocks per person.

For that small amount of data, mscgen worked wonders and gave me a wonderful overview :)

As a sidenote, I really should try to post a “my picks” from the FSCONS schedule soon. Yet another TODO to push onto the stack… ;D

Java

A couple of nights ago Pontus told me about an “array shuffling algorithm” (e.g. good for when you have an array representing a deck of cards and want it shuffled) which basically revolves around iterating through the array once, starting at the back of the array, counting down and for each iteration use the loop-counter as the max value for the random number generator so that it always delivers a number (index) which is within the array itself, and then swap places if the index:th place and the loop-counter:th place of the array. That was a fun excercise :)

2011w23

Sunday, June 12th, 2011

myConf

This is a technology demonstrator of the FSCONS myConf concept that doesn’t rely on any server-side programming.

It also became my first project under git versioning.

myConf is a concept we’ve (FSCONS) been thinking about implementing since, IIRC, 2009.

Basically it should allow a participant to tailor a personalized conference schedule, instead of having to mark it up in a dead-tree version.

Or so is at least my understanding of the myConf concept.

In short it is a Javascript (jQuery) / JSON-powered site, from which I have now learnt two things:

  • It is as important (if not more so actually) to have a good JSON structure as it is to have a good database design, otherwise it WILL come back and bite you, hard
  • It is actually quite fascinating what one can do with Javascript (at least when a library is used so that you don’t need to even think about platform irregularities)

Expect a public release shortly.

vim foldsearch plugin

I was editing my sudoers file (I still haven’t gotten myself off sudo) and started wondering if there perchance wasn’t a way in vim to hide lines according to some pattern.

The default archlinux sudoers file is full of comments, to the point that it is almost hard to see the uncommented lines.

:g/pattern and :v/pattern only takes you so far, i.e. it shows you the lines, but immediately disappears when trying to edit or move or anything except just looking at it.

Luckily for me other people had already asked the same question, and yet other people had answered it.

Which lead me to the vim foldsearch plugin. Best of all, it is easy to use.

Search for something, i.e.:

/my pattern here

and then use <Leader>fs (I have mapped <Leader> to \ in my config, so for me that would be \fs) and voilà, all the lines not matching the search are folded away.

renameutils

I am sure I have already written about renameutils, or more likely about qmv, but it is worth repeating. qmv rocks!

wallpaper-switcher.sh

wmii is my window-manager, although I am probably running version 3.6 or something (i.e. not 3.9) so this might not be usable for people other than wmii 3.6 users.

Anyway, last Friday I got the idea to write a little script to switch wallpapers for me. Today I sat down and hacked it together:

#!/bin/bash
 
tmpList="$(ls -l ${HOME}/wallpapers/*.jpg | awk '{ print $NF }')"
tmpList=($tmpList)
 
randomWallpaper="${tmpList[$(($RANDOM % ${#tmpList[@]}))]}"
 
ln -fs "$randomWallpaper" "${HOME}/wallpaper.jpg"
exit 0

Links

shunit2 Unit-testing for (Bash) shell scripts, this is so cool :D
Akka for a simple way of writing concurrent applications in Java
Protolol jokes for nerds

2011w20

Sunday, May 22nd, 2011

Update: I suck! I forgot to make the URLs in the links section actually links… updated now.

Thunderbird / Lightning / iCal

I recently started receiving emails including iCal invitations that needed to be answered, but I had no idea how, and Thunderbird does not come with any such functionality out of the box.

The “Lightning” add-on, however, does give Thunderbird features to handle that, and it works really well (if one makes one configuration change in Thunderbird: go to “View” and ensure that “Display Attachments Inline” is checked)

On my netbook, I  must have already set this, as it just worked there, whilst I was dumbfounded and needed the guidance from this thread to get it to work on the desktop.

Now every email with an iCal thingy that needs to be responded to will display a question at the top of the email window/tab

chromebooks

I pretty much agree with everything said in this article and for those reasons, a computer running a “cloud OS” will not become a consideration for me, until the computer is running my cloud which I and only I have full control over.

Most people won’t, but I’ll take privacy over simplicity/ease of use (or whatever other selling point is being made about these products) any day of the week.

And while we are speaking about the cloud, and why I dislike it so much, it is convenient that the next topic is related:

dropbox

So, Dropbox, the simple cloud storage and file synchronization service turned out to have a rather huge security flaw: Their employees can (to my knowledge it hasn’t happened, but how would anyone except a potential offender know, and it is this uncertainty which makes me shy away from such services) access their users encrypted shares, since Dropbox stores the encryption keys.

Dropbox is probably just as legit as they have always been, and they have probably never done anything wrong, but I can’t say that incidents like these strengthen my confidence in “the cloud”, at least not clouds operated by third parties, or actually, anyone except for the individual herself.

And that is why it is good to see that alternatives are beginning to crop up.

Jsoup

A friend of mine is doing some Java (Android) hacking, and asked me if I knew of any good web scraping libraries. For Java, my answer was no. For Python I would have instantly responded “Beautiful Soup”. So my answer became: “If I were you, I’d Google for beautiful soup for Java”. And then I did that myself, finding this post which inevitably lead me here.

Links

If you aren’t embarrassed by v 1.0 you didn’t release it early enough
Why we need version control
Interesting ways of making the most of a small living space

Revelation of the week

Learning a programming language by using an IDE can be damaging almost beyond repair.

This might perhaps just be me, but I learned html (albeit not strictly a programming language) in notepad, and have had no problems with html ever.

The same is true for bash, Python, javascript and Erlang. C would be the exception, those damn pointers continues to elude my understanding (not the concept of them, the syntax).

And then there is Java. We were taught to interact with it through an IDE, Eclipse as it was. That was 2005. This Wednesday was the first time I managed to write some barely non-trivial Java, compile it, and execute it, outside the “safety” of an IDE.

The reason for this was that Eclipse, for reasons beyond my understanding, keep crashing a couple of seconds after starting up, and that I had a friend in need of a little technology demonstrator.

And that’s when it really dawned on me. Outside Eclipse… it’s not that I am all that lost, it’s just that everything takes longer, is more tedious, and I have thusly shied away from it, thereby reinforcing that exact pattern.

And that is surely detrimental. I don’t want to be tied to a specific tool in order to be able to perform above average. A specialized tool might perhaps increase the effectiveness further, but being lost without it… that’s just wrong.

My software stack revisited – Programming

Friday, December 24th, 2010

Programming is one of my primary interests, mainly because it allows me to stimulate my brain with solving problems, but also force it to think in new ways.

Languages

I started programming in PHP, picked up Java and Erlang during classes at ITU, picked up Python on my own during my studies at ITU, and my latest addition would be shell scripting.

Slightly tangent to the topic are the markup languages I have picked up as well, html and css in high-school and LaTeX at ITU. I dabbled around for a while with both creole and markdown, but that didn’t last long.

Editor / IDE

My first and foremost tool of choice given nearly any situation will be (g)vim. The only two exceptions I can think of off the bat is Java (for which I use Eclipse and if I need to write a whole lot of text, with minimal distraction (more on that later).

The pragmatic programmers recommend learning one text-editor, and learn it well. If the name of that editor is vim, emacs, kate, gedit, or whatever, I really don’t care. Just pick up one that fits you, and LEARN IT WELL!

I have extended vim with a couple of plugins, the most prominent being NERD Commenter, matchit, snipMate and sparkup. There are at least two more plugins, but I will write more about those later.

And for Python, I usually install the IPython interactive prompt as it is a fair bit more useful than the standard python-prompt.

Version Control

While studying at ITU I had my eyes opened about the wonderful concept of version control.

I was first exposed to SVN, and while quite capable, I figured it was too much of a hassle to set it up myself, since that would require the presence of a server somewhere to host the SVN repositories.

But then mercurial entered the stage. Git or bazaar would have done the job just as good, but the people orchestrating the fourth term settled on mercurial, and it is so dead simple and still powerful enough for what I need that I haven’t had a reason to look elsewhere.

Issue tracking

For a course at ITU I tried using Mantis, a web-based bug tracker written in PHP, and while it worked well, it was a hassle to manipulate bug reports since it meant I’d have to go online and log in to yet another system.

I have however found a different solution which I am currently trying out: a plugin to mercurial called b with the tagline “distributed bug tracking”. It is a bit too early to tell if it will do, but for the time being it solves the immediate problem of having to go online somewhere to handle bugs.

Next post in line: “Office Suite” software

:wq

Vim indoctrination

Thursday, April 9th, 2009

Having used Vim (mostly gVim) exclusively for the last year or so my muscle-memory has thoroughly set. Which I was reminded of yesterday when a classmate from Uni asked me if I could lighten his load a bit by quickly adding a piece of functionality to some code he was working on.

“Sure” I thought, I can do this. So I launched Eclipse to carry out a small scale controlled environment kind of test. The task was to, from an existing for-loop with its own functionality, add in the required statements to have the loop also build a comma-separated string of the values retrieved by the loop.

That little adventure made  me discover of two things:

  1. “:w” won’t save the file in Eclipse… it will however insert those very characters into the code, breaking it. The same goes for “V”, “d” and “Y”. Also, “,cc” won’t comment out a line… that readjustment from Vim to Eclipse took way more time than hacking the actual Java
  2. Python has spoiled me

But all in all, it worked out pretty good, and I got to use StringBuilder for the first time ever. The resulting code looked something like this:

StringBuilder sb = new StringBuilder();
for-loop here {
    pre-existing code here...
    if (sb.length() > 0) {
        sb.append(",");
    }
    sb.append(obj.toString());
}
String s = sb.toString();

I’m sure there are better ways to accomplish this, like just tacking on a comma after each append and then on the resulting string working some string-manipulation magic to remove the final trailing comma, but for some inexplicable reason this just “felt” like a better solution.

It might just be that now most of the actual code is grouped together, so in the event of a refactoring, there is less of a risk that the string manipulation code is forgotten.

Anyway, it was almost fun to hack Java again… almost… ;)

Thinking outside the box

Monday, October 20th, 2008

Edit: Refurbished the post with a better title, as suggested by mra.

A friend from my old class at the University sent me a message earlier tonight asking me some programming advice. I was in the process of stepping away from the computer but he caught me just in time, and he’s a good friend, and I have to admit that his sporadic inquiries, which also gives me some slight insight into how code in the corporate world can look like, sometimes prove quite challenging, and I do enjoy trying to provide help, guidance and the occasional solution. So I sat back down, made myself comfortable and read his situation:

One thing I have come to notice, whomever it is that asks me, is that they have a pre-conceived notion about how they would like to solve the problem. More often than not, the help I provide is more in line of  offering an alternative way of looking at the problem. And the key my apparent success lies in that I gather as much information beforehand, and then try to either remember a similar situation, or think up a solution based on previous situation or knowledge bordering the problem at hand.

He described that there existed a loop, in which values were being generated, and used to build a query, which was then generated (inside the loop) resulting in a result set. And following the loop, he wanted to make use of the result set derived from the iterations of the loop.

The primary problem, at first, seemed to be that the result set was being overwritten with newer information at every iteration, and at the loops end, there was only data in the result set from the last iterations query.

Problem solving mode kicked in, but soon reported that there was too many unknowns to come up with something resembling a solution, more information was needed.

Well, no, the first thought was how difficult it would be to go into the internal storage-structure of the result set object and “append” the information returned in this query with the result set from the previous query. I rejected that idea within a few seconds time of deliberation. (Don’t know about you, but I would not feel comfortable fracking around with the internal state of a Java object I really don’t know anything about.)

So, more information, did query X+1 depend on the information returned in query X? If not, what was the data being generated in the loop, which was fed to the query?

I am ashamed to admit it, but my next idea was almost as big an ugly hack as the first idea, which essentially was to create a result set array, and store the result set from each query in that array, and instead of manipulating one big result set at the end of the loop, just manipulate each element in the array individually. I honestly cannot say why I thought of that idea, but I think that may be a rather “hard-coded” notion I get when I see Java code… in any case, all the while I was examining this hair-brained idea for flaws (I cannot say that I liked the idea, it would probably work, but it didn’t feel “right”) I started getting answers to my previously stated questions. The generated data was integers, id numbers to be specific, and they did not relate depend on the previous query.

The code, then, was basically something along the lines of:

for (i=0; i<something_predetermined; i++)
{
    resultSet = SQLquery(query, i);
}

The little code snippet I got to see looked a little different, it has been obfuscated to preserve his anonymity :)

That little tidbit of information made me think about what he had going into the problem, where the problem could be broken up into smaller pieces etc. Basically, he had a list of id numbers, or could at least generate such a list, independently from the loop and queries. That’s when I remembered a piece of SQL syntax that I have never had a chance to try out for myself. Something I’d read, but never had any use of personally. Something along the lines of:

SELECT * FROM foo WHERE id IN [1,2,3,4];

And I was almost correct. My only hurdles was that I a.) wasn’t sure that it was correct syntax, and b.) the specification use parentheses to represent a list of values, not square brackets. I.e.:

SELECT * FROM foo WHERE id IN (1,2,3,4);

This solution was something I would be able to live with. Not that I really had any stake here, but I couldn’t really recommend the usage of a piece of code I wouldn’t want to be caught dead using myself, but this, outsourcing the work to the database, work it was written to excel at performing, yes, this felt “right”.

Next question, would this syntax work in whatever database his project was powered by? As it turns out, it would seem so. A quick Google revealed that SQL Server should indeed support this syntax. My friend did some limited testing and that also checked out, so I considered my work, at least for now, done.