Skip to content

Instantly share code, notes, and snippets.


Aaron Luna a-luna

View GitHub Profile
a-luna /
Last active Mar 24, 2020
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.
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 /
Last active Sep 4, 2019
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 /
Last active Jul 29, 2019
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 / ConsoleProgressBar.cs
Last active Apr 3, 2018 — 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.
View ConsoleProgressBar.cs
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 / NetworkUtilities.cs
Last active Aug 14, 2020
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.
View NetworkUtilities.cs
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 / TplSocketExample.cs
Last active Jun 27, 2019
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.
View TplSocketExample.cs
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 /
Last active Sep 3, 2020
Shell Script: Build NGINX from Source with Third Party Modules (Cache Purge, GeoIP2), Completely Non-Interactive, No User Input Required
#!/bin/bash -e
# Author: Aaron Luna
# Website:
# 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:
You can’t perform that action at this time.