Any good suggestions on a Java REPL implementation like ‘scala’ and ‘python’ or ‘php -a’? The java-repl tool https://github.com/albertlatacz/java-repl/ works nicely for most situations for me. It is released as a .jar. Hence, it is easy to download and run: $ wget –quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar One usage example is as
Read more
Tag: Programming
Any good Go REPL implementation / tool?
Posted onGo lang is fast to compile and can be used as a script by go run prog.go. However, a REPL is more convenient for testing/trying some small pieces of code. Any good Go REPL implementation / tool? Try gore https://github.com/motemen/gore Get gore $ go get -u github.com/motemen/gore An example $ gore gore version 0.2.6 :help
Read more
How to get hostname in Python on Linux?
Posted onIn Python, how to get hostname as the command hostname does on Linux? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Reference: https://www.systutorials.com/dtivl/20/how-to-get-the-hostname-of-the-node?show=34#a34
How to process a file line by line in Python?
Posted onIn Python, how to process read a file line by line to process it? Like those lines in Bash: while read line ; do echo $line done < ./input.txt In Python, you can process a file line by line by a for in the file like with open(“./input.txt”, “r”) as thefile: for line in thefile:
Read more
How to get an environment variable in Python?
Posted onIn Python, how to get an environment variable? In Python, you may use this piece of code to get an environment variable: os.environ.get(‘ENV_MIGHT_EXIST’) or this piece of code: os.getenv(‘ENV_MIGHT_EXIST’) It will return None if the environment variable is not present. Reference and for more ways, please check https://www.systutorials.com/dtivl/13/how-to-get-an-environment-variable?show=80#answer-80 .
How to get an environment variable in Go?
Posted onIn Go lang, how to get an environment variable? To get an environment variable (e.g. “VAR”) in GO: import “os” import “fmt” fmt.Println(“VAR:”, os.Getenv(“VAR”))
How to convert a byte[] to io.Reader in Go?
Posted onHow to convert a byte[] to io.Reader in Go directly? You can use the NewReader() func from the bytes package of Go to convert bytes to io.Reader. For example, reader := bytes.NewReader(thebytes)
How to read the whole file content into a string in Go?
Posted onHow to read the whole file content into a string in Go? You can use the ReadFile() func from the io/ioutil package in Go to read the whole file content: func ReadFile(filename string) ([]byte, error) For example, read file filepath content and check the error: if bytes, err := ioutil.ReadFile(filepath); err != nil { log.Fatal(“Failed
Read more
How to use ioprio_set in linux c
Posted onSince there is no glibc wrapper for ioprio_set in linux c, we need to call this API with some definition. Using syscall in linux as follows. syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, IOPRIO_PRIO_CLASS(IOPRIO_CLASS_IDLE)); Some other definitions to declare above macros. 69 enum { 70 IOPRIO_CLASS_NONE, 71 IOPRIO_CLASS_RT, 72 IOPRIO_CLASS_BE, 73 IOPRIO_CLASS_IDLE, 74 }; 75 76 enum { 77
Read more
In Bash script, how to join multiple lines from a file?
Posted onIn Bash script, how to join multiple lines from a file? For example, to join the lines a good boy to a good boy You can use tr command, something like: tr -s ‘n’ ‘ ‘ < file.txt It just goes through its input and makes changes according to what you specify in two sets
Read more
How to detect whether a file is readable and writable in Python?
Posted onBefore reading or writing a file, access should be checked first. How to detect whether a file is readable and writable in Python? You can use the os.access(path, mode) library function https://docs.python.org/release/2.6.6/library/os.html#os.access like the Linux access library function for C. It returns True if access is allowed, False if not. For readable and writable, you
Read more
How to make tee catch the stderr only in Linux?
Posted onI would like to make tee catch the stderr and log it into a file. A brute force method by let tee listen on both stderr and stdout is okay like cmd 2>&1 | tee -a log But how to make tee catch the stderr only? You can make use of “process substitution” (>(…)) to
Read more
How to make a unique temporary file in Bash on Linux?
Posted onIt is common to make a unique temporary file. How to make a unique temporary file in Bash on Linux safely and quickly? You can use the mktemp program. In the simplest way, tmpfile=$(mktemp) The file will be like /tmp/tmp.j0wD39Mr3b if you do not prefer any meaningful names. You can set the file to your
Read more
In Python, how to check whether a key already exists in a dict?
Posted onHow to check whether a key already exists in a dict in Python since version 2.6? If d is a dict(), you can directly test whether a key k is in d in Python by the in keyword like if k in d: ..
How to collect VM exit numbers in KVM
Posted onSometimes, we may want to know the specific benchmark can cause what kind of VM exits. 1, Install perf. 2, # perf kvm stat record -a ^C 3, # perf kvm stat report This will report your VM exit numbers and what kind of operation lead to VM exit. You may need to compare many
Read more
How to get the IP addresses of VMs in KVM with virsh
Posted onWhen we create VMs in KVM, we may don’t know the IP addresses because they are automatically assigned by DHCP. So we want to get them in VMM so that can login by SSH. Do the commands as follows. $ virsh domiflist hvm1 Interface Type Source Model MAC ——————————————————- vnet0 network default virtio 52:54:00:1c:36:cd $
Read more
How to change CONFIG_HZ parameter for Linux Kernel
Posted onIf we want to change the tick time for Linux Kernel, we need to change CONFIG_HZ parameter in Linux Kernel. Do we have other better ways to change it rather than compiling Linux Kernel. Please ignore the way to add ‘divider=10’ in grub config file because it is limited only for RH/CentOS distros. Zhiqiang, please
Read more
How to install PARSEC correctly.
Posted onPARSEC is the most important CPU-bound benchmark for systems. It is huge and hard to install because it needs lots of 3-part libs. PARSEC download link for 3.0 version: http://parsec.cs.princeton.edu/download.htm#parsec I remembered I added the answer yesterday night but I could not see the answer currently. Anyway, let me add the answer again after I
Read more
In Vim, are there shortcuts to set tab widths to 2 and 4?
Posted onTab widths may be different for different languages. Even with a common language, the tab width settings can be different for different projects. In Vim, are there shortcuts to set tab widths to 2 and 4? To quickly change the tab widths when changing projects. I am afraid no. But you create shortcuts for setting
Read more
In Golang, how to convert a string to unicode rune array and back?
Posted onIn Golang, how to convert a string to unicode rune array and do the reverse way? Convert string s to a rune array: runes := []rune(s) Convert a rune array runes to string: str := string(runes)