Monday, May 11, 2015

Everything Happens For A Reason

Everything happens for a reason. I know it sounds cliche but it's true. You may not realize what the reason is now. But there's a big chance that you'll know it later.
I've always dreamed of being a pilot as a kid. I grew up with that dream and took a good care of my eyes. When I was 18 and about to graduate from highschool, my Mother disapproves of my dream. So, I moved on and took computer science. One thing stayed. I always take a good care of my eyes, until this day.

I got married when I was 24, had my daughter when I was 25 and had my son when I was almost 27. Now I had a second daughter at 31. When my son was about 1 year old, we realized that something wasn't right with his right eye. We went to the doctor for diagnosis and teatment. The doctor said that we'd have to wait until he's 2 years old for futher treatment. The diagnosis was Strabismus.

When he was 2, we went to the ophthalmologist. He had his first pair of glasses because his right eye had a bad eyesight. That condition is impacting his left eye as well. We went for another check last weekend. This was about 2 years after the first check. His right eye condition was not getting any better. So he had his retina photographed. I could tell from the doctor's face that something was wrong. My son had Toxoplasmosis attack when he was still a fetus. It affected his right eye but he survived. That left a scar that prevented the propper nerves to form. The scar is near the center of his eyesight. This is why his right eye is not getting any better.

The doctor said that there is currently no cure for this condition. This is when I realized why I have a good pair of eyes. Anyone reading this please mark this promise I made to my self. I'll take a good care of my eyes until 2035. By that time I'll be 51 and have seen a lot of this world with my own eyes. By then, I'll be giving both of my eyes to my Son and took his eyes. He'll still have a lot of adventure by that time and I want him to see it with a better eye sight. I'm hoping that he'll be happy to see it through my eyes. I love you my son.



regards

-E-

Thursday, January 29, 2015

Android TDD Using JUnit, Robolectric and Mockito

Android now supports unit testing. Android unit testing runs on devices or emulators that adds extra time during development. I'm a big fan of TDD and the way android testing is done does not help me much.

However...

Thanks to Robolectric, headless testing is now possible by leveraging gradle. I wrote this post as a result of trying out several different approaches that confused me. Hopefully this one will help you set it up better.

How?

First, current common Android development projects are tightly coupled. Most stuffs are done in the Activity or Fragment, blame the tutorials. This makes it hard for us to actually do unit testing since a unit test should be isolated. The tight coupling makes unit testing impossible.

Then, come this approach called Model - View - Presenter (MVP). MVP aims at separating concerns.
The three components involved handles different concerns:


Model

This component groups all data related concerns. This component handles data queries (be it via SharedPreference or SQLite) and network connections. The Model will be the provider of data for the Presenter. The presenter will then sends the data to the View.


View

Just like the name suggests, the View handles all the presentation layers. You change the views, texts and contents in this component including navigation. The View should not handle view state and logic.


Presenter

This component handles the logic of the view and maintains the view state. The presenter will be the one communicating with the models and updates the view when data is retrieved.

The MVP components are created using interfaces that standardize it. This separates concerns from each layers. The components communicates via the interfaces and does not connect directly to the implementations. This removes tight coupling of each layers. You pose no effect to other layers when you subsitute your implementation.

For more references on MVP implementation try reading this blog post by Antonio Leiva.

Setting Unit Test on Android Studio Project

We need a special gradle plugin to set the environment for unit testing on Android. I use the Robojava gradle plugin by Gautam Korlam. It is simple and easy to set.

The first step you need to do is to create another module on your android project.


Make sure that you select the Java Library module.


After the module is created, delete all other files except the build.gradle so it looks like this


I named my library tests.

Now we need to tweak the gradle settings.

Make sure that your new library is listed in the settings.gradle file.
Mine looks like this:
include ':app', ':tests'
After that, we need to change the root build.gradle. That is the one on the root of your project.
Mine looks like this:


The next thing you need to change is the build.gradle of your main app. Make sure you list all the dependencies in this file and make sure that you set the test source to src/androidTest. Mine looks like this:


Then, finally you need to change the test module's build.gradle. This is to make sure that the test are executed in the right source and can be done headlessly. Mine looks like this:

And there you go, it's ready.

Anytime you wish to create a test, simply create a test on the presenter class, make sure it's a JUnit 4 test case and it's placed in the androidTest/ directory.

Anytime you wish to run the test go to the command line, cd to your project root folder and run:

$ ./gradlew test

If it is successful you should see something like this:


Robolectric tests can also be run for integration tests. You can also use this for continuous integration. Simply run:

$ ./gradlew build

Gradle will build and run tests. If any of the tests failed the build will fail.

I have the full example of this project on github. However, the project does not demonstrate the use of dependency injection as suggested by Antonio Leiva.

