Thursday, September 21, 2017

sed command line tool

The sed command line tool allows you to pipe a string of text and substitute part of it.

Note that it does for the first occurrence only:



$ echo "my, this is my sentence" | sed 's/my/My/'
My, this is my sentence


Escaping forward slashes with the backslashes:


$ echo "convert /usr/local/bin to /common/bin" | sed "s/\/usr\/local\/bin/\/common\/bin/"
convert /common/bin to /common/bin



$ echo "Repeat me 5 times." | sed 's/[0-9]/& & & & &/'

Repeat me 5 5 5 5 5 times.

Friday, September 15, 2017

How to change Python version using Anaconda?

$ python --version

Python 3.6.1 :: Anaconda 4.4.0 (x86_64)

$ conda create -n py27 python=2.7 anaconda

$ source activate py27

$ python --version

Python 2.7.13 :: Anaconda 4.4.0 (x86_64)


Also posted on GitHub

How to show hidden files in Apple OS X Finder (AppleShowAllFiles)


Open Terminal and execute the following line:

$ defaults write com.apple.finder AppleShowAllFiles YES

Thursday, August 10, 2017

Python CAN bus

Install Python CAN library


~ uki$ sudo pip install python-can
Collecting python-can
Downloading python-can-1.5.2.tar.gz (77kB)
100% |████████████████████████████████| 81kB 3.3MB/s
Installing collected packages: python-can
Running setup.py install for python-can ... done
Successfully installed python-can-1.5.2

Follow the Directions


https://python-can.readthedocs.io/en/latest/installation.html

Monday, July 17, 2017

Android Adaptive Icons

Android allows us to create adaptive icons (various shapes for one icon for different devices) for:
  • launcher icons
  • shortcuts
  • Setting app
  • sharing dialogs
  • overview screen
You have to provide the following layers 108x108 dp:
  • top mask (circle, squircle, rounded square, square) provided by device OEM (e.g. Samsung)
  • foreground layer 72x72 dp should be visible in the mask viewport
  • background layer

The edge 18 dp are reserved for system's special effects.

Use Android Studio 3.0 Asset Studio to create these adaptive icons.



Reference:

https://developer.android.com/preview/features/adaptive-icons.html

Tuesday, April 25, 2017

Python UnicodeDecodeError



When you are reading CSV file (spreadsheet), make sure you set encoding="utf-8", or you might get:

UnicodeDecodeError: 'utf-8' codec can't decode byte
(result, consumed) = self._buffer_decode(data, self.errors, final)



def read_rows(CSV_file_path: str):
 
    import sys
    print("default encoding", sys.getdefaultencoding())
    import csv
    rows = []
    with open(CSV_file_path, 'rt', encoding="utf-8") as csvfile:
        cvs_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for row in cvs_reader: # row is a list
            rows.append(row)
    return rows

Sunday, April 16, 2017

Using Docker containers on Mac

Install Docker on Mac


https://download.docker.com/mac/stable/Docker.dmg


$ docker --version
Docker version 17.03.1-ce, build c6d412e

$ docker-compose --version
docker-compose version 1.11.2, build dfed245

$docker-machine --version
docker-machine version 0.10.0, build 76ed2a6

Docker for Udacity Machine Learning

$ docker pull kyoungrok0517/tensorflow-udacity

Docker for Raspberry Pi

Having created a large Pi cluster, the next logical step is to deploy various microservices in Docker containers on Pi.

https://www.raspberrypi.org/blog/docker-comes-to-raspberry-pi/










Thursday, April 6, 2017

Python OCR for robotics.

I was thinking that it would be cool for my robots to have text reading capability, also known as OCR.

I was testing Python pytesseract library:


https://github.com/UkiDLucas/uki.guru/blob/master/OCR/ocr.ipynb


So far no success, but it will be worth trying.

Monday, March 27, 2017

Python: Passing *args and **kwargs into a function

# *args is the usual notation for optional, non-keyword arguments
# **kwargs is the usual notation for optional, keyword arguments
def test_var_args(regular_argument, *args, **kwargs):
    print ("- regular argument:", regular_argument)
    for arg in args:
        print ("- unnamed arguments arg:", arg)
    for key in kwargs:
        print ("- named arguments: %s: %s" % (key, kwargs[key]))
test_var_args(1, "two", 3, four=4, five="five")


- regular argument: 1
- unnamed arguments arg: two
- unnamed arguments arg: 3
- named arguments: four: 4
- named arguments: five: five

Wednesday, March 22, 2017

Python bubble sort

Since I had to learn python for my machine learning and self-driving classes and I had to review the basics. Not to waste my efforts, I am creating a new guide, you can see more in:

https://github.com/UkiDLucas/python_zen_interviews

def bubble_sort(the_list: list, verbose: int=0):
    """
    author: @UkiDLcuas
    This function changes the provided list.
    The list can contain integers, decimal numbers, strings, etc.
    The list is sorted in ascending order (first to last).
    The function does not return anything.
    """
    if verbose > 0:
        iteration = 0
   
    # count remining bubbles ( aka step backwards)
    start = len(the_list)-1 # end, zero-based list
    stop = 0 # beginning
    step = -1 # backwards
    for remaining_bubbles in range(start, stop, step):
        for i in range(remaining_bubbles):
            if verbose > 0:
                iteration = iteration + 1
                print("iteration", iteration, "remaining_bubbles", remaining_bubbles, "index", i)
                print("  ", the_list)
                print("    comparing if is", the_list[i], "bigger than", the_list[i+1])
            if the_list[i] > the_list[i+1]:
                # swap
                temp = the_list[i+1] # temp placehoder for the value to be moved
                the_list[i+1] = the_list[i] # bubble up
                the_list[i] = temp # bubble down
    if verbose > 0:
        print("*** finished", len(the_list), "element list in ", iteration, "iterations")