__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. matplotlib.use('Agg')
  12. import matplotlib.pyplot as plt
  13. __all__ = ['init', 'warning_ignore', "context"]
  14. class Context:
  15. def __init__(self):
  16. # 上下文,只在当前线程内有效,notebook下会失效
  17. self._instance_lock = threading.Lock()
  18. self.context = {}
  19. def set(self, k: str, data: object):
  20. with self._instance_lock:
  21. self.context.update({k: data})
  22. def get(self, k: str):
  23. return self.context.get(k, None)
  24. def set_filter_info(self, key, overview, detail=None):
  25. data = {"overview": overview, "detail": detail}
  26. self.set(key, data)
  27. class ContexThreading:
  28. def __init__(self):
  29. # 上下文,只在当前线程内有效,notebook下会失效
  30. self.context = ContextVar('context')
  31. self.context.set({})
  32. def set(self, k: str, data: object):
  33. context_map: dict = self.context.get()
  34. context_map.update({k: data})
  35. self.context.set(context_map)
  36. def get(self, k: str):
  37. context_map: dict = self.context.get()
  38. return context_map.get(k, None)
  39. def set_filter_info(self, key, overview, detail=None):
  40. data = {"overview": overview, "detail": detail}
  41. self.set(key, data)
  42. context = Context()
  43. def init():
  44. plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置支持中文的字体
  45. plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
  46. plt.rcParams['figure.figsize'] = (8, 8)
  47. plt.rcParams['figure.max_open_warning'] = 1000
  48. # plt.ioff()
  49. def warning_ignore():
  50. import warnings
  51. # warnings.simplefilter(action="ignore", category=RuntimeWarning)
  52. # warnings.simplefilter(action="ignore", category=UserWarning)
  53. warnings.simplefilter(action="ignore", category=FutureWarning)
  54. warnings.filterwarnings(action="ignore", module="matplotlib")
  55. warnings.filterwarnings(action="ignore", module="dataframe_image")
  56. warnings.filterwarnings(action="ignore", module="pandas")
  57. warnings.filterwarnings(action="ignore", module="scorecardpy")
  58. if "3.6" in sys.version:
  59. from pandas.core.common import SettingWithCopyWarning
  60. warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
  61. if "3.10" in sys.version:
  62. from pandas.errors import SettingWithCopyWarning
  63. warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
  64. if __name__ == "__main__":
  65. pass