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