Archive for the ‘Python’ 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.


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.

[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:, , ,

InaSAFE 1.0

1 Desember 2012 1 komentar

After I graduated, I work for AIFDR to develop InaSAFE. What is it? InaSAFE is free software that produces realistic natural hazard impact scenarios for better planning, preparedness and response activities. This is an open source software, developed by developers around the world. From all (five) continents actually.

The latest InaSAFE’s version is 1.0. It’s launched in the 5th¬†AMCDRR ¬†(Asian Ministerial Conference on Disaster Risk Reduction)¬†in Yogyakarta. InaSAFE was presented by Trevor Dhu from AIFDR. You can watch it here. And the other good news, our Indonesian President, SBY, came to our booth and got explanation about InaSAFE by Trevor Dhu also. You can watch it here.

Can I use InaSAFE? Of course. You can go to the website,¬†and read the instruction. Or basically, just install¬†QGIS¬†(it’s free also) and fetch the plugin to get InaSAFE.

The other thing, if you want to look at the code, you can go to the github. And may be you can help us ūüôā

My personal opinion. This is my first real job. And this is amazing. A big project (awesome developers, I learnt many things from them, I’m very lemcups, I’m new to everything in the project except the python thing and the programming) and I like it.

But of course, there are something I couldn’t get. Yeah, life is about choosing your path. As long as you can choose, be graceful.

PS : I write this post in English because I will take iBT next Saturday, and I haven’t studied.

PS II : Sorry, I can’t keep my word to post one post in a month. I got sick twice this semester. And another excuses.

Kategori:Project, Python Tag:, ,

Python : List, Tuple, Set, dan Dictionary (1)

31 Maret 2012 2 komentar

Salah satu bagian penting dari pemrograman adalah struktur data. Kali ini, saya ingin berbagi hal yang saya tahu mengenai struktur data di Python. Ada empat yakni List, Tuple, Set, dan Dictionary. Akan saya jelaskan secara singkat masing-masing jenis struktur data.

  1. List
    Sama seperti pada bahasa lain, list di Python merupakan sekumpulan elemen atau objek. Tapi, kita bisa memiliki bermacam-macam jenis elemen dalam satu list. Misal, string, integer, objek, list lain, maupun struktur data lain. Karakteristik utama dari list ini adalah, memiliki index dan mutable (bisa di-otak-atik). List di python menggunan zeroth based index. Contoh :

    list1 = [] # deklarasi list
    list1.append('elemen_0') #menambah 1 buah elemen di ujung list
    print list1[0] # mencetak element pertama di list1

    Hal penting lain yang menarik adalah, kita tidak perlu mendeklarasikan ukuran list di awal ataupun tipe isi list. Cukup deklarasikan, dan pakai.

  2. Tuple
    Berbeda dengan list, Tuple merupakan struktur data yang immutable, artinya tidak bisa diotak-atik setelah dideklarasikan. Tapi, masih sama seperti list, isi tuple bisa macam-macam, tidak harus satu jenis. Dan, bisa diakses dengan menggunakan indexnya. Contoh penggunaan :

    tuple0 = () # deklarasi tuple kosong
    tuple0 = tuple() # deklarasi tuple kosong
    tuple1 = ('bakso',) # deklarasi tuple dengan 1 elemen, perhatikan ada tanda koma setelah 'bakso'
    tuple2 = ('soto', 'mie ayam') # deklarasi tuple dengan 2 elemen, tanpa tanda koma
    print tuple2[0] # menampilkan element pertama
  3. Set
    Sama seperti set di matematika, set di python merupakan sebuah himpunan. Tanpa ada index. Yang paling saya suka adalah adanya fungsi seperti irisan dan gabungan. Contoh :

    set1 = set() # deklarasi set kosong
    set1.add(1) # menambahkan isi set1
    set2 = set()
    print set1 | set2 # menampilkan union dari set1 dan set2
  4. Dictionary
    Terakhir adalah dictionary. Layaknya kamus, kita menyimpan suatu value dengan key tertentu. Semua bisa jadi key, asal dia immutable. Sama seperti yang lainnya, kita bisa mengisi semau kita, tipe data semau kita juga. Contoh :

    dict1 = {} # deklarasi dictionary kosong
    dict['key'] = 'value' # mengisi dengan kunci 'key' dengan 'value'
    print dict['key'] # menampilkan isi dict['key']

Itu adalah perkenalan struktur data di Python. Oh iya, string itu termasuk list of char namun immutable lho. Hmm, sedikit trik-trik yang saya gunakan. Misalnya, mengurutkan isi dictionary terhadap value, menghapus semua element list dengan nilai tertentu, atau membuat dictionary dari list. Sampai ketemu di tulisan selanjutnya. CMIIW lho…

Kategori:Code, Python Tag:

Hello, Python

Kali ini saya akan berbagi ilmu saya yang sedikit mengenai Python. Python mirip dengan PHP dalam penggunaannya, menurut saya. Kemiripan yang paling terlihat, tidak perlu di-compile dan tiap variabel bisa berganti tipe. Postingan ini tidak akan banyak membahas apa itu Python, tapi lebih ke “Hello World” saja.

Pertama yang harus kita lakukan adalah memastikan ada Python di komputer kita. Biasanya, di Linux sudah tersedia, sedangkan untuk Windows belum ada. Untuk pengguna Windows, silahkan download Python di website Python. Versi terbaru adalah 3.2. Namun, saya sarankan untuk menggunakan versi 2.7. Kenapa? Karena, menurut saya, banyak modul yang tidak bisa jalan di Python 3.x namun bisa jalan di 2.x. Misalnya, NLTK (Natural Language Toolkit). Tapi, mungkin ke depannya versi 3.x akan lebih populer.

Setelah melakukan instalasi, akan muncul beberapa menu baru di Start Menu Anda. Salah satunya adalah IDLE (Python GUI). Silahkan jalankan program tersebut.

Python IDLE

Python IDLE

Selanjutkan, ketiklah

print 'hello world'

Nah, Anda akan melihat ‘hello world’ muncul. Selamat, Anda telah menjalankan¬†script pertama Anda.

Layaknya PHP, tidak ada acara compile di Python. Cukup tulis kode, dan run.

Untuk membuat kode yang lebih panjang, silahkan pilih File->New Window pada jendela IDLE Anda. Akan muncul jendela kerja baru. Ketika kode berikut :

print 'hello world'

Kemudian tekan F5 atau pilih Run->Run Module. Akan ada perintah untuk menyimpan kode terlebih dahulu. Simpanlah dengan ekstensi “.py” dan beri nama sesuai selera, misal “”. Kemudian akan muncul keluaran dari kode tersebut di jendela IDLE tadi.

Untuk menjalankan melalui command prompt, Anda harus terlebih dahulu memasukan directory dari Python ke dalam variabel PATH. Caranya sebagai berikut (untuk Windows 7) :

  1. Buka My Computer
  2. Pilih System properties
  3. Pilih Advance system settings
  4. Pilih tab menu : Advanced
  5. Pilih Environment variables
  6. Edit value untuk variabel PATH
  7. Tambahkan PATH milik directory Python Anda. Pisahkan dengan karakter ‘;’ jika sudah ada path lain

Untuk mengetesnya, silahkan buka folder tempat file Python Anda tersimpan. Buka command prompt di folder tersebut. Ketika perintah ini :


jika muncul tulisan “hello world” di command prompt, maka Anda telah berhasil melakukan seting PATH untuk Python. Selamat.

Sekian untuk “Hello Python” sebagai pembuka dari tulisan mengenai Python selanjutnya. Semoga bermanfaat.

NB : Saya juga masih dalam tahap belajar, jika ada kesalahan, mohon koreksinya.

Kategori:Python Tag:
%d blogger menyukai ini: