Compiling Proto File

I always forgot how to compile proto file and here is how compile .proto file for my personal note. 😀

$ protoc -I proto_dir/ proto_dir/proto_file.proto --go_out=plugins=grpc:proto

Example above for golang implementation, another programming language can use another out parameter.

  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.

Git: Fixing “error: There was a problem with the editor ‘vi’.”

I just installing vim on my machine, but it affected to my installed git. Every I merge my working branch, I always got this error.

error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.

After that, I have to commit manually to the merged changes.

$ git commit "merge from ... to ..."

The error because git using “vi” as the editor and I installed “vim” in my machine.
Here is how to fix this.

$ which vim
$ git config --global core.editor /usr/local/bin/vim


Grep: Find File by Content Keyword

Here is how to use grep to find file by content keywords recursively.

# Find in current directory.
$ grep -nr 'keyword' .

# Find in particular directory.
$ grep -nr 'keyword' target/directory

# Find in particular directory and file extension.
$ grep -nr 'keyword' target/directory --include=*.html


Save Media Files to Local Disk on Django-Storages

I’m using django-storages for storing my media files such us images, or document files to amazon S3. But if we using django-storages, by default all media files will uploaded to remote storage, in my case I’m using amazon S3.

I’m writing simple script to save both on remote and also local disk. To use this class, you just need to subclass this mixin class. Keep in mind to pay attention with mixin class order.

import os
from django.conf import settings
from django.core.files import File
from import FileSystemStorage
class LocalMediaSaveMixin(object):
"""Mixin class to for backing up media files on model that has
image/file fields. To use it, ensure to keep in my python multiple
inheritance order. The top most hierarchy must be on the most right.
class MyModel(LocalMediaSaveMixin, models.Model):
def __init__(self, *args, **kwargs):
super(LocalMediaSaveMixin, self).__init__(*args, **kwargs)
# Setup django local file system storage. = FileSystemStorage() = settings.MEDIA_ROOT
self.media_fields = [
i for i in self._meta.fields
if i.__class__.__name__ in ['ImageField', 'FileField']
def save(self, *args, **kwargs):
"""Save media file (Image, File) to local storage."""
from product.models import file_path # This import was from function that use to define path location and used on `upload_to=`
super(LocalMediaSaveMixin, self).save(*args, **kwargs)
# Getting file or image fields.
for field in self.media_fields:
raw_file = getattr(self,
file_name = os.path.split([1]
django_file = File(raw_file, file_name)
root, _ = os.path.split(file_path(None, file_name))
root = root.replace('static/', '')
# Ensure the file is not exists.
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, root, file_name)): = os.path.join(settings.MEDIA_ROOT, root), django_file)
def local_media(self):
"""Listed all local media directory."""
media = {}
for i in self.media_fields:
raw_file = getattr(self,
media[] = os.path.join(settings.MEDIA_ROOT,'static/', ''))
return media
view raw hosted with ❤ by GitHub

Currently only support save to local disk, but not supported yet to remove local file if the instance removed. Don’t hesitate to fork or improve my code. 😀

Generate Excel File in Django and Write it on Memory

I’m creating report to excel format using XlsxWriter, and here is some trick to generate excel file on memory instead of write the file to disk before we throw it to http response attachment.

import xlsxwriter

    import cStringIO as StringIO
except ImportError:
    import StringIO

from django.http import HttpResponse

def download_view(request):
    # Create file on memory
    output = StringIO.StringIO()

    # Generate excel.
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet()
    worksheet.write('A1', 'Hello export.')

    # Construct response.
    response = HttpResponse(, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=your_file_name.xlsx'
    return response


Homebrew: Fixing error “/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory”

My homebrew accidentally error when I’m running update command “brew update” and below are the error stackstrace.

$ brew update
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/ line 32: /usr/local/Library/ENV/scm/git: No such file or directory
Error: update-report should not be called directly!

I’m fixing my issue by run this single command.

$ cd "$(brew --repository)" && git fetch && git reset --hard origin/master


Bower: Install package to custom directory

Usually I stored all my static files on “static” directory within my django project and today I start to using Bower to manage my frontend dependency.

By default all bower package will stored on “bower_components” directory and here is how to store bower installed dependency to a custom directory.

First of all, what you need to do is just create “.bowerrc” inside your project root. then put this configuration on it.

  "directory" : "your/custom/path"

Now your bower will installed to your defined directory.