Skip to content

Instantly share code, notes, and snippets.

@monkut
monkut / hexcolor2grayscale.py
Created February 17, 2015 05:45
Used this script to minimalize osm-bright mml colors to grayscale for creating a data base layer map in tilemill.
"""
Script to replace color #RRGGBB formated values to grayscale values inplace.
usage:
python hexcolor2grayscale.py filename1 filename2
"""
import fileinput
import sys
import re
import colorsys
@monkut
monkut / backends.py
Last active March 12, 2018 13:30
django (1.8) python3 LDAP (via ldap3) Authentication backend
"""
django (1.8) python3 LDAP (via ldap3) Authentication backend
Assumes 'AUTH_INFORMATION' is defined in settings.py in the form:
AUTH_INFORMATION = { 'LDAP'{
'USERNAME_SEARCH_FILTER': <ldap search filter, for example, '(uid={})'>,
'HOST': <ldap server host>,
'PORT': <ldap server port (integer)>,
'BASE_DN': <ldap base DN>,
@monkut
monkut / python_welfords_class.py
Last active February 6, 2018 00:42
Sample implementation and sample usage of Welford's method for calculating running variance (and standard aggregations)
"""
A class for managing running calculations for:
- min
- average
- max
- stddev
- var
- count
Example Usage:
from math import sqrt
class WelfordRunningVariance(object):
"""
Python implentation of Welford's running variance algorithm
http://www.johndcook.com/standard_deviation.html
"""
def __init__(self):
self._count = 0
self._mean = None
@monkut
monkut / nginx_uwsgi_application_setup.md
Created November 22, 2015 06:44
(ubuntu 14.04) uwsgi アプリとnginxの基本設定

uwsgi アプリとnginxの基本設定

長い間、pythonでウエブアプリを使う場合、apache+mod_wsgiを使ってきました。 この数年でnginxが早いと評判となってきて、apacheとの設定違いはきになっていました。

去年、自分が作ったウエブアプリのrequests/秒が上げたくて、apache用のmod_wsgiの代わりに、インストールしやすいpython用のウエブサーバ探したら、 uwsgiを見つけました。uwsgiで同じアプリ試したところ、apache+mod_wsgiより早い結果が出ました。

次は、virturalenv/venvを使った開発のながれです。 これも、数年前から動きは、聞きましたが、なかなかその必要なユースケースがなく、virutalenvの使い方を勉強しなかったんですが、

Anaconda(ウインドウズ)のインストールでパッケージを更新する方法

  1. プロクシーを設定

> ウインドウズ用のプロクシーを通すため、https://github.com/heupel/ntlmaps を使っています

# ポートは、NTLMAPS内で設定
@monkut
monkut / argparse_date_datetime_custom_types.py
Last active May 10, 2023 15:47
Custom date/datetime type validators for python's argparse module
def valid_date_type(arg_date_str):
"""custom argparse *date* type for user dates values given from the command line"""
try:
return datetime.datetime.strptime(arg_date_str, "%Y-%m-%d")
except ValueError:
msg = "Given Date ({0}) not valid! Expected format, YYYY-MM-DD!".format(arg_date_str)
raise argparse.ArgumentTypeError(msg)
def valid_datetime_type(arg_datetime_str):
"""custom argparse type for user datetime values given from the command line"""
@monkut
monkut / hls_legend_scaling.py
Last active March 11, 2021 09:29
HLS Color Scaling for legends
from colorsys import rgb_to_hls, hls_to_rgb
class ScaledFloatColorManager:
"""
Intended to provide a scaled legend between to colors in the HSL space
"""
def __init__(self, name, minimum_value, maximum_value, hex_color_min="66b219", hex_color_max="cc0000", display_band_count=6):
self.name = name
Tool Creation Guidelines for High Performance Teams
The objective of these guidelines are to clearly identify what is desired of a tool for use in fast moving teams with clear objectives.
- Minimize necessary user actions/input