Skip to content

Instantly share code, notes, and snippets.

View a-luna's full-sized avatar

Aaron Luna a-luna

View GitHub Profile
@a-luna
a-luna / build_nginx_from_source.sh
Last active February 28, 2023 18:29
Shell Script: Build NGINX from Source with Third Party Modules (Cache Purge, GeoIP2), Completely Non-Interactive, No User Input Required
#!/bin/bash -e
# build_nginx_from_source.sh
# Author: Aaron Luna
# Website: alunablog.com
#
# This script downloads the source code for the latest version of NGINX
# (libraries required to build various NGINX modules are also downloaded)
# and builds NGINX according to the options specified in the ./configure
# command. Many of the possible configuration options are explained at
# the link below:
@a-luna
a-luna / TplSocketExample.cs
Last active November 28, 2023 12:40
C# Extension Methods which wrap Socket APM methods in awaitable TPL wrappers. All methods return Task<Result> or Task<Result<T>> objects, the code for the Result class is given as well.
namespace AaronLuna.TplSockets
{
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using AaronLuna.Common.Result;
public class TplSocketExample
@a-luna
a-luna / NetworkUtilities.cs
Last active October 12, 2023 20:00
Helpful C# Network/IP functions. Parse IPs from any arbitrary block of text, retrieve private/public IP addresses and check if IP exists in any range defined by an address in CIDR notation.
namespace NetworkUtilitiesTest
{
using AaronLuna.Common.Network;
using AaronLuna.Common.Extensions;
using System.Threading.Tasks;
class Program
{
// async Main is a C# 7.1 feature, change your project settings to the
// new version if this is flagged as an error
@a-luna
a-luna / ConsoleProgressBar.cs
Last active April 16, 2021 04:31 — forked from DanielSWolf/Program.cs
Progress bar for console applications, added the ability to customize the various components of the progress bar. Added the ability to display or hide the progress bar, percent complete and animation. And just for fun, I created a set of animation sequences which can be used instead of the default "|/-\-" progress indicator.
namespace AaronLuna.Common.Console
{
using System;
using System.Linq;
using System.Text;
using System.Threading;
public class ConsoleProgressBar : IDisposable, IProgress<double>
{
readonly TimeSpan _animationInterval = TimeSpan.FromSeconds(1.0 / 8);
@a-luna
a-luna / dotenv_parser.py
Last active July 29, 2019 01:30
Parse the text of a .env file to a dictionary of environment variables using only the Python standard library. IMUBVHCIO*, the list/dict comprehensions perfectly straddle the line between an elegant use of syntax and an infinitely dense mass of code (the dreaded "neutron star effect"). And extra bonus: Type hints!
"""Parse .env file to a dictionary of environment variables."""
import errno
import os
from pathlib import Path
from typing import Dict, List
def parse_dotenv_file(filepath: Path) -> Dict[str, str]:
"""Parse .env file to a dictionary of environment variables."""
if not filepath.exists():
@a-luna
a-luna / datetime_util.py
Last active May 31, 2023 15:38
Helpful Python datetime functions I've curated over time: timezone conversion, formatting aware/naive objects as strings, custom "timespan" namedtuple that provides more data than timedelta object, and more!
import time
from collections import namedtuple
from datetime import datetime, timedelta, timezone
from dateutil import tz
DT_AWARE = "%m/%d/%y %I:%M:%S %p %Z"
DT_NAIVE = "%m/%d/%y %I:%M:%S %p"
DATE_MONTH_NAME = "%b %d %Y"
DATE_ISO = "%Y-%m-%d"
@a-luna
a-luna / log_call.py
Last active March 11, 2024 16:45
Here are three Python decorators that I use quite often: add timeout functionality to potentially long-running functions, add retry-logic to unreliable functions, and measure the execution time of a function. Also, pytest functions are included as usage examples of each decorator.
"""decorators.log_call"""
import inspect
import logging
from datetime import datetime
from functools import wraps
DT_NAIVE = "%Y-%m-%d %I:%M:%S %p"
class LogCall:
@a-luna
a-luna / 00-jest-ts-svelte-setup.txt
Last active January 2, 2022 20:48
Whenever I start a new project, I need to figure out exactly which packages are required for testing svelte components with jest. I am documenting the list of packages and required config files here to avoid repeating this in the future.
npm i -D @babel/core @babel/preset-env @babel/preset-typescript @testing-library/jest-dom @testing-library/svelte @types/jest @types/node @types/testing-library__jest-dom babel-jest jest svelte-jester ts-jest ts-node typescript