Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/python
import re
import urllib
import urllib2
class Spreadsheet(object):
def __init__(self, key):
super(Spreadsheet, self).__init__()
@mtik00
mtik00 / sendmail2.4.py
Created June 9, 2015 15:53
Python 2.4-compliant sendmail function
def sendmail(subject, to, sender, body, mailserver, body_type="html", attachments=None, cc=None):
"""Send an email message using the specified mail server using Python's
standard `smtplib` library and some extras (e.g. attachments).
NOTE: This function has no authentication. It was written for a mail server
that already does sender/recipient validation.
WARNING: This is a non-streaming message system. You should not send large
files with this function!
@mtik00
mtik00 / wp_to_hugo.py
Created July 24, 2015 18:45
This script is used to convert a WordPress XML dump to Hugo-formatted posts.
#!/usr/bin/env python2.7
"""
This script is used to convert a WordPress XML dump to Hugo-formatted posts.
NOTE: The WP post data is kept as-is (probably HTML). It is not converted to
Markdown. This is to reduce the amount of "fixing" one has to do after the
data is converted (e.g. line endings, links, etc). This is generally not an
issue since Markdown allows HTML.
The post Metadata is converted to TOML.
@mtik00
mtik00 / usps.py
Created June 24, 2017 21:39
USPS Address Standardization
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This script shows an example of using `requests` and the USPS Address
Information API.
In order to use this, you must first register so you can get your USERID. Your
ID must be in the environment variable `USPS_USERID`.
For information on the API see here:
@mtik00
mtik00 / vcf2abx.py
Created June 25, 2017 18:00
Python script to convert a VCF file (address book) to a DYMO Address Book (ABX)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r'''
This script converts a vCard file to a Dymo ABX address book.
Usage:
vcf2abx <input VCF file> <output ABX file>
Example:
@mtik00
mtik00 / rotate.py
Created July 26, 2017 16:22
Script to rotate log files in a directory
#!/usr/bin/env python2.7
# coding: utf-8
'''
This script shows an example of rotating log files in a directory.
'''
# Imports #####################################################################
import os
import sys
import tarfile
@mtik00
mtik00 / thread_pool.py
Created November 14, 2018 17:39
Thread pool in Python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This sample script is an example use of the threading pool to process a
sequence of items.
This is both python2 and python3 compatible.
"""
from __future__ import print_function
def format_timespan(seconds: float) -> str:
"""Formats the number of seconds in h:m:s"""
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
return f"{hours:d}h:{minutes:02d}m:{seconds}s"
@mtik00
mtik00 / stdin-check.py
Last active June 13, 2023 18:21
read password from stdin
# You can check to see if someone ran your script with `echo -n test | myscript.py`.
# In that case, there's no tty attached. If there _is_ an attached tty, then you
# can just prompt the user like normal.
import sys
def read_secret_key():
if sys.stdin.isatty():
return getpass("Enter the secret key: ")
else:
return sys.stdin.readline()
@mtik00
mtik00 / array_join.sh
Last active December 27, 2022 20:12
Joining an array in Bash (like " ".join() in Python)
function array_join() {
# Join an array with a a separator character.
# WARNING: This only works in bash. You should also pass the array by
# reference. Example:
# my_array=( "one" "two" "three")
# joined=$( array_join my_array "|")
local -n arr=$1
local sep=${2:-" "}
printf -v result "%s${sep}" "${arr[@]}"