Packaging for Python App

Posted on Fri 23 March 2018 in articles

It is not quite convenient for our customer to setup Python environment on Windows or other platforms to run our Python app. So we need to pack our Python app into stand-alone .exe file. Fortunately, there are quite a few tools to help us. And among these, PyInstaller is my …


Continue reading

Cross Compile Python 3

Posted on Thu 22 March 2018 in arm

I need to run Python 3 on an ARM board, so I need to cross compile it. To cross compile Python, we need to compile Python and Parser in the host(build) machine.

$ cd $HOME
$ mkdir PythonSrc
$ cd PythonSrc
$ wget https://www.python.org/ftp/python/3.5.5/Python-3 …

Continue reading

Change Your Github Repository's Language

Posted on Wed 21 March 2018 in tips

Github uses Linguist library to determine your repository's language. If you find that Github has done something wrong, you may fix by adding a .gitattributes file with the following content:

* linguist-vendored
*.py linguist-vendored=false

The above will let Github detect your repository's language as Python.


Continue reading

Autostart Service in Raspberry Pi

Posted on Tue 20 March 2018 in raspberry-pi

If our application needs to autostart as a service in Raspberry Pi, we will write a simple systemd service file.

[Unit]
Description=Example Application
After=bluetooth.service
Requires=bluetooth.service

[Service]
PermissionsStartOnly=true
Type=simple
User=pi
Group=pi
ExecStartPre=/bin/mkdir -p /var/log/example_app
ExecStartPre=/bin/chown pi …

Continue reading

Copy Static Files in Pelican

Posted on Tue 20 March 2018 in tips

GitHub Pages provide custom URL feature, which will create a CNAME file in the HTML root.

Using Pelican STATIC_PATHS, we can include this CNAME file. Copy the following to your Pelican configuration. (Supposing your CNAME file is in your content/extra directory.)

STATIC_PATHS = ['images', 'extra/CNAME']
EXTRA_PATH_METADATA = {'extra/CNAME': {'path …

Continue reading

Serial Programming in Raspberry Pi

Posted on Wed 14 March 2018 in raspberry-pi

Raspberry Pi uses serial port for console tty. In order to use this serial port to communicate with Mac machine, we will temporarily disable getty service:

$ sudo systemctl stop serial-getty@ttyS0.service
$ sudo chmod 666 /dev/ttyS0

Then the application can use /dev/ttyS0 to communicate with other device using …


Continue reading

Bluetooth Programming in Raspberry Pi

Posted on Tue 13 March 2018 in raspberry-pi

Raspberry Pi Zero W comes with added wireless LAN and Bluetooth connectiviy. We can use PyBluez library to program Bluetooth in Python.

In order to install PyBluez, we first need to install Bluetooth development. In Raspberry Pi, run the apt command:

$ sudo apt install libbluetooth-dev

Then install PyBluez using pip …


Continue reading

Charset Handling in Python

Posted on Tue 13 March 2018 in articles

As we know, Python 3 treats all string using UTF-8, and it is great. Sometimes, we need to unzip some zip files which come from Windows machine.

# Unzip the zipfile to a folder.
zf = zipfile.ZipFile(zipped_file_full_path)

for name in zf.namelist():
    # FIXME: Need to handle path string encoding.
    new_name …

Continue reading

Protobuf in Python

Posted on Tue 13 March 2018 in articles

Protobuf is a method to serializing structured objects. It helps to develop applications which are communicating with each other over a wire. protoc is the code generator by reading the .proto files and it provides Python support.

First, we will need to install the Protocol Buffers compiler protoc. In macOS …


Continue reading

Image Processing in Python

Posted on Mon 12 March 2018 in articles

PIL is the Python Imaging Library by Fredrik Lundh and Contributors. Pillow is the friendly PIL fork by Alex Clark and Contributors. We can use Pillow to processing image, e.g., adding watermark to an image.

from PIL import Image, ImageDraw, ImageFont

# Add watermark to the image.
image = Image.open …

Continue reading