How to Remove Old and Unused Docker Images

If you need to remove any old and unused Docker images, then you can do the following: How to Remove Old and Unused Docker Images Firstly you need to see all the images: 1 docker images You can also use ls to see the Docker Images: 1 docker image ls How to Remove a Single Docker Image The docker rmi command will remove a single Docker image as follows: 1 docker rmi <image_id> You can also use the Docker image names as follows:

How to Convert Milliseconds to Date in Javascript

If you need to convert Milliseconds to Date in Javascript, then you can do the following: How to Convert Milliseconds to Date in Javascript 1 2 let date = new Date(milliseconds); date.toString(); Common Date Conversions From Milliseconds in Javascript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let originalDate = new Date(milliseconds); // output: "D/MM/YYYY, H:MM:SS PM/AM" originalDate.toLocaleString(); //output: "D/MM/YYYY" originalDate.

How to Convert String to Title Case in Javascript

If you need to convert a String to Title Case in Javascript, then you can do one of the following: Option 1 – Using a for loop 1 2 3 4 5 6 7 8 function titleCase(str) { str = str.toLowerCase().split(' '); for (var i = 0; i < str.length; i++) str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); return str.join(' '); } console.log(titleCase("this is an example of some text!")); Output: This Is An Example Of Some Text!

How to Compile Multiple Java Files from a Single Command in Java

If you need to compile multiple Java files using a single command, then you can do the following. First, it’s good to learn how to compile a Java file. How to Compile a Java File Let’s take the following Java code: 1 2 3 4 5 class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } To compile this, we simply do the following: 1 javac HelloWorld.java Then when we need to run it, we do:

How to Convert JSON to a Java Object

If you need to convert JSON to a Java Object, then you can do one of the following: Option 1 – Using Gson 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 import com.

How to Calculate Powers of Integers in Java

