Sometimes I find myself converting google doc spreadsheets (CSV) to JSON format. I would later on use the JSON string to insert it into a JavaScript file. So here is a simple script to convert a CSV file to JSON format.
The WebP image format for the web is starting to get a lot of hype for its massive file compression. Chrome supports it since ver 9 and Opera since ver 11.10. Image file sizes are reduced by more than 50% in WebPs when compared to JPGs. Looking at Google Analytics on various sites, Chrome 9+ users constitute 20% - 25%. So providing one-fifth or one-fourth of the users with a much reduced page load is worth the extra effort.
for i in *.jpg; do j=`echo "$i" | cut -d . -f 1`; convert -colorspace RGB "$i" "$i"; ~/libwebp/cwebp -q 75 "$i" -o "${j}.webp"; done
convert is used to convert the colourspace from RGB to CMYK since as of now, webp encoder doesn't support CMYK colourspace.
2. Use 2 different CSS files for background images - one for standard jpg/png and another for webp. Chrome 9+ and Opera 11.10+ would be served webp.css while all others img.css.
get_browser is pretty useful for browser detection, but requires to set browscap in php.ini which is a PHP_INI_SYSTEM directive and can't be set in using ini_set(), .htaccess or user php.ini which rules out almost all shared hosting enviroments. But interestingly, this is possible in WebFaction where I could set browscap in a user php.ini file which was parsed.
If you are getting something like ...
<b>Warning</b>: get_browser() [<a href='function.get-browser'>function.get-browser</a>]: browscap ini directive not set in <b>/path/script.php</b> on line <b>7</b>
and if there is no way to set browscap, then you need to find an alternative browser detection like phpbrowscap.
I benchmarked this on a graphically heavy wordpress site, having 11 photographs in a slider on the homepage which takes a megabyte alone.
Browser
Requests
Size
Time
FireFox 3.6
31
1.9 MB
21.48s (onload: 20.62s)
Chrome 11
32
577.90KB
6.51s (onload: 6.51s, DOMContentLoaded: 2.88s)
(I could not find the option to view total bytes downloaded in Opera dragonfly's network tab)
Side Note : When uploading webp images to google storage, S3 or any other cloud service, specify the content-type / mime-type.
Google Apps Script is JavaScript based scripting for many of Google's services, most noteably, Google Spreadsheets, like VB Macros for Excel. There are some subtilities in JavaScript, like window and document not being defined. Though the scripting is mostly JavaScript, it is not 100% native JavaScript since it runs in an app and not in the browser. Currently Google Apps Script has a computeHmacSha256Signature() function but Amazon's AWIS requires a SHA-1 key for its signature which can be obtained from jsSHA. I've clipped the jsSHA code from here as its too lengthy. You can download it from sourceforge, copy-paste the code from /src/sha1.js. A small change is required though. Change b64pad="" to b64pad="=".
Flash & JavaScript examples of using markers & geocoding on Maps. I've never been a big fan of Flash, given that that's what dominates the web design animation spectrum. Agreed that you can make things look much cooler very easily using Flash, while achieving the same UI using JavaScript may take 10 times the effort. But this should change over time with the development of HTML5's canvas element. With Google's Visualization Geomap API you can get the nice map interface that you've seen in Google Analytics. But the Maps API is lighter and you can move it around & zoom using the mouse.
Limitations with Visualization API :
A maximum of 400 entries (markers)
Much slower if the data format is an address instead of a Latitude/Longitude pair
If you are targetting India, Maps is the better way to go as the Visualization API has cut off a major portion of the J&K !
Limitations with Google Maps :
Geocoder has a limit of 2500 requests per day, and looks about like 10 requests at a time.
Need to center the map and adjust zoom level manually to show withing the bounding div. Visualization API does this automatically by specifying region:'IN'
In both cases, storing Latitude/Longitude pairs offline and then geo-locating the same renders a whole lot faster.
If you have a simple contact / registration form for a non-CMS / non-framework website with many input fields, it becomes cumbersome to do a server check if all fields passed through doing isset for every field. Many people just check for the existance of a single POST field, but for security reasons, it is better to check for the existence of all required fields.
What if there were 20 fields ? That'll be one long line of issets. array_diff is a built-in array function in PHP that computes the difference between arrays - it returns an array containing all the entries from the first array that are not present in any of the other arrays.
All we need to check is if the list of required fields are all present as keys in $_POST - this is easily achievable by checking against array_keys of $_POST.
And finally for the count - if all required fields are present in the $_POST's keys, it'll return 0 which is what we want and how we can confirm that all POST field values got sent through.
A significant change from Python 2 to Python 3 is the way strings are dealt with. Python 3 doesnt always return a string when expected. For example, the return type of read() in version 2 has always been a string. But in version 3, its very often a "bytes" string. When you print a "bytes" string, you'll see every character in its byte format, special characters as escape secquences (newline as \n) and other unicode characters as escape sequences. This is because Python 3 differentiates between text (string) and data ("bytes" string) as oppossed to Unicode vs 8-bit string. (Text Vs. Data Instead Of Unicode Vs. 8-bit)
My localhost/index.html contains just this : <html><body><h1>It works!. stärke gläser</h1></body></html>
In Python 3, we need to need to explicitly convert it to string format via the str() function and specify the encoding-type. If you are getting errors using Python 3.0, you may want to update to atleast Python 3.0.1 - many have reported possible Unciode encoding/decoding bugs in 3.0.
So far, while developing web applications I have always taken one point into assumption - that it has to run on a web-server.
But when dealing with pure client-side web apps, we often overlook the fact that it should be able to be run by simply double-clicking on the homepage file (index.html).
When XMLHttpRequest'sreadyState reaches a value of 4, we further check for a response code 200 from the web-server before doing anything with the responseText / responseXML. But when running it locally - not on a webserver, the status code value will alway be 0.
I was playing around with HTML's Canvas element and I spent a whole day trying to figure out why I can't draw a 1-pixel wide straight line (parallel to the axes).
If you try dawing a black (#000000) straight line parallel to the axes, then for all odd numbered widths :
the first line is of colour #9d9d9d and the last one is of #8d8d8d
The width of the line is one pixel more
The above should give an output like this :
If you zooom in, you should be able to see the top and bottom border colours.
This happens in all canvas-supported browsers (FF 3, FF 3.5 beta 4, Opera 10 beta, Chrome 2 beta, Safari 4 beta).
Reason : https://developer.mozilla.org/en/Canvas_tutorial/Applying_styles_and_colors#section_8
Mozilla's canvas tutorial on drawing shapes shows a screenshot which has the inner most rectangle of exactly 1 pixel wide and colour black. But viewing the example itself in the browser, shows otherwise - 2 pixel wide with faded colours.
I've written a small function that draws exactly 1 pixel wide straight line parallel to the axis.