Skip to content

Instantly share code, notes, and snippets.

View Jwely's full-sized avatar
🏠
Working from home

Jeff Ely Jwely

🏠
Working from home
  • Amazon
  • DC Metro
View GitHub Profile
@Jwely
Jwely / minesweeper.py
Created December 12, 2018 23:51
Minesweeper implementation under 120 minutes
from typing import Tuple
import numpy as np
class MineSweeper(object):
def __init__(self, shape: Tuple, n_mines: int):
"""
/* create some database and administrator account, for example. */
create user dummy_admin;
alter user dummy_admin with password 'dummy_pass';
create database "dummy_dbase" owner dummy_admin;
\c "dummy_dbase"
create extension if not exists postgis;
create extension if not exists fuzzystrmatch;
create extension if not exists postgis_tiger_geocoder;
create extension if not exists postgis_topology;
\c postgres
@Jwely
Jwely / context_man.py
Last active June 13, 2017 19:29
context manager demo
from contextlib import contextmanager
@contextmanager
def manage_something(something):
""" this is the general structure of a contextmanager """
try:
# prepare the something
yield something
except Exception as e:
from queue import Queue
import threading
import os
from datetime import datetime
import time
def updater():
for i in range(10):
time.sleep(1)
@Jwely
Jwely / remove_png_alpha.py
Created May 20, 2016 17:56
Small script that "flattens" transparent png files by setting the alpha layer to a color.
from PIL import Image
class AmbiguousArgs(BaseException):
pass
def remove_png_alpha(image_path, overwrite=False, color=(255, 255, 255), out_path=None):
"""
Removes the alpha layer form an input png (probably works with other filetypes as well)
@Jwely
Jwely / download_ftp_tree.py
Last active March 4, 2024 11:39
recursive ftp directory downloader with python
import ftplib
import os
import re
"""
MIT license: 2017 - Jwely
Example usage:
``` python
import ftplib
@Jwely
Jwely / dynamic_butts.py
Created January 4, 2016 00:05
example of dynamically calling functions, even class instance methods, with key word arguments.
__author__ = 'Jwely'
class Butt():
def __init__(self):
self.times_farted = 0
def fart(self, times):
self.times_farted += times
@Jwely
Jwely / write_list_to_csv.py
Last active November 23, 2015 20:25
write_list_to_csv.py
# super barebones list to csv
list = ["some", "kind", "of", "data"]
with open("myfile.csv", "w+") as f:
for item in list:
f.write(str(item) + "\n")
# same deal but with lists of lists
list_of_lists = [["bob", "20"],
@Jwely
Jwely / rast_math.py
Last active October 13, 2022 17:20
An arcpy-like raster math function implemented with gdal
#!/usr/bin/env python
# in the input expression
#from osgeo import gdal, gdalconst, gdalnumeric
import gdal
import gdalconst
import gdalnumeric
import numpy
import os
import math # imported so it is available to the users expression evaluation
@Jwely
Jwely / masked_numpy_array.py
Last active October 13, 2015 18:21
syntax for numpy masked arrays
__author__ = 'Jwely'
import numpy
# in order to handle Nodata values, we must use floats, not integer datatypes.
array = numpy.array([0, 5, 6, 3, 0, 11, 5, 2, 3, 6, 4, 1, 2, 5, 12], "float32")
# mask this array, where all "0" values are masked
# see also: https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html
masked_array = numpy.ma.masked_equal(array, 0)