Kopimi

Many perform the act of copy-paste (act of sharing), but we recognize it is as a sacred procedure. Copying information is a way of storing it within the communication process though preserving it in time. We choose what information must be preserved by copying it further. It is a very sacred and profound idea, which can not be ignored.

This religion seems to me as rational as religion can be. Thanks Isak!

Wrapping built-in python types

Python distribution includes such modules for custom override as UserDict, UserList and UserString. The respective classes might be helpful in providing quick behavior of a built-in type with mixed-in user-typing (e.g. exposing the concept of type oriented programming).

But before going into further details of dynamical type declaration, please note that according to [1]

Historically (until release 2.2), Python’s built-in types have differed from user-defined types because it was not possible to use the built-in types as the basis for object-oriented inheritance. This limitation no longer exists.

Which means that one can extend a built-in type in python 2.2+ as in here:

class index(int):
    pass

i = index(1)
print type(i)  # produces <class '__main__.index'>

and this might seems satisfying. As disguised by dir(index) all the methods with “working” code are inherited. But since the data typing inside the “borrowed” methods operates with built-in ‘int’ type, this causes type-switching in most cases.

i = index(1)
i += 1
print type(i)  # produces <type 'int'>

The result on second line is int, since the inherited __radd__() mehtod has no idea of our new user-type called index.

That leaves us with no user-type support and two options of implementing it:

  • Re-implement type-depended methods of python data-model, inherited from the built-in type
  • Wrap an instance of the the built-in type and reroute the respective python data-model to it

Classes as UserDict go for second option. In the following code snippet I explore a technique allowing dynamical construction of the user type with aggregated (composition-relation) instance of desired type. The idea is to simplify the “routing” the more the better:

class UserValueType(object):

    def __init__(self, value):
        self.value = value

@borrow_methods(int)
class UserInt(UserValueType):
    pass

Obviously, this looks so simple because all the work is done by @borrow_methods() decorator, which proxies the desired type.

def partialmethod(method, *args, **kw):
    def call(obj, *more_args, **more_kw):
        call_kw = kw.copy()
        call_kw.update(more_kw)
        return getattr(obj, method)(*(args+more_args), **call_kw)
    return call

def borrow_methods(source_type, overwrite=False, exclude=None, include=None):
    '''
    Decorator for borrowing methods from other classes.
    Note: 'include' has priority over 'exclude'.
    '''
    if not exclude:
        exclude = ['__getnewargs__']
    if not include:
        include = ['__repr__', '__format__', '__str__']

    def invoke_method(self, method_name, *args, **keywords):
        method = getattr(self.value, method_name)
        return method(*args, **keywords)

    def decorator(cls):
        setattr(cls, '_invoke_method', invoke_method)
        for (name, member) in inspect.getmembers(source_type):
            if (not overwrite and hasattr(cls, name))
                or (name in exclude and not name in include)
                or not inspect.ismethoddescriptor(member):
                continue
            setattr(cls, name, partialmethod('_invoke_method', name))
        return cls
    return decorator

Decorator proxies all the necessary methods of the built-in type. Arguably an alternative of implementing descriptors to redirect calls can turn out much more sophisticated than “copying” of attributes. Such copying of methods uses partialmethod() function (similar to functools.partial) and expects that proxy _invoke_method redirects call to self.value.

Generally, using decorator is preferable to meta-class, since the first is more refine and simplistic. Finally, one would want to control the list of borrowed methods by exclusion/inclusion or similar.

Presented solution still does not address the issue of typing, i.e.

uint = UserInt(1)
uint += 1
assert type(unit) == UserInt

We add the modification into the decorator function by introducing uncasted parameter – a list of functions the result of which we do not cast. Normally, these are functions like __int__ or __str__.

