- Here is a list
- Context timer timer.py
Last active
November 1, 2016 17:05
-
-
Save phaustin/81812fb20bfb6036132c to your computer and use it in GitHub Desktop.
Python snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
test docstring | |
https://docs.python.org/3/library/argparse.html#nargs | |
""" | |
import argparse, textwrap | |
if __name__ == '__main__': | |
linebreaks=argparse.RawTextHelpFormatter | |
descrip=textwrap.dedent(globals()['__doc__']) | |
parser = argparse.ArgumentParser(formatter_class=linebreaks,description=descrip) | |
parser.add_argument('files',nargs='*',type=str,help='filelist') | |
#parser.add_argument('outdir', nargs=1,type=str,help='output directory') | |
parser.add_argument("--verbose", help="increase output verbosity", | |
action="store_true") | |
#--verbose will set args.verbose=True | |
parser.add_argument("-y", "--year", type=str, choices=['2013','2014'], | |
help="fiscal year in database name") | |
args=parser.parse_args() | |
print('args.files: ',args.files) | |
print('args.year ',args.year) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from mpl_toolkits.basemap import Basemap | |
from matplotlib import cm | |
from matplotlib.colors import Normalize | |
import numpy.ma as ma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# useful Python snippet | |
import contextlib,time | |
@contextlib.contextmanager | |
def timeit(): | |
t=time.time() | |
yield | |
print(time.time()-t,"sec") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
comment=''.join([i if ord(i) < 128 else ' ' for i in comment]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
end=dt.datetime(end_year-1,12,31) + dt.timedelta(days=end_day) | |
start_date=start.strftime('%Y-%m-%d') | |
end_date=end.strftime('%Y-%m-%d') | |
f.attrs['created_on']=time.strftime("%c") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
except TypeError as ex: | |
ex_type, ex_val, tb = sys.exc_info() | |
print('bummer: ',ex_val) | |
print('\nhere is the traceback:\n') | |
traceback.print_tb(tb) | |
print("python3 reads band_names as bytes, won't split on comma") | |
using loggin | |
#https://realpython.com/blog/python/the-most-diabolical-python-antipattern/ | |
#!/usr/bin/env python3 | |
from __future__ import division,print_function | |
import traceback,sys,logging | |
logging.basicConfig(filename='example.log',level=logging.DEBUG) | |
def get_number(): | |
return int('foo') | |
def log_traceback2(ex, ex_traceback=None): | |
#works under 2.7 and 3.4 | |
## if ex_traceback is None: | |
## ex_traceback = ex.__traceback__ | |
## tb_lines = [ line.rstrip('\n') for line in | |
## traceback.format_exception(ex.__class__, ex, ex_traceback)] | |
#print('here is exception: 2.7 -- ',tb_lines) | |
logging.exception(ex) | |
def log_traceback3(ex): | |
#3.4 only | |
tb_lines = traceback.format_exception(ex.__class__, ex, ex.__traceback__) | |
tb_text = ''.join(tb_lines) | |
# I'll let you implement the ExceptionLogger class, | |
# and the timestamping. | |
## print('here is the exception: 3.4 -- ',tb_text) | |
## print('now with logger') | |
logging.exception(ex) | |
try: | |
x = get_number() | |
except Exception as ex: | |
if sys.version > '3': | |
log_traceback3(ex) | |
else: | |
log_traceback2(ex) | |
and | |
pdb.set_trace() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import Image | |
EmbedNorth = Image('http://oceancolor.gsfc.nasa.gov/cmsdocs/product_definitions/L3Bins/L3Bins.north.png',width=500) | |
prob6_9 = Image('figures/prob6_9.png',width=500) | |
prob6_28 = Image('figures/prob6_28.png',width=500) | |
cp ~/Documents/Snagit/prob6_28.png $ecode/notebooks/figures/. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://stackoverflow.com/questions/16074376/how-do-you-get-the-python-profiler-to-work | |
import cProfile, pstats | |
pr = cProfile.Profile() | |
pr.enable() | |
... | |
pr.disable() | |
f = open('x.prof', 'a') | |
sortby = 'cumulative' | |
pstats.Stats(pr, stream=f).strip_dirs().sort_stats(sortby).print_stats() | |
f.close() | |
#using a contextlib | |
#http://yt-project.org/doc/_modules/yt/funcs.html | |
@contextlib.contextmanager | |
def parallel_profile(prefix): | |
r"""A context manager for profiling parallel code execution using cProfile | |
This is a simple context manager that automatically profiles the execution | |
of a snippet of code. | |
Parameters | |
---------- | |
prefix : string | |
A string name to prefix outputs with. | |
Examples | |
-------- | |
>>> with parallel_profile('my_profile'): | |
... yt.PhasePlot(ds.all_data(), 'density', 'temperature', 'cell_mass') | |
""" | |
import cProfile | |
from yt.config import ytcfg | |
fn = "%s_%04i_%04i.cprof" % (prefix, | |
ytcfg.getint("yt", "__topcomm_parallel_size"), | |
ytcfg.getint("yt", "__topcomm_parallel_rank")) | |
p = cProfile.Profile() | |
p.enable() | |
yield fn | |
p.disable() | |
p.dump_stats(fn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import errno | |
try: | |
os.makedirs(the_dir) | |
except OSError, e: | |
if e.errno == errno.EEXIST: | |
pass #not a problem if file exists | |
or | |
if not os.path.exists('output'): | |
os.mkdir('output') | |
if not os.path.exists('hdf5'): | |
os.mkdir('hdf5') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
from mpl_toolkits.basemap import Basemap | |
import numpy.ma as ma | |
plotdir='{}/{}'.format(os.getcwd(),'plots') | |
if not os.path.exists(plotdir): | |
os.makedirs(plotdir) | |
fig, ax = plt.subplots(1, 1, figsize = (12, 12) ) | |
out = ax.plot(small_lons, small_lats, 'b+') | |
ax.set_title() | |
from matplotlib import cm | |
from matplotlib.colors import Normalize | |
import seaborn as sns | |
cmap=cm.YlGn #see http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps | |
cmap.set_over('r') | |
cmap.set_under('b') | |
cmap.set_bad('0.75') #75% grey | |
vmin= -1.5 | |
vmax= 1.5 | |
# | |
# tell the colormap what the maximum and minimum | |
# values are | |
# | |
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False) | |
chlor=ma.array(chlor,mask=np.isnan(chlor)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import h5py | |
import pandas as pd | |
import pandas as pd | |
with h5py.File(binned_file,'r') as infile: | |
root_key=infile.keys()[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
def days_hours_minutes(td): | |
days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60 | |
return days,hours,minutes | |
hours=timedelta(hours=3.4512) | |
print(days_hours_minutes(hours)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contexttimer import Timer | |
with Timer() as t: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#python3 remove all non-ascii characters and replace with _ | |
>>> text=text + 'αβγιορ' | |
>>> text | |
'a star is bornαβγιορ' | |
>>> ''.join([i if ord(i) < 128 else '_' for i in text]) | |
'a star is born______' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment