Tuesday, December 17, 2019

Keep the color prompt in tmux

To keep your prompt (PS1) with color shown in tmux screen, add the following property in .bashrc:

force_color_prompt=yes


Create docker container with the simple flask application

I will try to create a docker container with the simple flask application created some times ago.



First, I create a shell script for starting the flask application, namely "boot.sh":

boot.sh
#!/bin/sh
export FLASK_APP=simple.py
flask run --host 0.0.0.0



Second, we need to create a requirements.txt file:

requirements.txt
flask==1.1.1

Actually you can run the following to get the requirements.txt:

pip freeze > requirements.txt



Third, together with the simple.py file created in the simple flask application, put those 3 files in an "app" directory:

app/
   - boot.sh
   - requirements.txt
   - simple.py



Forth, create a Docker file outside the app directory:

Dockerfile
FROM python:3.6-alpine
WORKDIR /home/app
COPY app/ ./
RUN chmod +x ./boot.sh
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]

Build the docker image using the Dockerfile:

     docker build -t simple:latest .

Run the docker container with the docker image we just created:

     docker run -d -p 5000:5000 simple:latest

Check "http://localhost:5000" to check the Flask application.

Simple Flask sample

Just wanna to create a very simple Flask application in a single python source file:

simple.py

from flask import Flask
application = Flask("simple")
@application.route("/")
def index():
return "Hello World"


To run this Flask application, run the following:
  • export FLASK_APP=simple.py
  • flask run
If you want to put this application in a Docker container, you need to bind to 0.0.0.0:
  • flask run --host 0.0.0.0

Now, navigate "http://localhost:5000" to see the word "Hello World" displayed on the browser.

Monday, December 16, 2019

Navigate tmux splitted pane using mouse

To enable navigation on tmux splitted pane using mouse

In tmux environment, do the following:
  1. Ctrl-b
  2. Type :setw -g mouse on
Done, try to switch your tmux pane with mouse.

Note:
To make this setting persistent, save this line in .tmux.conf:
setw -g mouse on





Tuesday, December 10, 2019

Assign locale for moment.js

To assign locale in moment.js, we can use the following:

     moment.locale("en")

It will returns "en". However, if you assign an unaccepted locale to it, it won't update its value. For example:

     moment.locale("zh")

It still returns "en", since "zh" is not an accepted locale for moment.js.

Up till now, moment.js accepts:

     zh-hk
     zh-tw
     zh-cn

Reference: momentjs

Tuesday, December 3, 2019

QGIS freezes while editing features

There are many possibilities for QGIS to hang / freezes while editing features. But for my experience, I found it often hangs when using a Shapefile layer.

To minimize this kind of problem, I tried to use other feature format (e.g. SpatialLite). And the problem is relieved to some extent.



Have a try and let me know if it helps or not.

Monday, December 2, 2019

Windows equivalent of "wc -l" command in Linux

To count the number lines of files / stdout in Linux, we uses "wc -l" to achieve. In Windows, we use the following instead:
  • find /c /v ""

For example, suppose we have a file "sample_file.txt" and we want to know the number of lines of that file, we can perform the following:
  • type sample_file.txt | find /c /v ""



Sunday, December 1, 2019

Handling table names with underscore ("_") in flask_sqlalchemy / Python

When you create table class using flask_sqlalchemy (the Flask extension for SQLAlchemy), it can maps the modal class to a proper table automatically. This is cool but if you have a table name with underscore (e.g. MY_SAMPLE_TABLE) and you name your model class with the underscore ('_') as usual, you will find SQLAlchemy will map your table name as something the following:

"MY__SAMPLE__TABLE"

Each single underscore (_) with becomes a double underscores (__). This is annoying and you might believe this is a bug, but in fact you can resolve this issues by naming your table class using the PEP8 naming convention, and named your class as:

class MySampleTable(db.Model):
     field_id = db.Column(db.Integer, primary_key = True)
     field_data = db.Column(db.String(200))

The the SQLAlchemy will map the MySampleTable class to table name as "MY_SAMPLE_TABLE" correctly.

