def test_tool_get_path_name2(): '''Verify returns path name correctly, for a dashboard; pretend "dash_".''' path = ut.get_path(filename=RANDOMCHARS, suffix='.csv', dashboard=True) actual1 = path.endswith('.'.join([RANDOMCHARS, 'csv'])) actual2 = os.path.basename(path).startswith('dash_') nt.assert_true(actual1) nt.assert_true(actual2)
def test_tool_get_path_name4(): '''Verify returns path name correctly, for a dashboard; no "dash_" for xlsx.''' # The following should inform dashboards are not separate files. path = ut.get_path(filename=RANDOMCHARS, suffix='.xlsx', dashboard=True) actual1 = path.endswith('.'.join([RANDOMCHARS, 'xlsx'])) actual2 = os.path.basename(path).startswith('dash_') nt.assert_true(actual1) nt.assert_false(actual2)
def test_tool_get_path_warn3(): '''Verify correct path for no filename; uses default path.''' actual = ut.get_path(dashboard=True) # should raise a logger warning expected = ut.get_path() # default path nt.assert_equals(actual, expected)
def test_tool_get_path_warn2(): '''Verify correct path for no filename; uses default path.''' actual = ut.get_path(suffix='.csv') # should raise a logger warning expected = ut.get_path() # default path nt.assert_equals(actual, expected)
def test_tool_get_path_warn1(): '''Verify correct path for no extension; no suffix kwarg.''' path = ut.get_path(filename=RANDOMCHARS) # should raise a logger warning actual = path.endswith(RANDOMCHARS) nt.assert_true(actual)
def test_tool_get_path_name5(): '''Verify overwrite protected filepath is returned if overwrite is False.''' # Just testing that it's triggered; not that it increments. path = ut.get_path(filename=RANDOMCHARS, suffix='.xlsx', overwrite=False) actual = path.endswith('.'.join([RANDOMCHARS, 'xlsx'])) nt.assert_true(actual)
def test_tool_get_path_name3(): '''Verify returns path name correctly.''' path = ut.get_path(filename=RANDOMCHARS, suffix='.xlsx') actual = path.endswith('.'.join([RANDOMCHARS, 'xlsx'])) nt.assert_true(actual)
def test_tool_get_path_default(): '''Verify returns default export path if no args.''' path = ut.get_path() actual = path.endswith(os.path.join('lamana', 'export')) nt.assert_true(actual)
def to_xlsx(self, filename=None, offset=3, temp=False, overwrite=False, prefix=None, keepname=True, order=None, delete=False): '''Write all LaminateModels and FeatureInput dashboards as one file. Returns ------- str Path name of the written file. See Also -------- - `utils.tools.export`: for comparable kwargs and docstring. - `utils.tools.reorder_featureinput`: for order of dashboard data Notes ----- All files for a given case are written to one file. This is a reimplementation of the pandas `to_excel` method. This is separated from `export` due to how sheets are added in the ExcelWriter. ''' if filename is None: filename = 'case_LaminateModels' if prefix is None: prefix = '' suffix = '.xlsx' if temp: data_des, workbook_filepath = tempfile.mkstemp(suffix=suffix) else: workbook_filepath = ut.get_path(filename, suffix=suffix, overwrite=overwrite) try: # Excel code block ---------------------------------------------------- writer = pd.ExcelWriter(workbook_filepath) for LM in self.LMs: # Parse LaminateModel variables ##nplies = LM.nplies ##p = LM.p # TODO: Fix units ##t_total = LM.total * 1e3 # (in mm) geo_string = LM.Geometry.string FI = LM.FeatureInput data_df = LM.LMFrame converted_FI = ut.convert_featureinput(FI) reordered_FI = ut.reorder_featureinput(converted_FI, order) # Data sheet sheetname = geo_string.replace('[', '|').replace(']', '|') data_sheetname = ' '.join(['Data', sheetname]) data_df.to_excel(writer, data_sheetname) # Dashboard sheet dash_sheetname = ' '.join(['Dash', sheetname]) for i, dict_df in enumerate(reordered_FI.values()): if dict_df.size == 1: # assumes string strs are ordered first dict_df.to_excel(writer, dash_sheetname, startrow=4**i) else: dict_df.to_excel(writer, dash_sheetname, startcol=(i-1)*offset) writer.save() if temp: os.close(data_des) if temp and keepname: workbook_filepath = ut.rename_tempfile( workbook_filepath, ''.join(['t_', filename, suffix])) logging.info('Data and dashboard written as {} file in: {}'.format( suffix, workbook_filepath)) finally: if delete: os.remove(workbook_filepath) logging.info('File has been deleted: {}'.format(workbook_filepath)) pass return workbook_filepath