Skip to content

Commit

Permalink
Merge pull request #79 from sciris/develop
Browse files Browse the repository at this point in the history
Update PyPI version
  • Loading branch information
RomeshA committed Feb 18, 2020
2 parents a7497e0 + 1af7166 commit bcae7d6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
48 changes: 31 additions & 17 deletions sciris/sc_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pickle
import types
import datetime
import importlib
import traceback
import numpy as np
from glob import glob
Expand Down Expand Up @@ -480,20 +481,29 @@ class Spreadsheet(Blobject):

def xlrd(self, *args, **kwargs):
''' Return a book as opened by xlrd '''
import xlrd # Optional import
try:
import xlrd # Optional import
except ModuleNotFoundError as e:
raise Exception('The "xlrd" Python package is not available; please install manually') from e
book = xlrd.open_workbook(file_contents=self.tofile().read(), *args, **kwargs)
return book

def openpyexcel(self, *args, **kwargs):
''' Return a book as opened by openpyexcel '''
import openpyexcel # Optional import
try:
import openpyexcel # Optional import
except ModuleNotFoundError as e:
raise Exception('The "openpyexcel" Python package is not available; please install manually') from e
self.tofile(output=False)
book = openpyexcel.load_workbook(self.bytes, *args, **kwargs) # This stream can be passed straight to openpyexcel
return book

