__init__.py 2.6 KB

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