Archive for the ‘Code’ Category

Development Environment Setup, A Story

One of the parts that I don’t really like when starting/joining a new project, is setting up my development environment (dev env). Sometimes, it takes a long time. Once, at my first job, the first three days were spent to set up my dev env. It was creating a plugin for a desktop application. Well, I think again, it was more on my first real experience using non-Windows OS. It was Ubuntu. ūüėÄ

I also had an experience with¬†setting up a development environment for a web project that uses Virtual Machine. It was very exhausting. VM is very heavy for my computer. I don’t really recommend it. I use it since I needed to use specific dependency version that not compatible with my OS (mine was Ubuntu 14, it needs Ubuntu 16). Ouch.

Virtual Machine is not my favorite for sure. It’s very heavy, you need to install many things.

Then there is this cool guy, docker. A smaller version of Virtual Machine. It’s very light. You can spin up more than one easily. With a docker compose, you can orchestrate your whole system easily. It’s really helpful. You can have the same environment like in production. No longer “but, it works on my machine“.

I am still using it.

Another thing to set up is your IDE. For example in my favorite IDE, PyCharm, you can set your custom python env, running env, and so on. Sometimes, you miss one step, and it’s not working like in your friend’s laptop.

My friend had this idea, to use Ansible to setup PyCharm. It’s a little bit hacky, but it works. It hacks the PyCharm configuration (which is an XML file). By using Ansible also, he customizes the docker compose override file to match with our computer env. It works really great. Only by updating some variables in all.yml, run it, you can have the same setup as your friend.

Another thing is Rancher. Docker is great, but you need to remember all the commands. I don’t really like it. My company uses it to deploy on the server, for production and staging. I think it’s a great product also. No need typing command, no need to log in to the server. It has nice UI to manage your system. It’s recommended to use also.

So, my current choices are Docker, Ansible, and Rancher. It can be changed for sure. I recommend these tools for your work.

Btw, for a small project, I also use virtual env for python development. It’s easier to setup when I just need to have clean python environment. I also recommend it. But for a¬†bigger project that have many parts, I don’t really recommend it, although I have ever done it :).

Thanks for reading, and please share if you have tips for setting up dev env.

P.S.: Looking back, it’s very funny to remember that I need to spend 3 days for dev env setup. Well, I couldn’t even exit from Vim.


While on Bash: Converting ODT to PDF

I can say I couldn’t memorize commands or syntaxes easily. For example this easy¬†while statement on¬†bash:

Short explanation:

ls : listing all files in current directory

grep '.odt' : filter file that has .odt in filename (from the previous ls output)

while read line; do $line; done;¬†It means, it will read all output in the previous output as line¬†(variable) then run the¬†script for each $line¬†(don’t forget to add $ sign). Then finish with done

For the script‚Äč:

Next time I forget, I will just open this post.

Good luck, my future self.

Kategori:Bash Tag:, , , , ,

QgsExpression with Python

In QGIS, there is this cool feature called by Expression. I use it mostly on Field Calculator in attribute table. There are some places that you can find also. You can read more about this Expression here.

This week, I tried to use it in Python. It turned out very simple (of course, I need to Google it here and there first how to use it). The class name is QgsExpression. You can import it from qgis.core. Below a sample code how to use it:

