小编the*_*ish的帖子

什么时候python删除变量?

我知道python有一个自动垃圾收集器,因此它应该在没有更多引用时自动删除变量.

我的印象是局部变量(函数内部)不会发生这种情况.

def funz(z):
    x = f(z) # x is a np.array and contains a lot of data
    x0 = x[0]
    y = f(z + 1) # y is a np.array and contains a lot of data
    y0 = y[0]

    # is x and y still available here?
    return y0, x0
Run Code Online (Sandbox Code Playgroud)

del x是节省内存的正确方法吗?

def funz(z):
    x = f(z) # x is a np.array and contains a lot of data
    x0 = x[0]
    del x
    y = f(z + 1) # …
Run Code Online (Sandbox Code Playgroud)

python memory garbage-collection

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

Spring Boot Security - 如果缺少授权标头,则使用 Cookie 中的令牌

我目前根据授权服务器的 JWK 验证我的请求。我spring-boot-starter-oauth2-resource-server在 spring-boot 2.3 上使用该包。JWT 从标头中取出Authorization: Bearer <token>并针对 JWK 端点进行验证。我的安全配置如下所示:

安全配置.java

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  protected Environment env;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.authorizeRequests().antMatchers("/logs").permitAll();
    http.authorizeRequests().antMatchers("/", "/api/**").authenticated();
    http.oauth2ResourceServer().jwt();
  }
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://auth.work.com/v1/jwk
Run Code Online (Sandbox Code Playgroud)

外部用户的请求中没有Authorization: ...标头,而是具有以下 2 个构成 JWT 的 cookie:

auth.token_type = Bearer
auth.access_token = <token>
Run Code Online (Sandbox Code Playgroud)

有没有办法从 cookie 中提取 JWT 并在标头Authorization: ...丢失时针对身份验证服务器进行验证?我可以在尝试授权之前提取 cookie 并向请求添加标头吗?或者它甚至可能是身份验证链中的第二种方法。

java spring spring-security jwt spring-boot

8
推荐指数
2
解决办法
7271
查看次数

Xpath 使用 Scrapy 获取下一个兄弟标签中的信息

我正在尝试使用 Scrapy,现在我尝试从词源网站中提取信息:http : //www.etymonline.com 现在,我只想获取单词及其原始描述。这是 etymonline 中常见的 HTML 代码块的呈现方式:

<dt>
  <a href="/index.php?term=address&allowed_in_frame=0">address (n.)</a>
  <a href="http://dictionary.reference.com/search?q=address" class="dictionary" title="Look up address at Dictionary.com">
    <img src="graphics/dictionary.gif" width="16" height="16" alt="Look up address at Dictionary.com" title="Look up address at Dictionary.com"/>
  </a>
</dt>
<dd>
  1530s, "dutiful or courteous approach," from <a href="/index.php?term=address&allowed_in_frame=0" class="crossreference">address</a> (v.) and from French <span class="foreign">adresse</span>. Sense of "formal speech" is from 1751. Sense of "superscription of a letter" is from 1712 and led to the meaning "place of residence" (1888).
</dd> …
Run Code Online (Sandbox Code Playgroud)

html python xpath scrapy

7
推荐指数
2
解决办法
8095
查看次数

当Django models字段为空时,将value设置为Default value

每当用户不添加值时,我都需要Django模型用中设置的值替换原本为空的字段default
我的模型如下所示:

not_before = models.TimeField(blank=True, null=True, default='00:00:00')
max_num_per_day = models.IntegerField(blank=True, null=True, default=0)
Run Code Online (Sandbox Code Playgroud)

我试过的每个组合nullblankdefault但无论我做什么,字段被替换null,而不是'00:00:00'0

无论如何,default只要字段为空,我是否可以将其强制为值?

python django django-models

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

从bash命令中删除最终位置参数

我有一个名为dosmt的脚本,我输入了几个args然后打印了一些东西:

if [ "${@: -1}" == "--ut" ]; then
    echo "Hi"
fi
Run Code Online (Sandbox Code Playgroud)

我要做的是删除最后一个位置参数,即--ut如果该语句为真.因此,如果我的输入是$ dosmt hello there --ut,它会回应Hi,但如果我打印args之后,我只想拥有hello there.所以基本上我试图删除最后一个参数为好,我尝试使用shift但这只是暂时的,所以这不起作用...

linux bash

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

Rails 模型 - 更新 JSON 中的嵌套键/值

我有一个具有以下表格格式的模型:

string  "name"
integer "line_id"
json    "filters"
Run Code Online (Sandbox Code Playgroud)

其中filters字段包含带有嵌套键的 json 对象。我想修改特定的键而不覆盖 json 的其余部分。

目前,存储的 json 对象filters如下所示

{
  "ext": {"name": "filter", "id": 3},
  "int": {"name": "numb", "id": 1}
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将int.name的值更新为 ,"remove"而不修改 json 对象的其余部分。

如果我执行以下操作,它将简单地覆盖整个 json 对象,而不是修改该特定键:

Model.where("filters->>'int'->>'name' IS NOT NULL").update(
  filters: {
    int: {
      name: "remove"
    }
  }
)
Run Code Online (Sandbox Code Playgroud)

如何简单地用路径更新该一个键int.name,同时保持其余属性相同?

ruby postgresql json ruby-on-rails

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

无效语法 - 表达式返回 f-String 中的字符串

我喜欢 python 3.6 中的新 f-Strings,但是在尝试在表达式中返回 String 时我看到了一些问题。下面的代码不起作用并告诉我我使用了无效的语法,即使表达式本身是正确的。

print(f'{v1} is {'greater' if v1 > v2 else 'less'} than {v2}') # Boo error
Run Code Online (Sandbox Code Playgroud)

它告诉我,'greater'并且'less'是意料之外的标记。如果我用两个包含字符串的变量甚至两个整数替换它们,错误就会消失。

print(f'{v1} is {10 if v1 > v2 else 5} than {v2}') # Yay no error
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

python string python-3.x f-string

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

在 bash 的 case 语句中使用命令参数

我有一个 case 语句,它是我命令的一部分,我希望它遍历所有 args,直到它们都被消耗掉。

while [ ! -z ${@:2} ] ; do
    case "${@:2}" in
        -d|--delete*)
            YES_DELETE=1
            shift
            ;;        
        -nd|--nodelete*)
            NO_DELETE=1
            shift
            ;;
        -y|--yes*)
            SKIP_PROMPT=1
            shift
            ;;
        -*|*)
            echo "Bad command, try again."
            ;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

我的命令$@$ mpip stt=12 -nd -y使${@:2}="-nd -y". 如果它也像我想要的那样工作,它会在迭代两次后退出 while 循环并且NO_DELETE=1 SKIP_PROMPT=1为真。当我运行它时,一切都还没有初始化,它变成了一个无限循环,我不知道我做错了什么。

linux bash case

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