小编Hob*_* C.的帖子

如何避免“IndexError:列表索引超出范围”错误?

假设有一个名为“ my_list”的列表和一个名为“ ”的 int 变量list_index。基本上,列表 ' my_list' 可能会随着时间的推移而改变,因此 ' list_index' 可能会提高 ' IndexError: list index out of range'。但是,我只想在发生此错误时进行记录,因为它并不那么重要。为了避免这个错误,我目前的基本解决方案是:

# My first way
if my_list[list_index: list_index +1]: 
    print('item exists')
else:
    print('item does not exist')

# My second way
if -len(my_list) <= list_index < len(my_list):
    print('item exists')
else:
    print('item does not exist')
Run Code Online (Sandbox Code Playgroud)

除了try/except语句,是否还有其他解决方案可以避免“IndexError: list index out of range”错误?

python

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

一个空的提交在git中有一个空树吗?

我读了这篇Git Internals,关于树对象的一些事情不是很清楚。

例如,当前分支是master。master 上的最终提交对象是一个空提交,用 表示cmt_end。由 给出git-commit-tree,提交对象只有一个树对象。因此, 的树对象cmt_end用 表示tree_end

我的困惑是:

  • 由 给定git-commitgit-commit记录存储库中的更改。这是否表明包含与其父级tree_end之间的差异?cmt_end如果是这样,tree_end应该是一个空树,因为cmt_end是一个空提交。如果不是,是否可以为空提交提供一个空树?
  • --allow-empty中的选项表示git-commit空提交与其唯一父提交具有完全相同的树。这是否表明提交的树对象记录了整个工作目录而不是执行时的更改git-commit

git

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

我可以缩短 git 存储库的历史记录吗

首先,我做了一个浅克隆。之后,我发现一些旧的提交引入了一些大文件。例如,这些旧的提交一次又一次地替换了二进制文件。

现在,我不希望我的本地存储库包含这些重大更改。如何在不重新创建新存储库的情况下缩短历史记录git clone --depth=10

# shallow clone
git clone --depth 100 ssh://git@10.7.222.111:/home/my_repository01.git
git log --oneline | wc -l  # the result is 100

# Now, how can I shorten the current history to 10? 
# Otherwise, I need to execute `git clone --depth=10` to recreate the repository.
Run Code Online (Sandbox Code Playgroud)

git git-repo git-history

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

为什么添加中间件打印HTTP请求体时fastapi会挂起?

我使用中间件来打印 HTTP 请求正文,以避免print每个函数中出现语句。但是,运行客户端代码时fastapi没有任何响应。

服务器简化为以下代码:

import typing

import uvicorn
from fastapi import FastAPI
from fastapi import Request, Body

app = FastAPI()


@app.middleware('http')
async def debug_request(request: Request, call_next):
    _body = await request.body()
    print(_body)
    #
    response = await call_next(request)
    return response


@app.put("/")
def _(
    _body: typing.Dict
):
    # print(_body)  # this statement is replaced by the middleware
    return {"detail": "ok"}


if __name__ == '__main__':
    uvicorn.run(app, host='localhost', port=8000)
Run Code Online (Sandbox Code Playgroud)

客户端代码如下:

import requests

_url = 'http://localhost:8000/'
_json = {
    'row_id': '1'
}
resp = …
Run Code Online (Sandbox Code Playgroud)

python starlette fastapi

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

标签 统计

git ×2

python ×2

fastapi ×1

git-history ×1

git-repo ×1

starlette ×1