def pandas(self, *args, **kwargs):
''' Return a book as opened by pandas '''
import pandas # Optional import
try:
import pandas # Optional import
except ModuleNotFoundError as e:
raise Exception('The "pandas" Python package is not available; please install manually') from e
self.tofile(output=False)
book = pandas.ExcelFile(self.bytes, *args, **kwargs)
return book
Expand Down Expand Up @@ -623,7 +633,10 @@ def loadspreadsheet(filename=None, folder=None, fileobj=None, sheetname=None, sh
'''
Load a spreadsheet as a list of lists or as a dataframe. Read from either a filename or a file object.
'''
import xlrd # Optional import
try:
import xlrd # Optional import
except ModuleNotFoundError as e:
raise Exception('The "xlrd" Python package is not available; please install manually') from e

# Handle inputs
if asdataframe is None: asdataframe = True
Expand Down Expand Up @@ -713,7 +726,10 @@ def savespreadsheet(filename=None, data=None, folder=None, sheetnames=None, clos
formatdata[0,:] = 'header' # Format header
sc.savespreadsheet(filename='test5.xlsx', data=testdata5, formats=formats, formatdata=formatdata)
'''
import xlsxwriter # Optional import
try:
import xlsxwriter # Optional import
except ModuleNotFoundError as e:
raise Exception('The "xlsxwriter" Python package is not available; please install manually') from e
fullpath = makefilepath(filename=filename, folder=folder, default='default.xlsx')
datadict = odict()
formatdict = odict()
Expand Down Expand Up @@ -855,25 +871,20 @@ class RobustUnpickler(pickle.Unpickler):
def __init__(self, tmpfile, fix_imports=True, encoding="latin1", errors="ignore"):
pickle.Unpickler.__init__(self, tmpfile, fix_imports=fix_imports, encoding=encoding, errors=errors)

def find_class(self, module_name, name, verbose=False):
def find_class(self, module_name, name, verbose=True):
try:
module = __import__(module_name)
module = importlib.import_module(module_name)
obj = getattr(module, name)
except:
try:
string = 'from %s import %s as obj' % (module_name, name)
exec(string)
except Exception as E:
if verbose: print('Unpickling warning: could not import %s.%s: %s' % (module_name, name, str(E)))
exception = traceback.format_exc() # Grab the trackback stack
obj = makefailed(module_name=module_name, name=name, error=E, exception=exception)
except Exception as E:
if verbose: print('Unpickling warning: could not import %s.%s: %s' % (module_name, name, str(E)))
exception = traceback.format_exc() # Grab the trackback stack
obj = makefailed(module_name=module_name, name=name, error=E, exception=exception)
return obj


def unpickler(string=None, filename=None, filestring=None, die=None, verbose=False):

if die is None: die = False

try: # Try pickle first
obj = pkl.loads(string) # Actually load it -- main usage case
except Exception as E1:
Expand Down Expand Up @@ -916,7 +927,10 @@ def savepickle(fileobj=None, obj=None):

def savedill(fileobj=None, obj=None):
''' Use dill to do the sour work '''
import dill # Optional Sciris dependency
try:
import dill # Optional Sciris dependency
except ModuleNotFoundError as e:
raise Exception('The "dill" Python package is not available; please install manually') from e
fileobj.write(dill.dumps(obj, protocol=-1))
return None

Expand Down
56 changes: 25 additions & 31 deletions sciris/sc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,10 @@ def suggest(user_input, valid_inputs, n=1, threshold=4, fulloutput=False, die=Fa
'Foo '
"""
import Levenshtein # To allow as an optional import
try:
import Levenshtein # To allow as an optional import
except ModuleNotFoundError as e:
raise Exception('The "Levenshtein" Python package is not available; please install manually') from e

valid_inputs = promotetolist(valid_inputs, objtype='string')

Expand Down Expand Up @@ -1511,7 +1514,7 @@ def suggest(user_input, valid_inputs, n=1, threshold=4, fulloutput=False, die=Fa
return suggestions[:n]


def profile(run, follow=None):
def profile(run, follow=None, *args, **kwargs):
'''
Profile a function.
Expand All @@ -1525,7 +1528,7 @@ def profile(run, follow=None):
Returns
-------
The profile report.
None (the profile output is printed to stdout)
Example
-------
Expand Down Expand Up @@ -1556,41 +1559,32 @@ def inner(self):
foo = Foo()
sc.profile(run=foo.outer, follow=[foo.outer, foo.inner])
sc.profile(slow_fn)
# Profile the constructor for Foo
f = lambda: Foo()
sc.profile(run=f, follow=[foo.__init__])
'''
import line_profiler as lp

# Handle the functions to follow
if follow is None:
follow = run

try:
from line_profiler import LineProfiler
except ModuleNotFoundError as e:
raise Exception('The "line_profiler" Python package is required to perform profiling') from e

lp = LineProfiler()
follow = promotetolist(follow)
for f in follow:
lp.add_function(f)
lp.enable_by_count()
wrapper = lp(run)

# Define the profiling
def do_profile(follow=None):
def inner(func):
def profiled_func(*args, **kwargs):
try:
profiler = lp.LineProfiler()
profiler.add_function(func)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
return profiled_func
return inner

# Do the profiling
print('Profiling...')
@do_profile(follow=follow) # Add decorator to runmodel function
def runwrapper():
run()
runwrapper()

wrapper(*args, **kwargs)
lp.print_stats()
print('Done.')



##############################################################################
### NESTED DICTIONARY FUNCTIONS
##############################################################################
Expand Down
4 changes: 2 additions & 2 deletions sciris/sc_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ['__version__', '__versiondate__', '__license__']

__version__ = '0.15.4'
__versiondate__ = '2020-01-28'
__version__ = '0.15.5'
__versiondate__ = '2020-02-17'
__license__ = 'Sciris %s (%s) -- (c) Sciris.org' % (__version__, __versiondate__)
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Get the current folder
cwd = os.path.abspath(os.path.dirname(__file__))

# Define the requirements and extras
# Define the requirements for core functionality
requirements = [
'matplotlib>=1.4.2', # Plotting
'numpy>=1.10.1', # Numerical functions
Expand All @@ -28,7 +28,8 @@
'xlsxwriter', # Spreadsheet output
'requests', # HTTP methods
'python-Levenshtein', # For fuzzy string matching
'line_profiler', # For function profiling
'line_profiler ; platform_system != "Windows"', # For the line profiler -- do not install on Windows
'colorama ; platform_system == "Windows"', # For colored text output -- only install on Windows
]

# Optionally define extras
Expand Down

0 comments on commit bcae7d6

Please sign in to comment.