Mercurial > p > roundup > code
diff roundup/cgi/templating.py @ 8088:1045425c23b2
refactor!: replace os.listdir() with os.scandir()
In many places we did a listdir() then a stat to see if it's a file or
directory. This change removes the need for the stat call. Also for
larger directories, scandir() is an iterator, so less memory use.
There is one remnant of listdir used in an error handler. That
requires a stat on each element in the directory, so there is no
benefit to using scandir() other than a slight memory saving on a
rarely used piece of code.
BREAKING CHANGE:
Python 2 requires installation of scandir pip package after this
commit.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 16 Jul 2024 01:05:49 -0400 |
| parents | b63fcfc2c984 |
| children | 586f76eb33e8 |
line wrap: on
line diff
--- a/roundup/cgi/templating.py Sun Jul 14 13:36:06 2024 -0400 +++ b/roundup/cgi/templating.py Tue Jul 16 01:05:49 2024 -0400 @@ -334,9 +334,10 @@ def precompile(self): """ Precompile templates in load directory by loading them """ - for filename in os.listdir(self.template_dir): + for dir_entry in os.scandir(self.template_dir): + filename = dir_entry.name # skip subdirs - if os.path.isdir(filename): + if dir_entry.is_dir(): continue # skip files without ".html" or ".xml" extension - .css, .js etc.
