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