Skip to content

Instantly share code, notes, and snippets.

Avatar

Otto Wagner caffeine-potent

View GitHub Profile
@caffeine-potent
caffeine-potent / export_dc_xarray_to_netcdf.py
Created Apr 1, 2019
Exporting an xarray with datacube attributes
View export_dc_xarray_to_netcdf.py
import datacube
import xarray as xr
def export_xarray_to_netcdf(ds:xr.Dataset, path:str):
"""
Exports an xarray.Dataset as a single NetCDF file.
Parameters
----------
ds: xarray.Dataset
The Dataset to export.
View flatten_tuples_and_lists.py
from functools import reduce
flatten = lambda lst: reduce(lambda l, i: l + flatten(i) if isinstance(i, (list, tuple)) else l + [i], lst, [])
test = [[[[1,0],1],0],1]
print(flatten(test))
## Credit https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
View permutation_matrix.py
import numpy as np
def create_n_depth_matrix(n: int) -> np.array:
"""Function used to generate all binary permutations of length n.
Binary permutations of n=3 for example would include:
[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]
Binary permuataions are arranged in a 3d matrix of shape (n, a, b)
such that a*b == (2^n)
View expense_report_lodging.py
def calculate_taxed_ammount( spent:float, tax_rate:float):
cost = spent / (1 + tax_rate)
taxed = spent - cost
print("Spent: {}\nCost: {}\nTaxed: {}".format(spent, cost, taxed))
calculate_taxed_ammount( spent = 171.376666667, tax_rate = .10)
View ContentAccomplishedHashmaps.py
###############
spent = 171.376666667
tax_rate = .10
###############
cost = spent / (1 + tax_rate)
taxed = spent - cost
@caffeine-potent
caffeine-potent / quicksort.j
Created Mar 7, 2018
Quicksort from BabySnake#6314
View quicksort.j
quicksort=: (($:@(<#[), (=#[), $:@(>#[)) ({~ ?@#)) ^: (1<#)
List =: ?. 10 $100
echo quicksort List
@caffeine-potent
caffeine-potent / list_comp.py
Last active Jan 28, 2018
List comprehension with depth = 2
View list_comp.py
#
# Solution to a question posed in https://www.reddit.com/r/learnpython/comments/72ll3i/list_comprehension_help/
#
# def gen_function_that_can_be_one_lined(my_list):
# for i, x in enumerate(my_list):
# for y in x:
# yield ( i, y)
#
#List
@caffeine-potent
caffeine-potent / geom_mediod.py
Last active Jul 30, 2021
Geometric Median in one line of native python.
View geom_mediod.py
from scipy.spatial.distance import euclidean
points = [
[1,3],
[2,4],
[3,3],
[4,5],
[3,7],
[5,1],
[5,3],
@caffeine-potent
caffeine-potent / stretch.py
Created Jul 13, 2017
Scraping for performance improvements without the use of C or CYTHON. Stretching an array for an image resolution matching issue.
View stretch.py
def discrete_stretch(array, size):
""" Expands 2d array by some factor.
For example the following array is expanded by a factor of 3
[ 1 2 ]
[ 3 4 ]
[ 1 1 1 2 2 2]
[ 1 1 1 2 2 2]
[ 1 1 1 2 2 2]
@caffeine-potent
caffeine-potent / extending_dict_some_more.py
Created Feb 23, 2017
Extending Dict to Ask for user input.
View extending_dict_some_more.py
class info(dict):
def add_item(self, item_name, question, type = None):
answer = raw_input(question)
if type is not None:
answer = type(answer)
self[item_name] = answer
a = info()
a.add_item("name", "What is your name? ")
a.add_item("quest", "What is your quest? ")