Skip to content

Instantly share code, notes, and snippets.

View travis23's full-sized avatar

Travis Niederhauser travis23

View GitHub Profile
@travis23
travis23 / current_time_with_pendulum.py
Created January 11, 2019 14:14
[current time with pendulum] #pendulum #now #time #python
import pendulum
now = pendulum.now()
now.format('YYYYMMDD_HHmm')
@travis23
travis23 / files_in_directory.py
Created January 11, 2019 14:18
[files in directory] get a list of all the files in a directory #os #filename #python
# See
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
@travis23
travis23 / get_filename_from_path.py
Created January 11, 2019 14:26
[get filename from path] #python #filename #ntpath #path
# See
# https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format
import ntpath
path_to_file = r"C:\a\b\s\d\f_a\aasdas_o_g.json"
print(ntpath.basename(path_to_file)
# Output:
@travis23
travis23 / pytest_tmpdir_factory.py
Created January 12, 2019 01:03
[pytest tmpdir_factory] a demonstration of using a pytest temporary directory #pytest #python #directory
import json
import os
import pytest as pt
# See Python Testing with pytest. > Chapter 4 > Using tmpdir and tmpdir_factory
class TestStuff(object):
@pt.fixture(scope='class')
@travis23
travis23 / universal_directory.py
Last active January 21, 2019 20:55
[universal directory] ways to find home directory and other common directories on different machines and operating systems #python #directory #os
import os
import pathlib
# 1
# -------------------------------------------------
os.environ["ALLUSERSPROFILE"]
# The environment variable "ALLUSERSPROFILE" is available on Windows only.
# On Windows returns
# 'C:\\ProgramData'
@travis23
travis23 / filename_pattern_matching.py
Last active June 1, 2019 23:28
[filename pattern matching] #python #filename #fnmatch
import fnmatch
pattern = "aaa_*" # Special characters '*' and '?'
list_of_all_files = ['aaa_1.json', 'a.json', 'b.json']
for filename in list_of_all_files:
if fnmatch.fnmatch(filename, pattern):
print(filename)
@travis23
travis23 / mplcursors_demo.py
Last active June 2, 2019 02:10
[matplotlib mplcursors demo] mplcursors provides a very easy way to add hover text capability to matplotlib #matplotlib #mplcursors #window_title #GridSpec #layout #tooltip #hover_text #annotation #tick_labels
import matplotlib.pyplot as plt
from matplotlib import gridspec
import mplcursors
annotations0 = ['cat', 'dog', 'hare']
x = [1, 2, 3]
y = [0, 2.5, 3]
fontsize=20
@travis23
travis23 / find_index_of_first_nonzero_occurrence.py
Last active June 27, 2019 21:26
[Find Index of First Non-zero Occurrence] #numpy #nonzero
import numpy as np
def find_index_of_first_nonzero_occurrence(array):
"""
Find the index of the first non-zero occurrence in a numpy array.
This is essentially a wrapper around np.flatnonzero(). If there are
no non-zero values None is returned.
array (numpy array): Should be 1d array and all elements should be numeric
"""
@travis23
travis23 / sort_matched_arrays.py
Last active August 8, 2019 01:51
[sort matched arrays pairs] #python #numpy #argsort #unique #timeseries #monotonic #strictly_increasing
# Sometimes timeseries need to be concatenated and then sorted so that the time vector is monotonic.
# ---- Monotonic in x, but not strictly increasing
# ********************************************************************************
x1 = np.array([1, 2, 3, 4, 4]) # Not all of the values are unique
y1 = np.array([10, 20, 30, 42, 40])
x2 = np.array([1.5, 2.5, 3.5, 4.5])
y2 = np.array([15, 25, 35, 45])
x_ = np.concatenate((x1, x2))
@travis23
travis23 / change_tab_title.ipynb
Created August 29, 2019 23:46
[JupyterLab Change Tab Title] #jupyterlab
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.