Posts

Showing posts from 2012

CalcJS: Evaluating complex math expressions in JavaScript in 120 lines

Image
Sometimes it’s useful to express our math thought more explicitly, so instead of saying: x = 10 * 1.1 * 1.3 we’d prefer to say: cost = 10, price = cost * 1.1, RRP = price * 1.3 . CalcJS helps you to evaluate complex math expressions described in human readable format. Couple of days ago I’ve seen an interesting code snippet of a JavaScript evaluator. I thought the idea is great so I started to experiment: how hard would it be to create a framework that can evaluate complex math expressions that can use variables? Humans usually express the math expressions in infix format, so I needed to create a simple stack based evaluator that is able to parse infix formulas. The algorithm I used was the Shunting-yard algorithm that was invented by Dijsktra (like so many other great algorithms). The basic idea is to use two stacks, one for values and one for operators. When we see an operator that’s precedence is smaller than the previous (like + following a *), we evaluate what we...

Rabin Karp rolling hash - dynamic sized chunks based on hashed content

Image
The Rabin-Karp rolling hash algorithm is excellent at finding a pattern in a large file or stream, but there is an even more interesting use case: creating content based chunks of a file to detect the changed blocks without doing a full byte-by-byte comparison. The traditional computation of the hashes of interleaving windows generally works like this: Input: [a,b,c,d,e] Window size: 3 Hash#1: hash([a,b,c]) Hash#2: hash([b,c,d]) Hash#3: hash([c,d,e]) The Rabin-Karp implementation is very efficiently reuses the previous hash to calculate the new one: Hash#1 = hash([a,b,c]) Hash#2 = Hash#1 – hash(a) + hash(d) Hash#3 = Hash#2 – hash(b) + hash(e) So, to calculate the next window’s hash all we need to do is to remove the window’s first element's ‘hash value’ from the current hash and add the new to it. Even though a simple mathematical addition would do fine, it’s usually not preferred because it can cause high collision rate among the hash values: a+b+c = a+...

Fixing Do you want the application 'iTunes.app' to accept incoming network connections?

Image
Do you want the application 'iTunes.app' to accept incoming network connections? It's quite common to run into the problem that a signed app on Mac OS X thinks it's been modified so the firewall will keep asking whether you want to allow this app to accept connections. The fix is quite easy, all we need to do is find out why the firewall thinks that the app is damaged. To check, go to terminal and run: $ codesign -vvv /Applications/iTunes.app it will show something like this: /Applications/iTunes.app: a sealed resource is missing or invalid In architecture: i386 resource added: /Applications/iTunes.app/Contents/Resources/English.lproj/DevicePrefsMobileMe.nib/objects.xib resource added: /Applications/iTunes.app/Contents/Resources/English.lproj/Localized.rsrc resource added: /Applications/iTunes.app/Contents/Resources/English.lproj/SmartPlaylist.nib/objects.xib resource added: /Applications/iTunes.app/Contents/Resources/iTunesASUHelper resource...

Android - The active editor does not contain a main type.

