小编ssh*_*270的帖子

如何使用单个 docker 命令删除和重新创建 postgres 数据库?

我正在编写一个微服务应用程序,它有一个用于 postgres 数据库的 docker 容器。我知道在将 SQL 转储到数据库时,我们在终端中使用此 docker 命令:

cat <dump sql file> | docker exec -i <container ID> psql -U <postgres username> <database name>

我想知道是否有类似的 linux 终端 docker 命令可以从容器外部运行到:

  1. 创建数据库命名

  2. 删除数据库命名

甚至:

  1. 删除所有表,使其在一个命令中完全清空。

请注意,我应该能够通过主机操作系统终端 (linux) 从容器外部运行 docker 命令。

任何建议将不胜感激。提前致谢。

postgresql docker

8
推荐指数
3
解决办法
3万
查看次数

在SQLAlchemy中默认插入NULL吗?

我在SQLAlchemy中有以下专栏:

name = Column(String(32), nullable=False)
Run Code Online (Sandbox Code Playgroud)

我希望如果没有值传递给插入,则应输入一个完全空白的默认值,或者如果不可能,则输入默认值NULL。

我应该将列定义为:

name = Column(String(32), nullable=False, default=NULL)
Run Code Online (Sandbox Code Playgroud)

因为在python NULL对象实际上是None

有什么建议么?

python sqlalchemy models

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

如何找到多个字符串的最长公共子串?

我正在编写一个 python 脚本,其中有多个字符串。

例如:

x = "brownasdfoersjumps"
y = "foxsxzxasis12sa[[#brown"
z = "thissasbrownxc-34a@s;"
Run Code Online (Sandbox Code Playgroud)

在所有这三个字符串中,它们都有一个共同的子字符串,即brown. 我想以我想创建字典的方式搜索它:

dict = {[commonly occuring substring] => 
           [total number of occurrences in the strings provided]}
Run Code Online (Sandbox Code Playgroud)

这样做的最佳方法是什么?考虑到我每次都会有 200 多个字符串,那么简单/有效的方法是什么?

python string

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

使用其他列表中的项替换列表中的项而不使用词典

我在python中开发一个函数.这是我的内容:

list = ['cow','orange','mango']
to_replace = 'orange'
replace_with = ['banana','cream']
Run Code Online (Sandbox Code Playgroud)

所以我希望我的清单在更换后变成这样

list = ['cow','banana','cream','mango']
Run Code Online (Sandbox Code Playgroud)

我正在使用这个功能:

def replace_item(list, to_replace, replace_with):
    for n,i in enumerate(list):
      if i== to_replace:
         list[n]=replace_with
    return list
Run Code Online (Sandbox Code Playgroud)

它输出如下列表:

['cow', ['banana', 'cream'], 'mango']
Run Code Online (Sandbox Code Playgroud)

那么如何修改此函数以获得以下输出?

list = ['cow','banana','cream','mango']
Run Code Online (Sandbox Code Playgroud)

注意:我在这里找到了一个答案:用另一个列表的内容替换列表项, 但我不想在这里涉及字典.我还想修改我当前的功能,并保持简单直接.

python list

4
推荐指数
3
解决办法
539
查看次数

GitPython无法设置git config用户名和电子邮件

我正在编写一个Python脚本,该脚本使用GitPython(https://gitpython.readthedocs.io/en/stable/)将本地文件提交到远程存储库。对文件进行编辑后,我设置了用户名和电子邮件的值,如以下代码片段所示:

      repo = git.Repo.init("my local clone path")
      repo.config_writer().set_value("name", "email", "myusername").release()
      repo.config_writer().set_value("name", "email", "myemail").release()
      repo.git.add("filename")
      repo.git.commit("filename")
      repo.git.push("my remote repo url")
Run Code Online (Sandbox Code Playgroud)

我总是遇到以下错误:

 stderr: '
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.
Run Code Online (Sandbox Code Playgroud)

即使我已经使用config_writer()函数设置了用户名和密码,如此处所述:http ://gitpython.readthedocs.io/en/stable/tutorial.html#handling-remotes

任何有关如何解决此问题的建议将不胜感激。

python git git-push gitpython

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

如何根据颜色范围检查RGB颜色?

我正在编写一个python脚本,需要在其中检查以下三种颜色的颜色代码:红色,黄色和绿色:

if (255,255,255)  is in green range:
    print("green")
else if (255,255,255)  is in yellow range:
    print("yellow")
else if (255,255,255)  is in red range:
    print("red")
else:
    print("none")
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是如何查看它is in yellow range?

任何建议将不胜感激。

编辑

下图代表了我对黄色,绿色和红色的感觉:

在此处输入图片说明

python rgb

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

Pandas replace indexes of rows according to a dictionary

我有一个名为 df 的 Pandas 数据框:

             1   2   3   4   5   6   7   8   9   10
dog   jumps   1   1   1   1   1   1   0   1   1   1
      fox     1   1   1   1   1   1   0   0   1   1
      the     1   1   1   1   1   1   1   0   1   1
      dog     1   1   1   1   1   1   0   0   1   1
      over    1   1   1   1   1   1   0   0   1   1
fox   jumps   1   1   1   1   1   1   0 …
Run Code Online (Sandbox Code Playgroud)

python dictionary dataframe pandas

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

如何将嵌套字典的所有值转换为字符串?

我正在编写一个 python 应用程序,其中有一个可以嵌套到任何级别的变量字典。

任何级别的键都可以是 int 或 string。但我想将所有级别的所有键和值都转换为字符串。字典的嵌套方式是可变的,这使得它有点复杂。

{
    "col1": {
        "0": 0,
        "1": 8,
        "2": {
            0: 2,
        }
        "3": 4,
        "4": 5
    },
    "col2": {
        "0": "na",
        "1": 1,
        "2": "na",
        "3": "na",
        "4": "na"
    },
    "col3": {
        "0": 1,
        "1": 3,
        "2": 3,
        "3": 6,
        "4": 3
    },
    "col4": {
        "0": 5,
        "1": "na",
        "2": "9",
        "3": 9,
        "4": "na"
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找最短和最快的功能来实现这一目标。还有其他问题,例如在嵌套字典中将 python 中的字典值从 str 转换为 int ,这些问题建议了这样做的方法,但没有一个涉及字典的“变量嵌套”性质。

任何想法将不胜感激。

python dictionary

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

如何使用 django-cors-headers 从数据库动态填充 CORS_ORIGIN_WHITELIST?

我正在编写一个 Django 应用程序,其中有一个名为 的模型Website,其中包含人员网站。我只允许在我的数据库中拥有网站的人使用我的 Django REST API。我正在使用该django-cors-headers包将人们的域列入白名单: https: //github.com/adamchainz/django-cors-headers。

CORS_ORIGIN_WHITELISTsettings.py 中的变量允许我将域列入白名单,如https://github.com/adamchainz/django-cors-headers#cors_origin_whitelist所示

问题是我必须查询我的模型以获取网站域,将它们附加到列表中,然后将该列表放入CORS_ORIGIN_WHITELIST. 但我无法在 settings.py 中执行此操作,因为模型是在应用程序启动后加载的,而 settings.py 是启动应用程序的模型。

有谁知道解决办法吗?任何建议将不胜感激。提前致谢。

python django

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