def test_data_frame(self): """Should render data Frame""" df = pd.DataFrame([{ 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }]) result = texts.head(df, 3) self.assertGreater(len(result), 1) result = texts.tail(df, 3) self.assertGreater(len(result), 1)
def test_list(self): """Should render list""" source = [{ 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }, { 'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9) }] result = texts.head(source, 3) self.assertGreater(len(result), 1) result = texts.tail(source, 3) self.assertGreater(len(result), 1)
def test_tail_mock(self): """Should show last 3 lines of mock object""" target = MagicMock() target.tail.side_effect = ValueError('FAKE') target.__len__.return_value = 10 target.__iter__.return_value = range(10) result = texts.tail(target, 3) self.assertLess(0, len(result), 'Result should not be empty')
def test_dict(self): """Should render dictionary""" source = {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)} result = texts.head(source, 2) self.assertGreater(len(result), 1) result = texts.tail(source, 2) self.assertGreater(len(result), 1)
def test_array(self): """Should render array""" array = np.ndarray([1, 2, 3, 4, 5, 6]) result = texts.head(array, 2) self.assertGreater(len(result), 1) result = texts.tail(array, 2) self.assertGreater(len(result), 1)
def test_object(self): """Should render object""" class TestObject(object): def __str__(self): return 'a\nb\nc\nd\ne' result = texts.head(TestObject(), 2) self.assertGreater(len(result), 1) result = texts.tail(TestObject(), 2) self.assertGreater(len(result), 1)
def test_tail_mock(self): """Should show last 3 lines of mock object""" target = MagicMock() target.tail.side_effect = ValueError('FAKE') target.__len__.return_value = 10 target.__iter__.return_value = range(10) result = texts.tail(target, 3) self.assertLess( 0, len(result), 'Result should not be empty' )
def test_data_frame(self): """Should render data Frame""" df = pd.DataFrame([ {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)} ]) result = texts.head(df, 3) self.assertGreater(len(result), 1) result = texts.tail(df, 3) self.assertGreater(len(result), 1)
def test_list(self): """Should render list""" source = [ {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)}, {'a': 1, 'b': 'hello', 'c': True, 'd': date(2016, 9, 9)} ] result = texts.head(source, 3) self.assertGreater(len(result), 1) result = texts.tail(source, 3) self.assertGreater(len(result), 1)
def tail(source, count: int = 5): """ The opposite of the head function. Displays the last *count* elements of the *source* object. :param source: DataFrames will show the last *count* rows of that DataFrame. A list, tuple or other iterable, will show the last *count* rows. Dictionaries will show *count* keys from the dictionary, which will be randomly selected unless you are using an OrderedDict. Strings will show the last *count* characters. :param count: The number of elements to show from the source. """ _get_report().append_body(render_texts.tail(source, count=count))
def tail(source, count: int = 5): """ The opposite of the head function. Displays the last *count* elements of the *source* object. :param source: DataFrames will show the last *count* rows of that DataFrame. A list, tuple or other iterable, will show the last *count* rows. Dictionaries will show *count* keys from the dictionary, which will be randomly selected unless you are using an OrderedDict. Strings will show the last *count* characters. :param count: The number of elements to show from the source. """ r = _get_report() r.append_body(render_texts.tail(source, count=count)) r.stdout_interceptor.write_source('[ADDED] Tail\n')
def test_tail_string(self): """Should show last 3 lines of string""" source = 'a\nb\nc\nd\ne\nf' result = texts.tail(source, 3) self.assertTrue(0 < result.find('d\ne\nf'))
def test_tail_empty_data_frame(self): """ Should return an empty string when there are no rows to head. """ result = texts.tail(pd.DataFrame([])) self.assertEqual('', result)
def test_tail_no_rows(self): """Should return an empty string when no rows are set for display.""" result = texts.tail(None, 0) self.assertEqual('', result)