Example #1
0
 def test_colors(self):
     color = '\033[94m'
     msg = "color"
     txt = colors.blue(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
     color = '\033[92m'
     txt = colors.green(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
     color = '\033[93m'
     txt = colors.yellow(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
     color = '\033[95m'
     txt = colors.purple(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
     color = '\033[1m'
     txt = colors.bold(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
     color = '\033[4m'
     txt = colors.underline(msg)
     res = color + msg + "\033[0m"
     self.assertEqual(txt, res)
Example #2
0
 def _dateindex(self, col: str) -> pd.DataFrame:
     try:
         index = pd.DatetimeIndex(self.df[col])
         df = self.df.set_index(index)
         return df
     except Exception as e:
         self.err(e, "Can not index with column " +
                  colors.bold(col))
Example #3
0
 def _index(self, col: str) -> pd.DataFrame:
     try:
         df = self.df.set_index(self.df[col])
     except Exception as e:
         self.err(e, self._index, "Can not find column " +
                  colors.bold(col) + " in data")
         return
     self.ok("Added an index from column", col)
     return df
Example #4
0
    def show(self,
             rows: int = 5,
             dataframe: pd.DataFrame = None) -> pd.DataFrame:
        """
        Display info about the dataframe

        :param rows: number of rows to show, defaults to 5
        :param rows: int, optional
        :param dataframe: a pandas dataframe, defaults to None
        :param dataframe: pd.DataFrame, optional
        :return: a pandas dataframe
        :rtype: pd.DataFrame

        :example: ``ds.show()``
        """
        try:
            if dataframe is not None:
                df = dataframe
            else:
                df = self.df
            if df is None:
                self.warning("Dataframe is empty: nothing to show")
                return
            num = len(df.columns.values)
        except Exception as e:
            self.err(e, self.show, "Can not show dataframe")
            return
        f = list(df)
        fds = []
        for fi in f:
            fds.append(str(fi))
        fields = ", ".join(fds)
        num_rows = len(df.index)
        self.info("The dataframe has", colors.bold(num_rows), "rows and",
                  colors.bold(num), "columns:")
        print(fields)
        return df.head(rows)
Example #5
0
 def _endmsg(self, rd):
     """
     Returns an end message with elapsed time
     """
     msg = ""
     s = ""
     if rd.hours > 0:
         if rd.hours > 1:
             s = "s"
         msg += colors.bold(str(rd.hours)) + " hour" + s + " "
     s = ""
     if rd.minutes > 0:
         if rd.minutes > 1:
             s = "s"
         msg += colors.bold(str(rd.minutes)) + " minute" + s + " "
     # if rd.seconds > 0:
     #    msg+=str(rd.seconds)
     # else:
     #    msg+="0."
     milliseconds = int(rd.microseconds / 1000)
     if milliseconds > 0:
         msg += colors.bold(str(rd.seconds) + "." + str(milliseconds))
     msg += " seconds"
     return msg
Example #6
0
    def tables(self):
        """Print the existing tables in a database

        :example: ``ds.tables()``
        """
        if self._check_db() is False:
            return
        try:
            pmodels = self._tables()
            if pmodels is None:
                return
            num = len(pmodels)
            s = "s"
            if num == 1:
                s = ""
            msg = "Found " + colors.bold(str(num)) + " table" + s + ":\n"
            msg += "\n".join(pmodels)
            self.info(msg)
        except Exception as e:
            self.err(e, "Can not print tables")