If you need to calculate the powers of Integers in Java, then you can do one of the following: Option 1 – Using for loops 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Power { public static void main(String args[]){ int number = 5; int power = 3; int result = calculatePower(number,power); System.out.println(number+"^"+power+"="+result); } static int calculatePower(int num, int power){ int answer = 1; if (num > 0 && power==0){ return answer; } else if(num == 0 && power>=1){ return 0; } else{ for(int i = 1; i<= power; i++) answer = answer*num; return answer; } } } Option 2 – Using Recursion 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Power { public static void main(String args[]){ int number = 3; int power = 3; int result = CalculatePower(number,power); System.

How to Get Today’s Date in Java

If you need to get today’s date in Java, then you can do one of the following: Option 1 – Using LocalDate 1 2 3 4 5 6 7 8 import java.time.LocalDate; public class GetTodayDate { public static void main(String[] args) { LocalDate todaysDate = LocalDate.now(); System.out.println(todaysDate); } } Option 2 – Using Calendar and SimpleDateFormat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.

How to Copy Files from Docker Container to Host

If you need to copy files from a Docker Container to the Host, then you can do one of the following: Option 1 – Using docker cp Syntax: 1 docker cp [OPTIONS] CONTAINER: SRC_PATH DEST_PATH Setup the container: 1 2 3 4 5 6 7 8 # pull the ubuntu image docker pull ubuntu # run the container locally docker run -it -d ubuntu # connect to the container docker exec -it abcde123456 /bin/bash Create a file from the container:

How to Get the IP Address of a Docker Container

If you need to get the IP Address of a Docker Container, then you can do the following: Option 1 – Connect to the Bridge Network Find out the network setup: 1 docker network ls Create a docker container and assign it to the bridge network: 1 docker run -dt <nginx> Get the information about the container: 1 docker ps Inspect the network: 1 docker network inspect bridge Now you can see the container IP Address.

How to Convert Time to String in Golang

If you need to convert Time to a String in Go, then you can do one of the following: Option 1 – Using time.Now 1 2 3 4 5 6 7 8 9 10 11 package main import ( "fmt" "time" ) func main() { currentTime := time.Now() fmt.Println("Time: ", currentTime.String()) } Option 2 – Using time.Time.String() 1 2 3 4 5 6 7 8 9 10 11 12 package main import ( "fmt" "time" ) func main() { Time := time.

How to Perform a Deep Copy in Golang

To perform a Deep Copy in Go, you can use a struct type as follows: Deep Copying using a struct in Go 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package main import ( "fmt" ) type Dog struct { age int name string friends []string } func main() { john := Dog{1, "Harry", []string{"Steve", "Matt", "Sarah"}} jack := john jack.

How to Return Lambda Functions in Golang

Go doesn’t typically have Lambda Expressions, but synonymous to Lambdas, or Closures if Anonymous Functions for Go. How to return a value from an Anonymous Function in Go 1 2 3 4 5 6 7 8 9 10 11 12 package main import "fmt" func main() { var sum = func(n1, n2 int) int { sum := n1 + n2 return sum } result := sum(5, 3) fmt.Println("Sum is:", result) } How to return an Area from an Anonymous Function in Go 1 2 3 4 5 6 7 8 9 10 11 12 package main import "fmt" var ( area = func(l int, b int) int { return l * b } ) func main() { fmt.

How to Create an Empty Slice in Golang

If you would like to create an empty slice in Go, then you can do the following: Option 1 – Initialize an Empty Slice in Go 1 2 3 4 5 6 7 8 package main import "fmt" func main() { b := []string{} fmt.Println(b == nil) } Option 2 – Using make() 1 2 3 4 5 6 7 8 package main import "fmt" func main() { c := make([]string, 0) fmt.

How to Parallelize a for Loop in Python

If you need to run a for loop in parallel, then you can do one of the following: Option 1 – Using multiprocessing 1 2 3 4 5 6 7 8 9 import multiprocessing def sumall(value): return sum(range(1, value + 1)) pool_obj = multiprocessing.Pool() answer = pool_obj.map(sumall,range(0,5)) print(answer) Option 2 – Using joblib module 1 2 3 4 5 6 7 8 from joblib import Parallel, delayed import math def sqrt_func(i, j): time.

How to Reverse an Integer in Python

If you need to reverse an integer using Python, then you can do the following: Option 1 – Mathematical Palindrome Check 1 2 3 4 5 6 7 8 9 10 11 12 13 original_number = 123454321 copy_number = original_number reversed_number = 0 while original_number > 0: remainder = original_number % 10 reversed_number = reversed_number * 10 + remainder original_number = original_number // 10 if copy_number == reversed_number: print(copy_number, 'is a palindrome number') else: print(copy_number, 'is not a palindrome number') Option 2 – String Reversal Number Palindrome 1 2 3 4 5 6 number = 123454321 if number == int(str(number)[::-1]): print(number, 'is palindrome.

How to Save a Python Dictionary to a File in Python

If you need to save a Python Dictionary object type to a file using Python, then you can do one of the following: Option 1 – Using pickle module 1 2 3 4 5 6 import pickle my_dict = { 'Bob': 31, 'Jane': 50, 'Harry': 13, 'Steve': 23} with open("dictionaryFile.pkl", "wb") as tf: pickle.dump(my_dict,tf) Then you can load the pickle file back as follows: 1 2 3 4 5 6 import pickle with open("dictionaryFile.

How to Move Files From One Directory to Another Using Python

If you need to move files from one directory to another directory, using Python, then you can do one of the following: Option 1 – Using shutil.move() 1 2 3 4 5 6 7 8 9 10 import shutil import os file_source = 'source/directory' file_destination = 'destination/directory' get_files = os.listdir(file_source) for file in get_files: shutil.move(file_source + file, file_destination) Option 2 – Using os.replace() 1 2 3 4 5 6 7 8 9 import os file_source = 'source/directory' file_destination = 'destination/directory' get_files = os.

How to Get the Number of Lines in a File in Python

If you need to get the number of lines in a file, or the line count total from a file, using Python, then you can use one of the following options: Option 1 – Using open() and sum() 1 2 3 4 with open('directory/file.txt') as myfile: total_lines = sum(1 for line in myfile) print(total_lines) Option 2 – Using mmap 1 2 3 4 5 6 7 8 9 10 import mmap with open('directory/file.

How to Read Specific Lines From a File in Python

If you need to read a specific line from a file using Python, then you can use one of the following options: Option 1 – Using fileobject.readlines() If you need to read line 10: 1 2 3 with open("file.txt") as f: data = f.readlines()[10] print(data) If you need to read lines 10, to 20: 1 2 3 with open("file.txt") as f: data = f.readlines()[10:20] print(data) Option 2 – Using for in fileobject 1 2 3 4 5 6 7 8 9 10 11 12 lines =[10, 20] data = [] i = 0 with open("file.

How to Get All Files in a Directory in Python

If you need to get all the files in a directory using Python, then you can do the following: Option 1 – Using os.listdir() 1 2 3 4 5 6 import os dirPath = r"/your/directory/path/" result = [f for f in os.listdir(dirPath) if os.path.isfile(os.path.join(dirPath, f))] print(result) Option 2 – Using os.walk() 1 2 3 4 5 6 import os dirPath = r"/your/directory/path/" result = next(os.walk(dirPath))[2] print(result) Option 3 – Using glob.