def test_dailySumStyle(self): ''' Test the method MistakeSummary.mstkSumStyle. ''' wb = Workbook() ws = wb.active tf = TradeFormat(wb) numTrades = 5 ms = MistakeSummary(numTrades) ms.dailySumStyle(ws, tf) dispath = "out/SCHNOrK.xlsx" if os.path.exists(dispath): os.remove(dispath) wb.save(dispath) anchor = (1, ms.anchor[1] + ms.numTrades + 5) wb2 = load_workbook(dispath) ws2 = wb2.active # test that each listed item in ms.mistakeFields has a corresponding style set in the # appropriate cell in the re-opened workbook. for x in ms.dailySummaryFields: cell = '' entry = ms.dailySummaryFields[x] if isinstance(entry[0], list): cell = tcell(entry[0][0], anchor=anchor) else: cell = tcell(entry[0], anchor=anchor) self.assertEqual(entry[1], ws2[cell].style) # Get the entries with the static values-- the headers if len(entry) == 3: self.assertEqual(ws2[cell].value, ms.dailySummaryFields[x][2]) os.remove(dispath)
def test_mstkSumStyles(self): ''' Test the method MistakeSummary.mstkSumStyle. ''' wb = Workbook() ws = wb.active tf = TradeFormat(wb) ms = MistakeSummary(5) ms.mstkSumStyle(ws, tf) dispath = "out/SCHNOrK.xlsx" if os.path.exists(dispath): os.remove(dispath) wb.save(dispath) wb2 = load_workbook(dispath) ws2 = wb2.active # test that each listed item in ms.mistakeFields has a corresponding style set in the # appropriate cell in the re-opened workbook. for x in ms.mistakeFields: cell = '' entry = ms.mistakeFields[x] if isinstance(entry[0], list): cell = tcell(entry[0][0]) else: cell = tcell(entry[0]) # print(cell, entry[1], ws[cell].style) self.assertEqual(entry[1], ws[cell].style) if len(entry) == 3: self.assertEqual(ws2[cell].value, ms.mistakeFields[x][2]) os.remove(dispath)
def test_populateXLDailySummaryForm(self): ''' Test the method populateXLDailySummaryForm. Specifically test that given a specific anchor, 12 specific spots are written to by default. ''' for jf, lf, df, ldf in zip(self.jfs, self.lfs, self.dframes, self.ldfs): NUMROWS = 6 mstkAnchor = (3, 3) # arbitrary mistake = MistakeSummary(numTrades=len(ldf), anchor=mstkAnchor) t = ExportToExcel(lf.ts, jf, df) wb = Workbook() t.populateXLDailySummaryForm(mistake, wb.active, mstkAnchor) # t.saveXL(wb, self.jf) anchor = (mstkAnchor[0], mstkAnchor[1] + len(ldf) + 5) cell = tcell(mstkAnchor, anchor=anchor) for i in range(0, NUMROWS): cell = tcell((mstkAnchor[0], mstkAnchor[1] + i), anchor=anchor) cell2 = tcell((mstkAnchor[0] + 1, mstkAnchor[1] + i), anchor=anchor) self.assertIsInstance(wb.active[cell].value, str) self.assertIsInstance(wb.active[cell2].value, str)
def exportExcel(self): ''' Export to excel the trade tables, trade summaries, and daily forms ''' # Create the space in dframe to add the summary information for each trade. # Then create the Workbook. settings = QSettings('zero_substance', 'structjour') val = settings.value('inputType') # Get a list of Trades from self.df tu = DefineTrades(val) ldf = tu.getTradeList(self.df) # Lay out a dataframe with space for charts imageNames = self.getImageNamesFromTS() imageLocation, dframe = self.layoutExcelData(self.df, ldf, imageNames) assert len(ldf) == len(imageLocation) # Create an openpyxl wb from the dataframe ls = LayoutSheet(self.topMargin, len(self.df)) wb, ws, nt = ls.createWorkbook(dframe) tf = TradeFormat(wb) ls.styleTop(ws, len(nt.columns), tf) mstkAnchor = (len(dframe.columns) + 2, 1) mistake = MistakeSummary(numTrades=len(ldf), anchor=mstkAnchor) mistake.mstkSumStyle(ws, tf, mstkAnchor) mistake.dailySumStyle(ws, tf, mstkAnchor) # tradeSummaries = ls.runSummaries(imageLocation, ldf, self.jf, ws, tf) self.placeImagesAndSumStyles(imageLocation, ws, tf) self.populateXLDailyFormulas(imageLocation, ws) self.populateXLMistakeForm(mistake, ws, imageLocation) self.populateXLDailySummaryForm(mistake, ws, mstkAnchor) self.populateXLDailyNote(ws) self.saveXL(wb, self.jf) logging.info("Processing complete. Saved {}".format( self.jf.outpathfile))
def setUpClass(cls): ddiirr = os.path.dirname(__file__) os.chdir(os.path.realpath(ddiirr + '/../')) outdir = 'test/out' cls.outdir = os.path.realpath(outdir) cls.db = 'data/testdb.sqlite' cls.db = os.path.realpath(cls.db) if os.path.exists(cls.db): clearTables(cls.db) cls.rtg = RTG(db=cls.db) # cls.dates = ['20200203 09:30', '20200204 07:30', '20200205 09:35', '20200206 11:40', '20200207 10:39'] cls.theDate = '20200207 10:39' cls.infile = cls.rtg.saveSomeTestFiles([cls.theDate], cls.outdir)[0] settings = QSettings('zero_substance', 'structjour') # for i, name in enumerate(cls.infiles): name = os.path.join(cls.outdir, cls.infile) x, inputType = getStatementType(name) if inputType == 'DAS': ds = DasStatement(name, settings, cls.theDate) ds.getTrades(testFileLoc=cls.outdir, testdb=cls.db) elif inputType == "IB_CSV": ibs = IbStatement(db=cls.db) ibs.openIBStatement(name) else: raise ValueError(f'Unsupported File type: {inputType}') statement = StatementDB(db=cls.db) df = statement.getStatement(cls.theDate) dtrade = DefineTrades(inputType) dframe, cls.ldf = dtrade.processDBTrades(df) cls.jf = JournalFiles(indir=cls.outdir, outdir=outdir, theDate=cls.theDate, infile=name) cls.sc = SumControl() lf = LayoutForms(cls.sc, cls.jf, dframe) lf.runTtoSummaries(cls.ldf) # Setup mistake note fields to test cls.note = 'Ground control to Major Tom' for i, key in enumerate(lf.ts): tto = lf.ts[key] notex = cls.note + str(i + 1) tto['MstkNote'] = notex # Setup a couple images to add imdir = 'images/' img1 = os.path.join(imdir, 'fractal-art-fractals.jpg') img2 = os.path.join(imdir, 'psych.jpg') assert os.path.exists(img1) assert os.path.exists(img2) for key in lf.ts: tto = lf.ts[key]['chart1'] = img1 tto = lf.ts[key]['chart2'] = img2 t = ExportToExcel(lf.ts, cls.jf, df) imageNames = t.getImageNamesFromTS() cls.imageLocation, dframe = t.layoutExcelData(t.df, cls.ldf, imageNames) assert len(cls.ldf) == len(cls.imageLocation) # Create an openpyxl wb from the dataframe ls = LayoutSheet(t.topMargin, len(t.df)) cls.wb, ws, nt = ls.createWorkbook(dframe) # Place both forms 2 cells to the right of the main table mstkAnchor = (len(dframe.columns) + 2, 1) cls.mistake = MistakeSummary(numTrades=len(cls.ldf), anchor=mstkAnchor) cls.t = t cls.a = mstkAnchor