Skip to content

Commit

Permalink
rich tables for your_header/viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwkania committed Jan 3, 2021
1 parent ccfd38f commit 505acd8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
48 changes: 45 additions & 3 deletions bin/your_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,61 @@
import logging

from rich.logging import RichHandler
from rich.console import Console
from rich.table import Table

from your import Your
from your.utils.misc import YourArgparseFormatter


def nice_print(dic):
"""
Prints out data files into in a nice to view way
Inputs:
dic -- dictionary containing data file meta data to be printed
"""
for key, item in dic.items():
print(f"{key : >27}:\t{item}")


def read_header(f):
def table_print(dic):
"""
Prints out data using rich.Table
Inputs:
dic -- dictionary containing data file meta data to be printed
"""

console = Console()
table = Table(show_header=True, header_style="bold red")
table.add_column("Parameter", justify="right")
table.add_column("Value")
for key, item in dic.items():
table.add_row(key, f"{item}")
console.print(table)


def read_header(f, no_table):
"""
Makes your header for given files, hands header off to be nicely printed
Inputs:
--
f -- files to create the your header
no_table -- bool - if true, don't use rich.table
"""
y = Your(f)
dic = vars(y.your_header)
dic["tsamp"] = y.your_header.tsamp
dic["nchans"] = y.your_header.nchans
dic["channel_bandwidth"] = y.your_header.foff
dic["nspectra"] = y.your_header.nspectra
nice_print(dic)
if no_table:
nice_print(dic)
else:
table_print(dic)


if __name__ == "__main__":
Expand All @@ -39,6 +76,11 @@ def read_header(f):
required=True,
nargs="+",
)
parser.add_argument(
"--no_table",
help="Print plain text, don't use rich.table",
action="store_true",
)
parser.add_argument("-v", "--verbose", help="Be verbose", action="store_true")
values = parser.parse_args()
logging_format = (
Expand All @@ -58,4 +100,4 @@ def read_header(f):
handlers=[RichHandler(rich_tracebacks=True)],
)

read_header(values.files)
read_header(values.files, values.no_table)
16 changes: 12 additions & 4 deletions bin/your_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from rich.logging import RichHandler
from rich.console import Console
from rich.table import Table
import textwrap

from your import Your
Expand Down Expand Up @@ -82,15 +84,21 @@ def create_widgets(self):
self.prev["command"] = self.save_figure
self.prev.grid(row=0, column=4)

def nice_print(self, dic):
def table_print(self, dic):
"""
Prints out data files into in a nice to view way
Prints out data using rich.Table
Inputs:
dic -- dictionary containing data file meta data to be printed
"""

console = Console()
table = Table(show_header=True, header_style="bold red")
table.add_column("Parameter", justify="right")
table.add_column("Value")
for key, item in dic.items():
print(f"{key : >27}:\t{item}")
table.add_row(key, f"{item}")
console.print(table)

def get_header(self):
"""
Expand All @@ -101,7 +109,7 @@ def get_header(self):
dic["nchans"] = self.your_obj.your_header.nchans
dic["foff"] = self.your_obj.your_header.foff
dic["nspectra"] = self.your_obj.your_header.nspectra
self.nice_print(dic)
self.table_print(dic)

def load_file(self, file_name=[""], start_samp=0, gulp_size=1024, chan_std=False):
"""
Expand Down

0 comments on commit 505acd8

Please sign in to comment.