https://www.gravatar.com/avatar/485df9434f4908b5f6fab0750c113972?s=240&d=mp

Han

Enumerate variables with Enum!

Enum is a way that Python enumerate variables. The enum module allows for the creation of enumerated constants—unique, immutable data types that are useful for representing a fixed set of values. These values, which are usually related by their context, are known as enumeration members.

Enum provides…

  1. Uniqueness - Each member of an Enum is unique within its definition, meaning no two members can have the same value. Attempting to define two members with the same value will result in an error unless you explicitly allow aliases.
  2. Immutability - Enum members are immutable. Once the Enum class is defined, you cannot change the members or their values.
  3. Iterability and Comparability - Enum classes support iteration over their members and can be compared using identity and equality checks.
  4. Accessing Members - You can access enumeration members by their names or values:
  5. Auto - If you want to automatically assign values to enum members, you can use the auto() function from the same module:
from enum import Enum

class State(Enum):
	PLAYING=0
	PAUSED=1
	GAME_OVER=2

If we just want to make sure them to be unique and automatically assigned, then use auto()

Unit Test with Pytest

Unit testing involves testing individual components of software in isolation to ensure they function correctly. Automated frameworks facilitate this process, which is integral to ensuring that new changes do not disrupt existing functionality. Unit tests also serve as practical documentation and encourage better software design. This testing method boosts development speed and confidence by confirming component reliability before integration. Early bug detection through unit testing also helps minimize future repair costs and efforts.

Bash script tutorial

Let’s create our first simple shell script

#!/bin/sh
# This is a comment!
echo Hello World        # This is a comment, too!
  • The first line tells Unix that the file is to be executed by /bin/sh. This is the standard location of the Bourne shell on just about every Unix system. If you’re using GNU/Linux, /bin/sh is normally a symbolic link to bash (or, more recently, dash).
  • The second line begins with a special symbol: #. This marks the line as a comment, and it is ignored completely by the shell.
  • The only exception is when the very first line of the file starts with #! (shebang) - as ours does. This is a special directive which Unix treats specially. It means that even if you are using csh, ksh, or anything else as your interactive shell, that what follows should be interpreted by the Bourne shell.
  • Similarly, a Perl script may start with the line #!/usr/bin/perl to tell your interactive shell that the program which follows should be executed by perl. For Bourne shell programming, we shall stick to #!/bin/sh.
  • The third line runs a command: echo, with two parameters, or arguments - the first is "Hello"; the second is "World".
  • Note that echo will automatically put a single space between its parameters.
  • To make it executable, run chmod +rx <filename>

Variables

Let’s look back at our first Hello World example. This could be done using variables. Note that there must be no spaces around the “=” sign: VAR=value works; VAR = value doesn’t work. In the first case, the shell sees the “=” symbol and treats the command as a variable assignment. In the second case, the shell assumes that VAR must be the name of a command and tries to execute it.

Minimalism Through Linux

Linux, A Path to Digital Simplicity

In an age dominated by digital clutter and overwhelming software choices, the minimalist philosophy stands out as a beacon for those seeking simplicity and efficiency. This approach not only applies to physical possessions but extends into the digital realm, where Linux has become a preferred tool for minimalists.

Linux, an open-source operating system, embodies the principles of minimalism by offering users control over their digital environments. Unlike mainstream operating systems that often come loaded with non-essential features and bloatware, Linux allows users to select only the components they need, creating a lean and efficient system.

How to keep sensitive data in Python?

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires strict separation of config from code. Config varies substantially across deploys, code does not.

Type hint in Python

Type hinting is not mandatory, but it can make your code easier to understand and debug by

  1. Improved readability
  2. Better IDE support: IDEs and linters can use type hints to check your code for potential errors before runtime.

While type hints can be simple classes like float or str, they can also be more complex. The typing module provides a vocabulary of more advanced type hints.