How to measure the elapsed time in Python

Option 1 - using the time module import time start = time.time() print(23*2.3) end = time.time() print(end - start) Output: 52.9 3.600120544433594e-05 Option 2 - using the timeit module from timeit import default_timer as timer start = timer() print(23*2.3) end = timer() print(end - start) Output: 52.9 6.355400000000039e-05

March 13, 2023 · 1 min · 48 words · Andrew

How to Change a MariaDB/MySQL Data Directory to a New Location on Linux

Step 1 — Moving the MariaDB Data Directory mysql -u root -p select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec) exit sudo systemctl stop mariadb sudo systemctl status mariadb Output: mysql systemd[1]: Stopped MariaDB database server. sudo rsync -av /var/lib/mysql /mnt/my-volume-01 sudo mv /var/lib/mysql /var/lib/mysql.bak Step 2 — Pointing to the New Data Location sudo vi /etc/my.cnf [mysqld] . . ....

March 7, 2023 · 1 min · 122 words · Andrew

[Solved] Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running?

If you get the following error when trying to run a Docker container: Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Then you can resolve it by running the following: systemctl start docker If this doesn’t work because of a root user issue, then you can do the following first: gpasswd -a $USER docker Additional issues? Issue 1 If you have tried the above, and get this error:...

March 4, 2023 · 1 min · 117 words · Andrew

[Solved] M1 docker preview and keycloak 'image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8)' Issue

If you get the following error when trying to run a Docker container that was built on an M1 mac: M1 docker preview and keycloak 'image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8)' Issue The solution Then you can do simply add the following argument to your docker build command. --platform linux/amd64 Where this goes docker build --platform linux/amd64 -t your_docker_item .

March 3, 2023 · 1 min · 65 words · Andrew

How to get the SHA512 sum of a string using Python

If you need to get the SHA512 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the sha512 function of the hashlib module, and get the hexdigest() value from it: hashlib.sha512(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

March 2, 2023 · 1 min · 89 words · Andrew

How to get the SHA256 sum of a string using Python

If you need to get the SHA256 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the sha256 function of the hashlib module, and get the hexdigest() value from it: hashlib.sha256(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

March 1, 2023 · 1 min · 89 words · Andrew

How to get the MD5 sum of a string using Python

If you need to get the MD5 sum of a string using Python, then you can do the following. Step 1 Firstly, we need to use the hashlib module: import hashlib Step 2 Now make sure that the input string is encoded correctly. your_input = "this is your input string".encode('utf-8') Step 3 Finally use the md5 function of the hashlib module, and get the hexdigest() value from it: hashlib.md5(your_input).hexdigest() Putting it all together import hashlib your_input = "this is your input string"....

February 28, 2023 · 1 min · 89 words · Andrew

[Solved] TypeError: Object of type datetime is not JSON serializable

If you get the following error: TypeError: Object of type datetime is not JSON serializable ..then you can solve it by using this trick: json.dumps(your_dict, indent=4, sort_keys=True, default=str)

February 27, 2023 · 1 min · 28 words · Andrew

How to Return a List of All AWS Lambda Function Names in CLI

If you would like to list all AWS Lambda Function Names in your CLI using the AWS CLI, then you can do this: Get a List of all Lambda Functions aws lambda list-functions However, note that this will return a potentially large JSON payload back to your CLI. So what if you only want a list of the function names themselves? You can couple the AWS command above, with the jq command as follows:...

February 24, 2023 · 1 min · 88 words · Andrew

How much faster is Python code?

The speed of Python code compared to other programming languages depends on a variety of factors, such as the specific task being performed, the libraries and frameworks used, the quality of the code implementation, and the hardware on which the code is executed. In general, Python is an interpreted language, which means that code is executed line-by-line by an interpreter, rather than being compiled into machine code beforehand. This can make Python code slower than compiled languages like C or C++ for some tasks....

February 23, 2023 · 2 min · 230 words · Andrew