from qgis.core import QgsField, QgsExpression
def sum_fields(layer, output_field_name, input_fields):
    """Sum the value of input_fields and put it as output_field.

    :param layer: The vector layer.
    :type layer: QgsVectorLayer

    :param output_field_name: The output field name.
    :type output_field_name: str

    :param input_fields: List of input fields' name.
    :type input_fields: list
    # Creating expression
    string_expression = ' + '.join(input_fields)
    sum_expression = QgsExpression(string_expression)

    # Get the output field index
    output_idx = layer.fieldNameIndex(output_field_name)
    # Output index is not found
    if output_idx == -1:
        output_field = QgsField(output_field_name, QVariant.Double)
        output_idx = layer.fieldNameIndex(output_field_name)

    # Iterate to all features
    for feature in layer.getFeatures():
        feature[output_idx] = sum_expression.evaluate(feature)


The code above is a function to sum the value in list of fields (input_fields) into an output field (output_field_name) in a layer.

Basically, you just need to create the expression, and evaluate it to get the value from the expression. In this doc, I found out that using prepare will make the evaluation faster for many features.

static const in C++

When you want to use static const in your C++ code, you need to know¬†whether the variable is integral or not. If it’s integral, you can initialize it in the header file (.h). Otherwise, you need to initialize it in the souce file (.cpp). Here some example:

// myclass.h
class MyClass {
  static const int myInt = 0; // Can be initialized here
  static const string myString;  // Need to be initialized in the source file


// myclass.cpp
const string MyClass::myString = 'My String';

If you are not familiar with integral type, you can read here:

What’s the different between static and const?

static in class member means, you do not need to hava the class instance to access it.

const to make a variable constant (not mutable).

They can be used together as const static or static const. Both are the same.

Kategori:CPP Tag:, ,

Explicit Keyword in C++

I am trying to do more C++ lately (after 7-8 years). Ouch, I am feeling old now. I am finding something new (or perhaps refinding). I will try post about it regulary here. So, here we go my first post:

It’s about explicit keyword. This keywords is used to prevent an implicit convertion when creating an object. See this example:

class String {
    String(int n); // allocate n bytes to the String object
    String(const char *p); // initializes object with char *p

Then if you call

String a = '3';

It will implicitly converted ‘3’ to integer, and create String object with 3 bytes allocation. Here, we need the explicit keywords. Like this:

class String {
    explicit String(int n); // allocate n bytes to the String object
    String(const char *p); // initializes object with char *p

By doing this, we prevent the ‘3’ to be converted to int, rather it will create String object that has char *p = ‘3’.



Kategori:CPP Tag:

[Python] Comparing NaN, Inf, None, 0, 1,…

What is the result of this code snippet:

my_list = [1, 2, 3, 9, 1]

Yes, you are right, the result will be like this:

[1, 1, 2, 3, 9]

Hmm, that’s easy. What’s about if you’re asked which is bigger of None¬†and NaN? Or True¬†and NotImplemented? Or Ellipsis¬†(What the hell is this Ellipsis?).
Just try it:

my_weird_list = [True, False, 0, -1, 1, 100, float('nan'), float('inf'), -float('inf'), NotImplemented, Ellipsis, None]
print my_weird_list.sort()

What will be the output? If your answer is None, you are right because sort()¬†method return None¬†#Kidding ūüôā

Let’s print the my_weird_list. And, here we go :

[None, -inf, -1, False, 0, True, 1, 100, nan, inf, NotImplemented, Ellipsis]

For your note, int(True) == 1, so 1 and True is the same value when you compared them.

The good thing (for me at least), None is less than anything, even -inf. And, there is bigger value than inf. ūüôā

The other thing is, the position of nan in the sorted list. If you compare nan with anything it always give False. Even when nan == nan. It will return True if you use != operator.


So, it’s true that False is bigger (better?) than None or nothing.

Kategori:Python Tag:,

[Python] Return a or b or c

Apakah yang akan dilakukan fungsi ini:

def foo(a, b, c):
    return a or b or c
def bar(a, b, c):
    return a and b and c

Jika diberi input seperti ini:

foo(1, 2, 3)  # 1
bar(1, 2, 3)  # 2
foo('', None, 'pret')  # 3
bar('', None, 'pret')  # 4

Yak benar, si python bakal memberikan hasil sebagai berikut:

1 # 1
3 # 2
'pret' # 3
''  # 4

Mudah dipahami bahwa fungsi foo akan mengembalikan nilai pertama yang bernilai True dan fungsi bar akan mengembalikan nilai terakhir yang bernilai True sebelum ketemu nilai False.
Atau lebih mudahnya, akan mengembalikan nilai terakhir ketika ekspresi bernilai True
Menarik bukan? Minimal menurut saya (soalnya baru nemu).

Kategori:Python Tag:, , ,
%d blogger menyukai ini: