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

Monday, March 18, 2013

Creating Python Virtual Environment Without Installing To The System

When you develop projects in python, you often met the situation where you will need to install various packages that will clutter the general python installation in your system. Usually people will use the famous virtualenv. However, it often brings trouble when you have to run 2 or more python environments simultaneously.

I'm about to share a simple trick that you can use to separate those environment, still using virtualenv but we're using it differently. The examples are in unix/linux, but the usage is quite similar in windows.

First, download virtualenv.py from https://raw.github.com/pypa/virtualenv/master/virtualenv.py.
That should be done in seconds.

Next up, create any folder where ever you want to create the virtual environment and copy the downloaded virtualenv.py  file inside the folder.

Now, run the following command:
python virtualenv.py env
That will create a folder called env inside your current folder and the folder will contain your newly created virtual environment.

Installing package into the environment? Worry not, it's very simple.

simply run:
env/bin/pip install
after installting the packages, write your python applications inside the folder. When you want to run the file, you have 2 ways of doing it:


First, the fast way but a bit longer typing:
simply run:
env/bin/python .py
Second, longer at first but takes less time if you have to repeat it on and on.
Add the following line in the topmost line of your file
#! env/bin/python
then run

chmod a+x .py

afterwards to run it, you can simply call it this way:
./.py
Aaaaand you're done! No need to change stop virtual environments to run the other and your global system is clutter free.

In windows, change the env/bin/pip or env/bin/python into env/Scripts/pip or env/Scripts/python.

Hope that helps.

Happy hacking!!!

regards

-E-


follow @femmerling on twitter

Thursday, March 7, 2013

LDAP Login Authentication Using Python LDAP

About 2 days ago, I have to create an app for internal company use. I'm using EmeraldBox to develop it. At first I created the user model to save user credentials and using it during the login process.

However, the boss came with an idea that I should use LDAP login since we have it and it provides a single sign-on solution, which is better.

So, I googled around and found python-ldap.

You can easily install it by running:
pip install python-ldap
and it's ready to use.

The question is, how can I use it?

Check out the code I use below for references. The steps should be the same. However, you need to adjust some values accordingly to the LDAP settings in the server.

It's as easy as that and you can perform login and get the user credentials. In my app, I use Flask's session to put the user credentials so I can fetch it whenever I need to.

Feel free to use my code.

Happy hacking!!!


regards

-E-

Thursday, February 28, 2013

EmeraldBox: My New Boilerplate Python Framework And A Mini Tutorial

I believe that developing web apps should be done in the easiest and most efficient ways. Python offers that. However, most of the available frameworks required a deep learning curve for new users and most users have problems deploying the web app.
After using several python web frameworks as well as using other languages' frameworks like Rails, Django, CodeIgniter and Zend over the years, I came to a conclusion that python is the easiest and most efficient language.
However, it will require tools that may speed up development. It that sense, Rails has a very good approach and will be implemented for the helper tools. Integration with 3rd party packages should also comes easily without having the need to interfere with the main OS, hence comes virtualenv.
Thus, comes EmeraldBox, an easy-to-use, light-weight, and easy-to-deploy framework.
EmeraldBox comes in a localized environment and includes standard packages that are commonly used in web development. The tool includes framework management tools and is designed to give ease for developers in managing database and migrations.
I initially develop the boilerplate framework based on a challenge from my boss at work. Can python be as good and as easy as Rails? Challenge accepted! I started doing research on best practice for Flask structures. Then I started getting into sqlalchemy as well since I think it is not that tightly coupled with the framework. Then I think of things that might be helpful for new users and will speed things up for more advanced users.
To see how easy it is to use EmeraldBox, see the tutorial below.

EmeraldBox Tutorial
Part 1: Setup
To start using EmeraldBox, download the package from the github source.
Extract the package wherever you want and get inside the EmeraldBox folder.
The package comes with 5 basic components:
  • Flask
  • Flask-SQLAlchemy
  • SQLAlchemy
  • SQLAlchemy-Migrate
  • Tornado Web Server
