[英]accessing a value from within a function
I'm trying to access a value from within a function ideally without making it global or calling the function again.我试图在理想情况下从函数内部访问一个值,而不是将其设为全局或再次调用该函数。 I also can't use 'return' as the function is used by a class from a different library for a periodic call back.
我也不能使用“return”,因为该函数被来自不同库的类用于定期回调。
The function below reads in data from a CSV (which is periodically being updated), does all the necessary transforms then uses a function called 'stream' that essentially updates a dataframe with only the new data points.下面的函数从 CSV(定期更新)中读取数据,执行所有必要的转换,然后使用一个名为“stream”的函数,该函数基本上只用新数据点更新数据帧。 As can be seen below, I tried setting attributes for the values I'm trying to access - that is, 'y_range_start' and 'y_range_end'.
如下所示,我尝试为要访问的值设置属性 - 即“y_range_start”和“y_range_end”。 Finally the add_periodic_callback calls the update function every 1000ms to update the dataframe, and finally it's plotted (using Bokeh).
最后 add_periodic_callback 每 1000 毫秒调用一次更新函数来更新数据帧,最后绘制它(使用 Bokeh)。
However, in order to keep the axes updated I need to access the 'y_range_start/end' from within the function to update the figure that resides outside of the 'update' function.但是,为了保持轴更新,我需要从函数内部访问 'y_range_start/end' 以更新位于 'update' 函数之外的图形。
Accessing it as an attribute doesn't work (method mentioned above) as I need to call the function first ie update() \\n update.y_range_start - this is a problem as I don't want to be calling the update() function which will run the other lines above it again.将它作为属性访问不起作用(上面提到的方法),因为我需要先调用函数,即 update() \\n update.y_range_start - 这是一个问题,因为我不想调用 update() 函数这将再次运行它上面的其他行。 I also can't use the return value, as the update function is designed specifically to work with add_periodic_callback.
我也不能使用返回值,因为更新函数是专门为与 add_periodic_callback 一起使用而设计的。
It's been drilled into me not to use Global variables...我已经告诉我不要使用全局变量......
What other options do I have?我还有什么其他选择?
Thanks in advance!提前致谢!
def update():
MAU_ios = pd.read_csv('myapp/data/pplus_ios_data.csv')
MAU_ios['Date'] = pd.to_datetime(MAU_ios['Date'])
MAU_ios['Vol'] = MAU_ios.Vol.astype(int)
new_MAU_ios = {'Date':MAU_ios['Date'], 'Vol':MAU_ios['Vol']}
source_ios.stream(new_MAU_ios)
update.y_range_start = MAU_ios['Date'].min()
update.y_range_end = MAU_ios['Date'].max()
curdoc().add_periodic_callback(update, 1000)
You could wrap it inside a class (this code won't work out of the box, but you can use it as a starting point):您可以将其包装在一个类中(此代码无法立即使用,但您可以将其用作起点):
class MyFancyWrapper:
def __init__(self, source_ios):
self.source_ios = source_ios
# And other stuff you might need, e.g. for `update_y_axes`
def update(self):
MAU_ios = pd.read_csv('myapp/data/pplus_ios_data.csv')
MAU_ios['Date'] = pd.to_datetime(MAU_ios['Date'])
MAU_ios['Vol'] = MAU_ios.Vol.astype(int)
new_MAU_ios = {'Date': MAU_ios['Date'], 'Vol': MAU_ios['Vol']}
self.source_ios.stream(new_MAU_ios)
self.update_y_axes(MAU_ios['Date'].min(), MAU_ios['Date'].max())
def update_y_axes(self, start, end):
print(start)
print(end)
wrapper = MyFancyWrapper(source)
curdoc().add_periodic_callback(wrapper.update, 1000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.