Saturday, March 22, 2014

Reg for ZCA addicts - Part 1: Adapter

Recently I have been exploring Reg, a library inspired by zope.component written by Faassen, and I found its simplification of ZCA to be pretty interesting. Being really loving ZCA for a while now, I end up trying to figure out how the ZCA patterns fits if I were to use Reg in my future projects. So here are the list of items I discovered so far:

Adapters
This is the most common pattern I use with ZCA, primarily to simplify writing logic by standardizing the interface of objects I work with into a common set of behavior/functions:

In ZCA , adapters are done this way:

from zope import interface
from zope import component

class IDog(interface.Interface):
    def bark():
        pass

class Dog(object):
    interface.implements(IDog)

    def bark(self):
        return 'Woof'

class IDuck(interface.Interface):
    def quack():
        pass

class Duck(object):
    interface.implements(IDuck)

    def quack(self):
        return 'Quack'

class IAnimalSoundAdapter(interface.Interface):
    def make_sound():
        pass

class DogSoundAdapter(grok.Adapter):
    interface.implements(IAnimalAdapter)
    interface.adapts(IDog) 

    def __init__(self, dog):
        self._dog = dog

    def make_sound(self):
        return self._dog.bark()

class DuckSoundAdapter(grok.Adapter):
    interface.implements(IAnimalSoundAdapter)
    interface.adapts(IDuck)

    def __init__(self, duck):
        self._duck = duck

    def make_sound(self):
        return self._duck.quack()

gsm = component.getGlobalSiteManager()
gsm.registerAdapter(DogSoundAdapter, (IDog,), IAnimalSoundAdapter)
gsm.registerAdapter(DuckSoundAdapter, (IDuck,), IAnimalSoundAdapter)

dog = Dog()
duck = Duck()

dog.bark() # Woof
duck.quack() # Quack

IAnimalSoundAdapter(dog).make_sound() # Woof
IAnimalSoundAdapter(duck).make_sound() # Quack

And that is quite a code, which utilizes classes, and large external interfaces libraries and component libraries which might can make many pythonistas cringe. 

In Reg, things are simplified. Similar functionality is done not through interfaces and complex object/class markers, but through simple functions. Reg adapter interfaces are simply functions:

import reg
import reg.implicit


class Dog(object):
    def bark(self):
        return 'Woof'

class Duck(object):
    def quack(self):
        return 'Quack'

@reg.generic
def make_sound(obj):
    raise NotImplementedError

def dog_sound(dog):
    return dog.bark()

def duck_sound(duck):
    return duck.quack()

registry = reg.Registry()
reg.implicit.initialize(registry)

registry.register(make_sound, [Dog], dog_sound)
registry.register(make_sound, [Duck], duck_sound)

dog = Dog()
duck = Duck()

make_sound(dog) # Woof
make_sound(duck) # Quack

Now, ZCA users might shout by now, "I want class based adapters!" (I know I did). This is how it can be implemented in Reg:

import reg
import reg.implicit

class Dog(object):
    def bark(self):
        return 'Woof'

class Duck(object):
    def quack(self):
        return 'Quack'

@reg.generic
def animal_sound_adapter(obj):
    raise NotImplementedError

class DogSoundAdapter(object):

    def __init__(self, dog):
        self._dog = dog

    def make_sound(self):
        return self._dog.bark()

class DuckSoundAdapter(object):

    def __init__(self, duck):
        self._duck = duck

    def make_sound(self):
        return self._duck.quack() 

registry = reg.Registry()
reg.implicit.initialize(registry)

registry.register(animal_sound_adapter, [Dog], DogSoundAdapter)
registry.register(animal_sound_adapter, [Duck], DuckSoundAdapter)

dog = Dog()
duck = Duck()

animal_sound_adapter(dog).make_sound() # Woof
animal_sound_adapter(duck).make_sound() # Quack

Thats it for now, next one I will probably share about multiadapters on Reg.

Life updates

So .... I think by now plenty of people have known that I left Inigo Consulting a few months ago. Those who are wondering why, the reasons was primarily due to personal mental health reasons, which I identified the core source was that I pushed myself a bit too far, investing time and emotion on a business model that is not going to work out for my long term personal goals.

I realized things are not going to the right direction for about a year plus, but my philosophy of persistence kept me pushing it - possibly beyond my limits and nearing total mental breakdown. This led to generalized anxiety disorder, which destabilized myself, and led to other problems that cause plenty of things around me to collapse and destroyed, affected me emotionally (which, again, I did not realize my limits, due to I have a bit of alexithymia - inability to accurately identify and express complex emotions besides basic happy/sad) made me push myself into a mild form of emotional posttraumatic stress disorder which kept popping up until today if I forgot to take my pills before sleep. Kind-of silly really, it was not a big problem if viewed in a logical perspective, though probably I was already at the verge of mental breakdown before it happened, so the effect was amplified.

Took me 3 months struggling with GAD/PTSD aftermath where I was really dysfunctional , before I finally decide to seek professional help, and now on Alprazolam / Xanax therapy to help balance out the calming chemical in my brian, and recalibrate it to be able to handle stress and anxiety again. Some people might want me to stop taking this, but for me, I think I'm well informed enough on the purpose of the drug to take it properly for the therapy. My doctor himself had anxiety disorder before, and he is quite helpful in providing instructions and information on how I should self-tune the dosages, to reboot the brain to be able to handle high stress like how it used to.

Decided to make a drastic changes in my life, started with getting rid the core anxiety source, so I quit Inigo, revamping a lot of things, and in the process of selling my shares in Inigo to get back my previous time investments, anf to fend off and get rid of any financial related commitments while I figure out whats new to do. I would probably be better off as an independent dev, however, I lack the understanding of full business process and sales cycle flow due to being shielded from clients, and stuck with a single technology during in Inigo, I have to start from scratch on this one and pick up the skills elsewhere first.

Been on Alprazolam for more than 2 months now, at about 1mg-2mg dosage per day, and its been really helpful in keeping myself functional. I would probably be on this for the next few months to a year before I can slowly cut down on the dosages.

I'm currently in heavy research mode on Hadoop/BigData ecosystem for a Proof of Concept implementation for Abyres Sdn Bhd. Not sure about joining them full time yet, as I'm currently joining as an intern. Waiting for paperworks to be out first before giving my final decision. Nonetheless, I learned quite a bit of business process, client management processes, leveraging partner skills and their version of tech company business model, simply by observing the clients, the team, and the management. The most interesting lesson I learned so far was from Jeff of Hortonworks, where he helped me understand the separation of development, product, services and sales and how these 4 components can be separated into several companies, and how they function and interact with each other in a FOSS-based business ecosystem.  These knowledge are really useful if I were to decide to run my own company again in the future, or perhaps a return to Inigo.

I will be blogging about them more in the future, after I get this Tech Discovery POC fully running and I will share what I have learned and discovered.

P/S: I particularly like the reduced fear/reduced inhibition effects xanax have. Somehow I feel like I'm back to my university days, where I'm willing to take the risk of doing something big , cool, and which others are skeptical and afraid to try. For example, dealing with large corporations for a big project. Really helpful in making me take that first few steps of the stuff I really want to do.

To those still in Inigo, all the best. I might come back in future, either joining it back, or to create a separate branch of it, but things will be run in a totally different way than the model we used these past 5 years. Though that will all depends on how things are progressing, and what I will be learning and discovering throughout the months/years to come.

Friday, February 21, 2014

Reboot!!!!!!!!!!

So I guess I have been missing from posting things on this blog for over a year now ... Bunch of stuff happened in the past years .. theres a likelihood that I might be back ^_^ ..