From e8904b86e68ee3127668711569820ef5858b7c13 Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 9 Oct 2018 11:23:10 -0400 Subject: [PATCH 1/9] simple but non-generic solution --- task-03/get_top_names.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 4535204..6eee330 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -12,8 +12,8 @@ def extract_data_lines(filename, start_text, end_text): # fill in code as needed with open(filename) as fh: for line in fh: - # fill in code as needed - # use `yield line` to return desired lines but keep the function going + if line[:4] == '': + yield line if __name__ == '__main__': From a11fa03e8ca307da35afcf59462ddbf49f3e42bf Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 9 Oct 2018 11:25:05 -0400 Subject: [PATCH 2/9] solution that uses start_text and end_text --- task-03/get_top_names.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 6eee330..32cd981 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -11,9 +11,17 @@ def extract_data_lines(filename, start_text, end_text): """ # fill in code as needed with open(filename) as fh: + in_table = False for line in fh: - if line[:4] == '': - yield line + + if line == start_text: + in_table = True + elif line == end_text: + in_table = False + + if in_table: + if line[:4] == '': + yield line if __name__ == '__main__': From f0e4e49847e8a336acce5ed9c54d14e00f7122bd Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 9 Oct 2018 11:27:24 -0400 Subject: [PATCH 3/9] adds options to include the starting and ending lines --- task-03/get_top_names.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 32cd981..c4ea5bb 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -4,7 +4,13 @@ https://github.com/astropgh/learning-by-doing/tree/master/task-03 """ -def extract_data_lines(filename, start_text, end_text): +def extract_data_lines( + filename, + start_text, + end_text, + include_start=False, + include_end=False, +): """ open `filename`, and yield the lines between the line that contains `start_text` and the line that contains `end_text` @@ -16,8 +22,13 @@ def extract_data_lines(filename, start_text, end_text): if line == start_text: in_table = True + if include_start: + yield line + elif line == end_text: in_table = False + if include_end: + yield line if in_table: if line[:4] == '': From 0b4120ca7d3f524f65acf75e7aed376ffafe8781 Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 16 Oct 2018 14:17:55 -0400 Subject: [PATCH 4/9] strips \n from line --- task-03/get_top_names.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index c4ea5bb..63b2b6d 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -20,12 +20,12 @@ def extract_data_lines( in_table = False for line in fh: - if line == start_text: + if line.strip() == start_text: in_table = True if include_start: yield line - elif line == end_text: + elif line.strip() == end_text: in_table = False if include_end: yield line From 16e84ba68826331cbf56a27b4a75fe6595a5a225 Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 16 Oct 2018 14:26:31 -0400 Subject: [PATCH 5/9] includes all lines in between starting and ending text --- task-03/get_top_names.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 63b2b6d..3dd831f 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -31,8 +31,7 @@ def extract_data_lines( yield line if in_table: - if line[:4] == '': - yield line + yield line if __name__ == '__main__': From c3a693da43bacf80dd8de8e9cdf1d6ac7a3dd98e Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 16 Oct 2018 15:32:37 -0400 Subject: [PATCH 6/9] uses break --- task-03/get_top_names.py | 1 + 1 file changed, 1 insertion(+) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 3dd831f..13e594b 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -29,6 +29,7 @@ def extract_data_lines( in_table = False if include_end: yield line + break if in_table: yield line From c8da15dec00d303ac2f01db4bc8365a0bacc3d0b Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 16 Oct 2018 15:32:58 -0400 Subject: [PATCH 7/9] looks for lines containing starting and ending text instead of equaling --- task-03/get_top_names.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 13e594b..7d6291c 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -20,13 +20,12 @@ def extract_data_lines( in_table = False for line in fh: - if line.strip() == start_text: + if start_text in line: in_table = True if include_start: yield line - elif line.strip() == end_text: - in_table = False + elif end_text in line: if include_end: yield line break From 74c4d7aa5aab2a7cd7b9f529889f45b10c8c104b Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Tue, 16 Oct 2018 17:05:28 -0400 Subject: [PATCH 8/9] removes redundant yield --- task-03/get_top_names.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 7d6291c..70cb08b 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -22,8 +22,6 @@ def extract_data_lines( if start_text in line: in_table = True - if include_start: - yield line elif end_text in line: if include_end: @@ -39,5 +37,5 @@ def extract_data_lines( start_text = '2017' end_text = '' - for line in extract_data_lines(filename, start_text, end_text): + for line in extract_data_lines(filename, start_text, end_text, include_start=True): print(line) From 9e2d4889e1f2ecb67160187dcd354c294ecd6b8e Mon Sep 17 00:00:00 2001 From: Brett Andrews Date: Fri, 19 Oct 2018 13:20:43 -0400 Subject: [PATCH 9/9] fixes include_start option --- task-03/get_top_names.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/task-03/get_top_names.py b/task-03/get_top_names.py index 70cb08b..27809f4 100644 --- a/task-03/get_top_names.py +++ b/task-03/get_top_names.py @@ -22,6 +22,8 @@ def extract_data_lines( if start_text in line: in_table = True + if not include_start: + continue elif end_text in line: if include_end: @@ -36,6 +38,16 @@ def extract_data_lines( filename = 'top5names.html' start_text = '2017' end_text = '' + include_start = False + include_end = False - for line in extract_data_lines(filename, start_text, end_text, include_start=True): + args = ( + filename, + start_text, + end_text, + include_start, + include_end + ) + + for line in extract_data_lines(*args): print(line)