06-06-2008

Reference to a Method in PHP - Delegation Pattern

I've been just looking for possiblity to pass method by reference in PHP. Why ? Well, it is possible in Python where everything is an object.And it is sometimes really useful. Imagine that You want to call some particular specific method within some more general object. Imagine that the state of the general one depends on the results of this method. But as I said, implementation of this method may be different for different objects. It is sort of Delegation Pattern where responsibility of implementation certain behaviour is being delegated to some other object.
Of course it is possible to pass a name of a function as a string and or calling user function. But this is not I was looking for.
And again I found usefulness of magic methods in PHP.


class A{
public function say($word){
print $word;
}
}

class B{
/**
* try of calling not existing method on object B
* will execute the magic method
*/
__call($m,$a){
return $a[0]()->$m($a[1]);
}
}
$a = new A;
$B = new B;
// actually I pass object reference
$B->say($a,"Hallo");


Nice ? :) Quite... Of course body of magic __call method could be more sophisticated and more general. But this is just for testing of the behaviour that I was looking for. Anyway, I have found such sophisticated implementation using Reflection. However my apache is always blowing out while trying to call that code :).

18-03-2008

Problems with Floating-Point Comparisons

I like to repeat that man learns at every step. And this is what I've learned while writing unit test. I have wanted to retrieve the record from database that I have just inserted there. The value that I have been looking for has been type of float. What was my surprise when the query returned me empty sets but retrieving all records I have seen that value there...

This is general known float numbers issue. The solution is simple. You have to define the tolerance that is acceptable for You and while trying to get particular value write the where clause using range like that:


SELECT * FROM CUSTOMER WHERE budget>1224.56-0.0001 AND budget<1224.56+0.0001


You may find more about the issue on Problems with Floating-Point Comparisons

07-03-2008

Transliterator for UTF-8 text to Latin

I was looking for solution to change UTF-8 encoded text with national characters to latin equivalents. It would be useful while generating for instance url to post depending on blog post title. I saw that some services use it. (Strange but not blogger) And i wanted to have some global, multi language solution...

And here it is PrettyLatin - my contribution to PhpClasses...

So how it works - You call one method with UTF-8 encoded string and got returned text with removed diacritic characters. Pretty simple :)

For now it only fully supports russian characters and polish ones. (plus some other).

There are two possible way of class development:
- we can define new entity names so called friendly codes that are missing right now in HTML table (in that way maybe in future we will cover all of them :) ) for example:
280 = "Eogonek" (Ę=>&#280;=>&Eogonek; which gives latin E)
321 = "Lbar" (Ł=>&#321;=>&Lbar; which gives latin L)
1071 = "YAcyr"(Я=>&#1071;=>&YAcyr; which gives latin YA)
like here.This is especially helpful for not latin base alphabets like russian cyrillic...
- or we can map codes of national character to latin equivalent...

Feel free to comment and improve my code and pls notify me about newer, more complete version...

25-02-2008

on Events abstract approach

I have already met the events on my programming way. It was event based MVC framework - Mach-II. The approach is quite nice, and is applicable especially to GUI designing. However it's usage is much more wider. Anyway, I've decided for myself to make it finally clear, and set the concepts in proper order according to the abstraction of the idea...

So the clue of event-driven programming or event-based approach is event as the name says. But what the hell the event is ? :) I've already heard that someone codes the event or call it. Personally I find an event as signal that the action should be taken on. Such signal is actually being announced to the event handler. Event handler listens to the events that registered interests in and takes proper action. Of course there is an events model needed that helps with all that - understand the signal (variable, object, reference to a method) as the event, allows to register and handle it...

Good enough for now to use it as argument and to speak one language... Will be back to it with implementation of events in Python, Observer Pattern, and AOP ...

14-02-2008

Lesson Learned (from the agile development)

Man learns on each step. And the agile methods help to walk that way...

We are just applying these methods in my company and I moved here just three months ago. You can understand then my enthusiasm ;) This is really active time for me. I'm again pushed to use my mind which I missed not so long time ago. I'm surprised when looking at my watch and finding out that 5 pm has just arrived :) It is difficult for me to keep up blogging while so intensive days :)

So I will note here just few things I've learned according to agile development methods itself. Later hope to find time to write about things I've learned as programmer...

1 - it is really important to start implementing tasks with unit tests and after that to implement concrete methods
2 - unit tests should be really detailed ones not to miss any tarantula
3 - if requirement is related to the research on some issue then some bigger time buffer should be set for it (if we solve it faster we can still add some task to the requirement otherwise we can have to much opened tasks on the end of iteration)
4 - while number of iteration is increasing within a project, integration tests are more and more meaningful so the estimations should be done accordingly ...

that's all for now :)

cheers

03-01-2008

Class Properties Implementation - accessors and nested functions in Python

This is really quite nice in Python how You can define getters and setters (if You need them) for properties of a class ( actually in Python 2.2+ meaning new style classes ). There when the class derives from object then You can use built-in function property(). The one gets as arguments accessors methods accordingly fget, fset, fdel, doc as getter, setter, function for del'ing, and the docstring (let say a comment used for documentation) of the property attribute.
So we can create class property as an instance of property() like that:


class Foo(object):
def __init__(self):
pass

def get_bar(self):
return self._bar
def set_bar(self, value):
self._bar = value
def del_bar(self):
del self._bar
bar = property(get_bar, set_bar, del_bar, "I'm bar property.")

foo = Foo()
foo.bar = 'bar'
print foo.bar

This way is good either if You want to define visibility scope for attribute. Then, for example for protected attribute, You would not define setter method and not pass it to property().

As can be seen above, there is no more a need to call the helper methods for accessors directly. So why not to hide them and clean the name space. Here comes a tricky part :) with nice nested methods:

class Foo(object):
def __init__(self):
pass

def bar():
def fget(self)
return self._bar
def fset(self,value)
self._bar = value
return locals()
bar = property(**bar())

Built-in dictionary of locals returned from bar() is being projected onto named arguments of property. Values of dictionary are tied with instance of property.

To tuple or not to tuple
This is the question I asked myself after had found out that the above example will only work, when there is no any other local variable defined in a function ,that would be returned by locals() additional and wouldn't match arguments of property(). This is what You can do in such case - create property using tuple like that:

return fget, fset
bar = property(*bar())


Well, to be honest, most probably I would use a tuple as a immutable object - ordered list of constants defined explicite... meaning - if You put there something, You know it is there... yeah, can't be more clear ;)

02-01-2008

ORM does not like multiple primary keys

This is the first thing I've learned just at the very beginning of getting to know Object Relational Mapping. ORM Systems does not like multiple primary keys. Primary keys on several columns should be replaced by one unique PK. This sounds trivial while revealed to me. Finally this is about objects' relations but not database tables. And them, the objects, should be uniquely identified. The main, most important and the hardest thing at the beginning is to change thinking from RDBMS to ORD (ORMS). Object is the point.