rsbox.misc

File: misc.py

Miscellaneous utils.

 1"""
 2File: misc.py 
 3-------------- 
 4Miscellaneous utils. 
 5""" 
 6
 7import pickle
 8from datetime import datetime
 9from pytz import timezone
10import requests
11import cloudpickle as cp
12from urllib.request import urlopen
13
14
15def timestamp():
16    """
17    Simple function that retrieves the current date and time
18    and returns a properly formatted string (i.e., a timestamp).  
19    """
20    now = datetime.now()
21    date_time = now.strftime("%-I-%M-%p-%b-%d-%Y")
22    return str(date_time)
23
24
25def pickle(filepath, object_to_pickle):
26	"""
27	Pickles object_to_pickle saved at 
28	filepath. filepath is a 'str' and
29	should end in '.pkl'.  
30	"""
31	out_file = open(filepath, "wb")
32	pickle.dump(object_to_pickle, out_file)
33	out_file.close()
34
35
36def unpickle(filepath):
37	"""
38	Takes in a path to a pickled 
39	.pkl file (type: 'str') and
40	returns the unpickled object. 
41	"""
42	in_file = open(filepath, 'rb')
43	loaded_object = pickle.load(in_file) 
44	return loaded_object
45
46
47def load_dataset(urlpath=None):
48	"""
49	Given a url to a .pkl dataset file,
50	loads and returns the dataset object. 
51	"""
52	if urlpath is None:
53		urlpath = r"https://stanford.edu/~rsikand/assets/datasets/mini_cifar.pkl"
54	
55	dataset = cp.load(urlopen(urlpath)) 
56	return dataset
def timestamp():
16def timestamp():
17    """
18    Simple function that retrieves the current date and time
19    and returns a properly formatted string (i.e., a timestamp).  
20    """
21    now = datetime.now()
22    date_time = now.strftime("%-I-%M-%p-%b-%d-%Y")
23    return str(date_time)

Simple function that retrieves the current date and time and returns a properly formatted string (i.e., a timestamp).

def pickle(filepath, object_to_pickle):
26def pickle(filepath, object_to_pickle):
27	"""
28	Pickles object_to_pickle saved at 
29	filepath. filepath is a 'str' and
30	should end in '.pkl'.  
31	"""
32	out_file = open(filepath, "wb")
33	pickle.dump(object_to_pickle, out_file)
34	out_file.close()

Pickles object_to_pickle saved at filepath. filepath is a 'str' and should end in '.pkl'.

def unpickle(filepath):
37def unpickle(filepath):
38	"""
39	Takes in a path to a pickled 
40	.pkl file (type: 'str') and
41	returns the unpickled object. 
42	"""
43	in_file = open(filepath, 'rb')
44	loaded_object = pickle.load(in_file) 
45	return loaded_object

Takes in a path to a pickled .pkl file (type: 'str') and returns the unpickled object.

def load_dataset(urlpath=None):
48def load_dataset(urlpath=None):
49	"""
50	Given a url to a .pkl dataset file,
51	loads and returns the dataset object. 
52	"""
53	if urlpath is None:
54		urlpath = r"https://stanford.edu/~rsikand/assets/datasets/mini_cifar.pkl"
55	
56	dataset = cp.load(urlopen(urlpath)) 
57	return dataset

Given a url to a .pkl dataset file, loads and returns the dataset object.