def borrow_methods(source_type, overwrite=False, exclude=None, include=None,
    uncasted=None):
    '''
    Decorator for borrowing methods from other classes.
    Note: 'include' has priority over 'exclude'.
    '''
    if not exclude:
        exclude = ['__getnewargs__']
    if not include:
        include = ['__repr__', '__format__', '__str__']
    if not uncasted:
        uncasted = ['__int__', '__str__', '__cmp__']

    def invoke_method(self, method_name, *args, **keywords):
        method = getattr(self.value, method_name)
        result = method(*args, **keywords)
        if method_name not in uncasted and type(self) != type(result):
            result = self.__class__(result)
        return result

    def decorator(cls):
        setattr(cls, '_invoke_method', invoke_method)
        for (name, member) in inspect.getmembers(source_type):
            if (not overwrite and hasattr(cls, name))
                or (name in exclude and not name in include)
                or not inspect.ismethoddescriptor(member):
                continue
            setattr(cls, name, partialmethod('_invoke_method', name))
        return cls
    return decorator

Casting happens on this line:

   if method_name not in uncasted and type(self) != type(result):
            result = self.__class__(result)
  1. Built-in Types, http://docs.python.org/library/stdtypes.html#

Filtering Magento logs in DB SQL dump

A nice tip for Magento developers that work with existing shops (hence big DB) is to kill/filter out all the logs and similar. To do so, one should care to cut out all the SQL INSERT statements into the respective tables. Assuming SQL was prepared with mysqldump script, full table content will be found packed into an a single

INSERT .. (), (), ()

BTW, similar logic is true for any mysql dump file.

This shell command should do the trick with sieving out the INSERT lines:

egrep -v "INSERT.*(log_visitor_online|log_visitor_info|log_visitor\

|log_url_info|log_url|log_customer|log_quote|report_event)" old.sql > new.sql

Also don’t forget about the shell/log.php, e.g.

cd shell; php log.php clean

Scientific method in schools (on the matter of rational knowledge)

“Theology is the systematic and rational study of religion .. Mythology is a learned study of myths.” Is there philosophy, psychology and/or other-logy on the menu? ;)

I strongly believe that any piece of rational human knowledge is valuable enough to be considered (once again, giving that knowledge is classified as rational). I will strongly support the importance of staying open to study any rational knowledge as such. As long as the task of labeling priorities is computationally complex enough, it makes little immediate theoretical sense to sort the knowledge. But one can, of course, do so in order to practically solve a problem in some concrete setting (requiring importing and applying respective pieces of knowledge). That’s is basically about being of a general practical attitude towards knowledge, i.e. being prepared (or even trained) to work with knowledge as a “living” (dynamical) body.

Back to the issue of studying anything (for instance, in schools). The problem may well be not in *what* to study but *how* to do so. Teaching evolution in biology class one can do equally bad job as not teaching it, simply by preaching out statements into the thin air. It is again about that most important thing – to teach how to think (otherwise the moment of reaching the efficiency limit while gaining new knowledge occurs very fast – impact of each (inf.) small gain will remain (inf.) insignificant).

Now I would like to develop the case when teaching the methods is even more important than the factual truth. I believe one can teach a “fairy tale“ by telling it in such a manner that it would be more useful than a bunch of uncorrelated facts or factiods (btw I am actually trying to write a book “for kids and scientists” exposing that principle). Let us work with a unicorn statement by saying out-loud that “A unicorn is a magical creature with a single horn and it lives in an old mystical forest.” This sounds like a fairy tale. But in fact most of the school subjects were told to me in similar manner. Let us try to “scientify” this with different contexts.

(Mythology) The unicorn is a legendary animal commonly portrayed as a white horse with a goat’s beard and a large, pointed, spiraling horn projecting from its forehead. It was commonly described as an extremely wild woodland creature.
(Biology) The unicorn is a fossil animal dating back to 30 000 BC. Woodland can be seen as predominant habitat of this animal. The unicorn shares anatomical features of other equities, except for the males having long featured single horns, that start developing after the age of 3.
(Taxonomy) Unicorns together with horses and related animals, including the extant horses, donkeys, and zebras, and many other species known only from fossils belongs to the taxonomic family of horses and related animals, including the extant horses, donkeys, and zebras, and many other species known only
from fossils.

