base64

Base64 encoding is widely used and there are nice online tools, for example https://www.base64decode.org/.

On command line could be convenient choice to use openssl with argument base64 to encode or two arguments base64 -d to decode. Tool reads from stdin, or from file with argument -in and writes to stdout or to the file with arguments -out .

echo Blog | openssl base64
QmxvZwo=
echo AB | openssl base64
QUIK
echo QUIK | openssl base64 -d
AB
Posted in workday | Leave a comment

go: interface example

Go is fresh fast relatively new language. One of the key to utilize it’s power are interfaces. Here is example of the geometry interfaces. See https://play.golang.org/p/nQ_ylb2jXKS

package main

import (
	"fmt"
	"math"
)

//https://www.mathsisfun.com/area.html

type geometry interface {
	area() float64
	perimeter() float64
}

type square struct {
	side float64
}
type rectangle struct {
	width, height float64
}
type triangle struct {
	a, b, c float64
}
type circle struct {
	radius float64
}

func (s square) area() float64 {
	return s.side * s.side
}
func (s square) perimeter() float64 {
	return 4 * s.side
}
func (r rectangle) area() float64 {
	return r.width * r.height
}
func (r rectangle) perimeter() float64 {
	return 2*r.width + 2*r.height
}
func (t triangle) area() float64 {
	cosGama := (t.c*t.c - t.a*t.a - t.b*t.b) / (2 * t.a * t.b)
	if math.Abs(cosGama) > 1.0 {
		fmt.Println("Input values are not for triangle")
		return -1
	}
	sinGama := math.Sqrt(1.0 - cosGama*cosGama)
	heightA := t.b * sinGama
	// fmt.Printf("triangle:  cos=%f, sin=%f,hA=%f\n",cosGama,sinGama,heightA)
	return t.a * heightA
}
func (t triangle) perimeter() float64 {
	return t.a + t.b + t.c
}
func (c circle) area() float64 {
	return math.Pi * c.radius * c.radius
}
func (c circle) perimeter() float64 {
	return 2 * math.Pi * c.radius
}
func measure(g geometry) {
	fmt.Println(g)
	fmt.Printf("area     : %f \n", g.area())
	fmt.Printf("perimeter: %f \n", g.perimeter())
}
func main() {
	s := square{side: 2}
	r := rectangle{width: 3, height: 4}
	t1 := triangle{a: 3, b: 4, c: 5}
	t2 := triangle{a: 1, b: 1, c: 1}
	t3 := triangle{a: 1, b: 1, c: 30}
	c := circle{radius: 5}
	
	measure(s)
	measure(r)
	measure(t1)
	measure(t2)
	measure(t3)
	measure(c)
}

Posted in golang, workday | Leave a comment

HTTPS Tomcat

Here are some notes from install and run HTTPS Tomcat on digitalocean inpired by Mavi’s blog.

on digitalocean:
Ubuntu 18.04 x64
Memory:1GB, vCPU:1, SSD_Disk:25GB, Transfer:1TB, price:$5/mo
Hostname:ubuntu18
IP:1.2.3.4

on my DNS provider web:
Edit DNS A records for example.com and www.example.com

ssh session to example.com, user root

# snap install docker
2018-08-29T15:33:20Z INFO Waiting for restart...
docker 17.06.2-ce from 'docker-inc' installed

#snap info certbot
#snap install --edge certbot

#apt-get update
#apt-get install software-properties-common
#add-apt-repository ppa:certbot/certbot
#apt-get update
#apt-get install certbot 
.
.
.Setting up certbot (0.26.1-1+ubuntu18.04.1+certbot+2) ...
Created symlink /etc/systemd/system/timers.target.wants/certbot.timer → /lib/systemd/system/certbot.timer.
certbot.service is a disabled or a static unit, not starting it.

#certbot certonly
...I choosed 1. spin up a temporary webserver (standalone)


#apt install docker-compose


#cd /root/app
#docker-compose up

and, yes, I see Tomcat at https://example.com

Posted in workday | Leave a comment

CSV: How to sort by several columns in Java8



CSV_SortByThreeColumns.java