When you try to run you Android app in Eclipse using the keyboard shortcuts and you face the " The active editor does not contain a main type " error then simply go ahead and uninstall Android ADT form Eclipse (About / Installation Details, uninstall Android Development Tools), then re-install it from the original source (https://dl-ssl.google.com/android/eclipse/). All other settings, like run configuration and source location, did not help me.

Instant sleep on MacBook - no sleepimage

Apple really cares about the content of my Macbook RAM. But I don't - I'd prefer to have that extra 2-3 minutes back when closing the lid. The good news : You can customize the sleep mode on your Macbook so it does not save the content of your memory to the disk before sleeping. When you put a Macbook into sleep, by default, it will page out your memory to the disk (/var/vm/sleepimage) - in case the battery gets depleted you still have your work saved. This is called hibernatemode 3 :  hibernatemode = 3 (binary 0011) by default on supported portables. The system will store a copy of mem ory to persistent storage (the disk), and will power memory during sleep. The system will wake from  memory, unless a power loss forces it to restore from disk image. The issue is that if you have a lot of memory (8-16Gb), it takes a good while to swap it out and isn't particularly useful for storing on a small SSD drive or in fact extending the life of the drive itself. To tur...

Frustation guaranteed - be a newbie Java web developer

Image
Whenever I want to face ultimate frustration, I know where to go: give a new Java web framework a try. Generally, I love technology, different programming languages, tools and ideas. I think Java is a great platform and language, although it’s lagging behind the rest of the world by almost a decade now - lambda expressions , please? Python has it since 1994 . Yes, that’s before Internet Explorer 1.0. How can anyone blame IE6 (2001) when it is a super modern technology compared to the core of the Java language? Anyhow, the Java language is fine - unlike the open source frameworks built on top of it. It does not matter which one you choose, endless frustration and failure is guaranteed. Let’s pick the most popular one, Spring MVC. Where should we begin? Download Eclipse, install STS ( Spring Tool Suite ) from the marketplace and then… what? Ah, you have to find a web server for yourself, let’s say Tomcat. Unzip somewhere, configure with Eclipse somehow. Now what? Follow the...

Installing SSH, VNC and SFTP on the MK802, the Android PC on a stick

Image
There are some deals you just cannot ignore: a full Android system for $50? The RikoMagic MK802 sounded too good. The processor is fairly good (1GHz), got plenty of RAM (1Gb), comes with 2 USB ports (for mouse and keyboard) and a HDMI output. Plays YouTube and HD videos fairly well and for mail/rss it's more than enough. To remotely access it, I needed to have the basic tools installed, like SSH, VNC and SFTP. These tools exists for Android but most of them are buggy or slow or both. I wanted to collect all the tools and settings I used as it took several hours to set up everything perfectly. SSH This should be an easy part, but in fact, it’s trickier than I thought. I tried different SSH servers from the Google Play store, and this is what I’ve found: Name SSH SFTP Comments DropBear OK (no coloring) If you obtain an SFTP binary and place it into /usr/libexec , it will work. Generally it’s an ol...

If Parallels does not start (Make sure that the prl_disp_service is active)

Image
If you want to start Parallels and you get the following error message: "Unable to connect to Parallels Service. Make sure that the prl_disp_service process is active and it is not blocked by Firewall. For details, please refer to http://kb.parallels.com/en/8089. If the problem persists, contact the Parallels support team for assistance." no need to worry. All you need to do is open Terminal , stop and restart Parallels: sudo launchctl stop com.parallels.desktop.launchdaemon Then restart it: sudo launchctl start com.parallels.desktop.launchdaemon If you run the following command now: sudo launchctl list | grep com.parallels.desktop.launchdaemon The output should be something like this: 94248 - com.parallels.desktop.launchdaemon Try again, should be okay now.

Sorting CSS styles and classes in your files with a Python script

Image
When editing CSS files, it’s quite common that the length of the file gets so big that it’s hard to find the classes in it. Some IDEs like Eclipse help you to show a sorted outline of the CSS file, but it will not sort the file for you as you want to. I’ve barely seen any larger projects with nicely structured CSS files, which is partially because neither the editors nor the tools are perfect for CSS editing. To highlight one of the many problems with CSS, let’s see a simple snippet: #mystyle li {       color : green ; } input { background-color : red ; } #mystyle {       /*should not use . and # with the same name!*/ } b { font-size : 1.2em ; } .mystyle {       font-weight : bold ; } It’s a bit mixed up here and there, not well formatted, the DOM level CSS styles are mixed with the ID and class definitions, the names are not in sorted order. It’s pretty...

Ignore non visible fields from JQuery Validation / ASP.NET MVC fluent validation

Image
The JQuery Validation plugin is quite a smart tool to check the data consistency on client side but might need some extra tweaking to work as expected. Depending on the settings and version, it might not want to ignore the fields that are non-visible, like like a text box placed on a hidden DIV, even though it would ignore any field that is disabled itself. The JQuery Validation plugin version that comes with ASP.NET MVC 3 (v1.8) will not ignore non-visible inputs. To change the settings and ignore every control that is placed somewhere within a hidden DIV, use the following simple script: $(function () { $('form').validate().settings.ignore = 'div:hidden *'; }); To ignore everything that is not visible, no matter what the hierarchy is, use simply (the current version, v1.9 uses this as a default): $(function () { $('form').validate().settings.ignore = ':hidden'; });

Automatically reload HTML, CSS and Javascript files in browser when editing

Image
When designing web applications or webpages, the early stages require very frequent modifications: alter the source code, check in the browser, go back and refine it. However, the switching (ALT+TAB), reloading (CTRL+R), switching back again (ALT+TAB) breaks the smooth workflow. Refresh on edit: to solve this problem, there is a really short (233 lines) Javascript tool/library called LiveJS , that is able to constantly check for changes of any referenced files and if it finds something, it will reload the page automatically from the server (obviously it’s only useful for development and on localhost). The script only supports server-client environment, you cannot use local filesystem for checking changes. To fire up a webserver, you have plenty of options in increasing complexity: Use python to create a simple HTTP server for the current directory: $ python -m SimpleHTTPServer 8080 Or use the Chrome Engine based NodeJS platform to run this script  and create a ser...

Automatically setting the GPS location in Android emulator

Image
The Android emulator is quite good at working with geolocation, but it’s a bit tedious to set the current latitude and longitude coordinates using Eclipse, especially if you want to test with many different points. I’ve created a simple Python script to do it for me. In Eclipse the emulator’s GPS coordinates can be set by changing to DDMS view (Window->Open Perspective->DDMS), then setting the location in the Location Controls. As the emulator is listening on localhost:5554 for a telnet-like connection, you can set the coordinates manually using the simple telnet application: telnet localhost 5554 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Android Console: type 'help' for a list of commands OK geo fix 151.195733 -33.866745 Watch out for the latitude-longitude order, as it is different in Google Maps: Maps will give you -33.866745,151.195733 so you have to change the order before entering it into the emulator! However, manu...

Manually schedule Time Machine / Capsule backup with iCal to run weekly or daily

Image
If the Mac OS X Time Machine backup frequency is too short or too long, you can manually schedule the backups using your calendar (yes, iCal). iCal is able to run AppleScript as an alert, so a very simple script that triggers the Time Machine backup will just do fine. If you want to schedule it to run weekly instead of hourly, all you need to do is to turn off the automatic backup so the default scheduler will not run it too frequently and set up an weekly recurring iCal event in one of your calendar. The alert should be a message to warn you (like 15 minutes before starting the backup) and the actual backup script. The following AppleScript will trigger the Time Machine backup. To save it, open up the AppleScript Editor, paste it and save wherever you want to. do shell script "tmutil startbackup" When setting up the iCal event, add an alert saying ‘Run Script’ and choose the saved script.

Generate & resize different Android launcher icon sizes with a Python script

Image
When you change your launcher icon in Android, you have to create four different sizes (96x96, 72x72, 48x48, and 36x36) for different screen sizes. The process is even more tedious when you want to have two or more different icons for different resolutions. I've created a very simple Python script that does the resizing for you. Usually you start with the "High Resolution Application Icon" (512x512) which you create for Google Play then just simply run this script to generate the launcher icon sizes. The script requires to have the Python PIL (imaging library) installed. On Mac OS X, follow these steps to install PIL : Install XCode is you don't have already. Install XCode command line tools (Xcode -> Preferences -> Downloads -> Commande Line Tools -> Install) #Download & extract PIL curl -O -L http://effbot.org/downloads/Imaging-1.1.7.tar.gz tar -xzf Imaging-1.1.7.tar.gz #Build PIL cd Imaging-1.1.7 python setup.py build #Install python ...

MurMurHash3, an ultra fast hash algorithm for C# / .NET

Image
Finding good hash functions for larger data sets is always challenging. The well know hashes, such as MD5, SHA1, SHA256 are fairly slow with large data processing and their added extra functions (such as being cryptographic hashes) isn’t always required either. Performance and low collision rate on the other hand is very important, so many new hash functions were inverted in the past few years. One of the most notable ones is MurMurHash3, which is an improved version of its predecessor (v2). The new version can create both 32 bit and 128 bit hash values, making it suitable for a wide range of applications. 64 bit architecture The 32 bit hash isn’t too important for large amount of data matching, so I only worked with and tested the 128 bit version. The latter one is optimized for x86-64 architecture, giving amazing speeds for data processing. As a 128 bit value consists of two 64 integers, the processor is fairly good with working with such large hash values – they are not byte...

Ultra fast HashTable (Dictionary) with direct indexing

Image
One of the most common usages of hash tables is to count/group occurrences of different elements in it, something like this: dict[“key”]++ . The direct access hash table outperforms any generic implementations, even though hash tables are supposed to be excellent at this task. Although this operation is considered to be constant time ( O(1) ), the generic implementations of dictionaries do an unnecessary step: double hashing of the key. If we look at the disassembled code of the example above, this is what happens in the background (.NET ILDASM simplified code snippet): get_Item(!0) // retrieve the item using the key (hashing happens) ldc.i4.1 // store the result integer add // increment the integer set_Item(!0,!1) // store the new value with the original key (hashing happens) Generally hashing is fairly quick, however, if the key is a string for instance, the hash function has to check every character of the string to create the hash value, which is an integer. The CPU is reall...

Quick select algorithm - find the Kth element in a list in linear time

Image
Quick select algorithm (Hoare's selection algorithm) – select the Kth element or  the first K element  from a list in linear time Working with large datasets is always painful, especially when it needs to be displayed in a ‘human readable’ format. It is a very frequent task to display only the largest, newest, most expensive etc. items. While sorting the whole dataset definitely gives a correct result, it is much slower than it needs to be – it needs at least O(n*log(n)) time and an it often uses recursion for the sorting, so in practice it can be quite slow. The quick select algorithm can get the top K element from a list of N items in linear time, O(n), with a very reasonable multiplication factor. The quick select does not use recursion so the performance is great for even large datasets. Algorithm The idea of the quick select is quite simple: just like with quicksort, select a random element from the list, and place every item that is smaller to the first half o...

Optional HTML content based on screen resolution - mobiles and high resolution desktops

Image
Displaying optional content for high resolution or mobile users on your web page The W3C Media Queries provide a great opportunity to display a webpage content differently based on the capabilities of the user’s browser or machine. This can be especially useful for mobile browsers or variable screen width/resolution. Many webpages do not care at all if you have a high resolution screen, the content will stay static and will just waste the extra space on your monitor –as two wide white columns on the left and right. However, without any special programming we can provide extra, non essential but useful content to the users with a decent resolution (or remove these for mobile devices). If we want to target a specific resolution, we can address them using the following CSS media query: @media only screen and ( max-width : 600px) { /* CSS styles here*/ } These styles will only apply on the users screen if the browser window size is maximum 600 pixels – meaning probably on s...

Smooth scrolling on Google Chrome

Image
If you ever envied Safari on Mac - or to be precise envied how smooth it's scrolling was compared to Google Chrome, it's time for a simple fix! Open up advanced/experimental settings of Google Chrome by visiting chrome://flags/ Find a setting called   GPU compositing on all pages and Enable it. This settings requires a restart, so go ahead, restart Chrome. When it comes back, the scrolling should be as smooth as with Safari!