Feel free to comment or ask me.

regards

-E-

follow @femmerling on twitter
visit my website

Thursday, November 20, 2014

Minimalist Distraction Free Setup


I got a new MacBook air at the new company I work at.

Unlike my previous MacBook air, this one only runs i5, 4GB Memory and 128 SSD which means I have to preserve a lot of space to have a nice coding rig.

I have to be able to code android and iOS on this one. Plus, I should be able to hack stuffs on it for my personal fun as well.

So how do I deal with that?

I need to reserve around 9GB for XCode, 7 GB for Android Studio + SDK and around 20 GB for basic OSX stuffs. Plus I have to reserve the iWork and iLife stuff since this is a company laptop and someone else who's gonna use it later after me.

I'm a big fan of music and movies, and the space limits it. So how do I deal with it? Use the cloud. I put all my old codes in a private git repo. I stream all my music using deezer and put all my favorite movies on my Google Drive account.

Now I can still have it while I run on a minimal setup.

As a comparison, now I have 83.59GB of free space out of 128GB on the new machine compared to around 60GB of free space out of 256GB on my old machine.

Another thing that I am currently trying is to minimize distractions. I don't have any social media app installed on my coding rig other than Slack, which is the company's official app. No Skype, no Twitter, none whatsoever even on my browser. I use all of it on my phone, which I randomly check. I can focus more because of that.

Try out a minimal setup, you'll be surprised at how it increases your productivity.

regards

-E-

follow @femmerling on twitter
visit my website

Sunday, November 9, 2014

Thank You IceHouse

Last Friday was my graduation day from IceHouse University. It was a wonderful 2 years journey and I consider it as a university since I learned a lot more stuffs there compared to my previous 7 years in the tech industry. 

Not only that, I met a lot of interesting people there. I met the team who rose from being laid off by Yahoo!, and they're truly inspirational people to work with. I met an unknown guy from Singapore who became my best friend at IceHouse and now became a CTO at bridestory. I met one of the most prominent geek in Indonesia who now leads an RnD lab. I also met a travelling designer, a cool frontend guy who likes to go to beer garden, a tech guy who refuses his parent's wish to be a government guy just because he thinks it's not geeky enough, a really smart geek who comes from no man's land and become the first winner of code of the week at IceHouse, a developer evangelist turned engineering lead, a photonic science master degree graduated from Germany turned into QA lead, finally got a mentor who graduated from MIT and most of all, I met with the brightest minds in Indonesia that I'm sure will be the future tech leaders in Indonesia.

However, the show must go on. Starting tomorrow, I am starting my new career at Traveloka, a cool young company in the travel tech industry. A lot of people are asking me why. I've always followed Traveloka since their first launch. I like how the company is run and I like the idea that they want to do different things with the industry, most of them I can't disclose here.

Thank you IceHouse, for giving me the chance to get exposed more to the edge of technology. You guys are the geekiest crowd ever and I'm proud to be one of the alumnee. I now follow the vision of IceHouse to create future leaders of the Indonesian tech scene.

Here's to a wonderful 2 years at IceHouse and to an exciting step forward with Traveloka!

regards

-E-

follow @femmerling on twitter
visit my website

Tuesday, October 28, 2014

Been a long while

It's been a long while since I last wrote on this blog. I know.

It's been almost a year but I was busy doing a lot of stuffs. Along with my team, we built a retail platform that combines augmented reality, location technology and cool analytics as one product.
I also built zappr in my free time and I have several more ideas that I want to build within 1-2 months.

I have learned a lot in the past 11 months. I learned more on how to lead engineers to actually build products and how to market it. I learned how to manage engineers by treating them as normal human beings, not production machines. I believe that I have had some of the best lessons so far in my life.

Also, I'm going to go for a new challenge. After more than 2 years at Ice House, I finally decided on taking up a new challenge in a product company. Make no mistake, doing services for others are cool and you can learn a lot of things. I just want to do things that I can actually pour my heart into. Build things that people can use and make their lives easier. I'll finish and my career at Ice House in less than two weeks and then I'm starting my new challenge.

That's all from me now.

regards

-E-

follow @femmerling on twitter
visit my website

Sunday, November 17, 2013

Form and JSON Post Request Value to Model Mapper Functions For Flask

One of the iritating part that you have to deal with when you're handling POST requests in Flask is how you map the values into the model. It could be in JSON or in common form format.

Usually this is what you do
# say this is your model
# using SQLAlchemy
class User(db.Model):
username = db.Column(db.String(50))
password = db.Column(db.String(50))
        email = db.Column(db.String(50))
first_name = db.Column(db.String(50))
last_name = db.Column(db.String(50))
# this handles the common form request
app.route('/process_form',methods=['POST'])
def data_process():
new_user = User(
                                 username = request.values.get('username')
                                 password = request.values.get('password')
                                 email = request.values.get('email')
                                 first_name = request.values.get('first_name')
                                 last_name = request.values.get('last_name')
                            )
       # okay and then you do the rest
# or perhaps JSON post request handling
app.route('/process_json',methods=['POST'])
def data_process():
data = json.loads(request.data)
new_user = User(
                                 username = data['username']
                                 password = data['password']
                                 email = data['email']
                                 first_name = data['first_name']
                                 last_name = data['last_name']
                            )
       # okay and then you do the rest
That's if the model has 5 properties, what if you have more than 10 or perhaps more than 20 properties? That would be tedious. We should try making it simple and DRY, right? I wrote helper functions to help you simplify it.
import json 
def new_json_parser(passed_object, payload_data):
"""
Maps passed json object from client into expected object.
Use this for creation of new object by passing an instantiated
empty object into the passed_object variable
"""
payload = json.loads(payload_data)
for key, value in payload.items():
if hasattr(passed_object, key):
setattr(passed_object, key, value)
return passed_object
# this part is for form request value handling
def new_form_parser(passed_object, request_data):
        """
Maps form request object from client into expected object.
Use this for creation of new object by passing an instantiated
empty object into the passed_object variable
"""
        for item in request_data.values:
            if hasattr(passed_object, item) and request_data.values.get(item):
                 setattr(passed_object, item, request_data.values.get(item))
        return passed_object

Using these helpers, all you need to do for the previous request is way simpler.

# using form
app.route('/process_form',methods=['POST'])
def data_process():
new_user = User()
        new_user = new_form_parser(new_user, request)
       # and there you go, your new user object based on form post request is ready
#using json
app.route('/process_json',methods=['POST'])
def data_process():
        new_user = User()
        new_user = new_json_parser(new_user, request.data)
       # and there you go, your new user object based on json post request is ready

Neat codes, eh? And no matter how many attributes in your class, it is that simple. You can also use this for handling edit operations. Simply passed in the object having query results into the parser functions.
def edit_json_parser(passed_object, payload_data):
"""
Maps value from passed json object for data edit purposes.
You need to pass in object resulting from query into the
passed_object variable
"""
payload = json.loads(payload_data)
for key, value in payload.items():
if key != "id" and value != None:
if hasattr(passed_object, key):
setattr(passed_object, key, value)
return passed_object
def edit_form_parser(passed_object, request_data):
        """
Maps value from passed json object for data edit purposes.
You need to pass in object resulting from query into the
passed_object variable
"""
        for item in request_data.values:
            if item != "id" and hasattr(passed_object, item) and request_data.values.get(item):
                setattr(passed_object, item, request_data.values.get(item))
        return passed_object

Have a better and more DRY code? Great news! Hope this helps.

Btw, I'll be shipping these methods with my EmeraldBox framework very soon, while my backyard framework already has it :)

regards

-E-

follow @femmerling on twitter
visit my website

Wednesday, June 5, 2013

Simple FTP Server Solution With Python

I spent the last few days dealing with setting up an ftp server on by ubuntu vps and haven't figured out a solution. Then I tried asking my friends thru my facebook account. My friend Sakti came up with a very simple and elegant solution using python.

The solution uses pyftpdlib package. To install the package is really simple:
$ easy_install pyftpdlib
or
$ pip install pyftpdlib
after that create the folowing script and save it as a python file:
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
authorizer = DummyAuthorizer()
authorizer.add_user("user", "password", "/home/user", perm="elradfmw")
authorizer.add_anonymous("/home/nobody")
handler = FTPHandler
handler.authorizer = authorizer
server = FTPServer(("0.0.0.0", 21), handler)
server.serve_forever()
Then run the script and try accessing your server using ftp.



Only 10 lines but it works like a charm, a very python way :)

If you want to download the script you can get it here.
Hope that helps.
regards
-E-
follow @femmerling on twitter

Thursday, May 9, 2013

Why Python?

A lot of people asked me this when I told them that I'd always prefer to use python whenever possible.

The answer is:

For me it is the language that is elegant, very readable, easy to use and learn and it has limitless potentials. I can develop more things after using it for 2 years compared to what I can develop after using PHP for more than 5 years.

I'm not saying that the other languages are not good and that python is the best. All I'm saying is that it's the language that fits me best, makes me happy and performed accordingly to my expectation. For other uses I'd use other language as the right tool for the right job.

What's your preferred programming language? Why?


regards

-E-


follow @femmerling on twitter.
I recently revamped my website after 2 years. Visit http://www.emfeld.com if you have time