| MooTools setOptions() nullifies object referencesPosted on August 19, 2008 by Tuukka MustonenFiled Under debugging, javascript, mootools, performance, solution, web development I bumped up into a problem where the object references where resolved as object copies when I passed them to class instances. That might sound easy to resolve, but unfortunately I was already deep in code and it was difficult to see this. Therefore, here’s a little explanation for those who are facing the same frustrating issue. Say, you have variable ‘a’ and you want to pass it to a MooTools class B instance during creation. In the easiest case you’d use new B({ myReference: a}) and trust on MooTools’ Class.setOptions() to minify the need of code lines. This is what you should do… well at least that’s what I did and in this case it was a mistake. It turns out that Class.setOptions() merges it’s arguments to this.options and then takes copy of them via $merge(). That means that any variable references you pass to setOptions() will get copied to this.options and.. well, that’s it. See lines 1170-1173 in uncompressed version of MooTools 1.2: var Options = new Class({
setOptions: function(){
this.options = $merge.run([this.options].extend(arguments));
That effectively nullifies the benefits of Class.setOptions() if you want to pass in variable references.. Here’s a longer example to clarify (use Firebug): // The most basic MooTools class that implements options
// ref is a variable meant for pointing at given object
// (won't do that, however)
var B = new Class({
Implements: Options,
options: {
ref: null
},
initialize: function(options) {
this.setOptions(options);
}
});
// Ok let's create an instance that we can pass to B
// It's similar with all sorts of variables
var A = new Class({
initialize: function() {
this.somevar = 'untouched';
}
});
var a = new A();
// Create an instance of B and give it somevar as reference
var b = new B({ ref: a });
// prints out "untouched" as should
console.log(b.options.ref.somevar);
// Let's change the variable (direct access, bad)
a.somevar = "changed";
// b's reference should still point to a, right?
// In that case the following should print "changed",
// but because our reference object was copied instead
// of retaining reference to it, we just get "untouched"
console.log(b.options.ref.somevar);
I don’t know why MooTools wants to make a copy of arguments in setOptions() – propably for performance reasons. Viivi & Wagner strip scraperPosted on May 7, 2008 by Mikko OhtamaaFiled Under python I wrote this little script as a mental exercise and to prove the power of Python programming language. If anyone accepts the challenge, I’d like to see submissions in other programming langauges For the foreigners: this is the best comic in Finland, so I hope you’ll get translations soon! It tells about the relationship of a woman and a pig (sic) reflecting the deepest shadows of Finnish social life. """
Creats local mirror from Viivi & Wagner strips by fetching all of them from hs.fi.
Will create downloaded strips as
2004/1.1.2004.gif
2004/2.1.2004.gif
...
until today
Try this in C++!
Motivation: No one has build Viivi & Wagner search engine with speech bubble OCR support
and I desperately wanted to find "Kottarainen lentaa korvaan" strip for my gf.
Time to complete: 20 min.
"""
__docformat__ = "epytext"
__author__ = "Mikko Ohtamaa"
__license__ = "BSD"
__copyright__ = "2008 Mikko Ohtamaa"
import os
import re
import urllib
from BeautifulSoup import BeautifulSoup
# 1.1.2004 start page
url = "http://www.hs.fi/viivijawagner/1073386660690"
# Loop until there is no longer next link
while True:
stream = urllib.urlopen(url)
html = stream.read()
stream.close()
soup = BeautifulSoup(html)
# Parse strip date from contents
date = None
# Find strip date, which is next to a title
h1 = soup.findAll(text="Viivi ja Wagner")
# Should be present always
date = h1[0].parent.parent.p.string
print "Fetching " + date
# Scrape strip
strip = soup.findAll("div" , { "class" : "strip" })
img = strip[0].img
stream = urllib.urlopen(img["src"])
data = stream.read()
stream.close()
# For each year, give a new folder to avoid file system stress
# (lotsa files in a folder kill poor Gnome)
day, month, year = date.split(".")
folder = year
if not os.path.exists(folder):
os.mkdir(folder)
# Store contents
fname = os.path.join(folder, date + ".gif")
f = open(fname, "wb")
f.write(data)
f.close()
# Find next url, it is a containing one img tag
img = soup.findAll(alt="seuraava")
if len(img) == 0:
break
a = img[0].parent
url = a["href"]
See preview |
