我正在使用该cachetools库,我想包装该库中的装饰器,并添加一个类 self 参数以在类级别 ee 上启用/禁用缓存MyClass(enable_cache=True)
一个示例用法如下:
class MyClass(object):
def __init__(self, enable_cache=True):
self.enable_cache = enable_cache
self.cache = cachetools.LRUCache(maxsize=10)
@cachetools.cachedmethod(operator.attrgetter('cache'))
def calc(self, n):
return 1*n
Run Code Online (Sandbox Code Playgroud)
我不确定如何将缓存保留为共享自类对象,并允许使用此库在我自己的包装装饰器中使用enable_cache 标志。
我有一个绘图对象条形图,我想为其显示 2 个 y 轴(不同的货币,因此转换因子是常数)。
目前,我每条轨迹绘制 1 条,而对于第二条轨迹,我将不透明度设置为 0,禁用图例和悬停信息。这个 hack 有效,但维护起来很难。
我知道https://plotly.com/python/multiple-axes/
我当前的解决方案如下所示
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# make up some data
dates = pd.DataFrame(pd.date_range('1/1/2023','1/7/2023'), columns=['date'])
dates["key"] = 0
items = pd.DataFrame(["A","B","C"], columns=['items'])
items["key"] = 0
df = dates.merge(items,on="key",how="outer").drop("key",axis=1)
df['price_USD'] = np.random.randint(0, 100, df.shape[0])
df['price_EURO'] = df['price_USD']/1.5
fig = make_subplots(specs=[[{"secondary_y": True}]])
for item, _df in df.groupby("items",sort=True):
## we may set the colors of the individual items manually …Run Code Online (Sandbox Code Playgroud) 我试过这个:
print u"\u221A"
Run Code Online (Sandbox Code Playgroud)
但它在 Python 中不起作用。
有人能告诉我如何在 python 中做到这一点吗?我需要它来展示我所做的事情。