You can add the basic package to install by opening the config.py file and add the ADDITIONAL_PACKAGE list, i.e:
ADDITIONAL_PACKAGE = ['PIL','nose']
If you don't want to add any additional package just leave it as it is.
That's that, now open your command line and run the setup file
python setup.py
The setup will now run and install the pack in your virtual environment. Once the setup is done, try out the development server by typing
./box.py -t
or on windows
box\Scripts\python box.py -t
You should now see:

 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
Head to http://localhost:5000 and you should see EmeraldBox welcome note there.
Please remember that the testrun.py is a development server and should not be use for production usage. 

To run the production server under tornado, simply type:
./box.py -s
or on windows
box\Scripts\python box.py -s

To run the production server under gunicorn, simply type:
./box.py -g
or on windows
box\Scripts\python box.py -g


Congratulations, your framework is ready to use.


Part 2: Usage
The best thing about EmeraldBox is the helper tools available. The tools help you to skip basic stuffs a framework needs and just go ahead and focus at things that matter more.

For this tutorial we will build a simple personal site application with a sort of a contact module inside it.
We will need the following views:
  • Home
  • About
  • Contact
I put the contact in the last part because it will need some sort of database and we will generate it later with a different tool.
Now let's build your first view.
To initiate a new view all you need to to from the command line is:
linux/unix
./box.py -i
windows
box\Scripts\python box.py -i 
So to build the home view, all you need to do is:


linux/unix
./box.py -i home
windows
box\Scripts\python box.py -i home
and your home view is generated.

You should see the following messages:

Controller generated
view file generated and available at \path\to\app\templates\home.html

Now, run your server and go to http://localhost:5000/home and you should see the basic home view. You can edit this view to your hearts content :D

Do the same thing for about view. These two pages will be the static content.

Now, let's head to the contact view. In this view, you will need database access to store the data. By default, EmeraldBox is configured to run with SQLite database. However, you can configure the connection with other database engine in the config.py file.

Now, what would you normally need in a blog? I'd say you'd need the following information:
  • Contact Name
  • Contact Email
  • Message
Let's make them with our tools:
linux/unix
./box.py -n contact contact_name:string contact_email:string message:text
windows
box\Scripts\python box.py -n 
contact contact_name:string contact_email:string message:text
 Simply copy-paste the lines above to your command line and you will see the following results:



...........

Generating model controller for JSON data access
JSON data controller added
Entries view controller added
Single entries view and controller added
Entries add form controller added
Entries creation controller added
Entries edit and update form controller added
Entries deletion controller added

Database creation completed
New migration saved as /path/to/app-root/db_repository/versions/001_migration.py
Current database version: 1
Please run box.py -m to complete the migration process 
That shows that the model is generated already.

Now, you would want your contact page to display only the contact form, right? Let's edit the codes a little bit now. Open app/main.py and you will see the following codes automatically generated from the commands we used above.
Now, let's edit line the contact controllers from line 50 up to 136.
First thing, we would want the contact add page to show up when you open /contact. To do so, let's change line 50's routing into the following
then change the line 82 into the following.
Now you have the right view when accessing those views.
However, the topbar is still not displaying all the required information and the home page is still not showing the home page we created. We can do this in two ways.
First, let's open the templates/base.html and edit it into the following:
Second, let's edit main.py on lines 19-27 into the following to make it correct:

Part 3: Running
Now that we have all those stuffs sorted, just run:

./box.py -t
or on windows
box\Scripts\python box.py -t
And voila! Your website is ready!

To access it simply open http://localhost:5000/
To see the sent contacts go to http://localhost:5000/contact/view-all

You can get EmeraldBox at https://github.com/femmerling/EmeraldBox

This example can be downloaded from https://github.com/femmerling/EmeraldBox-Web-Example

Happy coding then!

regards

-E-


Wednesday, September 12, 2012

Sleeping Child

I enjoy waking up at night, glancing at my children in their sleep.  They both look so peaceful.

It is the best feeling in the world, simply because they can truly enjoy their sleep.

They can sleep without a burden in their mind.
Without worry and fear.
And most importantly, they smile in their sleep because their hearts are smiling.

I want that again.
I want to be able to sleep like my children.
Without fear or worry or thoughts.

Just a nice sleep.


regards

-E-

follow @femmerling on twitter