From 83ed6b960eafdc4763c6a0aebc691ec2ef3d2db2 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sun, 9 May 2021 10:49:40 +0200 Subject: Use f-strings instead of .format(). --- epubgrep.py | 23 +++++++++++------------ setup.py | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/epubgrep.py b/epubgrep.py index 189e8b7..718554a 100755 --- a/epubgrep.py +++ b/epubgrep.py @@ -34,7 +34,7 @@ def _colorize_found(dline: str, res: re.Match, col: bool) -> str: found_line = dline.replace( res.group(1), "\033[31;1m" + res.group(1) + "\033[31;0m") - out += '{}\n'.format(found_line) + out += f'{found_line}\n' else: out += dline + '\n' return out @@ -53,8 +53,8 @@ def _multiline_search(inf, sought_RE, filename, chap_info = get_chapter_title(metadata.toc, zif.filename) if chap_info: - out += "{}. {}:\n\n".format(chap_info[1], chap_info[0]) - out += '{}\n\n'.format(res.group(0)) + out += f"{chap_info[1]}. {chap_info[0]}:\n\n" + out += f'{res.group(0)}\n\n' return count, out @@ -69,7 +69,7 @@ def _singleline_search(inf, sought_RE, out_title, filename, counting, res = sought_RE.search(decoded_line) if res: if not out_title: - out_title = '{}'.format(filename) + out_title = '{filename}' if counting or w_counting: count += 1 else: @@ -77,8 +77,7 @@ def _singleline_search(inf, sought_RE, out_title, filename, counting, chap_info = get_chapter_title(metadata.toc, zif.filename) if chap_info is not None: - out += "{}. {}:\n\n".format(chap_info[1], - chap_info[0]) + out += f"{chap_info[1]}. {chap_info[0]}:\n\n" printed_title = True if not (counting or w_counting): out += _colorize_found(decoded_line, res, color) @@ -119,7 +118,7 @@ def _metadata_search(mdata: dict, sre: re.Pattern, fname: str, def grep_book(filename: str, opts: argparse.Namespace, re_flags: int) -> Optional[str]: - assert os.path.isfile(filename), "{} is not EPub file.".format(filename) + assert os.path.isfile(filename), f'{filename} is not EPub file.' sought_RE = re.compile('(' + opts.pattern + ')', re_flags) count = 0 icount = 0 @@ -132,7 +131,7 @@ def grep_book(filename: str, opts: argparse.Namespace, try: metadata = epub_meta.get_epub_metadata(filename) except (epub_meta.EPubException, KeyError, IndexError): - log.exception('Failed to open {}'.format(filename)) + log.exception(f'Failed to open {filename}') return None book = zipfile.ZipFile(filename) printed_booktitle = False @@ -148,7 +147,7 @@ def grep_book(filename: str, opts: argparse.Namespace, opts.weighted_count, metadata, zif) if (not printed_booktitle) and iout: - out += '\n{}\n'.format(filename) + out += f'\n{filename}\n' printed_booktitle = True count += icount out += iout @@ -159,17 +158,17 @@ def grep_book(filename: str, opts: argparse.Namespace, metadata, zif, opts.color, count) if (not printed_booktitle) and iout: - out += '\n{}\n'.format(filename) + out += f'\n{filename}\n' printed_booktitle = True count += icount out += iout if count > 0: if opts.count: - out += '{:02d}:{}'.format(count, filename) + out += f'{count:02d}:{filename}' if opts.weighted_count: size = metadata['file_size_in_bytes'] - out += '{:05d}:{}'.format(int((count/size)*1e5), filename) + out += f'{int((count/size)*1e5):05d}:{filename}' return out diff --git a/setup.py b/setup.py index 077de0a..48809b3 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages setup( name="epubgrep", - version="0.6.0", + version="0.7.0", description='Grep through EPub files', author=u'Matěj Cepl', author_email='mcepl@cepl.eu', -- cgit