Create tar.gz archives

tar czf new-tar-file-name.tar.gz file-or-folder-to-archive

c - create new archive z - compress the archive using gzip f - use archive file

Extract archives

tar -xzf tar-file-name.tar.gz

x - extract the archive. z - uncompress the archive using gzip. f - use archive file.

Let’s do it.

I only want to compress 3 files. When I extract archive what I want to see is files in the directory.

$ tree file_dir/
file_dir/
├── file1.txt
├── file2.txt
└── file3.txt

$ cd file_dir
$ tar czf files.tar.gz .
tar: .: file changed as we read it

# moved files.tar.gz to different directory
$ tar -xzf files.tar.gz 
$ ls
file1.txt  file2.txt  file3.txt  file_dir  files.tar.gz

Cheers!

What is GraalVM?

GraalVM is a high-performance, embeddable, polyglot virtual machine for running applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages such as C and C++.

Here is the Official doc link

Hmm… Okay, I have to see it.

Let’s install

Below is the way I installed GraalVM Community Edition on Ubuntu 18.04. For other platform, the official doc installation guide is here.

# update this number to latest version from here: https://github.com/oracle/graal/releases
version=1.0.0-rc15
wget https://github.com/oracle/graal/releases/download/vm-${version}/graalvm-ce-${version}-linux-amd64.tar.gz
tar -xvzf graalvm-ce-${version}-linux-amd64.tar.gz
# clean up
rm graalvm-ce-${version}-linux-amd64.tar.gz
# to wherever you want. 
mv graalvm-ce-1.0.0-rc15/ ~/bin/graalvm
# if you want to make it permanent, put this in your bashrc 
export PATH=$HOME/graalvm/bin:$PATH

Now that your graalvm/bin in your path, you’ll get the GraalVM versions of those runtimes.

before:

$ js --version
v8.10.0

now:

$ js --version
Graal JavaScript 1.0 (GraalVM CE Native 1.0.0-rc15)

Now let’s play with the Polyglot Shell

The Polyglot Shell is useful to quickly test interactive Graal languages. This is how you can start it:

$ polyglot --jvm --shell
GraalVM MultiLanguage Shell 1.0.0-rc15
Copyright (c) 2013-2019, Oracle and/or its affiliates
  JavaScript version 1.0
Usage: 
  Use Ctrl+L to switch language and Ctrl+D to exit.
  Enter -usage to get a list of available commands.
js> 

Ah, it is listing only JavaScript. Got to install “python component” to run Python

gu install python

Now invoke it again

$ polyglot --jvm --shell
GraalVM MultiLanguage Shell 1.0.0-rc15
Copyright (c) 2013-2019, Oracle and/or its affiliates
  JavaScript version 1.0
  Python version 3.7.0
Usage: 
  Use Ctrl+L to switch language and Ctrl+D to exit.
  Enter -usage to get a list of available commands.
js> console.log('hello')
hello
python> print('hello')
hello
python> 

Oh yay!

For just python interactive shell

$ graalpython
Python 3.7.0 (Thu Apr 04 19:04:32 UTC 2019)
[GraalVM CE, Java 1.8.0_202] on linux
Type "help", "copyright", "credits" or "license" for more information.
Please note: This Python implementation is in the very early stages, and can run little more than basic benchmarks at this point.
>>> print('hello')
hello
>>> 
Debugger?

Debugger is available to all the GraalVM languages.

Let’s play with it. Prep the FizzBuzz program in python fizzbuzz.py

def fizzbuzz(n):
    if n % 3 == 0 and n % 5 ==0:
        return 'FizzBuzz'
    if n % 3 == 0:
        return 'Fizz'
    if n % 5 == 0:
        return 'Buzz'
    return n


if __name__ == '__main__':
    for n in range(1, 21):
        print(fizzbuzz(n))

Let’s start debugger

$ graalpython --jvm --inspect fizzbuzz.py
Debugger listening on port 9229.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/js_app.html?ws=127.0.0.1:9229/2c9f9fb0-8067ef52f64c8
  1. Open the URL in Chrome
  2. Click “Add folder to workspace” on the left to navigate the folder your fizzbuzz.py is located
  3. Open fizzbuzz.py from Workspace and place breakpoint then resume script execution
