Monday, April 30, 2018

golang - compile and run a Go program

One very useful functionality which we would like to use is - Compilation and Execution in one command:

The syntax is:

go run <<fileName.go>>

To illustrate this point, let us consider the following example:

Suppose we have the file:

$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 82 Apr 30 12:00 hello.go


Let us check the contents of the file hello.go:

$ more hello.go 
package main

import "fmt"

func main() {
    fmt.Printf("hello, world 001\n")
}


Let us run it to see the output:

$ go run hello.go
hello, world 001


Now, let us make a small change in the program to print 002 in the output instead of 001:

$ vi hello.go 
$ more hello.go 
package main

import "fmt"

func main() {
    fmt.Printf("hello, world 002\n")
}


Let us compile and run again to review the output:

$ go run hello.go 
hello, world 002

We find that the output has changed. 






golang - seek help in Go

Go has a lot of useful utilities and packages. Here are the two ways to get information:

1. Check on the official website: https://golang.org/pkg/

    This page has the complete documentation for the Go libraries on it. Say, if i want to know about the fmt package. I can read up on it by visiting: https://golang.org/pkg/fmt/
2. godoc utility 
    This is the utility installed on our local machine. It is setup when we installed the go software. godoc will help us to access any package - including the one which we have written ourselves. 

$ godoc fmt Printf

use 'godoc cmd/fmt' for documentation on the fmt command 

func Printf(format string, a ...interface{}) (n int, err error)

    Printf formats according to a format specifier and writes to standard
    output. It returns the number of bytes written and any write error
    encountered.

$

    
This gives me information on how Printf works. Here, it tells me what the signature of the function is: 
- first is s format string
- it is followed by a number of arguments

It also tells me the return parameter. It returns the number of bytes written and also, error message encountered. 


godoc is especially useful when you are not connected to internet and are seeking information.  Whereas, in online documentation - we can read up right up to the source. This is because Go is a open source language by Google.

Thursday, April 26, 2018

golang - install Go in Ubuntu

First, visit the golang website to check for the latest version available:
https://golang.org/dl/

Step 1: Now, we download the latest version:

$ wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
--2018-04-26 13:12:17--  https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 172.217.163.142, 2404:6800:4007:80e::200e
Connecting to dl.google.com (dl.google.com)|172.217.163.142|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119914627 (114M) [application/octet-stream]
Saving to: go1.10.1.linux-amd64.tar.gz

go1.10.1.linux-amd64.tar.gz               100%[=====================================================================================>] 114.36M  16.1MB/s    in 7.2s    


2018-04-26 13:12:25 (15.8 MB/s) - go1.10.1.linux-amd64.tar.gz saved [119914627/119914627]




Step 2: Use sha256sum to verify the tarball:

$ sha256sum go1.15.2.linux-amd64.tar.gz 
72d820dec546752e5a8303b33b009079c15c2390ce76d67cf514991646c6127b  go1.10.1.linux-amd64.tar.gz


Step 3: Compare sha256sum hash with the one on the downloads page:



Step 4: Use tar to extract the tarball:

$ tar xvf go1.15.2.linux-amd64.tar.gz

x - extract
v - verbose output
f - specify a filename

Step 5: Observe that a directory named go is created at the current location:

$ ls -l | grep go
drwxr-xr-x 11 ubuntu ubuntu      4096 Mar 29 10:06 go
-rw-rw-r--  1 ubuntu ubuntu 119914627 Mar 29 20:17 go1.15.2.linux-amd64.tar.gz

Step 6: Change the ownership of this folder (go) to root:

$ sudo chown root:root go
$ ls -l | grep go
drwxr-xr-x 11 root root      4096 Mar 29 10:06 go
-rw-rw-r--  1 mhc  mhc  119914627 Mar 29 20:17 go1.10.1.linux-amd64.tar.gz

Step 7 : Move it to the location: /usr/local

$ sudo mv go /usr/local

Step 8: Setting Go Path:

$ sudo vi ~/.profile 

At the end of this file add the following lines. This tells Go where to look for its files

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Step 9: Let us now, refresh our profile by running:

$ which go
$ source ~/.profile
$ which go
/usr/local/go/bin/go



Wednesday, April 11, 2018

Python - calculate arctangent in radians and degrees

We know that the arctangent of 1 is 45 degrees or pi/4 radians. Now, let see how we can calculate the value using python:

Using Numpy:

import numpy
import math
numpy.arctan(1) # calculates in radians
0.78539816339744828
numpy.arctan(1)*180/math.pi # calculates in degrees
45.0

Using regular python:

import math
math.atan(1) # calculates in radians
0.7853981633974483
math.atan(1)*180/math.pi # calculates in degrees
45.0





References:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.arctan.html
https://docs.python.org/2/library/math.html#math.atan

Friday, April 6, 2018

Python - download youtube videos

To download youtube videos, we need to install the module: youtube-dl

$ sudo pip install youtube-dl

Check File Types: To check out the filetypes available for downloading, we use the following command:

$ youtube-dl -F https://www.youtube.com/watch?v=2eumjKINo6w
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube] 2eumjKINo6w: Downloading webpage
[youtube] 2eumjKINo6w: Downloading video info webpage
[youtube] 2eumjKINo6w: Extracting video information
WARNING: unable to extract uploader nickname
[info] Available formats for 2eumjKINo6w:
format code  extension  resolution note
249          webm       audio only DASH audio   51k , opus @ 50k, 3.56MiB
250          webm       audio only DASH audio   55k , opus @ 70k, 4.06MiB
171          webm       audio only DASH audio   89k , vorbis@128k, 6.32MiB
251          webm       audio only DASH audio  127k , opus @160k, 9.37MiB
140          m4a        audio only DASH audio  128k , m4a_dash container, mp4a.40.2@128k, 9.66MiB
278          webm       256x144    144p   96k , webm container, vp9, 15fps, video only, 6.22MiB
160          mp4        256x144    144p  117k , avc1.42c00c, 15fps, video only, 8.63MiB
242          webm       426x240    240p  216k , vp9, 30fps, video only, 8.97MiB
133          mp4        426x240    240p  248k , avc1.4d4015, 30fps, video only, 18.57MiB
243          webm       640x360    360p  417k , vp9, 30fps, video only, 18.09MiB
134          mp4        640x360    360p  534k , avc1.4d401e, 30fps, video only, 23.52MiB
244          webm       854x480    480p  758k , vp9, 30fps, video only, 32.44MiB
135          mp4        854x480    480p 1082k , avc1.4d401f, 30fps, video only, 49.89MiB
247          webm       1280x720   720p 1455k , vp9, 30fps, video only, 74.34MiB
136          mp4        1280x720   720p 2065k , avc1.4d401f, 30fps, video only, 99.03MiB
248          webm       1920x1080  1080p 2791k , vp9, 30fps, video only, 170.39MiB
137          mp4        1920x1080  1080p 4141k , avc1.640028, 30fps, video only, 208.59MiB
17           3gp        176x144    small , mp4v.20.3, mp4a.40.2@ 24k
36           3gp        320x180    small , mp4v.20.3, mp4a.40.2
43           webm       640x360    medium , vp8.0, vorbis@128k
18           mp4        640x360    medium , avc1.42001E, mp4a.40.2@ 96k
22           mp4        1280x720   hd720 , avc1.64001F, mp4a.40.2@192k (best)
 $


Download video of a particular filetype:

$ youtube-dl -f 18 https://www.youtube.com/watch?v=2eumjKINo6w
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube] 2eumjKINo6w: Downloading webpage
[youtube] 2eumjKINo6w: Downloading video info webpage
[youtube] 2eumjKINo6w: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: The_Story_of_Integration_2_of_4_-_Riemann_s_Integral-2eumjKINo6w.mp4
[download] 100% of 30.23MiB in 00:01
 $ 

Notice, the -f flag used.

Downloading the entire playlist can be done by using the below command:

$ youtube-dl -f 18 -o '%(playlist_index)s. %(title)s.%(ext)s'  https://www.youtube.com/playlist?list=PLrsBhRPS9dWHGfBTLJcmeopwjbTCn6cUe
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube:playlist] PLrsBhRPS9dWHGfBTLJcmeopwjbTCn6cUe: Downloading webpage
[download] Downloading playlist: Khan Academy Linear Algebra | Vectors and spaces
[youtube:playlist] playlist Khan Academy Linear Algebra | Vectors and spaces: Downloading 41 videos
[download] Downloading video 1 of 41
[youtube] br7tS1t2SFE: Downloading webpage
[youtube] br7tS1t2SFE: Downloading video info webpage
[youtube] br7tS1t2SFE: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: 01. Vector_intro_for_linear_algebra_Vectors_and_spaces_Linear_Algebra_Khan_Academy.mp4
[download] 100% of 4.44MiB in 00:00
[download] Downloading video 2 of 41
[youtube] lCsjJbZHhHU: Downloading webpage
[youtube] lCsjJbZHhHU: Downloading video info webpage
[youtube] lCsjJbZHhHU: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: 02. Real_coordinate_spaces_Vectors_and_spaces_Linear_Algebra_Khan_Academy.mp4


Notice here, the -o flag used.