Archive for the ·

Programming

· Category...

Touch events in JavaScript

Comments Off

Looking to extend my “HTML5 Canvas Panorama Viewer” to make it work on mobile phones, I’ve started to dig into the wonderful world of incompatible touch events between all the browsers. It’s IE vs. WebKit vs. Gecko vs. Opera, or back to the 90s in other words.

Peter-Paul Koch site quirksmode does a good job of summarising the various aspects of mobile web development.  In particular, the touch table is relevant to what I wanted to achieve. However, it does not look too promising right now, as most events are listed as either no supported, or incomplete.

Still, an old tutorial by nroberts, “Touching and Gesturing on the iPhone” gives hope. It includes a small example (http://tinyurl.com/sp-iphone) which actually do work to some extent on both the Android  native browser, and Firefox 6.

Now the challenge is to combine it all, and have both desktop and mobile browser behave in an expected and functional manner. Using the same events for the same actions does probably not make sense, however the user should be able to achieve the same across all browsers. The panorama viewer needs only three actions: zoom in, zoom out, and move. Zoom in already works, move works but is buggy, and some touch gesture must replace right-click to zoom out. I’ll come back to this later.

Comments Off

Programming Books

Comments Off

StackOverflow recently voted on the “most influential programming book”. The Internet Security Blog brings a neatly formatted list. Finally, the question was repeated on Slashdot; which is of course like asking a class of ten year olds about their favourite bubble gum. Surprisingly, some interesting information could be extracted from the noise.

The top ten:

Code Complete (2nd Edition)
The Pragmatic Programmer: From Journeyman to Master
Structure and Interpretation of Computer Programs (2nd ed.)
The C Programming Language (2nd ed.)
Introduction to Algorithms
Refactoring: Improving the Design of Existing Code
Design Patterns: Elements of Reusable Object-Oriented Software
The Mythical Man-Month: Essays on Software Engineering
Art of Computer Programming, Volume 1: Fundamental Algorithms (3rd Edition)
Compilers: Principles, Techniques, and Tools (2nd Edition)

Other

The C Programming Language
Design Patterns
Deitel & Deitel
The Mythical Man-Month
John von Neumann: Theory of self-reproducing automata
The Art of Unix Programming
Starting Forth by Leo Brodie
Peter Norton’s Guide to Programming the IBM PC
Bruce Eckel’s Using C++ and Thinking in C++
The Design of Everyday Things by Donald Norman
Algorithms + Data Structures = Programs by Niklaus Wirth

And there’s even more.

Comments Off

Java 7 Released

1 comment

Oracle (finally) released Java 7 today, 4 years and 7 months after the initial release of Java 6. The previous version updates used to be on a 18 months schedule, so that makes this release 37 months late. It’s getting a pretty good bashing on Slashdot, primarily for its current owner, Oracle.

New features includes syntactic sugar like Strings in switch statements; underscores in numeric literals; and type inference for generic instance creation. The Open JDK site lists the same features, so I’m guessing that means everything is open sourced already. However, it seems smaller items on my wish-list did not make it, like the Immutable annotation. (The JSR 305 has been inactive for ages, and still the only implementation is a Google Code hosted project.)

The new Java Doc is here, and latest download from Oracle here. The Open JDK binaries are also available in the repositories of major distributions.

yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel

or
sudo apt-get install openjdk-7-jre openjdk-7-jdk

Sparklines

Comments Off

Looking for a Python sparkline library, I found Perry Geo’s excellent code. “In the minimalist spirit of sparklines, the interface was kept simple”:

import spark
a = [32.5,35.2,39.9,40.8,43.9,48.2,50.5,51.9,53.1,55.9,60.7,64.4]
spark.sparkline_smooth(a).show()

That’s it, and here’s the result. Just download his single Python module, start up interactive Python, and off you go.

This of course sent me on a tangent, off to Edward Tufte’s work and creation of sparklines. It seems I have a book or two to buy.

Comments Off

The Art of Computer Programming, Volumes 1-4A Boxed Set

Comments Off

The forth volume of Donald Knuth’s The Art of Computer Programming is now ready for print, and can be pre-ordered at Amazon. At about $200, I think I might add it to my next order.

Comments Off

Java Applet Test

Comments Off

Mostly for my own reference: An old Java Applet which I continue to use to verify that Java Applets works. The applet itself is demonstrating Casteljau’s algorithm to draw a Bézier_curve.

You can move the red control points, and then animate the drawing of the curve.

Comments Off

Back references in Java regexp

Comments Off

The Java API documentation for regular expressions says that:

Back references
\n Whatever the nth capturing group matched

This seems to be difficult to get working though. Here’s an example of a work-around:
str.replaceAll(”user(.*)”, “$1″)

Example:
“username” => name

Comments Off

A note about multiple inheritance in Python

Comments Off

Consider the following three classes, where C inherits from A and B, and the method m() is present in both A and B.


class A(object):
def m(self):
print "a.m"

class B(object):
def m(self):
print "b.m"

class C(A, B):
def test(self):
pass

Now, if you create an instance of class C and invoke method m, what will be the result?


>>> c = C()
>>> c.m()
a.m

The Python docs tutorial on the subject says “the resolution rule used for class attribute references (…) is depth-first, left-to-right.”, which seems like an easy rule. However, it also warns about some maintenance nightmares, e.g “a class derived from two classes that happen to have a common base class”.

Comments Off

Class attributes in Python

Comments Off

After almost two hours of debugging, I finally made an interesting (at least to me) discovery in Python: Attributes of type dictonary and list (and presumably all other object types) at the class level are “static” (as used in Java classes), while primitive types are not. Does that make sense? I don’t know.

Consider the following dummy class:

class MyClass:
e = {"a":None}
f = [0]
i = 50
def __init__(self):
self.d = {"a":None}
def set(self, v):
self.d["a"] = v
self.e["a"] = v
self.f[0] = v


We have four attributes here:
e – a class level dictionary
f – a class level list
i – an int
d – another dictionary referenced by self.d inside the methods.

Now consider the following result from these interactive lines:

>>> x = MyClass()
>>> x.set(5)
>>> x.d
{'a': 5}
>>> x.e
{'a': 5}
>>> x.f
[5]
>>> y = MyClass()
>>> y.set(10)
>>> x.d
{'a': 5}
>>> x.e
{'a': 10}
>>> x.f
[10]
>>>
>>> y.i = 100
>>> x.i
50
>>> y.i
100
>>>


As you can see, the first call to set method, set the value 5 in x.e, x.f and x.d. However, the second call, y.set(10), will also set the value of x.e and x.f (in red). Finally, it is shown that the int i does not conform to this behaviour.

Strange, I say.

Comments Off

Bad Behavior has blocked 349 access attempts in the last 7 days.