小编k3n*_*jiy的帖子

更好的方法来检查python中的值是否更大,更小或相等

我有一个函数,可以查看值是更大,更小还是等于零,并根据结果返回三种颜色之一.此外,如果存在某个属性,它不会返回三种颜色中的一种,而是单独的第四种颜色.我这样解决了:

def set_colour(x, trigger=False):
    if x > 0.0:
        colour = 'green'
    elif x < 0.0:
        colour = 'red'
    else:
        colour = 'black'

    if trigger:
        colour = 'blue'

    return colour
Run Code Online (Sandbox Code Playgroud)

但我不喜欢它.有更好的方式,更优雅,高效和pythonic?我发现这篇文章非常有趣,但不能使用字典,因为我没有检查静态值,而是比较值.

为了使用字典解决方案,是否有可能为所有数字更大的零定义变量以进行测试?也许与lambda?我尝试了一些东西,但没有得到任何结果.

目前我所拥有的解决方案可能正常工作但未来可能会为小于-1.0,-2.0或更大1.0或2.0等的值添加其他颜色,此时代码将变得越来越长我觉得可以更优雅地解决.

我想我在这里检查所有相关的帖子,所以我希望这不是重复的.

python if-statement idioms idiomatic

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

在 /docker-entrypoint-initdb.d/db_init.sh 中运行 psql 命令时出错(psql:无法连接到服务器:连接被拒绝)

我曾经使用脚本 ( /docker-entrypoint-initdb.d/db_init.sh) 在复制到 postgres 容器的文件夹中循环数据库转储并恢复它们。它曾经很好地工作,但最近它停止工作。

我收到以下错误:

postgres_server_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/db_init.sh
postgres_server_1  | --> db_init.sh: Found /dumps/dataset_1.dump as dataset_1
postgres_server_1  | psql: could not connect to server: Connection refused
postgres_server_1  |    Is the server running on host "localhost" (127.0.0.1) and accepting
postgres_server_1  |    TCP/IP connections on port 5432?
Run Code Online (Sandbox Code Playgroud)

db_init.sh脚本循环遍历包含数据库转储的文件夹并检查数据库是否已存在。如果不是,它会恢复转储。

/docker-entrypoint-initdb.d/db_init.sh 内容:

shopt -s nullglob
for i in /dumps/*.dump;
do
    db_name=${i##*/}
    db_name=${db_name%.dump}
    echo "--> db_init.sh: Found $i as $db_name"

    if psql -U postgres -h localhost -lqt | …
Run Code Online (Sandbox Code Playgroud)

postgresql docker docker-compose

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