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 odt2pdf.sh $line; done; It means, it will read all output in the previous output as line (variable) then run the odt2pdf.sh script for each $line (don’t forget to add $ sign). Then finish with done

For the script odt2pdf.sh​:

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

Good luck, my future self.

Iklan
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)
    sum_expression.prepare(layer.pendingFields())

    # 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)
        layer.startEditing()
        layer.dataProvider().addAttributes([output_field])
        layer.commitChanges()
        output_idx = layer.fieldNameIndex(output_field_name)

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

    layer.commitChanges()

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 {
public:
  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: http://en.cppreference.com/w/cpp/language/type.

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

Archive Submodule in git

One of project that I worked on needs to make a package to be released. The packaging it’s very simple since it’s python. Basically, removing all un-needed code for production (e.g. unit test code, test data, script). Then, we archive it by using git archive command.

It works well until we use git submodule. We found out that the git archive  command doesn’t do the archiving recursively. Thus we need a better tool to do it.

After a quick googling, I find this nice tool to solve the problem. It’s git-archive-all. Basically, what it does is archiving repository: https://github.com/Kentzo/git-archive-all.

It’s python based. You can install it using pip. Or if you use macOS, you can install it by using brew.

Another nice tool to help human being 🙂

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 {
public:
    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 {
public:
    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’.

 

Reference: http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean-in-c

Kategori:CPP Tag:

FreeCodeCamp first part

FreeCodeCamp adalah suatu tempat belajar yang sangat menyenangankan, diperoleh gratis dan mudah untuk dipahami.

Untuk pertama kali masuk pada FreeCodeCamp kita akan diberi sejumlah petujuk tentang langkah bagaiman cara masuk FreeCodeCamp. Setelah masuk dalam FreeCodeCamp kita harus bergabung dengan komunitas yang menggunakan FreeCodeCamp, dsinilah tempat berdiskusi segala macam hal tentang “coding”.

Membuat code portofolio kita sehingga orang akan tahu seberapa jauh kemampuan kita dalam melakukan dan menyelesaikan suatu pekerjaan.

Kita juga perlu bergabung dengan komunitas yang berada satu kota dengan kita untuk memudahkan kita dalam berkomunikasi, berdiskusi, meminta bantuan menyelesaikan latihan dsb.

Dan  kita harus selalu belajar, mencoba dan menyelesaikan masalah dalam coding.

Kategori:Tak Berkategori

Embed Peta dari CartoDB

5 Maret 2015 1 komentar

Belum lama ini saya mendapatkan pemberitahuan jika peta dari https://cartodb.com/ bisa di-tanam (embed) pada blog wordpress gratisan, seperti blog ini.

Caranya cukup mudah:

  1. Buat peta dan visualisasi di CartoDB
  2. Klik tombol share di sudut kanan atas seperti di bawah ini

    Share Button

    Share Button

  3. Kemudian, akan muncul jendela seperti di bawah ini:

    Get Simple URL

    Get Simple URL

  4. Selanjutnya, akan terbuka tab baru di browser Anda.
  5. Anda dapat menambahkan peta Anda tersebut di post blog Anda dengan cara Add Media –> Insert From URL, lalu copy URL dari tab yang terbuka tadi. Dan tekan Insert to Post.

Contoh hasilnya, bisa dilihat di peta di bawah ini:

Peta di atas merupakan peta visualisasi tornado yang terjadi di Amerika Serikat dalam suatu rentang waktu. Peta ini saya buat ketika mengikuti online course dari CartoDB, berjudul Online Mapping for Beginners

Selamat mencoba 🙂

%d blogger menyukai ini: