Wednesday, September 23, 2015

Change encoding for main and test sources in Gradle

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

Reference:
https://github.com/huxi/sulky/blob/master/build.gradle

Tuesday, September 22, 2015

QueryDSL + JPA + Gradle in IntelliJ

`build.gradle`:

apply plugin: 'idea'

idea {
    module {
        sourceDirs += file('generated/')
    }
}

dependencies {
    ...
    compile("com.mysema.querydsl:querydsl-jpa:$querydslVersion")
    compile("com.mysema.querydsl:querydsl-apt:$querydslVersion:jpa")
    ...
}

Setup IntelliJ:

File -> Settings...

Compiler -> Annotation Processors

`Enable annotation processing`

Store generated sources relative to: `Module content root`

Reference:
http://bsideup.blogspot.kr/2015/04/querydsl-with-gradle-and-idea.html

Monday, September 21, 2015

Print errors in Flask

Use debug flag as follows:

app.run(debug=True)

Reference:
http://blog.luisrei.com/articles/flaskrest.html

Create an object in Python

You can create an object as follows:

class DiskSpace(object):
  def __init__(self, total, free):
    self.total = total
    self.free = free

  def __str__(self):
    return "total: %d, free: %d" % (self.total, self.free)

diskSpace = DiskSpace(100, 50)
print diskSpace

References:
http://stackoverflow.com/questions/15081542/python-creating-objects
http://stackoverflow.com/questions/727761/python-str-and-lists
http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

`Hello, world!` by Flask in Linux

Install pip if not installed:

sudo yum install python-pip

Install Flask if not installed:

sudo pip install Flask

Don't make your sample filename as `flask.py`.

If you do so, you will get the following error:

Traceback (most recent call last):
  File "flask.py", line 1, in <module>
    from flask import Flask
  File "/home/izeye/workspaces/izeye/python/flask.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask

In `test_flask.py`:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
  return "Hello, world!"

if __name__ == "__main__":
  app.run()

You can run it as follows:

python test_flask.py

References:
http://flask.pocoo.org/
http://stackoverflow.com/questions/14792605/python-flask-import-error

How to get disk usage by Python in Linux

To get disk usage,

you can use the following code:

import os

statvfs = os.statvfs('/')

total_disk_space = statvfs.f_frsize * statvfs.f_blocks
free_disk_space = statvfs.f_frsize * statvfs.f_bfree
disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space

print total_disk_space
print free_disk_space
print disk_usage

Reference:
http://stackoverflow.com/questions/4260116/find-size-and-free-space-of-the-filesystem-containing-a-given-file

Saturday, September 19, 2015

How to download JDK with `wget`

To download JDK with `wget`,

use the following commnad:

wget --no-check-certificate --header="Cookie: oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz"

Reference:
https://gist.github.com/hgomez/4697585

Monday, September 7, 2015

How to get 2 days ago in Linux

To get 2 days ago in Linux,

you can do as follows:

date --date="-2day" "+%Y.%m.%d"

Reference:
http://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

Delete documents in Elasticsearch

If you want to delete documents of a specific type,

you can delete the documents as follows:

$ curl -XDELETE http://localhost:9200/logstash-2015.09.05/logs                          
{"acknowledged":true}
$

If you want to delete documents of all types under a specific index,

you can delete the documents as follows:

$ curl -XDELETE http://localhost:9200/logstash-2015.09.05                                
{"acknowledged":true}
$

References:
http://stackoverflow.com/questions/23917327/delete-all-documents-from-index-type-without-deleting-type
https://www.elastic.co/blog/what-is-an-elasticsearch-index

How to get the number of documents and the total size in an index in Elasticsearch

To get the number of documents and the total size in an index,

use the following command:

curl -XGET 'http://localhost:9200/logstash-2015.09.05/_stats?pretty'

`logstash-2015.09.05` will be your index.

Reference:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

How to get all indices in Elasticsearch

To get all indices in Elasticsearch,

use the following command:

curl -XGET 'http://localhost:9200/*?pretty'

Reference:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html

Saturday, September 5, 2015

How to handle nested backticks (`) in Bash

If you use nested backticks (`),

you will get the following errors:

$ echo `printf '%02d' $((10#`date +%M` / 30 * 30))`
-bash: command substitution: line 1: unexpected EOF while looking for matching `)'
-bash: command substitution: line 2: syntax error: unexpected end of file
-bash: command substitution: line 1: syntax error near unexpected token `)'
-bash: command substitution: line 1: ` / 30 * 30))'
date +%M
$

Use $() instead as follows:

$ echo `printf '%02d' $((10#$(date +%M) / 30 * 30))`
00
$

Reference:
http://stackoverflow.com/questions/2657012/how-to-properly-nest-bash-backticks

Friday, September 4, 2015

How to add a leading zero in Bash

If you want to add a leading zero to a number,

you can do as follows:

$ echo `printf '%02d' 8`
08
$

Reference:
http://stackoverflow.com/questions/55754/bash-script-to-pad-file-names

How to handle a leading zero in Bash

If you have a leading zero in a number,

the number is handled as octal number as follows:

$ echo $((08 + 1))
-bash: 08: value too great for base (error token is "08")
$

To handle it as decimal number you have to do as follows:

$ echo $((10#08 + 1))
9
$

Reference:
http://blog.famzah.net/2010/08/07/beware-of-leading-zeros-in-bash-numeric-variables/

Wednesday, September 2, 2015

How to import a Gradle project from a Git repository in STS (or Eclipse)

1. Clone the Git repository:

Window -> Show View -> Other...

Git -> Git Repositories

Clone a Git repository -> Clone URI

2. Install Gradle plugin:

Help -> Eclipse Marketplace... -> Gradle Integration for Eclipse 3.7.0.RELEASE

3. Import the project:

Import -> Gradle -> Gradle Project