naming.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  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. # http://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. #
  16. import hashlib
  17. class NamingService(object):
  18. __instance = None
  19. @classmethod
  20. def get_instance(cls):
  21. if cls.__instance is None:
  22. raise EnvironmentError("naming service not set")
  23. return cls.__instance
  24. @classmethod
  25. def set_instance(cls, instance):
  26. prev = cls.__instance
  27. cls.__instance = instance
  28. return prev
  29. def __init__(self, init_name="ss"):
  30. self._name = hashlib.md5(init_name.encode("utf-8")).hexdigest()
  31. def next(self):
  32. self._name = hashlib.md5(self._name.encode("utf-8")).hexdigest()
  33. return self._name