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
import ogr
__author__ = 'Jwely'
class Shape:
"""
Helps read data from shapefiles with ogr
"""
# quick overview of function definitions and function calls.
import some_module
# this below is a function definition, use it to generalize code to repeatedly do something.
# dnppy is full of function definitions
def my_function(argument1, argument2, default_argument3="some_default_string")
"""
this is a function DEFINITION , and this docstring should tell you
something about how it works. This docstring will be displayed if someone
@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)
@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 / 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 / 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 / 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 / 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)
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 / 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: