Posts

Showing posts from August, 2012

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...