我有一个小python项目,具有以下结构 -
Project
-- pkg01
-- test01.py
-- pkg02
-- test02.py
-- logging.conf
Run Code Online (Sandbox Code Playgroud)
我计划使用默认日志记录模块将消息打印到stdout和日志文件.要使用日志记录模块,需要进行一些初始化 -
import logging.config
logging.config.fileConfig('logging.conf')
logger = logging.getLogger('pyApp')
logger.info('testing')
Run Code Online (Sandbox Code Playgroud)
目前,我在开始记录消息之前在每个模块中执行此初始化.是否可以在一个地方只执行一次初始化,以便通过整个项目记录重复使用相同的设置?
我想删除用conda创建的某个环境.我怎样才能做到这一点?假设我有一个活跃的testenv环境.我通过以下文档尝试:
$ conda env remove
CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again
Run Code Online (Sandbox Code Playgroud)
我然后停用它:
$ source deactivate
Run Code Online (Sandbox Code Playgroud)
我尝试再次运行命令删除它,我仍然得到相同的错误.这里出了什么问题?
我正在使用vim编辑器作为python IDE.下面是一个简单的python程序来计算数字的平方根:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
squareRoot = num**(1/2)
print("The square Root of ", num, " is ", squareRoot)
return
def complex(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
Run Code Online (Sandbox Code Playgroud)
警告是:
1-square-root.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
1-square-root.py|15 col 1 C| E302 …
Run Code Online (Sandbox Code Playgroud) 列出所有可用的环境非常简单:
$ conda env list
Run Code Online (Sandbox Code Playgroud)
现在如何列出当前安装的内核,而不必转到路径:
$ ls /home/{{user}}/.local/share/jupyter/kernels/
Run Code Online (Sandbox Code Playgroud) 我正在检查一些代码,发现了以下函数:
def foo(self, arg1=1, *, arg2=2):
pass
Run Code Online (Sandbox Code Playgroud)
*
我很惊讶地看到关键字参数位于位置参数的左侧。然后我注意到我可以foo
通过以下两种方式进行调用:
>>> foo(1)
>>> foo(arg1=1)
Run Code Online (Sandbox Code Playgroud)
我想我会期望第二次调用失败,因为我foo
通过提供关键字来使用命名参数进行调用arg1
。
话虽如此,我在这两种情况下都使用位置参数,还是第二次调用foo
命名参数?
考虑下面的代码:
class Test:
l = [1, 2, 3]
foo = lambda x: x
for x in l:
print(foo(x))
[print(foo(x)) for x in l]
if __name__ == '__main__':
test = Test()
Run Code Online (Sandbox Code Playgroud)
输出如下:
1
2
3
... in <listcomp>
[print(foo(x)) for x in l]
NameError: name 'foo' is not defined
Run Code Online (Sandbox Code Playgroud)
我不明白为什么foo
列表迭代中的函数不是可见的。可能与范围有关,但我正在寻找正确的解释,并在可能的情况下提供支持它的文档。
ps:为了修复代码,我对替代实现不感兴趣。
我的 Dockerfile 中有以下内容:
...
USER $user
# Set default python version to 3
RUN alias python=python3
RUN alias pip=pip3
WORKDIR /app
# Install local dependencies
RUN pip install --requirement requirements.txt --user
Run Code Online (Sandbox Code Playgroud)
构建图像时,我得到以下信息:
Step 13/22 : RUN alias pip=pip3
---> Running in dc48c9c84c88
Removing intermediate container dc48c9c84c88
---> 6c7757ea2724
Step 14/22 : RUN pip install --requirement requirements.txt --user
---> Running in b829d6875998
/bin/sh: pip: command not found
Run Code Online (Sandbox Code Playgroud)
pip
如果我在其上设置别名,为什么无法识别?
Ps:我不想.bashrc
用于加载别名。
有人可以帮助我理解 Nativescript 中使用的网格布局,特别是 GridLayout 吗?auto 后面的(星号)实际上是什么意思?
<GridLayout rows="*, auto">
Run Code Online (Sandbox Code Playgroud)
然后我遇到了这个更令人困惑的例子:
<GridLayout rows="*, auto, auto, auto, 2*">
Run Code Online (Sandbox Code Playgroud)
我发现 Nativescript 的文档不太清楚。
尝试使用以下命令运行基于图像的 Pod Dockerfile
:
...
ENTRYPOINT [ "./mybashscript", ";", "flask" ]
CMD [ "run" ]
Run Code Online (Sandbox Code Playgroud)
我期望完整的命令是./mybashscript; flask run
. 然而,在此示例中,pod
/container
执行./mybashscript
,但 不执行flask
。
我还尝试了一些变体,例如:
...
ENTRYPOINT [ "/bin/bash", "-c", "./mybashscript && flask" ]
CMD [ "run" ]
Run Code Online (Sandbox Code Playgroud)
现在,flask
被执行但run
被忽略。
PS:我试图理解为什么这不起作用,并且我知道我可以将所有内容放入脚本中entrypoint
或将所有内容放入bash
脚本中,但这不是重点。
像这样定义几个变量:
VARS="VAR1=1 VAR2=2"
Run Code Online (Sandbox Code Playgroud)
然后我想将这些变量提供给后面的命令。我可以这样做:
VAR1=1 VAR2=2 mycommand
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我希望动态设置这些局部作用域变量,例如:
$ $VARS mycommand
VAR1=1: command not found
Run Code Online (Sandbox Code Playgroud)
这似乎$VARS
作为命令执行,这不是预期的。
python ×7
conda ×2
docker ×2
dockerfile ×2
bash ×1
config ×1
containers ×1
grid-layout ×1
jupyter ×1
kubernetes ×1
logging ×1
nativescript ×1
python-3.x ×1
shell ×1
unix ×1
vim ×1