Bash Parameter Substitution Table 1: Bash Parameter Substitution Variable Description ${parameter:-defaultValue} Get default shell variables value ${parameter:=defaultValue} Set default shell variables value ${parameter:?"Error Message"} Display an error message if parameter is not set ${#var} Find the length of the string ${var%pattern} Remove from shortest rear (end) pattern ${var%%pattern} Remove from longest rear (end) pattern ${var:num1:num2} Substring ${var#pattern} Remove from shortest front pattern ${var##pattern} Remove from longest front pattern ${var/pattern/string} Find and replace (only replace first occurrence) ${var//pattern/string} Find and replace all occurrences ${! Linux Bash Development
BASH - Check IP is in range of ip pools I have a list of IPs in MAXCDN_ARRAY to be used as whitelist. I want to check if a specific IP address is in range in this array. How can I structure the code so that it can compare all IPs in the array and say the specific IP in in range of this list or not? GrepCIDR You can use grepcidr to check if an IP address is in a list of CIDR networks. Linux Bash Development
Pyhon Lru Cache with time expiration In this tutorial, you'll learn: How to expand the functionality of the @lru_cache decorator and make it expire after a specific time Python Development
Asynchronous HTTP Requests in Python with aiohttp and asyncio Asynchronous code has increasingly become a mainstay of Python development. With asyncio becoming part of the standard library and many third party packages providing features compatible with it, this paradigm is not going away anytime soon. Let's walk through how to use the aiohttp library to take advantage of this for making asynchronous HTTP requests, which is one of the most common use cases for non-blocking code. What is non-blocking code? Python Development
A Comprehensive Guide to NumPy Data Types NumPy, one of the most popular Python libraries for both data science and scientific computing, is pretty omnivorous when it comes to data types. It has its own set of 'native' types which it is capable of processing at full speed, but it can also work with pretty much anything known to Python. Python Development
Python Logging Config via Dictionary and Config File Logging is one of the most important aspects of software development. The developers log events like successful completion, warnings, errors, fatal errors, etc. These logs can help developers in various ways like find out the root cause in case of when some failure happens in the future, look for slow parts of the code for optimization, etc. Python provides a module named logging which has a vast API that can be used to log events in Python. Python Development
Underscores in Python Single and double underscores have a meaning in Python variable and method names. Some of that meaning is merely by convention and intended as a hint to the programmer — and some of it is enforced by the Python interpreter. If you're wondering "What's the meaning of single and double underscores in Python variable and method names?" I'll do my best to get you the answer here. In this article I’ll discuss the following five underscore patterns and naming conventions and how they affect the behavior of your Python programs: Python Development
How Validate Object Attributes in Python Generally speaking, type checking and value checking are handled by Python in a flexible and implicit way. Python has introduced typing module since Python3 which provides runtime support for type hints. But for value checking, there is no unified way to validate values due to its many possibilities. One of the scenarios where we need value checking is when we initialize a class instance. We want to ensure valid input attributes in the first stage, for example, an email address should have the correct format xxx@xxxxx. Python Development
Python Decorators Guide The Power of Python Decorators At their core, Python’s decorators allow you to extend and modify the behavior of a callable (functions, methods, and classes) without permanently modifying the callable itself. Any sufficiently generic functionality you can tack on to an existing class or function’s behavior makes a great use case for decoration. This includes the following: logging enforcing access control and authentication instrumentation and timing functions rate-limiting caching and more Sure, decorators are relatively complicated to wrap your head around for the first time, but they’re a highly useful feature that you’ll often encounter in third-party frameworks and the Python standard library. Python Development
Fabulous Python Decorators @cache @functools.cache(user_function) Simple lightweight unbounded function cache. Sometimes called "memoize" . Returns the same as lru_cache(maxsize=None), creating a thin wrapper around a dictionary lookup for the function arguments. Because it never needs to evict (remove) old values, this is smaller and faster than lru_cache() with a size limit. example: from functools import cache @cache def factorial(n): return n * factorial(n-1) if n else 1 >>> factorial(10) # no previously cached result, makes 11 recursive calls 3628800 >>> factorial. Python Development
Python Decorators Cheat Sheet Using decorators The normal way of using a decorator is by specifying it just before the definition of the function you want to decorate: @decorator def f(arg_1, arg_2): ... If you want to decorate an already existing function you can use the following syntax: f = decorator(f) Decorator not changing the decorated function If you don’t want to change the decorated function, a decorator is simply a function taking in and returning a function: Python Development
Complex Request Validation in FastAPI with Pydantic Why should you care? 🤔 Pydantic + FastAPI gets along very well, and provide easy to code, type-annotation based basic validations for atomic types and complex types (created from atomic types). What happens when these basic validations aren’t sufficient for you and you would like to do much more complex? 😲 We use @validator decorator in Pydantic to perform complex validation per request parameter. Let’s dive in.. 🤿 Here’s how we’ll go about this, we are gonna construct a simple API endpoint for printing the date given the day number of the present year. Python Development