Drawing lots online

As a member of Toastmasters club I sometimes lead so-called Table-Topics in which I need to give participants tasks from a list of tasks – randomly, without repeats (unique task per participant).

I’ve used a free iPhone app in which I could configure the texts of tasks and which returned a random result on shaking phone. But I wasn’t satisfied with it. First of all it had a fixed amount of item slots so I had to leave blanks for non-used slots, and often got blank instead of a result. And I was getting results that were already used – repeats – so I had to shake more and more until I got valid result. The number of slots was big enough though, but still it was also theoretically possible to have a need for more tasks than the slots in the app. So 3 downsides in total.

So I spent several hours to quickly code my own solution – in HTML+JS+CSS, so I could use it on desktop and on mobile easily.

Here it is: http://mvmn.ho.ua/lot.html?items=one;;two;;three…

It’s all single file HTML+JS+CSS, no external libs are used, so it can be cached locally and used offline. All the data (list of items to choose from) is going into URL. This imposes a limit on total text length unfortunately, but for me that’s not a problem – hopefully won’t be a problem for you either. Otherwise storing data in URL only makes it very convenient – again, you can use it offline, and there’s no need for a server-side storage, which makes it all work faster.

I also tried to make UI touch-friendly, and tested it on my good old iPhone 4.

The source code is on GitHub: https://github.com/mvmn/lot

A couple of screenshots from iPhone: Continue reading

Advertisements

BASH script and regexp of the day

Here’s my BASH script and regexp of the day.
I’m not particularly proficient in BASH scripting (in fact, not that proficient at all), so sorry if there are some obvious improvements to be made. But I suppose you can find the script useful anyway.

The purpose of the script is to get  values of a column of a CSV file by column name. The CSV file is comma-separated (not Excel style semicolon separated FYI) and may contain quoted values with commas, and double-quote escaped quotes (i.e “this is a value”,”this is another value with “” (quote) symbol”).

Parameters are: <file name> <column name>. Additional parameters -n or -u can be used to either output line numbers or output only unique values. Note: you can’t use both at the same time, -u takes precedence.

Also the script will output you the number of the column.

The script was written/tested against GNU grep (version 2.12 in particular). It doesn’t work correct for column #1 on OS X due to weird way BSD grep treats condition “^x{1}”. Installing GNU grep for OS X from sources like Rudix or Homebrew is recommended for Mac users.

You’re welcome (-:

unique=
addp=

for p in "$@"
do
     if [ "$p" == "-u" ]
     then
          unique="true"
     fi
     if [ "$p" == "-n" ]
     then
          addp=-n
     fi
done;

colMatch=$(head -n 1 $1 | grep -E "(,|^).*$2[^,]*" -o)

if [ "$colMatch" == "" ]
then 
	echo "Column not found."
else
	colNumber=$(head -n 1 $1 | grep -E "(,|^).*$2[^,]*" -o | grep "," -o | wc -l | grep -E "[0-9]+" -o)
	echo "Column number: $colNumber. Column name: $(head -n 1 $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$')"

	if [ "$unique" == "true" ]
	then
		tail -n "+2" $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$' | sort | uniq
	else
		tail -n "+2" $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep $addp -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$'
	fi
fi

Example of usage with results (tested on Ubuntu linux):
Continue reading

Screen area to Animated GIF

Recently I’ve discovered that there is a plethora of platform-specific utilities that can repeatedly capture region of the screen and then save the resulting images as an animated GIF. To me this sounds like a perfect task for JVM, since JVM includes support for screen capturing and creating animated GIF images, and all this comes with cross-platformness.

Thus I took the time to implement such utility. And here it is:
https://github.com/mvmn/mvmnJScrCap
Executable JAR of first complete release can be downloaded here:
https://github.com/mvmn/mvmnJScrCap/releases

Let’s start with the demo right away!
(UPD: see demo on Raspberry Pi at the end of the post)

Here is the GIF that shows the utility in action. This gif was produced by second instance of the utility capturing the screen part where the demoed instance was displayed. Nice, huh? I ran screen capturing while I ran screen capturing so I could capture screen while I capture screen (-;

Continue reading

Liferay 6 (5): get portlet content programmatically in hooked JSPs

Sometimes it is necessary to obtain a content of the portlet programmatically in hooked JSPs, but the APIs do not make it clear at all how to accomplish that.

There are some suggestions on the web on how this can be achieved, but the code seems to be somewhat complex, which makes it a problem to use in hooked JSPs.

For hooks there is a simpler method, but it relies on being executed in hooked JSPs by referring to some classes in portal-impl.jar:
Continue reading

Guitar frets notes map with Codea

Couldn’t find a pic where standard tuned guitar frets would be displayed as black+white piano keys. So I’ve coded a little script/app for that using Codea (IDE+runtime for coding for iPad using Lua):

This is 7-string (electric) guitar in Standard tuning (i.e. just like 6-string + 1 additional string for lower notes), ’cause I have a 7-string one – so I needed that additional string too.

Teh Code:
https://github.com/mvmn/GuitarPiano-for-Codea/blob/master/src/Main.lua

Yep, it also produces sounds when keys/frets are touched (with multitouch). Codea is fun.

Reinventing the wheel – custom Dates API

I’ve started on the re-implementation of command-line java calendar tool in order to provide support for relative dates and other features, and while working on it I got a bit carried away with implementation of what turned out to be no less that custom dates API+implementation.

It’s pretty basic, and was done more as an exercise than for any particular need, but still I believe some interesting concepts can be looked up from it.

Though not yet complete, the code of JCCal was already committed to GitHub: https://github.com/mvmn/jccal . The dates API+impl classes can be looked up there already (in package x.mvmn.util.dates). Please also check the README there for general explanations.

I won’t be doing much explanation here, since it would mostly be copy-pasting of the readme for general things, and copypasting of actual code for the details. As someone said, “The truth is in the code” – so I encourage anyone who is interested to actually look up the code.

Custom telnet Groovy shells – version with shared context

A follow-up to the previous postcustom Telnet shared Groovy shells. This time having shared context for all shells, i.e. shared variables!

The trick was to get Binding object from current Groovy shell Interpreter and pass it in constructor to Groovy shells that we instantiate programmatically on socket connections. Not sure if it’s thread safe BTW (this isn’t production ready (-: ).

The code is based on Groovy Shell internals a bit, so should probably be run only from Groovy shell. (And just in case – I’m using version 1.8.4 of Groovy).

In any case, here’s again some demo video:

UPD: Also there’s the thread-safe version.
In it each shell will have it’s own Binding object, i.e. separate variables context.
But on creating shell we’ll inject two variables into that context:
sharedConcurrentMap – an instance of ConcurrentHashMap which will serve as thread-safe shared context for all shells, and is pre-populated with “server” and “sockets” (the latter are also made thread-safe);
thisShellSocket – a socket that is used for this child shell.

See source code at the end of the post.

As usual, a little demo video:

The source code Continue reading