For more information about PEP8, click here for details.

A job with mixture of joy and technology

Job with LEGO + IT, what else I can asked for ?


Wednesday, November 27, 2019

Connecting Oracle with Python via cx_Oracle extension


Installation Prerequisites

  • Python 2.7, 3.5 or above
  • Oracle Client / Oracle Instant Client "Basic" or "Basic Light"
  • libaio / libaio1 package


Installation Reference



Sample Python Code

import cx_Oracle

# Make DSN address. Real values for host address, port and service name were replaced by '<>'.

dsn = cx_Oracle.makedsn('<host_address>', '<port>', service_name='<service_name>')

# Connect to DSN

orcl = cx_Oracle.connect(user='<username>', password='<password>', dsn=dsn)

# Get cursor

cursor = orcl.cursor()

# Execute SQL statement

cursor.execute('<SQL SELECT STATEMENT>')

# Fetch result

data = cursor.fetchone()

Thursday, November 21, 2019

Sending F10 in Ubuntu Terminal

By default, you just can't send F10 to Ubuntu Terminal's curses program. This is because F10 is being captured by Terminal.app already.

A quick workaround is to use "ESC 0" to simulate sending F10 inside Terminal.app.

Reference

Tuesday, November 19, 2019

Customized Dash (Launcher / Taskbar) Icon in Ubuntu

To customize application icon in Ubuntu:

  1. Create a .desktop file in ~/.local/share/applications/
    e.g. /home/user/share/applications/<application name>.desktop
  2. Add the following content to the desktop file
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=ApplicationName
GenericName=ApplicationName
Icon=/home/user/application-icon.png
Exec=/home/user/application/application-executable
Categories=Category;Subcategory
Done!

Wednesday, October 30, 2019

Removing unused packages in Linux

To remove unused packages in Linux, we will use the following software:

  • deborphan
  • gtkorphan (GUI based)
  • rpmorphan
References

Deleting files specified in a text based file list

To delete a list of files specified in a text based file list, do the following:

> for /f %i in (files_to_delete.txt) do del %i

Reference:
https://superuser.com/questions/355584/how-to-delete-files-from-a-folder-using-a-list-of-file-names-in-windows

Export text based file list for the difference between 2 directories (Windows version)

The following is a tutorial for exporting a text-based file list showing the difference between 2 directories (e.g. D:\DirA, D:\DirB):

Step 1: Create a text-based file list for both directories

> cd D:\DirA
> dir /s /b /a:-d > file-list-dir-a.txt
> cd D:\DirB
> dir /s /b /a:-d > file-list-dir-b.txt

Step 2: Use WinMerge to get the difference between the 2 file lists:


Note: To display difference only, you need to set "View > Diff Context > 0 Lines".

Step 3: Copy the difference from WinMerge and create a text file to store it.



Ubuntu dock shows applications in current workspace only

In Ubuntu dock, to show applications in current workspace only, run the following command:

$ gsettings set org.gnome.shell.extensions.dash-to-dock isolate-workspaces true

References

Wednesday, October 23, 2019

Running Springboot web app in IntelliJ Idea Community Edition

When you run your springboot application in IntelliJ idea Community Edition having the following error, you can try the solution below to solve the problem.

Error "Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer"

If your "pom.xml" has dependency with "provided" scope, you may have trouble when running your application in IntelliJ Idea Community Edition.

<dependency>   
  <groupId>org.springframework.boot</groupId>   
  <artifactId>spring-boot-starter-tomcat</artifactId>   
  <scope>provided</scope>
</dependency>

Some suggests that you can change the scope from "provided" to "compile". Here is another solution for this issue:


In your "Run/Debug Configurations", check "Include dependencies with 'Provided' scope". Then you can run the application from IntelliJ Idea Community Edition.

Tuesday, October 1, 2019

Image bytes to np array in Python

For reference:

# data - image bytes

im = Image.open(BytesIO(data))
I =  numpy.asarray(im)
print("I type:", type(I), "I shape:", I.shape)