All those contexts did not helped much. I hope I was successful in making them look artificial enough with a bit of controversy mixed in.

(Paleontology) Recent evidence based on the studies of the fossil material of the horns shows that unicorns most likely to be related with goats than equities.

So you can just preach this, but how do you tell the difference between a fantasy and the real thing (even in case it is a fake). Well, probably you start with analytical thinking and do some brain storming::

  • goats can evolve into unicorns after living long enough in the forest
  • equities descent from goats
  • we need more data
  • giving facts are logically inconsistent hence statements are neither complete nor sound

Obviously, we lack proper rational setting in our knowledge framework, (actually to have knowledge framework would be a benefit in order to argue more productively). We can also do more experiments but we have to define how and what an experiment is. This can be seen as a basis for scientific thinking.

I wish to stay consistent with my previous statements (“start doing Phd in school”) – we should teach science not subjects (in a sense of scientific methods as instruments of gaining knowledge, instead of dogmatics). One should be able to check it, check it, check it.. :) It does not kill to be trained to check on Aristotle’s Organone just to comprehend why there is a possibility of redefining the logical framework in “if () {} else {}” statements of the modern programming language (ultimately it is also important to be able to do this with and without such “check” simultaneously). Dogmatics is super dangerous, especially in a rational body of knowledge (Aristotle’s Fly — the longest lasting error in the history of science.)

There are tons of literature both popular and special that teaches Scientific method. But one should do it as soon as possible in schools. In my school I was never given such an opportunity, just fed with facts. Most quotes I could try to end this entry with are to be found in this video.

Mate with Knight+Queen

When playing beginning players one can keep in mind a very interesting development – mate by simultaneous attack with Knight plus another piece (Qa4):

But after taking a closer look at the technique, it turns out that it was mastered by players like Alekhine to a much greater extend. For instance, in the game of 1928 he was able to win by trading more valuable piece (Qxe4!!) for the strategic benefit. The final move involves mate by knight + piece combination and makes it a nice puzzle to follow.

age of web wonders

The speed of modern javascript engines preludes the age of virtual web wonders. Indeed, if you missed the biggest IT news of the yesterday – it is a linux image which you can start in your browser. This was made possible by a prominent french programmer Fabrice_Bellard. The author has included the port of his TinyCC (compiler). One can compile a “Hello world” program as in here:

Welcome to JS/Linux
~ # ls
hello.c
~ # cat hello.c
/* This C source can be compiled with:
   tcc -o hello hello.c
*/
#include                                                              

int main(int argc, char **argv)
{
    printf("Hello Worldn");
    return 0;
}
~ # tcc hello.c -o hello && ./hello
Hello World

The possibility to compile any C code in a linux image attached to a website provides access to the biggest repository of the existing software (libs and algorithms). Obviously the list of supported languages can be extended.

Further, the main limitation for now remains a lack of peripherals. First of all, no other networking device is available beside the loopback interface. In many blogs and forums it was proposed that emulator of eth or similar virtual device will use websockets to communicate to a local server (which will provide access to a driver of the real device) or to share the remote network on the side of the http server (from where the image was started).

What’s next? One can download the source code and start playing with it:

But please note that the most of the code is obfuscated optimized (whitespaces, line breakers are removed; identifiers are shortened to 2 letters) and according to the copyright notice “Redistribution or commercial use is prohibited without the author’s permission”.

Bellard uses BusyBox linux distribution, for which he provides his kernel configuration (see details). A very nice technical insight into what is going on is given in this quora explanation.

Related

Building JS emulators of processing units is not new. For instance, JSNES emulates Nintendo gaming console allowing to run ROMs of old games as tetris (by Alexey Pazhitnov), contra, donkey kong, etc. Similar (x86) emulators were also developed in pure Java, e.g. JPC.

