Tag Archives: #linux

Refresh with Python

I started not as a developer or an engineer but as a “solution finder.” I needed to resolve an issue for a client, and Python was the code of choice. That’s how my code journey into Python began. I started learning about libraries, and my knowledge grew from there. Usually, there was an example of how to use the library and the solution. I would review the code sample and solution, then modify it to work for what I needed. However, I need to refresh whenever I step away from this type of work. Sometimes the career journey takes a detour, but that doesn’t mean you can’t continue to work and build in your area of interest.

If you want to refresh your Python skills or brush up on certain concepts, this blog post is here to help. Let’s walk you through a code sample that utilizes a famous library and demonstrates how to work with a data array. So, let’s dive in and start refreshing those Python skills!

Code Sample: Using NumPy to Manipulate a Data Array

For this example, we’ll use the NumPy library, which is widely used for numerical computing in Python. NumPy provides powerful tools for working with arrays, making it an essential data manipulation and analysis library.

This same example can be used with Azure Data Studio, my tool of choice for my coding, with the advantage of connecting directly to the SQL database in Azure, but I will save that for another blog post.

Another of my favorites is Windows Subsystem for Linux; this example would apply.

Let’s get started by installing NumPy using pip:

pip install numpy

Once installed, we can import NumPy into our Python script:

import numpy as np

Now, let’s create a simple data array and perform some operations on it:

# Create a 1-dimensional array
data = np.array([1, 2, 3, 4, 5])

# Print the array
print("Original array:", data)

# Calculate the sum of all elements in the array
sum_result = np.sum(data)
print("Sum of array elements:", sum_result)

# Calculate the average of the elements in the array
average_result = np.average(data)
print("Average of array elements:", average_result)

# Find the maximum value in the array
max_result = np.max(data)
print("Maximum value in the array:", max_result)

# Find the minimum value in the array
min_result = np.min(data)
print("Minimum value in the array:", min_result)

In this code sample, we first create a 1-dimensional array called “data” using the NumPy array() function. We then demonstrate several operations on this array:

  1. Printing the original array using the print() function.
  2. Calculating the sum of all elements in the array using np.sum().
  3. Calculating the average of the elements in the array using np.average().
  4. Finding the maximum value in the array using np.max().
  5. Finding the minimum value in the array using np.min().

By running this code, you’ll see the results of these operations on the data array.


Refreshing your Python skills is made easier with hands-on examples. In this blog post, we explored a code sample that utilized the powerful NumPy library for working with data arrays. By installing NumPy, importing it into your script, and following the walk-through, you learned how to perform various operations on an array, such as calculating the sum, average, maximum, and minimum values. Join me on my journey deeper into the world of data manipulation and analysis in Python.

Key-Value-Based Data Storage

Submitting to speak for technical events can be tedious as the number of people competing for a few spots grows. I have found myself on more than one occasion with a presentation that didn’t get selected. I discovered some I wanted to share as I went through this body of work. Although this is not a presentation platform at a conference, I wanted to share my experience working with Redis Database. This presentation is a few years old, so I needed to revisit it to see what’s changed. I also find it inspiring to review this technology to see what it can do. Enjoy.

Open-source databases have gained significant popularity due to their flexibility, scalability, and cost-effectiveness. When storing key-value-based data, an open-source database like Redis offers several advantages. Let’s explore the benefits of using Redis and delve into a technical demonstration of how data is stored in Redis.

Items that could be used as a presentation deck:

  1. High Performance: Redis is known for its exceptional performance, making it ideal for applications that require low latency and high throughput. It stores data in memory, allowing for swift read and write operations. Additionally, Redis supports various data structures, such as strings, hashes, lists, sets, and sorted sets, providing the flexibility to choose the appropriate structure based on the application’s requirements.
  2. Scalability: Redis is designed to be highly scalable vertically and horizontally. Vertical scaling involves increasing the resources of a single Redis instance, such as memory, CPU, or storage, to handle larger datasets. Horizontal scaling involves setting up Redis clusters, where data is distributed across multiple nodes, providing increased capacity and fault tolerance. This scalability allows Redis to handle growing workloads and accommodate expanding datasets.
  3. Persistence Options: While Redis primarily stores data in memory for optimal performance, it also provides persistence options to ensure data durability. Redis supports snapshotting, which periodically saves a snapshot of the in-memory data to disk. Additionally, it offers an append-only file (AOF) persistence mechanism that logs all write operations, allowing for data recovery in case of failures or restarts.
  4. Advanced-Data Manipulation: Redis provides a rich set of commands and operations to manipulate and analyze data. It supports atomic operations, enabling multiple commands to be executed as a single, indivisible operation. Redis also includes powerful features like pub/sub messaging, transactions, and Lua scripting, allowing for advanced data processing and complex workflows.
  5. Community and Ecosystem: Redis benefits from a large and active open-source community, contributing to its continuous development and improvement. The Redis community provides support, documentation, and a wide range of libraries and tools that integrate with Redis, expanding its capabilities and making it easier to work with.

Technical Demonstration: Storing Data in Redis

Prerequisite:

Install Redis on WSL2 for Windows

Let’s consider an example where we want to store user information using Redis. We’ll use Redis commands to store and retrieve user data.

  1. Setting a User Record:
    To set a user record, we can use the SET command, specifying the user’s ID as the key and a JSON representation of the user’s data as the value. For example:
SET user:1234 "{\"name\": \"John Doe\", \"email\": \"john@example.com\", \"age\": 30}"
  1. Retrieving User Information:
    To retrieve the user information, we can use the GET command, providing the user’s ID as the key. For example:
GET user:1234

This command will return the JSON representation of the user data: "{\"name\": \"John Doe\", \"email\": \"john@example.com\", \"age\": 30}"

  1. Updating User Information:
    To update a user’s information, we can use the SET command again with the same user ID. Redis will overwrite the existing value with the new one.
  2. Deleting User Information:
    To delete a user record, we can use the DEL command, specifying the user’s ID as the key. For example:
DEL user:1234

This command will remove the user record from Redis.

Using an open-source database like Redis for key-value-based data storage provides numerous benefits, including high performance, scalability, persistence options, advanced data manipulation capabilities, and a vibrant community. Redis offers an efficient and flexible solution.

General Installation Guides for Redis