>> I type: <class 'numpy.ndarray'> I shape: (512, 512, 3)

Thursday, September 26, 2019

Slow Ubuntu startup time due to apt-daily.service

The  apt-daily.service  makes users feel very slow when running at startup. There is a workaround to delay its execution after system boot up:

Do the following:
 sudo systemctl edit apt-daily.timer 

And paste the config to the config file:
 # apt-daily timer configuration override 
 [Timer] 
 OnBootSec=15min 
 OnUnitActiveSec=1d 
 AccuracySec=1h 
 RandomizedDelaySec=30min 

References:
https://askubuntu.com/questions/800479/ubuntu-16-04-slow-boot-apt-daily-service
https://ubuntu-mate.community/t/shorten-boot-time-apt-daily-service/12297

Thursday, September 19, 2019

Android Emulator 29.2.0 problem with Ubuntu 19.04

It just crashed with segmentation fault.

Display card tested: Radeon RX 550, Intel UHD Graphics 630

From https://androidstudio.googleblog.com/2019/09/emulator-2920-stable.html, the OS may crashes with incompatible Vulkan drivers. 

Temporary rollback to emulator 29.1.12-5839083 as a workaround.


Monday, September 2, 2019

Adjust VirtualBox VMS BIOS time


Syntax

   VBoxManage modifyvm "VMS Name" --biossystemtimeoffset -ms

   Note:
   ms - milliseconds


For example

   VBoxManage modifyvm "VMS Name" --biossystemtimeoffset -86400000

Tuesday, July 30, 2019

Kalman filter

Just finding some resources for learning about Kalman filter, seems it is a very useful technique which applicable on a lot of different areas.

References:

https://blog.maddevs.io/reduce-gps-data-error-on-android-with-kalman-filter-and-accelerometer-43594faed19c

Thursday, July 18, 2019

Conflict between pandas 0.25.0 and pandas-datareader 0.7.0

For Python users, there is a conflict between "pandas 0.25.0" and "pandas-datareader 0.7.0".

If you do an "from pandas_datareader import data", you will got a conflict:

    "ImportError: cannot import name StringIO from pandas.compat"

To get rid of this issue, use "pandas 0.24.2" + "pandas-datareader 0.7.0" instead.

Tuesday, May 7, 2019

Linux mounted NTFS partition as read-only suddenly

Sometimes in dual boot system (Windows / Linux), the NTFS partitions might have some problem causing the Linux mounted the NTFS partitions as read-only. To solve this problem, one might try following to see if it works:


Step 1: Unmount the partition first

This can be done via Disks application OR command line:
  • e.g. umount -v /dev/sda1

Step 2: Run ntfsfix on the target partition
  • e.g. sudo ntfsfix /dev/sda1


If the command is successful, the following output should be shown:
  Mounting volume... The disk contains an unclean file system (0, 0).
  Metadata kept in Windows cache, refused to mount.
  FAILED
  Attempting to correct errors... 
  Processing $MFT and $MFTMirr...
  Reading $MFT... OK
  Reading $MFTMirr... OK
  Comparing $MFTMirr to $MFT... OK
  Processing of $MFT and $MFTMirr completed successfully.
  Setting required flags on partition... OK
  Going to empty the journal ($LogFile)... OK
  Checking the alternate boot sector... OK
  NTFS volume version is 3.1.
  NTFS partition /dev/sda1 was processed successfully.

Step 3: Remount the partition

    The partition should be read-write enabled






Tuesday, January 8, 2019

NFC notes on NDEF message for URL

Just a note for encoding URL in NDEF message, the NDEF record should user TNF_WELL_KNOWN as its TNF. And NDEF record type should be RTD_URI. And the first byte could be encoded for different protocols (e.g. 0x01 = "http://www", 0x02 = "https://www", etc.

References:
Beginning NFC
Professional NFC Application Development for Android


Sync multiple git repo at once

Use the following command in Linux will do the job:  ls -d RepoNames* | xargs -I{} git -C {} pull