forked from metabrainz/python-discid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
57 lines (46 loc) · 1.98 KB
/
util.py
File metadata and controls
57 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright (C) 2013 Johannes Dewender
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Please submit bug reports to GitHub:
# https://github.com/metabrainz/python-discid/issues
"""utility functions"""
import math
SECTORS_PER_SECOND = 75
def _encode(string: str | bytes) -> bytes:
"""Encode (unicode) string to byte string"""
if isinstance(string, str):
return string.encode()
elif isinstance(string, bytes):
return string
raise TypeError("Unexpected type, expected string or bytes")
# UnicodeDecodeError (Python 2) is NOT caught
# device names should be ASCII
def _decode(byte_string: bytes | str) -> str:
"""Decode byte string to (unicode) string"""
# this test for bytes works on Python 2 and 3
if isinstance(byte_string, bytes):
return byte_string.decode()
elif isinstance(byte_string, str):
return byte_string
raise TypeError("Unexpected type, expected string or bytes")
def _sectors_to_seconds(sectors):
"""Round sectors to seconds like done on MusicBrainz Server
The result is forced to :obj:int to make formatted output easier.
"""
# note that `round(2.5) == 2` on Python 3
# and math.floor doesn't return :obj:int on Python 2
# additionally always `int / int =^= int` in Python 2
return int(math.floor((sectors / float(SECTORS_PER_SECOND)) + 0.5))
# vim:set shiftwidth=4 smarttab expandtab: