小编gvo*_*sey的帖子

使用命名元组作为事实上的常量——聪明还是愚蠢?

我有 C# 背景,但现在使用 Python 3.x 进行大量科学计算工作,因此我想了解一下我的风格或口音按照 Python 标准有多“奇怪”。

特别是,Python 中没有这样的东西,这让我非常恼火const。我的用例是这样的:我正在保存*.npz文件(numpy 序列化数据字典)、传递字典、写入文件等,并且字典键、文件名等需要具有一致、可靠的命名模式。

很明显,在 8 个地方输入相同的神奇愚蠢字符串是错误的。

所以在我的模块根目录中,我有一个通常调用的文件base.py

import os
from collections import namedtuple
from os import path

# This is the tuple that contains operational constants
RuntimeConstants = namedtuple("Const", " ModelDirectoryLabelName \
                                  DefaultModelOutputDirectoryRoot \
                                  ResultDirectoryNameFormat \
                                  PeripheryOutputFilePrefix \
                                  NCOutputFilePrefix \
                                  SummaryPlotFileName \
                                  PeripheryConfigurationName \
                                  ResourceDirectoryName \
                                  StimulusTemplateName")

runtime_consts = RuntimeConstants(ModelDirectoryLabelName=".model-output-root",
                                  DefaultModelOutputDirectoryRoot="model-output",
                                  ResultDirectoryNameFormat="%d %b %y - %H%M",
                                  PeripheryOutputFilePrefix="periphery-output-",
                                  NCOutputFilePrefix="nc-output-",
                                  PeripheryConfigurationName="simulation-configuration.yaml",
                                  SummaryPlotFileName="summary-plots.pdf",
                                  ResourceDirectoryName="resources",
                                  StimulusTemplateName="default_stimulus.yaml"
                                  )
# This is …
Run Code Online (Sandbox Code Playgroud)

python coding-style constants

9
推荐指数
1
解决办法
1583
查看次数

以编程方式创建 jupyter 4 笔记本?

我想以编程方式生成一些*.ipnb作为我的建模工作的一部分,以便我可以将带注释的图形和带有 Markdown 注释的代码转储到公共 git 存储库。理想情况下,我将创建一个新笔记本并在每次运行新模型时发布它。

这个要点中,我发现了一种执行此操作的模式,其中涉及:from IPython.nbformat import current as nbf

然而,IPython.nbformat.current自去年春天以来已被弃用,并且实际上在 jupyter/ipython 4.0.1 中不存在,所以这对我不起作用。

现在什么是正确的模式?

python ipython jupyter-notebook

4
推荐指数
1
解决办法
902
查看次数

列上的pandas dataframe sort会在索引上引发keyerror

我有以下数据框df:

   peaklatency        snr
0        52.99        0.0
1        54.15  62.000000
2        54.12  82.000000
3        54.64  52.000000
4        54.57  42.000000
5        54.13  72.000000
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式对此进行排序snr:

df.sort_values(df.snr)
Run Code Online (Sandbox Code Playgroud)

但这提高了

_convert_to_indexer(self, obj, axis, is_setter)
   1208                 mask = check == -1
   1209                 if mask.any():
-> 1210                     raise KeyError('%s not in index' % objarr[mask])
   1211 
   1212                 return _values_from_object(indexer)

KeyError: '[ inf  62.  82.  52.  42.  72.] not in index'
Run Code Online (Sandbox Code Playgroud)

我没有明确地在这个DataFrame上设置索引,它来自列表理解:

    import pandas as pd
    d = []
    for run in runs:
        d.append({            
            'snr': run.periphery.snr.snr, …
Run Code Online (Sandbox Code Playgroud)

python syntax pandas

4
推荐指数
1
解决办法
7294
查看次数