小编Jam*_*ght的帖子

当任何命令(包括非简单命令)失败时退出的 Bash 脚本

我有一个脚本

#! /bin/sh
set -eux

command_a

command_b | command_c

while [ true ]; do
    command_d
done
Run Code Online (Sandbox Code Playgroud)

当任何命令失败时,我希望这失败。如果command_a失败,脚本将失败,但如果command_bcommand_d(不确定command_c)失败,脚本将继续执行。

bash

6
推荐指数
1
解决办法
2万
查看次数

Matplotlib报告找不到字体家族“ serif”

例如,当我尝试运行时:

mpl.rcParams['font.family'] = 'serif'
plt.rcParams['figure.figsize'] = [15,7]
plt.plot(data['flow-time'], data['staticpressurerecovery'])
plt.xlabel('Time [s]')
plt.ylabel('Static Pressure Recovery [-]')
plt.title('McD13_4S3 Plenum: Performance Coefficient ')
plt.ylim((0.33, 0.4))
plt.grid()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在Jupyter笔记本中,我收到以下错误消息:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\font_manager.py:1331: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 删除fontList.cachefontList.json以及fontList.py3.cache

  2. 反注释的字体家族相关的部分matplotlibrc文件

  3. matplotlib使用pip uninstall matplotlib和卸载并重新安装pip install matplotlib

没有任何办法解决这个问题。现在我获取不同字体的唯一可能方法是使用LaTeX作为后端,但这是缓慢且不必要的。

有什么想法我接下来可以尝试吗?

编辑:我正在使用Windows 10,所以不apt-get适合我。这似乎是解决这些问题的常用方法,但是我做不到。看来这些解决方案只是将Microsoft字体添加到Linux字体管理器中,所以由于我已经在Microsoft机器上,所以它甚至可能无关紧要。

最低工作示例:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

mpl.rcParams['font.family'] = …
Run Code Online (Sandbox Code Playgroud)

python fonts matplotlib

6
推荐指数
1
解决办法
2772
查看次数

全局变量在 ipython 中的模块函数中不起作用

我确信已经有人回答过这个问题,但我找不到。

我有一个脚本test.py,其存在的唯一目的是将变量var放入全局命名空间中:

def test():
    global var
    var = 'stuff'
Run Code Online (Sandbox Code Playgroud)

test.py以下是我在 ipython 中使用运行%run,然后运行test()​​,然后尝试访问 时发生的情况var

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %run test.py

In [2]: test()

In [3]: var
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-84ddba356ca3> in <module>()
----> 1 var …
Run Code Online (Sandbox Code Playgroud)

python global-variables ipython

3
推荐指数
1
解决办法
1305
查看次数

循环列表列表并使用第 i 个迭代器

所以我有以下列表:

test = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)

我想遍历i内部列表中的第 th 个元素。我可以通过使用来做到这一点zip

for x, y, z in zip(test[0], test[1], test[2]):
    print(x, y, z)
Run Code Online (Sandbox Code Playgroud)

返回:

1 4 7
2 5 8
3 6 9
Run Code Online (Sandbox Code Playgroud)

有没有更干净、更 Pythonic 的方法来做到这一点?像zip(test, axis=0)什么?

python zip iterator for-loop list

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

重新加载本地模块不起作用

首先,我知道这之前已经发布过,但是要么 A)建议不起作用,要么 B)建议从命名空间中手动删除模块并像平常一样重新导入它。

我有以下模块结构

basedir/
    pytools/
        __init__.py
        tools.py
    setup.py
    test.py
Run Code Online (Sandbox Code Playgroud)

如果我在basedir,并导入pytools并创建一个 类 的对象testcls。该类的实际属性可在 中找到tools.pytestcls有一个名为的方法,它现在testfunc就简单地打印出来:AAA

>>> import pytools
>>> test = pytools.testcls()
>>> test.testfunc()
AAA
Run Code Online (Sandbox Code Playgroud)

假设我更改testfunc()为现在打印出来BBB。我这样做,并保存文件。然后我重新加载模块并重试,它没有打印出来BBB

>>> from importlib import reload
>>> reload(pytools)
>>> test = pytools.testcls()
>>> test.testfunc()
AAA
Run Code Online (Sandbox Code Playgroud)

但是,如果我执行完全相同的过程,但改为更改test.py,将该文件作为模块导入,编辑其中的函数,然后重新加载它,它的行为将按预期进行:

>>> import test
>>> testvariable = test.testcls()
>>> testvariable.testfunc2()
AAA
# Change the function here
>>> from importlib …
Run Code Online (Sandbox Code Playgroud)

python ipython python-3.x python-importlib

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

git repo从WSL提供的矛盾信息比从标准Windows提供的矛盾

当我git status通过Windows目录(/mnt/c/Users/....)上的WSL(Linux的Windows子系统)在git存储库上运行时,我知道目录中的每个文件都已被修改。每次修改都只是删除并重写原始文件。见下文: 在此处输入图片说明

以下是git diff其中一个文件 特定文件上的git diff

现在,如果我git status通过Powershell在Windows端的完全相同的存储库上运行a ,那么我什么也没得到:

在此处输入图片说明

这背后有什么特殊原因吗?我知道这^M与Linux和Microsoft使用的不同行尾有关,但是git在是否进行了更改方面意见不一致,这对我来说似乎很奇怪。

注意:我一直在通过仓库的单独克隆在WSL侧上处理仓库(因此,为什么Windows端要落后15个)。但是,尚未对Windows存储库进行任何编辑。不知道这是否会改变任何东西,但是我想我会提到它。

git windows-subsystem-for-linux

1
推荐指数
2
解决办法
448
查看次数

标签中的下划线不呈现

在标签中使用下划线实际上并不创建下划线。我不确定发生了什么事

最小工作示例:

variable = range(5)
plt.plot(variable, variable, label='test_underscore')
plt.plot(variable, variable, label='escape\_underscore')
plt.plot(variable, variable, label=r'rawtest_underscore')
plt.plot(variable, variable, label=r'rawescape\_underscore')
_=plt.legend()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑:

这是我的rcParams

plt.rcParams.update({
            'font.family': 'serif',
            'font.serif': 'cmr10',
            'mathtext.fontset': 'cm',
            'axes.unicode_minus': False,
            'font.size': 11,
            'figure.dpi': 200,
            'lines.linewidth': 0.5,
            'axes.grid': True
})
Run Code Online (Sandbox Code Playgroud)

当我在没有这个的情况下运行时,它可以正常工作: 在此输入图像描述

python matplotlib

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