__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: yq
  4. @time: 2024/10/31
  5. @desc: 一些资源初始化
  6. """
  7. import os
  8. import sys
  9. import threading
  10. import matplotlib
  11. from contextvars import ContextVar
  12. from config import BaseConfig
  13. if BaseConfig.java_home is not None:
  14. java_home = BaseConfig.java_home
  15. if os.path.basename(java_home) != "bin":
  16. java_home = os.path.join(java_home, 'bin')
  17. os.environ['PATH'] = f"{os.environ['PATH']}:{java_home}"
  18. matplotlib.use('Agg')
  19. import matplotlib.pyplot as plt
  20. __all__ = ['init', 'warning_ignore', "context"]
  21. class Context:
  22. def __init__(self):
  23. # 上下文,适合notebook下单个用户
  24. self._instance_lock = threading.Lock()
  25. self.context = {}
  26. def set(self, k: str, data: object):
  27. with self._instance_lock:
  28. self.context.update({k: data})
  29. def get(self, k: str):
  30. return self.context.get(k, None)
  31. def set_filter_info(self, key, overview, detail=None):
  32. data = {"overview": overview, "detail": detail}
  33. self.set(key, data)
  34. class ContexThreading:
  35. def __init__(self):
  36. # 上下文,web下多用户需要线程隔离,notebook下会失效
  37. self.context = ContextVar('context')
  38. self.context.set({})
  39. def set(self, k: str, data: object):
  40. context_map: dict = self.context.get()
  41. context_map.update({k: data})
  42. self.context.set(context_map)
  43. def get(self, k: str):
  44. context_map: dict = self.context.get()
  45. return context_map.get(k, None)
  46. def set_filter_info(self, key, overview, detail=None):
  47. data = {"overview": overview, "detail": detail}
  48. self.set(key, data)
  49. if BaseConfig.run_env == "jupyter":
  50. context = Context()
  51. else:
  52. context = ContexThreading()
  53. def init():
  54. plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置支持中文的字体
  55. plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
  56. plt.rcParams['figure.figsize'] = (8, 8)
  57. plt.rcParams['figure.max_open_warning'] = 1000
  58. # plt.ioff()
  59. def warning_ignore():
  60. import warnings
  61. # warnings.simplefilter(action="ignore", category=RuntimeWarning)
  62. # warnings.simplefilter(action="ignore", category=UserWarning)
  63. warnings.simplefilter(action="ignore", category=FutureWarning)
  64. warnings.filterwarnings(action="ignore", module="matplotlib")
  65. warnings.filterwarnings(action="ignore", module="dataframe_image")
  66. warnings.filterwarnings(action="ignore", module="pandas")
  67. warnings.filterwarnings(action="ignore", module="scorecardpy")
  68. if "3.6" in sys.version:
  69. from pandas.core.common import SettingWithCopyWarning
  70. warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
  71. if "3.10" in sys.version:
  72. from pandas.errors import SettingWithCopyWarning
  73. warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
  74. if __name__ == "__main__":
  75. pass