Skip to content

Instantly share code, notes, and snippets.

@StanGenchev
StanGenchev / valid_filename.py
Created April 22, 2021 11:44
A python function which converts a string into a valid filename string.
import re
def get_valid_filename(string: str) -> str:
"""
Return the given string converted to a string that can be used for a clean
filename. Remove leading and trailing spaces; convert other spaces to
underscores; and remove anything that is not an alphanumeric, dash,
underscore, or dot.
>>> get_valid_filename("Cold/Freezing Location (between 50-14 F)")
'ColdFreezing_Location_between_50-14_F'
@StanGenchev
StanGenchev / CustomSegmentedControl.swift
Last active March 5, 2021 13:35
CustomSegmentedControl for iOS in Swift 5. Rounded corners, custom colors, animations, shadow, etc.
//
// CustomSegmentedControl.swift
//
// Created by Stan Genchev on 2.03.21.
//
import UIKit
@IBDesignable
class CustomSegmentedControl: UIControl {
@StanGenchev
StanGenchev / example.py
Created November 15, 2020 18:24
Python GTK 3 example using Glade and Gtk.Builder
#!/usr/bin/env python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
UI_FILE = "example.ui"
@StanGenchev
StanGenchev / stage.py
Last active November 25, 2020 17:12
Python function to get stage name from git branch name.
# Note that this function assumes that you are in the parent directory of ".git"
from pathlib import Path
def get_active_stage(default: str = "dev") -> str:
"""
Gets active stage from the git branch name.
If name is unknown, it return "dev" or what you have passed as default.
"""
stages = {"master": "prod", "prod": "prod", "test": "test", "dev": "dev"}
stage = ""
@StanGenchev
StanGenchev / timezone_mapping.json
Last active December 4, 2020 17:20
Mapping of timezones (Sep. 2020). It maps canonical to canonical, alias to canonical and deprecated to canonical.
{
"Africa/Nairobi": "Africa/Nairobi",
"Africa/Asmera": "Africa/Nairobi",
"Africa/Asmara": "Africa/Nairobi",
"Africa/Abidjan": "Africa/Abidjan",
"Africa/Timbuktu": "Africa/Abidjan",
"America/Argentina/Catamarca": "America/Argentina/Catamarca",
"America/Argentina/ComodRivadavia": "America/Argentina/Catamarca",
"America/Adak": "America/Adak",
"America/Atka": "America/Adak",
@StanGenchev
StanGenchev / https_local_domain_centos_linux.md
Last active September 30, 2020 09:45
Generate a CA and domain certificate and add them to trusted in CentOS Linux, Red Hat Linux and Fedora.

Certificate authority (CA)

Do not use self-signed certificates in production !

Generate RootCA.pem, RootCA.key & RootCA.crt:

openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=<country>/CN=<name>-Root-CA"
openssl x509 -outform pem -in RootCA.pem -out RootCA.crt

Note: You have to replace with your country code ("US" for example) and with your organization name.

@StanGenchev
StanGenchev / template.yml
Created August 5, 2020 10:40
CloudFormation CodePipeline Conditional Action
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Parameters:
ProjectId:
Type: String
Stage:
Type: String
@StanGenchev
StanGenchev / emqx-cert-renew.sh
Created July 21, 2020 09:24
Share and keep Let's Encrypt/Certbot certificates synchronized with your EMQX service.
#!/bin/sh
set -e
for domain in $RENEWED_DOMAINS; do
case $domain in
your.site.com)
EMQX_CERT_ROOT=/etc/emqx/certs/your.site
# Make sure the certificate and private key files are
@StanGenchev
StanGenchev / get_scaled_resolution.py
Created June 21, 2020 16:44
Calculate resolution while preserving the aspect ratio with 'fit' effect in python.
def get_scaled_resolution(
self,
old_width: int = None,
old_height: int = None,
new_width: int = None,
new_height: int = None,
):
"""Returns the closest new resolution while preserving the aspect ratio.
You can specify new width, height or both.
It will automatically calculate the resolution.
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Parameters:
RootDomainName:
Description: Domain name for your website
Type: String
Mappings: