隐藏Python模块中的中间计算(Hiding intermediate computations in Python module)
我在Jupyter笔记本
src/data.py
有一个Python文件,它用于读取数据文件并提供一些输出。import numpy as np import pandas as pd sha256_perf = ( pd.read_csv('data/hashbench-output.txt', sep='\t', na_filter=False) .query('Algorithm == "SHA256"') ) mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() variance = sha256_perf['Error (± MiB/s)'] ** 2 total_variance = variance.sum() row_count = sha256_perf.shape[0] variance_of_mean = total_variance / (row_count ** 2) error_of_mean = variance_of_mean ** 0.5 sha256_summary = pd.DataFrame(data=[[mean_throughput, error_of_mean]]) sha256_summary.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)']
其中,我关心的唯一变量是输出表 -
sha256_perf
和sha256_summary
。 然而,Python无法知道,所以如果我dir()
模块,我会得到一切:>>> import src.data as data >>> dir(data) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'assumptions', 'error_of_mean', 'mean_throughput', 'np', 'pd', 'prd_scratch_2018', 'row_count', 'sha256_perf', 'sha256_summary', 'total_variance', 'util', 'variance', 'variance_of_mean']
如果这是Ruby或Scala,我可以在块中初始化
sha256_summary
,如下所示:sha256_summary = begin mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() # ... etc. ... df.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)'] df end
即使在Java(8+)中,我也可以与
Supplier
和lambda一起破解。但据我所知,Python没有匿名块或多行lambda。 到目前为止,我所能做到的最好的一切就是把所有的东西放在一个函数中:
def create_summary(): mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() # ... etc. ... sha256_summary.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)'] return sha256_summary sha256_summary = create_summary()
但这仍然导出
create_summary
符号,我宁愿避免:>>> dir(data) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'assumptions', 'create_summary', 'np', 'pd', 'prd_scratch_2018', 'sha256_perf', 'sha256_summary', 'util']
Pythonic避免污染全球命名空间的方式是什么?
I have a Python file in a Jupyter notebook,
src/data.py
, that's meant to read a data file and make some outputs available.import numpy as np import pandas as pd sha256_perf = ( pd.read_csv('data/hashbench-output.txt', sep='\t', na_filter=False) .query('Algorithm == "SHA256"') ) mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() variance = sha256_perf['Error (± MiB/s)'] ** 2 total_variance = variance.sum() row_count = sha256_perf.shape[0] variance_of_mean = total_variance / (row_count ** 2) error_of_mean = variance_of_mean ** 0.5 sha256_summary = pd.DataFrame(data=[[mean_throughput, error_of_mean]]) sha256_summary.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)']
Of this, the only variables I care about are the output tables --
sha256_perf
andsha256_summary
. However, Python has no way of knowing that, so if Idir()
the module, I get everything:>>> import src.data as data >>> dir(data) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'assumptions', 'error_of_mean', 'mean_throughput', 'np', 'pd', 'prd_scratch_2018', 'row_count', 'sha256_perf', 'sha256_summary', 'total_variance', 'util', 'variance', 'variance_of_mean']
If this was Ruby or Scala, I could initialize
sha256_summary
in a block, something like:sha256_summary = begin mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() # ... etc. ... df.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)'] df end
Even in Java (8+), I could hack something together with a
Supplier
and a lambda.But as far as I can tell, Python doesn't have anonymous blocks or multiline lambdas. So so far, the best I've been able to come up with is putting everything in a function:
def create_summary(): mean_throughput = sha256_perf['Throughput (MiB/s)'].mean() # ... etc. ... sha256_summary.columns = ['Mean Throughput (MiB/s)', 'Error (± MiB/s)'] return sha256_summary sha256_summary = create_summary()
But this still exports the
create_summary
symbol, which I'd rather avoid:>>> dir(data) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'assumptions', 'create_summary', 'np', 'pd', 'prd_scratch_2018', 'sha256_perf', 'sha256_summary', 'util']
What's the Pythonic way to avoid polluting the global namespace?
原文:https://stackoverflow.com/questions/49459373
最满意答案
是的,你需要覆盖onLowMemory()函数。
yes it's onLowMemory() function you need to overwrite.
相关问答
更多-
iOS 6 - viewDidUnload迁移到didReceiveMemoryWarning?(iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?)[2022-04-16]
简而言之,在很多情况下,你不需要改变任何东西。 而且,你绝对不希望简单地将viewDidUnload所有内容迁移到didReceiveMemoryWarning 。 通常,我们大多数人将IBOutlet引用设置为在viewDidUnload nil (很大程度上是因为Interface Builder会将它放在那里),并执行内存的一般释放(例如清除缓存,轻松重新创建模型数据等)在didReceiveMemoryWarning 。 如果这是你的方式,那么你可能不需要任何代码改变。 根据iOS 6的viewDi ... -
一个例子我发布...我从somwhere复制...它可能会给你一些想法... - (void)didReceiveMemoryWarning { // Release anything that's not essential, such as cached data (meaning // instance variables, and what else...?) // Obviously can't access local variables such as define ...
-
SHK中的什么使你能够使用flushOfflineQueue以及clearNullWorkouts是做什么的? 这些可能是您在didReceiveMemoryWarning过程中需要释放的资源。 也只是因为didReceiveMemoryWarning引发了这并不意味着你必须清理任何东西,例如,如果你仍然在显示视图,播放被引用的音频文件等。当这个调用你需要发布的东西,如图像不是正在播放的音频等等。在didReceiveMemoryWarning被调用后,未显示的视图控制器将调用viewDidUnload , ...
-
根据dbarker的指针,这个答案是,当-willRotateToInterfaceOrientation:duration:的默认实现已经卸载了视图后,视图控制器的旋转方法(包括-willRotateToInterfaceOrientation:duration:和-willAnimateRotationToInterfaceOrientation:duration:将不会被重新调用视图。 我不知道为什么它会启动应用程序,但我有一个解决方法 我所做的就是在didReceiveMemoryWarning中将 ...
-
尝试将视图高约束更改为0,这样依赖于此视图的其他视图应该像Android线性布局中一样 更新1 从iOS 9.0 UIStackView可用,其作用与android中的LinearLayout相同。 要启用此功能,您可以在Interface Builder中选择2个或更多视图,在底部可以单击堆栈视图按钮: Try to change view high constraint to 0, this way other views which depends on this one should act lik ...
-
viewDidUnload和didReceiveMemoryWarning兼容性(viewDidUnload and didReceiveMemoryWarning Compatibility)[2022-06-22]
在iOS 6中,现在不推荐使用UIViewController的viewWillUnload和viewDidUnload方法。 如果您使用这些方法来释放数据,请改用didReceiveMemoryWarning方法。 如果未使用此方法,也可以使用此方法释放对视图控制器视图的引用。 当前在viewDidUnload使用的代码应该移动到didReceiveMemoryWarning 。 它也适用于iOS5和iOS6。 实际上,在此更改发生之前,仅当应用程序确实收到内存警告时才会调用viewDidUnload方法 ... -
对非关键数据使用延迟加载。 清除didReceiveMemoryWarning这些数据。 然后,数据将在您下次尝试访问时再次延迟加载。 Use lazy loading for your non-critical data. Clear up this data in didReceiveMemoryWarning. Then the data will be lazy loaded again the next time you try to access it.
-
如何使用didReceiveMemoryWarning和viewDidUnload?(How to use didReceiveMemoryWarning and viewDidUnload?)[2023-03-13]
如果您只是在谈论iOS 3.0,请在viewDidUnload释放任何可以轻松重新创建的对象。 然后,您可以在viewDidLoad从nib重新创建或重新加载它们。 确保在释放对象时,您要么将它们取出,要么让您的设置器执行此操作。 这样,您可以在重新加载或重新创建之前使用viewDidLoad来检查是否为nil。 至于从nib对象加载,如果要加载到属性中,并将它们设置为retain,则不必再次保留。 来自Apple : UIViewController类通过didReceiveMemoryWarning(页 ... -
关于DidReceiveMemoryWarning在这个问题上有一个很好的讨论: iOS: didReceiveMemoryWarning的乐于助人: There is a nice discussion about DidReceiveMemoryWarning in this question: iOS: helpfulness of didReceiveMemoryWarning:
-
是的,你需要覆盖onLowMemory()函数。 yes it's onLowMemory() function you need to overwrite.