Skip to content

Instantly share code, notes, and snippets.

View AlmightyOatmeal's full-sized avatar

Cat AlmightyOatmeal

View GitHub Profile
@AlmightyOatmeal
AlmightyOatmeal / Python.ConvertNumberToHumanReadableString.py
Created April 25, 2018 18:40
Convert number to human-readable string based on locale. Additional kwargs passed to locale.format().
import locale
def convert_number_to_human_readable_string(data, fmt=None, grouping=True, **kwargs):
"""Convert number to human-readable string based on locale. Additional kwargs passed to locale.format().
:param data: Some kind of number.
:type data: int, float, long
:param fmt: (optional) Number format. (default: None)
:param grouping: (optional) Take grouping into account. (default: True)
:type grouping: bool
@AlmightyOatmeal
AlmightyOatmeal / Python.Logging.Dictionary-or-JSON.py
Last active February 12, 2020 01:55
Python logging example from dictionary (or loaded JSON) featuring formatting that includes the local timezone, custom log format, logging to both console AND file, and automatically rotating log files.
import logging.config
import time
TZ_NAME = time.tzname[0] if not time.localtime().tm_isdst else time.tzname[1]
# Alternatively, convert the localtime to UTC/GMT but if you use this then update
# the logging formatter because this example is using the local timezone.
# logging_handler.formatter.converter = time.gmtime
# This is kept separate as it can be used to apply a default log level to your
# loggers otherwise you can use a method to change this to debug and apply the
@AlmightyOatmeal
AlmightyOatmeal / FreeBSD Boinc Client - Transient HTTP error.md
Last active April 25, 2018 20:57
After noticing boinc was not active for days, all I was receiving was an ambitious "transient HTTP error" message.

Problem: boinc is not running any tasks and logs show errors like:

25-Apr-2018 13:33:27 [World Community Grid] Temporarily failed upload of MCM1_0141526_9856_0_r1232334159_0: transient HTTP error

(that's not very helpful...)

Finding additional information:

Adding debug information to the cc_config.xml

@AlmightyOatmeal
AlmightyOatmeal / SignalFx.Kubernetes.SmartAgent.Example.md
Last active June 21, 2018 20:12
Example of setting-up the SignalFx Agent in Kubernetes 1.10.2 on CentOS 7.4.

SignalFx SmartAgent Kubernetes Example

 ____ ___ ____   ____ _        _    ___ __  __ _____ ____  
|  _ \_ _/ ___| / ___| |      / \  |_ _|  \/  | ____|  _ \ 
| | | | |\___ \| |   | |     / _ \  | || |\/| |  _| | |_) |
| |_| | | ___) | |___| |___ / ___ \ | || |  | | |___|  _ < 
|____/___|____/ \____|_____/_/   \_\___|_|  |_|_____|_| \_\

This is meant as one real-world example of setting up the SignalFx SmartAgent and is not intended to be a replacement for the official documentation. Please make sure that you have read the Kubernetes setup instructions available at https://github.com/signalfx/signalfx-agent/blob/master/docs/kubernetes-setup.md. I am not responsible if you don't read the documentation, you screw something up, or something spontaneously combusts.

@AlmightyOatmeal
AlmightyOatmeal / Kubernetes.Commands.Troubleshooting.md
Last active November 17, 2020 00:56
Useful commands for troubleshooting Kubernetes issues and their respective output
@AlmightyOatmeal
AlmightyOatmeal / python.Files.Gzip.py
Last active May 8, 2018 22:43
Fun with Python and Gzipped files! Works on Python 2.7.x.
import json
import gzip
import logging
import os
logger = logging.getLogger(os.path.splitext(os.path.basename(__file__))[0])
def write_gzip_file(path, data, overwrite_existing=False, compression_level=9):
@AlmightyOatmeal
AlmightyOatmeal / python.Files.UTF8.py
Last active May 8, 2018 22:43
Fun with Python and UTF-8 files! Works on Python 2.7.x.
import codecs
import logging
import os
logger = logging.getLogger(os.path.splitext(os.path.basename(__file__))[0])
# NOTE: Python 3.x doesn't have a `unicode()` built-in function.
@AlmightyOatmeal
AlmightyOatmeal / python.datetime.fun.py
Last active May 8, 2018 23:02
Fun with Python datetime objects! Works on Python 2.7.x.
import datetime
import pytz
def convert_dt_to_milliseconds(dt_obj):
"""Convert datetime object to a Unix epoch timestamp in milliseconds.
:param dt_obj: Datetime object to be converted.
:type dt_obj: instance
:return: Milliseconds since the Unix epoch.
@AlmightyOatmeal
AlmightyOatmeal / python.locale.py
Last active May 8, 2018 23:03
Fun with Python and locale! Works on Python 2.7.x.
import locale
import logging
import os
logger = logging.getLogger(os.path.splitext(os.path.basename(__file__))[0])
def set_locale(desired_locale=('en_US', 'UTF-8')):
"""Set the local for proper formatting.
@AlmightyOatmeal
AlmightyOatmeal / python.dict.fun.py
Last active May 8, 2018 22:41
Fun with Python dictionaries! Works on Python 2.7.x.
def flatten(obj, parent_key=None, sep='.'):
"""Flattens multi-level dictionary to a single-level dictionary.
:param obj: Dictionary object to flatten.
:type obj: dict
:param parent_key: (optional) Prefix for the flattened key. (default: None)
:type parent_key: basestring, str, or unicode
:param sep: (optional) Separator for the nested key names. (default: '.')
:type sep: basestring, str, or unicode
:return: Sexy flattened dictionary.