Exemplo n.º 1
0
def new_chart():
    """
    Take class name selection, chart name, score data, chart parameters from
    assemble_chart_data, form into chart_data_dict with key-value format:
        chart_data_dict = {
                    'class_name': class_name,  # str
                    'chart_name': chart_name,  # str
                    'chart_default_filename': chart_default_filename,  # str
                    'chart_params': chart_params,  # dict
                    'score-avatar_dict': student_scores,  # dict
                    }

    Then write this data to disk as *.cdf (ChartDataFile), generate and save the chart.

    :return: None
    """
    class_name, chart_name, chart_default_filename, student_scores, chart_params = assemble_chart_data()

    chart_data_dict = {'class_name': class_name,  # str
                       'chart_name': chart_name,  # str
                       'chart_default_filename': chart_default_filename,  # str
                       'chart_params': chart_params,  # dict
                       'score-avatar_dict': student_scores,  # dict
                       }

    write_chart_data_to_file(chart_data_dict)

    generate_chart_image(chart_data_dict)
Exemplo n.º 2
0
def new_chart(loaded_class: Class = None) -> None:
    """
    Create a new chart with supplied class, user input.

    Prompts user to select class if not provided by caller.
    Take chart name, score data, chart parameters from
    assemble_chart_data, form into chart_data_dict with key-value
    format:
        chart_data_dict = {
            'class_name': class_name,  # str
            'chart_name': chart_name,  # str
            'chart_default_filename': chart_default_filename,  # str
            'chart_params': chart_params,  # dict
            'score-students_dict': student_scores,  # dict
            }

    Then write this data to disk as *.cdf (ChartDataFile), generate and
    save the chart.

    :param loaded_class: Class = None
    :return: None
    """
    if not loaded_class:
        class_id = select_classlist()  # TODO: warn for empty classlist
        loaded_class = definitions.DATABASE.load_class(class_id)

    (chart_name,
     chart_default_filename,
     student_scores,
     chart_params) = assemble_chart_data(loaded_class)

    chart_data_dict = {'class_id': loaded_class.id,
                       'class_name': loaded_class.name,  # str
                       'chart_name': chart_name,  # str
                       'chart_default_filename': chart_default_filename,  # str
                       'chart_params': chart_params,  # dict
                       'score-students_dict': student_scores,  # dict
                       }

    definitions.DATABASE.create_chart(chart_data_dict)

    chart_image_location = generate_chart_image(chart_data_dict)

    # Show image to user, user save image.
    if show_image(chart_image_location):
        user_save_chart_image(chart_data_dict, chart_image_location)
Exemplo n.º 3
0
    def test_generate_chart_image(self, monkeypatch, empty_generic_database):
        called = {
            'set_axis_mock': False,
            'generate_avatar_coords_mock': False,
            'add_avatars_to_plot_mock': False,
            'save_chart_image_mock': False
        }

        test_database = empty_generic_database

        def mocked_save_chart_image(chart_data_dict, plt):
            assert (chart_data_dict, plt) == (test_chart_data_dict, mocked_plt)
            called['save_chart_image_mock'] = True
            return test_image_location

        test_database.save_chart_image = mocked_save_chart_image

        class MockPlt:
            def __init__(self):
                self.calls_to_mock_plt = {
                    'figure': False,
                    'subplot': False,
                    'subplots_adjust': False
                }
                self.calls_to_mock_ax = {'grid': False}
                self.mock_ax = self.MockAx(self.calls_to_mock_ax)

            class MockAx:
                def __init__(self, calls_to_mock_ax):
                    self.calls_to_mock_ax = calls_to_mock_ax

                def grid(self, arg):
                    assert arg is False
                    self.calls_to_mock_ax['grid'] = True

            def figure(self, figsize):
                assert figsize == (19.20, 10.80)
                self.calls_to_mock_plt['figure'] = True

            def subplot(self, xlim, ylim):
                assert xlim, ylim == ((-0, 105), (-0, 100))
                self.calls_to_mock_plt['subplot'] = True
                return self.mock_ax

            def subplots_adjust(self, left, right, top, bottom, wspace,
                                hspace):
                assert (left, right, top, bottom, wspace,
                        hspace) == (0.05, 0.95, 0.9, 0.1, 0.01, 0.01)
                self.calls_to_mock_plt['subplots_adjust'] = True

        def mocked_set_axis():
            called['set_axis_mock'] = True

        def mocked_generate_avatar_coords(score_students_dict, class_id):
            assert score_students_dict == test_chart_data_dict[
                'score-students_dict']
            assert class_id == test_chart_data_dict['class_id']
            called['generate_avatar_coords_mock'] = True
            return test_avatar_coord_dict

        def mocked_add_avatars_to_plot(ax, avatar_coord_dict):
            assert (ax, avatar_coord_dict) == (mocked_plt.mock_ax,
                                               test_avatar_coord_dict)
            called['add_avatars_to_plot_mock'] = True

        mocked_plt = MockPlt()

        monkeypatch.setattr(generate_image.definitions, 'DATABASE',
                            test_database)
        monkeypatch.setattr(generate_image, 'plt', mocked_plt)
        monkeypatch.setattr(generate_image, 'set_axis', mocked_set_axis)
        monkeypatch.setattr(generate_image, 'generate_avatar_coords',
                            mocked_generate_avatar_coords)
        monkeypatch.setattr(generate_image, 'add_avatars_to_plot',
                            mocked_add_avatars_to_plot)

        test_chart_data_dict = {
            'class_id': 'some class',
            'score-students_dict': {
                1: ['foo', 'spam', 'dead', 'parrot'],
                5: ['halibut', 'patties'],
                8: ['original', 'recipe', 'chicken'],
                9: ['foo', 'spam', 'dead', 'parrot'],
                11: ['halibut', 'patties'],
            }
        }
        test_avatar_coord_dict = {'avatar_coord': 'dict'}
        test_image_location = 'some image location'

        assert generate_chart_image(
            test_chart_data_dict) == test_image_location

        assert all([called[func] for func in called])
        assert all([
            mocked_plt.calls_to_mock_plt[func]
            for func in mocked_plt.calls_to_mock_plt
        ])
        assert all([
            mocked_plt.calls_to_mock_ax[func]
            for func in mocked_plt.calls_to_mock_ax
        ])