misc.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import datetime
  2. import tempfile
  3. def class_mixer(*args):
  4. class class3(*args):
  5. def __init__(self, **kwargs):
  6. super().__init__(**kwargs)
  7. return class3
  8. # Copyright (c) 2008-2019, Christoph Gohlke
  9. # Copyright (c) 2008-2019, The Regents of the University of California
  10. # Produced at the Laboratory for Fluorescence Dynamics
  11. # All rights reserved.
  12. #
  13. # Redistribution and use in source and binary forms, with or without
  14. # modification, are permitted provided that the following conditions are met:
  15. #
  16. # * Redistributions of source code must retain the above copyright notice,
  17. # this list of conditions and the following disclaimer.
  18. #
  19. # * Redistributions in binary form must reproduce the above copyright notice,
  20. # this list of conditions and the following disclaimer in the documentation
  21. # and/or other materials provided with the distribution.
  22. #
  23. # * Neither the name of the copyright holder nor the names of its
  24. # contributors may be used to endorse or promote products derived from
  25. # this software without specific prior written permission.
  26. #
  27. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  31. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. # POSSIBILITY OF SUCH DAMAGE.
  38. def excel_datetime(timestamp, epoch=None):
  39. """Return datetime object from timestamp in Excel serial format.
  40. Convert LSM time stamps.
  41. >>> excel_datetime(40237.029999999795)
  42. datetime.datetime(2010, 2, 28, 0, 43, 11, 999982)
  43. """
  44. if epoch is None:
  45. epoch = datetime.datetime.fromordinal(693594)
  46. return epoch + datetime.timedelta(timestamp)
  47. def interpret_string_as_boolean(str_value):
  48. """
  49. Tries to interpret string a boolean. Raises a Value error upon failure.
  50. """
  51. if str_value in ("TRUE", "True", "true", "1"):
  52. return True
  53. elif str_value in ("False", "FALSE", "false", "0"):
  54. return False
  55. else:
  56. raise ValueError(f"Could not interpret '{str_value}' as a boolean. Valid values are: "
  57. f"TRUE, True, true, 1, FALSE, False, false, 0")
  58. def get_system_temp_dir():
  59. if tempfile.tempdir is None:
  60. return tempfile.gettempdir()
  61. else:
  62. current_tempdir = tempfile.tempdir
  63. tempfile.tempdir = None
  64. system_tempdir = tempfile.gettempdir()
  65. tempfile.tempdir = current_tempdir
  66. return system_tempdir