Skip to content

Instantly share code, notes, and snippets.

Travis travis23

Block or report user

Report or block travis23

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@travis23
travis23 / advice.md
Last active Jun 20, 2019
[advice] #advice
View advice.md
@travis23
travis23 / mplcursors_demo.py
Last active Jun 2, 2019
[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
View mplcursors_demo.py
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 / levels_to_step_plot.py
Last active Jun 2, 2019
[levels_to_step_plot] #python #plot #step_plot
View levels_to_step_plot.py
import numpy as np
def levels_to_steps(level_list):
"""
Convert list of 2-element tuples that contain duration and level data (x, y) to 1d x,y numpy arrays.
The arrays plotted give a step plot.
Args:
level_list (list of tuples with two number elements): [(x1, y1), (x2, y2), ...]
@travis23
travis23 / simple_logging.py
Created Jan 12, 2019
[Simple Logging] #python #logging
View simple_logging.py
import logging
import logging.handlers
logger = logging.getLogger()
# handler = logging.handlers.WatchedFileHandler(path_log)
handler = logging.handlers.StreamHandler()
# formatter = logging.Formatter(logging.BASIC_FORMAT)
formatter = logging.Formatter('{levelname}:{message}', style='{')
handler.setFormatter(formatter)
logger.addHandler(handler)
@travis23
travis23 / pytest_tmpdir_factory.py
Created Jan 12, 2019
[pytest tmpdir_factory] a demonstration of using a pytest temporary directory #pytest #python #directory
View pytest_tmpdir_factory.py
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 Jan 21, 2019
[universal directory] ways to find home directory and other common directories on different machines and operating systems #python #directory #os
View universal_directory.py
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 Jun 1, 2019
[filename pattern matching] #python #filename #fnmatch
View filename_pattern_matching.py
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 / get_filename_from_path.py
Created Jan 11, 2019
[get filename from path] #python #filename #ntpath #path
View get_filename_from_path.py
# 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 / files_in_directory.py
Created Jan 11, 2019
[files in directory] get a list of all the files in a directory #os #filename #python
View files_in_directory.py
# 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 / current_time_with_pendulum.py
Created Jan 11, 2019
[current time with pendulum] #pendulum #now #time #python
View current_time_with_pendulum.py
import pendulum
now = pendulum.now()
now.format('YYYYMMDD_HHmm')
You can’t perform that action at this time.