From a2f272d025345312fe94c3597863268001f53de6 Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Sat, 1 Jun 2019 19:40:18 +0200 Subject: [PATCH] add lister sources --- sessions/python-13.md | 4 ++-- sources/lister/lister.py | 42 +++++++++++++++++++++++++++++++++++ sources/lister/test_lister.py | 31 ++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 sources/lister/lister.py create mode 100644 sources/lister/test_lister.py diff --git a/sessions/python-13.md b/sessions/python-13.md index 7c42912..a061611 100644 --- a/sessions/python-13.md +++ b/sessions/python-13.md @@ -252,7 +252,7 @@ On peut le voir si on utilise des parenthèses: >>> generator at 0x7f654c272138> >>> list(generator) -[1, 2, 3] +[0, 1, 2] ``` Les générateurs sont "feignants": ils ne calculent leur valeurs que quand @@ -263,7 +263,7 @@ c'est demandé ```python >>> generator = (x for x in range(0, 3)) >>> list(generator) -[1, 2, 3] +[0, 1, 2] >>> list(generator) [] ``` diff --git a/sources/lister/lister.py b/sources/lister/lister.py new file mode 100644 index 0000000..1ee0f91 --- /dev/null +++ b/sources/lister/lister.py @@ -0,0 +1,42 @@ +import sys +import os + +def parse_args(args): + options = Options() + if "-l" in args: + options.show_modification_time= True + return options + +class Options: + def __init__(self): + self.show_modification_time = False + +class Entry: + def __init__(self,name): + self.name=name + self.is_directory=False + self.mtime=0 + +def get_entries(): + names = os.listdir(".") + for name in names: + entry = Entry(name) + entry.mtime = os.stat(name).st_mtime + yield entry + +def list_entries(entries,options): + for entry in entries: + if options.show_modification_time : + yield f"{entry.name} {entry.mtime}" + else: + yield entry.name + +def main(): + options=parse_args(sys.argv) + entries = get_entries() + lines = list_entries(entries,options) + for line in lines: + print(line) + +if __name__=="__main__": + main() diff --git a/sources/lister/test_lister.py b/sources/lister/test_lister.py new file mode 100644 index 0000000..7af37ed --- /dev/null +++ b/sources/lister/test_lister.py @@ -0,0 +1,31 @@ +import lister + +def test_parse_args_empty_list(): + options = lister.parse_args([]) + assert options.show_modification_time is False + +def test_parse_args_dash_l(): + options = lister.parse_args(["-l"]) + assert options.show_modification_time is True + + +def test_list_empty_entries(): + options = lister.Options() + assert list(lister.list_entries([], options)) == [] + + +def get_listing(entries, show_modification_time=False): + options = lister.Options() + options.show_modification_time = show_modification_time + return list(lister.list_entries(entries, options)) + +def test_list_names(): + foo_txt = lister.Entry("foo.txt") + entries = [foo_txt] + assert get_listing(entries, show_modification_time=False) == ["foo.txt"] + +def test_list_mtimes(): + foo_txt = lister.Entry("foo.txt") + foo_txt.mtime = 1559404640 + entries = [foo_txt] + assert get_listing(entries, show_modification_time=True)== ["foo.txt 1559404640"]