Skip to content

Commit

Permalink
Move func def
Browse files Browse the repository at this point in the history
  • Loading branch information
tpwo committed Nov 7, 2023
1 parent d7578da commit b103f4b
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/pyzet/zettel.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,6 @@ def get_zettel(path: Path) -> Zettel:
return Zettel(title=title, id_=id_, tags=tags, path=md_path)


def _get_first_and_last_line(path: Path) -> tuple[str, str]:
"""Gets the first and the last line from a given file.
It uses file.seek() to look from the end of the file. It's fast but
requires the file to be opened in binary mode.
Reference:
https://stackoverflow.com/a/54278929/14458327
"""
with open(path, 'rb') as file:
title_line = file.readline().decode('utf-8')
try:
file.seek(-2, os.SEEK_END)
while file.read(1) != b'\n':
file.seek(-2, os.SEEK_CUR)
except OSError: # file has only a single line
file.seek(0)
tags_line = file.readline().decode('utf-8')
return title_line, tags_line


def get_zettel_repr(zet: Zettel, args: Namespace) -> str:
tags = ''
if args.tags:
Expand Down Expand Up @@ -150,3 +129,24 @@ def get_tags_str(zettel: Zettel) -> str:
raise ValueError
else:
return '#' + ' #'.join(zettel.tags)


def _get_first_and_last_line(path: Path) -> tuple[str, str]:
"""Gets the first and the last line from a given file.
It uses file.seek() to look from the end of the file. It's fast but
requires the file to be opened in binary mode.
Reference:
https://stackoverflow.com/a/54278929/14458327
"""
with open(path, 'rb') as file:
title_line = file.readline().decode('utf-8')
try:
file.seek(-2, os.SEEK_END)
while file.read(1) != b'\n':
file.seek(-2, os.SEEK_CUR)
except OSError: # file has only a single line
file.seek(0)
tags_line = file.readline().decode('utf-8')
return title_line, tags_line

0 comments on commit b103f4b

Please sign in to comment.