Esempio n. 1
0
def terminaltables_(columns_,
                    data_,
                    header_='*',
                    align_right=[],
                    align_left=[],
                    footer_=False):
    # align_left is actually default, so it should not be needed for now
    if header_ is not None:
        header_ = "   ".join(header_)
        header_ = colored.stylize(header_, colored.fg('38'))
    data_ = (columns_, *data_)

    table_instance = SingleTable(data_, header_)
    # table_instance.inner_row_border = True
    # table_instance.outer_row_border = Falsaaae
    table_instance.outer_border = True
    # table_instance.inner_heading_row_border = False
    table_instance.inner_footing_row_border = footer_
    table_instance.inner_column_border = True
    # table_outer_borders = table_instance.table.splitlines()

    if align_right:
        for a in align_right:
            table_instance.justify_columns[a] = 'right'
    if align_left:
        for a in align_left:
            table_instance.justify_columns[a] = 'left'
    print(table_instance.table)
Esempio n. 2
0
def print_table(data: List[List[str]] = None,
                title: str = '',
                colored_header: bool = True,
                no_borders: bool = False):
    if data is None:
        return

    # Make header blue
    if colored_header:
        for x in range(len(data[0])):
            data[0][x] = print_utils.color(data[0][x], 'blue')

    table = SingleTable(data)
    table.title = title
    table.inner_row_border = True

    if no_borders:
        table.inner_row_border = False
        table.inner_column_border = False
        table.outer_border = False
        table.inner_footing_row_border = False
        table.inner_heading_row_border = False

    print('')
    print(table.table)
    print('')
def test_single_line():
    """Test single-lined cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green'],
        [],
    ]
    table = SingleTable(table_data, 'Example')
    table.inner_footing_row_border = True
    table.justify_columns[0] = 'left'
    table.justify_columns[1] = 'center'
    table.justify_columns[2] = 'right'
    actual = table.table

    expected = (
        '\033(0\x6c\033(BExample\033(0\x71\x71\x71\x71\x71\x77\x71\x71\x71\x71\x71\x71\x71\x77\x71\x71\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x6b\033(B\n'
        '\033(0\x78\033(B Name       \033(0\x78\033(B Color \033(0\x78\033(B      Type \033(0\x78\033(B\n'
        '\033(0\x74\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x75\033(B\n'
        '\033(0\x78\033(B Avocado    \033(0\x78\033(B green \033(0\x78\033(B       nut \033(0\x78\033(B\n'
        '\033(0\x78\033(B Tomato     \033(0\x78\033(B  red  \033(0\x78\033(B     fruit \033(0\x78\033(B\n'
        '\033(0\x78\033(B Lettuce    \033(0\x78\033(B green \033(0\x78\033(B vegetable \033(0\x78\033(B\n'
        '\033(0\x78\033(B Watermelon \033(0\x78\033(B green \033(0\x78\033(B           \033(0\x78\033(B\n'
        '\033(0\x74\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x75\033(B\n'
        '\033(0\x78\033(B            \033(0\x78\033(B       \033(0\x78\033(B           \033(0\x78\033(B\n'
        '\033(0\x6d\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x76\x71\x71\x71\x71\x71\x71\x71\x76\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x6a\033(B')
    assert actual == expected
Esempio n. 4
0
def test_single_line():
    """Test single-lined cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green'],
        [],
    ]
    table = SingleTable(table_data, 'Example')
    table.inner_footing_row_border = True
    table.justify_columns[0] = 'left'
    table.justify_columns[1] = 'center'
    table.justify_columns[2] = 'right'
    actual = table.table

    expected = (
        u'\u250cExample\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500'
        u'\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n'
        u'\u2502 Name       \u2502 Color \u2502      Type \u2502\n'
        u'\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n'
        u'\u2502 Avocado    \u2502 green \u2502       nut \u2502\n'
        u'\u2502 Tomato     \u2502  red  \u2502     fruit \u2502\n'
        u'\u2502 Lettuce    \u2502 green \u2502 vegetable \u2502\n'
        u'\u2502 Watermelon \u2502 green \u2502           \u2502\n'
        u'\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n'
        u'\u2502            \u2502       \u2502           \u2502\n'
        u'\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518'
    )
    assert actual == expected
Esempio n. 5
0
def print_table(rows):
    table = SingleTable(rows)
    table.inner_column_border = False
    table.inner_footing_row_border = False
    table.inner_heading_row_border = False
    table.inner_row_border = False
    table.outer_border = False
    table.padding_left = 0
    table.padding_right = 3

    print(table.table)
Esempio n. 6
0
def hinton_diagram(arr, max_arr=None):
    max_arr = arr if max_arr is None else max_arr
    max_val = max(abs(np.max(max_arr)), abs(np.min(max_arr)))
    diagram = [list([hinton_diagram_value(x, max_val) for x in _arr]) for _arr in arr]

    table = SingleTable(diagram)
    table.inner_heading_row_border = False
    table.inner_footing_row_border = False
    table.inner_column_border = False
    table.inner_row_border = False
    table.column_max_width = 1

    return table.table
def test_single_line():
    """Test single-lined cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green'],
        [],
    ]
    table = SingleTable(table_data, 'Example')
    table.inner_footing_row_border = True
    table.justify_columns[0] = 'left'
    table.justify_columns[1] = 'center'
    table.justify_columns[2] = 'right'
    actual = table.table

    expected = (
        '\033(0\x6c\033(BExample\033(0\x71\x71\x71\x71\x71\x77\x71\x71\x71\x71\x71\x71\x71\x77\x71\x71\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x6b\033(B\n'

        '\033(0\x78\033(B Name       \033(0\x78\033(B Color \033(0\x78\033(B      Type \033(0\x78\033(B\n'

        '\033(0\x74\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x75\033(B\n'

        '\033(0\x78\033(B Avocado    \033(0\x78\033(B green \033(0\x78\033(B       nut \033(0\x78\033(B\n'

        '\033(0\x78\033(B Tomato     \033(0\x78\033(B  red  \033(0\x78\033(B     fruit \033(0\x78\033(B\n'

        '\033(0\x78\033(B Lettuce    \033(0\x78\033(B green \033(0\x78\033(B vegetable \033(0\x78\033(B\n'

        '\033(0\x78\033(B Watermelon \033(0\x78\033(B green \033(0\x78\033(B           \033(0\x78\033(B\n'

        '\033(0\x74\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71\x71\x71\x71\x6e\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x75\033(B\n'

        '\033(0\x78\033(B            \033(0\x78\033(B       \033(0\x78\033(B           \033(0\x78\033(B\n'

        '\033(0\x6d\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x71\x76\x71\x71\x71\x71\x71\x71\x71\x76\x71\x71\x71\x71'
        '\x71\x71\x71\x71\x71\x71\x71\x6a\033(B'
    )
    assert actual == expected
def test_single_line():
    """Test single-lined cells."""
    table_data = [
        ['Name', 'Color', 'Type'],
        ['Avocado', 'green', 'nut'],
        ['Tomato', 'red', 'fruit'],
        ['Lettuce', 'green', 'vegetable'],
        ['Watermelon', 'green'],
        [],
    ]
    table = SingleTable(table_data, 'Example')
    table.inner_footing_row_border = True
    table.justify_columns[0] = 'left'
    table.justify_columns[1] = 'center'
    table.justify_columns[2] = 'right'
    actual = table.table

    expected = (
        u'\u250cExample\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500'
        u'\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n'

        u'\u2502 Name       \u2502 Color \u2502      Type \u2502\n'

        u'\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n'

        u'\u2502 Avocado    \u2502 green \u2502       nut \u2502\n'

        u'\u2502 Tomato     \u2502  red  \u2502     fruit \u2502\n'

        u'\u2502 Lettuce    \u2502 green \u2502 vegetable \u2502\n'

        u'\u2502 Watermelon \u2502 green \u2502           \u2502\n'

        u'\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n'

        u'\u2502            \u2502       \u2502           \u2502\n'

        u'\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500'
        u'\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518'
    )
    assert actual == expected
Esempio n. 9
0
def list_deployments():
    def process_versions(versions: List[dict]) -> (str, str):
        active_version = "None"
        versions_string = ""

        for v in sorted(versions, key=lambda version: version["created_on"]):
            if v["active"]:
                active_version = v["version"]

            if versions_string:
                versions_string += ", "
            versions_string += v["version"]

        return (active_version, versions_string)

    deployments = STATE.api_call("deployments/")
    table_headers = ["Name", "URL", "Creation Date", "Active Version", "All Versions", "Categories"]
    table_data = map(
        lambda datum: [
            datum["name"],
            "{}://{}.{}/".format(
                STATE.conf["hosting_protocol"], datum["subdomain"], STATE.conf["hosting_base_url"]
            ),
            dateutil.parser.parse(datum["created_on"]).strftime("%Y-%m-%d"),
            *process_versions(datum["versions"]),
            ", ".join(
                filter(None, map(lambda category: category["category"], datum["categories"]))
            ),
        ],
        deployments,
    )

    table = SingleTable([table_headers, *table_data])
    table.inner_column_border = False
    table.inner_footing_row_border = False
    table.inner_heading_row_border = False
    table.inner_row_border = False
    table.outer_border = False
    table.padding_left = 0
    table.padding_right = 3

    print(table.table)
Esempio n. 10
0
def testskill():
    """Tests a whole skill by taking a sample from different pages"""
    clear_screen()
    if check_config() == False:
        print("No DB configured")
        return
    db = connect()
    selected_skill = skill_selector(db)
    tests = []
    cursor = db.pages.find({"skill": selected_skill})
    for document in cursor:
        if 'test' in document:
            tests.extend(document['test'])
    if len(tests) < 20:
        print("Can't test you while the tests are less than 20")
        print("Currently the skill has only", len(tests), "tests")
        return
    totals = 0
    selected = random.sample(tests, 20)

    for test in selected:
        clear_screen()
        data = []
        data.append(["#", test['question']])
        answers = test['answers']
        unsorted = random.sample(answers, len(answers))
        for answer in unsorted:
            data.append([unsorted.index(answer) + 1, answer])
        data.append([
            totals,
            "Current Score",
        ])
        table = SingleTable(data)
        table.inner_footing_row_border = True
        print(table.table)
        # The correct one is always the first
        correct = test['answers'][0]
        try:
            user_answer = int(
                input("Please enter the index of the right answer: ")) - 1
        except (ValueError):
            print("That's not a valid answer")
            time.sleep(2)
            continue

        #Validate input
        if int(user_answer) in range(len(test['answers'])):
            if unsorted[user_answer] == correct:
                totals += 1
                print("Right answer, your points are: " + str(totals) +
                      " points")
            else:
                print("Wrong answer, moving on")
        else:
            print("That's not a valid answer")
            time.sleep(2)
            continue

    # From here is about what to do with the test:
    now = datetime.datetime.now()

    # This uploads the results
    results = db.skills.update(
        {"title": selected_skill},
        {"$push": {
            "scores": {
                "score": totals,
                "date": now
            }
        }})
    clear_screen()
    print("You got:", totals, "questions correct out of 20")
    percentage = str(round(totals / 20 * 100)) + "%"
    print("That's a score of", percentage, "right")
    if totals <= 15:
        level = "Learning"
    else:
        level = "Familiar"

    mastery = db.skills.update({"title": selected_skill},
                               {"$set": {
                                   "mastery": level
                               }})
    print("Level updated to:", level)
    print("Goodbye!")
Esempio n. 11
0
def testme():
    """Tests your knowledge of a page"""
    clear_screen()
    if check_config() == False:
        print("No DB configured")
        return
    db = connect()
    selected_skill = skill_selector(db)
    selected_page = page_selector(selected_skill, db)
    cursor = db.pages.find({"skill": selected_skill, "title": selected_page})
    for document in cursor:
        if 'test' in document:
            tests = document['test']
        else:
            print("There are no tests for that page")
            return

    # This should be limited to 20 questions, don't forget to change that.
    totals = 0

    if len(tests) < 20:
        print("Can't test you while the tests are less than 20")
        print("Currently the page has only", len(tests), "tests")
        return
    selected = random.sample(tests, 20)
    for test in selected:
        clear_screen()
        data = []
        data.append(["#", test['question']])
        answers = test['answers']

        unsorted = random.sample(answers, len(answers))
        for answer in unsorted:
            data.append([unsorted.index(answer) + 1, answer])
        data.append([
            totals,
            "Current Score",
        ])
        table = SingleTable(data)
        table.inner_footing_row_border = True
        print(table.table)
        # The correct one is always the first
        correct = test['answers'][0]
        try:
            user_answer = int(
                input("Please enter the index of the right answer: ")) - 1
        except (ValueError):
            print("That's not a valid answer")
            time.sleep(2)
            continue

        #Validate input
        if int(user_answer) in range(len(test['answers'])):
            if unsorted[user_answer] == correct:
                totals += 1
                print("Right answer, your points are: " + str(totals) +
                      " points")
            else:
                print("Wrong answer, moving on")
        else:
            print("That's not a valid answer")
            time.sleep(2)
            continue

    # From here is about what to do with the test:
    now = datetime.datetime.now()

    # This uploads the results
    results = db.pages.update({
        "skill": selected_skill,
        "title": selected_page
    }, {"$push": {
        "scores": {
            "score": totals,
            "date": now
        }
    }})
    clear_screen()
    print("You got:", totals, "questions correct out of 20")
    percentage = str(round(totals / 20 * 100)) + "%"
    print("That's a score of", percentage, "right")
    print("Goodbye!")