_metaclasses.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (c) "Neo4j"
  2. # Neo4j Sweden AB [https://neo4j.com]
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # https://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __all__ = [
  16. "DateTimeType",
  17. "DateType",
  18. "TimeType",
  19. ]
  20. class DateType(type):
  21. def __getattr__(cls, name):
  22. try:
  23. return {
  24. "fromisoformat": cls.from_iso_format,
  25. "fromordinal": cls.from_ordinal,
  26. "fromtimestamp": cls.from_timestamp,
  27. "utcfromtimestamp": cls.utc_from_timestamp,
  28. }[name]
  29. except KeyError:
  30. raise AttributeError(
  31. f"{cls.__name__} has no attribute {name!r}"
  32. ) from None
  33. class TimeType(type):
  34. def __getattr__(cls, name):
  35. try:
  36. return {
  37. "fromisoformat": cls.from_iso_format,
  38. "utcnow": cls.utc_now,
  39. }[name]
  40. except KeyError:
  41. raise AttributeError(
  42. f"{cls.__name__} has no attribute {name!r}"
  43. ) from None
  44. class DateTimeType(type):
  45. def __getattr__(cls, name):
  46. try:
  47. return {
  48. "fromisoformat": cls.from_iso_format,
  49. "fromordinal": cls.from_ordinal,
  50. "fromtimestamp": cls.from_timestamp,
  51. "strptime": cls.parse,
  52. "today": cls.now,
  53. "utcfromtimestamp": cls.utc_from_timestamp,
  54. "utcnow": cls.utc_now,
  55. }[name]
  56. except KeyError:
  57. raise AttributeError(
  58. f"{cls.__name__} has no attribute {name!r}"
  59. ) from None