package example.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Comparator;
import java.util.stream.Stream;
/**
 * Let's say there is a comma delimited file with the first line describing column names:
 * Name, State, Years of experience
 * Here is how to sort lines from the most experienced people, then by state and name
 */

public class CSV_SortByThreeColumns {
    private static void printSortedByExperienceAndStateAndNameFromStreamedLines(Stream<String> stream){
        Comparator<String> byExperience = Comparator.comparing(
                line -> Integer.parseInt(line.split(",")[2]), Comparator.reverseOrder()
        );// by Years of experience - reversed
        Comparator<String> byExperienceAndState = byExperience.thenComparing(
                line -> line.split(",")[1]
        );// by State
        Comparator<String> comparator = byExperienceAndState.thenComparing(
                line -> line.split(",")[0]
        );// by Name
        stream
                .skip(1L)
                .sorted(comparator)
                .forEach(System.out::println);
    }
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = Files.newBufferedReader(Paths.get("a.csv"));
        BufferedReader reader = new BufferedReader(new StringReader(
                        "Name,State,Years of experience\n" +
                        "John,MN,1\n" +
                        "Zita,CA,2\n" +
                        "Adam,CA,5\n" +
                        "Dough,CA,20\n" +
                        "Richard,MN,6\n" +
                        "Robert,CT,6"));
        printSortedByExperienceAndStateAndNameFromStreamedLines(reader.lines());
    }
}

Output:

Dough,CA,20
Robert,CT,6
Richard,MN,6
Adam,CA,5
Zita,CA,2
John,MN,1

Links: stream(package summary), Stream<T>, Comparator<T>

Posted in Java8, workday | Leave a comment

CSV: How to skip the first line in Java8



CSV_SkipFirstLine.java

package example.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.stream.Stream;

/**
 * Let's say there is a comma delimited file with the first line describing column names.
 * In this example a person name and state.
 * Here is how to skip the first line using Java8 stream
 */
public class CSV_SkipFirstLine {
    private static void skipFirstAndPrintRestOfLinesFromStream(Stream<String> stream){
        stream
                .skip(1L)
                .forEach(System.out::println);
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader(
                        "Name,State\n" +
                        "John,MN\n" +
                        "Zita,CA\n" +
                        "Adam,CA\n" +
                        "Dough,CA\n" +
                        "Richard,MN\n" +
                        "Robert,CT"));
        skipFirstAndPrintRestOfLinesFromStream(reader.lines());
    }
}

Output:

John,MN
Zita,CA
Adam,CA
Dough,CA
Richard,MN
Robert,CT
Posted in Java8, workday | Leave a comment

Handy notepad in Chrome tab or window

There is nice option to create editable tab on window in Chrome browser with URL like this:
data:text/html, <html contenteditable>

It is possible to save it into the file after pressing CTRL+<S>

Posted in workday | Leave a comment

Sky above from freezing cold nights

Nights are long in the Winter, with many clear nights, but with very cold temperatures as well. It looks like Kakslauttanen Arctic Resort in Finland built good solution how to observe beauty of the sky in Winter.

Posted in family | Leave a comment

Bubble tent to look at stars

Hmm, maybe bubble tent is right idea for fidget weather of Minnesota. One could be warm and look at stars, maybe even observe by telescope.

Posted in workday | Leave a comment

SVG: Elliptical arc path

Scalable Vector Graphics allows elliptical paths. Here is rendered image, which can help to realize meaning of the parameters. In case below all elipses and circles are have end points [0,0] and [70.71, 70.71]. Second point is on the circle with radius 100. Arc with small “a” means relative dimensions.

Posted in workday | Leave a comment

find a files with -maxdepth and -mindepth

Unix utility find allows many arguments. Here is how one can limit searched directories. Arguments -maxdepth and -mindepth should be used first before let’s say -name or -type.

Let’s created four sub directories to investigate how it works:

$ mkdir -p a/b/c/d

$ find a
a
a/b
a/b/c
a/b/c/d

$ find a -maxdepth 2
a
a/b
a/b/c

$ find a -maxdepth 2 -mindepth 2
a/b/c

$ find a -mindepth 2
a/b/c
a/b/c/d

See also find a files with mmin

Posted in workday | Leave a comment