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.

[Solved] dial tcp: lookup proxy.golang.org: i/o timeout

If you get a timeout when trying to install go dependencies, the error may look something like this: 1 2 3 $ go get github.com/aws/aws-sdk-go/aws go: module github.com/aws/aws-sdk-go/aws: Get "https://proxy.golang.org/github.com/aws/aws-sdk-go/aws/@v/list": dial tcp: lookup proxy.golang.org: i/o timeout How to Solve the Timeout Issue when installing Go Deps 1 export GOPROXY=direct Then re-run your go get command.

How to Check Operating System in Python

If you need to check the Operating System information from Python, then you can do one of the following: Option 1 – Using the platform module 1 2 3 import platform my_os = platform.system() print("Operating System is: ",my_os) Option 2 – Using the sys module 1 2 3 import sys my_os=sys.platform print("Operating System is: ",my_os)

How to Convert Hex to Byte in Python

If you need to convert Hex to Byte in Python, then you can do one of the following: Option 1 – Using binascii 1 2 3 4 5 import binascii str_val = 'This is a test'.encode('utf-8') hex_val = binascii.hexlify(str_val).decode('utf-8') print(hex_val) Option 2 – Using bytes.fromhex() 1 2 3 hex_val = 'This is a test' print(bytes.fromhex(hex_val)) Option 3 – Using unhexlify 1 2 3 4 5 6 7 8 9 import binascii from binascii import unhexlify str_val = 'This is a test'.

[Solved] fatal: Could not read from remote repository with Git

If you receive the following error when trying to clone a Git repository: fatal: Could not read from remote repository. The full message may look something like this: 1 2 3 4 5 6 7 $ git clone [email protected]:org/repo.git Cloning into 'repo'... Bad owner or permissions on /Users/ao/.ssh/config fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. How to solve this error 1 ssh-add ~/.

How to Install Homebrew on a Mac

Prerequisites You should have some familiarity with the Mac Terminal application since you’ll need to use it to install Homebrew. The Terminal application is located in the Utilities folder in the Applications folder. Dependencies. You need to install one other piece of software before you can install Homebew: Xcode. Install Apple’s Xcode development software: Xcode in the Apple App Store. Installation Overview Installing Homebrew is straightforward as long as you understand the Mac Terminal.

How to create an AWS EC2 instance in CloudFormation

Create an EC2 Instance in CloudFormation If you need to create an EC2 instance in CloudFormation, then you can do the following: 1 2 3 4 5 6 7 8 9 10 11 AWSTemplateFormatVersion: "2010-09-09" Resources: WebInstance: Type: AWS::EC2::Instance Properties: InstanceType: t2.nano ImageId: ami-80861296 KeyName: my-key SecurityGroupIds: - sg-abc01234 SubnetId: subnet-abc01234 You can set the Instance Name as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 AWSTemplateFormatVersion: "2010-09-09" Resources: WebInstance: Type: AWS::EC2::Instance Properties: InstanceType: t2.

Understanding the Network Modes in AWS ECS

If using the EC2 launch type, the allowable network mode depends on the underlying EC2 instance’s operating system. If Linux, awsvpc, bridge, host and none mode can be used. If Windows, only the NAT mode is allowed. If using the Fargate launch type, the ‘awsvpc’ is the only network mode supported. Amazon ECS task networking The networking behavior of Amazon ECS tasks hosted on Amazon EC2 instances is dependent on the network mode defined in the task definition.