python tracing in chrome deubber

Wow, that’s kind of impressive!

Web app in multiple languages

So this example is from this article Let’s create polyglot.js

const express = require('express')
const app = express()

const BigInteger = Java.type('java.math.BigInteger')

app.get('/', function (req, res) {
  var text = 'Hello World from Graal.js!<br> '

  // Using Java standard library classes
  text += BigInteger.valueOf(10).pow(100)
          .add(BigInteger.valueOf(43)).toString() + '<br>'

  // Using R interoperability to create graphs
  text += Polyglot.eval('R',
    `svg();
     require(lattice);
     x <- 1:100
     y <- sin(x/10)
     z <- cos(x^1.3/(runif(1)*5+10))
     print(cloud(x~y*z, main="cloud plot"))
     grDevices:::svg.off()
    `);

  res.send(text)
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})
Let’s install dependencies:

First r component

gu install r

Next npm package express

npm install express

Let’s start the app

node --polyglot --jvm polyglot.js

Ta-da!

js app in multiple language

Cheers!

How to determine my public IP address assigned by the ISP via CLI

Use dig command
dig +short myip.opendns.com @resolver1.opendns.com

Use 3rd party

curl ifconfig.me

Cheers!

The problem

I was on AWS and needed to select SSH Key pair. It has been a while that I needed to create one that might require SSH Key pair. So to be sure I got the correct key, I needed to compare fingerprint listed on Key Pair list. I do have pem key with name contain aws so I want to generate finger print for that key.

How to solve it

openssl pkcs8 -in path/to/private_key_file -nocrypt -topk8 -outform DER | openssl sha1 -c

or use AWS tool

sudo apt install ec2-api-tools
ec2-fingerprint-key path/to/private_key_file

That’s it!

Cheers!

Cython is designed as a C-extension for Python. The developers can use Cython to speed up Python code execution. Performance gains are most significant in CPU-bound programs.

Here is some cpu-intense code in python

# cat compute_it.py 
import datetime
import math


def main():
    t0 = datetime.datetime.now()
    do_math(num=30_000_000)
    dt = datetime.datetime.now() - t0
    print("Done in {:,.2f} sec".format(dt.total_seconds()))


def do_math(start=0, num=10):
	pos = start
	k_sq = 1000 * 1000
	while pos < num:
		pos += 1
		math.sqrt((pos - k_sq) * (pos - k_sq))


if __name__ == '__main__':
    main()

Took 7.39 sec

python compute_it.py 
Done in 7.39 sec

Let’s convert this to cython and run without GIL

Step 1. Write move do_math to cython code

# cat math_core.pyx 

from libc.math cimport sqrt
import cython


def do_math(start:cython.int = 0, num:cython.int = 10):
    pos: cython.int = start
    k_sq: cython.int = 1000 * 1000
    
    with nogil:
        while pos < num:
            pos += 1
            sqrt((pos - k_sq) * (pos - k_sq))
# cat compute_w_cython.py 
import datetime
from math_core import do_math


def main():
    t0 = datetime.datetime.now()
    do_math(num=30_000_000)
    dt = datetime.datetime.now() - t0
    print("Done in {:,.2f} sec".format(dt.total_seconds()))


if __name__ == '__main__':
    main()

Step 2. Create a setup, setup.py

# cat setup.py 
from distutils.core import setup
from Cython.Build import cythonize


setup(ext_modules = cythonize("math_core.pyx"))

Step 3. Compile the cython via setup

$ pip install -U cython
$ python setup.py build_ext --inplace
Compiling math_core.pyx because it changed.
[1/1] Cythonizing math_core.pyx
...
math_core.cpython-36m-x86_64-linux-gnu.so

Step 4. Run the code

$ python compute_w_cython.py 
Done in 0.09 sec

This is quite impressive isn’t it?

Note: it is important that before you convert to cython profile that code and ensure it is bottle-neck and cpu-bound.

Cheers!