Another remarkable JS simulator of MOS 6502 8-bit processor (both NES and Terminator’s chip) allows to visualize the computational state of the unit. This time authors have taken their time to actually digitalize the full image of the chip, instead of just approximating the computation results or following available VHDL-like semantics. As the result one can see how program runs inside the “X-rayed” chip.

Conclusion

Having a proper x86 emulator in Javascript is a very important step for the web, as in analogy of developing HTML5 towards substituting plugins (ActiveX, Browser extensions) or applets as Flash, Java, Silverlight and so on. Eventually this is just a part in the cycle of

  • => (stimulating better portability) thus ->
  • => (stimulating better standards) thus ->
  • => (resulting in better portability)  .. ->

Applying a very powerful concept of virtualization to the web will obviously shift the software paradigm in numerous ways.

Fast way to reset Magento admin password

Assuming a full file access to the Magento root, a best option of reseting an admin pass is to manipulate the EAV data model called ‘admin/user’, responsible for back-end authorization.

For a quick start just copy cron.php (with include statement of Mage.php), i.e.

cp cron.php resetpass.php

Throw away the unnecessary cron event handling and replace it with:

$username = 'admin';
$password = '123456';

$user = Mage::getModel('admin/user')
    ->load($username, 'username');
if (!$user->getId()) {
    throw new Exception('Failed to find ' . $username);
}
$user->setNewPassword($password)
    ->save();

Log all SQL queries in Magento

Since I don’t want to browse for it in the net again, I am just putting it in form of a patch (mostly for myself).

The best option I have found is to toggle boolean properties of the Varien_Db_Adapter_Pdo_Mysql class in the following file: lib/Varien/Db/Adapter/Pdo/Mysql.php

*** 83,89 ****
       *
       * @var bool
       */
!     protected $_debug               = false;

      /**
       * Minimum query duration time to be logged
--- 83,89 ----
       *
       * @var bool
       */
!     protected $_debug               = true; //false;

      /**
       * Minimum query duration time to be logged
***************
*** 97,103 ****
       *
       * @var bool
       */
!     protected $_logAllQueries       = false;

      /**
       * Add to log call stack data (backtrace)
--- 97,103 ----
       *
       * @var bool
       */
!     protected $_logAllQueries       = true; //false;

      /**
       * Add to log call stack data (backtrace)

A the result one can expect SQL logging at “var/debug/sql.txt”

Why CFG productions don’t care about the context

Such a great explanation of why CFG is considered context-free, i.e. why the context is irrelevant: “The grammars are called context free because – since all rules only have a nonterminal on the left hand side – one can always replace that nonterminal symbol with what is on the right hand side of the rule. The context in which the symbol occurs is therefore not important.” [1]  For example, if we have some sequence of symbols, then the context is understood as some subsequence of symbols except this concrete (occurred) non-terminal symbol we are considering for production input (left hand-side).

The dependence from the context is introduced by considering grammars with production rules, where there are more then one non-terminal on the left hand-side. In that case these non-terminals can be interpreted as a non-terminal with a context of other non-terminal(s). The later grammar can or can not be reduced to CFG case.

Unittesting in Vala

I have done some digging in vala-list on any state of art xUnit framework for Vala.

The best candidates are:

  1. vunit (had some forks)
  2. VUnit (partially based on previous one)
  3. Valadate

GTest is not in the list since it is simply too different from xUnit framework. I will play with the selection above more closely to build an opinion. The idea is to do better than just assertion as with help of GTest:

public class MyValaTest: GLib.Object {
	public static void doTest() {
		assert(true);
	}

	public static int main (string[] args) {
		Test.init(ref args);
		Test.add_func ("/MyValaTest/MyTest", doTest);
		Test.run();
		return 0;
	}
}

NB as you see, Test class in Vala contains bindings to GTest. This fact is not really well documented anywhere.