How Do You Get Python to Compute How Long Something Took?

Problem scenario
You want to check the runtime duration of a section of Python code. How do you compute the amount of time something took in Python?

Solution
Write a program like this:

import datetime, time
t1 = datetime.datetime.now()
time.sleep(5) # replace this line with the section of code you want to time
t2 = datetime.datetime.now()
t3 = t2 – t1
print(“Time format is in hours:minutes:seconds:seconds_decimals”)
print(t3)  » Read more..

How Do You Troubleshoot the Go Programming Error ‘non-name m1[“foo”] on left side of :=’?

Problem scenario
You are running a Go program. You see this message about “command-line-arguments”:

non-name m1[“foo”] on left side of :=

What should you do?

Possible Solution
Can you try to use “=” instead of “:=”? This may fix your problem.

The := is a variable declaration and an assignment. If your map has been declared, individual values in it should be assigned with an “=” (equals sign).

 » Read more..

How Do You Use a Function in Golang?

Solution
Prerequisite
This assumes that you have installed Golang; if you need assistance with this, see this posting.

Procedures
1. Create a file called b.go with this as the content:

package main

import “fmt”

// ContintFunction will display a message
func ContintFunction() {
fmt.Println(“Hello from Continual Integration”)
}

func main() {
ContintFunction()
}

2.

 » Read more..

In Python, How Do You Call a Bound Function as a New Thread with the thread Module?

Problem scenario
You want to write a program to call a bound function in a new thread. How do you do this?

Solution
Run this program (e.g., python foobar.py):

import _thread as thread
class mighty:
def cool():
print(“Cool.”)
def contint():
print(“Hello!”)
if name == “main”:
foobar = thread.start_new_thread(mighty.cool, () )
print(“thread finished…exiting”)

 » Read more..

How Do You Create Bytecode with Golang?

Problem scenario
You want to create bytecode using Golang. What do you do.

Solution
Prerequisite
Install Golang. If you need assistance, see this posting.

Procedures
Create a Go script that you can run with go run nameOfProgram.go
Run this: go build nameOfProgram.go
Run this: ./nameOfProgram

 » Read more..

How Do You Create a List of Strings in Golang?

Problem scenario
Lists have a fixed length number of discrete items. You want the items in the list to be strings. How do you create a list of strings in Golang?

Solution
1. Use this program called animal.go:

package main

import “fmt”

func main() {
var x [10]string
x[0] = “dog”
x[1] = “cat”
x[2] = “hamster”
x[3] = “rabbit”
x[4] = “chicken”

for i := 0;  » Read more..

How Do You Create Your Own RPM Package (Binary File) for Your Own C Program?

Problem scenario
You want to know more about RPM packaging so you can create your own .rpm file (a custom RPM package file).  In other words you want to create your own RPM package (a binary file).  You want it for installing an uncompiled C program you wrote.  The uncompiled C program is one .c file and very simple.  You don’t know how to use a spec (specification) file to create your own RPM package file. 

 » Read more..

In Python, How Do You Call an Unbound Function as a New Thread with the threading Module?

Problem scenario
You want to write a program to call a unbound function in a new thread with the threading module. How do you do this?

Solution
Run this program (e.g., python foobar.py):

from threading import Thread
def contint():
print(“Hello!”) if name == “main”:
thread = Thread(contint())
thread.start()
thread.join()
print(“thread finished…exiting”)  » Read more..

How Do You Get a PHP Program to Invoke a Python Program via Browsing a Website?

Problem scenario
You want Debian/Ubuntu Linux to support a website. You want a Python program to run every time a web page is downloaded. How do you get a PHP program to invoke a Python program on Debian/Ubuntu Linux?

Solution

Prerequisites
i. This assumes you have Apache2 and PHP installed. If you need assistance run this: sudo apt -y install apache2 php
ii.

 » Read more..

How Do You Read in User Input with Golang?

Problem scenario
You want to read in user input with Golang. What should you do?

Solution
This program will illustrate how you accept user input with a Golang program:

package main

import (
“bufio”
“fmt”
“os”
)

func main() {
fmt.Println(“Please enter some text:”)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString(‘\n’)
fmt.Println(“The text you entered was this: “, text)
}  » Read more..