AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-595305

mike rodent's questions

Martin Hope
mike rodent
Asked: 2025-04-10 15:15:18 +0800 CST

如何让 git diff 在两次提交之间产生更简单的输出?

  • 5

我查看了很多关于git diffbetween 2 commits 功能的解释,但我仍然对得到的输出感到困惑。我相信我看到的是“组合格式”,据说是“默认”格式,但如果是这样的话,我不知道还有哪些其他可用的格式。

例子:

>git diff af738ab0..bbbec26d > gitdiff_2025-04-09B.diff

通常我会遇到这样的情况:

...
+diff --git a/src/core/history_table_view.py b/src/core/history_table_view.py
+index 5b18236..05b262a 100644
+--- a/src/core/history_table_view.py
++++ b/src/core/history_table_view.py
+@@ -14,188 +14,279 @@ class HistoryTableView(QtWidgets.QTableView):
+     @thread_check(True)
+     def __init__(self, *args):
+         super().__init__(*args)
+-        self.setObjectName('history table')
+-        self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
+-        self.horizontalHeader().setStretchLastSection(True)
+-        self.horizontalHeader().hide()
+-        self.verticalHeader().hide()
+-        self.setModel(HistoryTableModel(self))
...

...在此部分输出中,它将第一次提交中的文件 history_table_view.py 与第二次提交中的相同文件进行比较。

上述所有行都存在于两个文件中。那么为什么这一行

+     def __init__(self, *args):    

以一个简单的“+”开头,而这一行

+-        self.setObjectName('history table')

以“+-”开头?这是什么意思?正如我所说,这两行在该文件的两个版本中都存在。我原则上希望 agit diff不会显示任何这些行(尽管在包含文本的行之间,空行方面存在一些差异)。

其次,当我看到一个简单的介绍git diff,比如这个,演示者在git diff两次提交时做了一个,在视频的 4:40 处我们看到了输出……但在这种情况下,他看到的不是行首的双符号,而是简单的“+”或“-”,也就是说,据我所知,这就是在diff两个文件之间执行 BASH 命令时可能出现的。我想要的就是这种简单的格式。

我怀疑可能有其他方法可以替代git diff“组合格式”,确实能产生这种更简单的输出,但我一直没找到。其次,我上面提到的视频是7个月前制作的:为什么那个Youtube用户在比较两个提交时默认得到的是简单的git输出(对我来说完全可以理解),而我得到的却是(对我来说)难以理解的输出?我的git版本是:

git version 2.48.1.windows.1

一个简单的链接,指向一个页面,它git diff用清晰的术语真正解释了比较两个提交的具体操作,并且关键地展示了如何获得一个简单的diff,这或许就是我需要的。我搜索了很久,却没有找到。

稍后。
请问 daoh-nvohter 能否解释一下为什么这个问题不值得问?

git
  • 1 个回答
  • 49 Views
Martin Hope
mike rodent
Asked: 2024-09-23 02:49:10 +0800 CST

在深度优先非递归树遍历器上产生深度?

  • 4

我正在尝试寻找一种非递归的“强大/多功能”树遍历算法,最终不仅可以产生节点,还可以产生节点的深度、其父节点和其兄弟索引,并且能够使用广度优先搜索(BFS)或深度优先搜索(DFS)。

可以像这样将深度优先和面包优先结合起来,只需产生节点(NB 假设节点可能有也可能没有键CHILD_NODES)。使用 Python 的示例 - 添加了“Python”标签但显然不具体:

def get_walker(walk_by_depth=True):
    def walk(root):
        queue = [root]
        while len(queue) > 0:
            # the first item in the queue
            node_to_yield = queue.pop(0)

            yield node_to_yield

            if CHILD_NODES in node_to_yield:
                depth += 1    
                new_nodes = node_to_yield[CHILD_NODES]
                if walk_by_depth:
                    queue = new_nodes + queue
                else:
                    queue.extend(new_nodes)
    return walk

...并添加少量代码让您仅产生广度优先搜索的深度:

def get_walker(walk_by_depth=True):
    def walk(root):
        queue = [root]
        depth = 0 
        depth_map_of_remaining_items = {0: 1}
        while len(queue) > 0:
            node_to_yield = queue.pop(0)
            if depth_map_of_remaining_items[depth] == 0:
                depth += 1
            depth_map_of_remaining_items[depth] -= 1

            yield node_to_yield, depth

            if CHILD_NODES in node_to_yield:
                depth += 1    
                new_nodes = node_to_yield[CHILD_NODES]
                if walk_by_depth:
                    queue = new_nodes + queue
                else:
                    queue.extend(new_nodes)
                    if depth not in depth_map_of_remaining_items:
                        depth_map_of_remaining_items[depth] = 0
                    depth_map_of_remaining_items[depth] += len(new_nodes)    
                depth -= 1
    return walk

上面的代码实际上不起作用walk_by_depth=True:它不会引发异常,只是深度错误。我本能地觉得代码可能只需要进行最小的调整就可以depth在 DFS 上产生(正确),但到目前为止我还没有成功。

问题是,如果我能找到一种使用 DFS 得出深度的方法,我相信对于 BFS 和 DFS 来说,通过维护“深度到最后一个节点”映射,得出父节点将是一个相当容易的步骤。如果您可以获得父节点,那么您也可以获得兄弟节点索引,最简单的方法是使用list的index方法(尽管可能有更聪明的方法来同时获得父节点和兄弟节点索引……)。

python
  • 2 个回答
  • 42 Views
Martin Hope
mike rodent
Asked: 2024-02-12 00:58:21 +0800 CST

如何测试我的 `__main__` 文件的作用?

  • 4

我正在使用 pytest。注意 W10。
我将通过运行我的应用程序> python src/core。“core”是我的中心模块,带有一个__main__.py文件。

因此,我将“tests”目录放在[项目根目录]/src/core 中。

我有一个测试文件 test_xxx.py。在其中我尝试导入__main__:

import __main__

... 

def test_xxxxx():
   print(f'main {__main__} type {type(__main__)}')

这将打印以下内容:

main <module '__main__' from 'D:\\apps\\Python\\virtual_envs\\doc_indexer v2.0.0\\Scripts\\pytest.exe\\__main__.py'> type <class 'module'>

...换句话说,它不是从本地“核心”模块导入我的本地模块,而是从其模块__main__.py导入 pytest 自己的模块。__main__到目前为止还算可以理解。然后我查看 sys.path:

path: D:\My Documents\software projects\EclipseWorkspace\doc_indexer\src\core\tests
path: D:\My Documents\software projects\EclipseWorkspace\doc_indexer\src\core
path: D:\apps\Python\virtual_envs\doc_indexer v2.0.0\Scripts\pytest.exe
path: D:\apps\Python\PyQt5
...

现在我正在挠头,这不是第一次使用 pytest。此 ...src\core 路径列在...Scripts\pytest.exe之前。为什么 pytest在从它之前的路径导入之前会识别自己的__main__.py并导入它?__main__.py

我也尝试过各种实验importlib。似乎什么都不起作用。

那么一般来说,有什么方法可以实际导入该文件 ...src/core/ 吗__main__py?

我意识到这个文件在传递到另一个文件之前应该做最少的事情,但为了完整性,我只想测试它main()实际上是否做到了这一点。有什么办法可以__main__.main()在测试中运行吗?

python
  • 1 个回答
  • 41 Views
Martin Hope
mike rodent
Asked: 2023-11-14 16:45:06 +0800 CST

跳出 Vec::retain() 吗?

  • 5

我想迭代 aVec对其中的对象执行各种操作(这比usizes 更复杂),并且如果命令停止迭代,则仅保留尚未处理的对象。

fn main() {
    let _ = iterate();
}

fn iterate(){
    let mut objs_for_processing = Vec::new();
    
    for i in 0..10 {
        objs_for_processing.push(i);
    }
    
    let mut n = 0;
    objs_for_processing.retain(|obj| {
        n += 1;
        if n > 3 {
            println!("STOP iteration!...");
            return true // i.e. retain
        }
        println!("iteration, do some business with obj {}", obj);
        false
    });
    println!("objs_for_processing.len() {} {:#?}", objs_for_processing.len(), objs_for_processing);
}

很明显,上述问题是您必须完成所有迭代,即使您知道要保留所有其他元素......

我发现了这个问题和这个问题。后者似乎更有希望。但事实证明,据我所知,您无法更改 闭retain包的返回类型(即接受 a Result,因此您可以返回 an Err,这将停止迭代)。

我发现的唯一解决方案是克隆Vec,使用普通迭代,然后在处理时for ... in从克隆(称为 )中删除项目,使用相当麻烦的depleted_vec

let index = depleted_vec.iter().position(|x| x == obj).unwrap();
depleted_vec.remove(index);

...然后分配objs_for_processing给depleted_vec. 至少我可以用这种方式break来停止迭代。

这似乎是一件相当合理的事情:难道没有一些更优雅的方法来做到这一点吗?

注意,我还想知道是否可能有一个Iterator基于 - 的解决方案,所以我尝试使用iter().filter(...).collect()... 这会产生Veca &usize,而不是usize。因此它也许可以使用,但需要额外的处理成本。不漂亮:只是感觉应该有更好的东西。

rust
  • 2 个回答
  • 38 Views
Martin Hope
mike rodent
Asked: 2023-10-04 18:41:10 +0800 CST

在 Rust 中执行 Elasticsearch“滚动”命令时出现令人费解的 400 Bad Request

  • 5

我一直在使用“scroll”从 Python 中的 ES 索引(v 8.6.2)获取数据序列。

由于各种原因,我想更改为reqwest使用 Rust (PyO3) 执行请求,而不是requests使用 Python 执行请求。我的第一个要求是:

let es_url = "https://localhost:9500"; // ES server is using this port
let data = json!({
    "size": 100,
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": [
                {
                    "term": {
                        "ldoc_type": "docx_doc"
                    }
                }
            ]
        }
    },
});
let data_string = serde_json::to_string(&data).unwrap();
let url = format!("{es_url}/{}/_search?scroll=1m", INDEX_NAME.read().unwrap());
let send_result = reqwest_client
.post(url)
.header("Content-type", "application/json")
.body(data_string)
.basic_auth("mike12", Some("mike12"))
.send(); 
let response = match send_result {
    Ok(response) => response,
    Err(e) => {
        error!("SEND error was {}", e);
        return false
    },
};
let text = match response.text() {
    Ok(text) => text,
    Err(e) => {
        error!("TEXT error was {}", e);
        return false
    },
};
let json_hashmap: HashMap<String, Value> = serde_json::from_str(&text).unwrap();
let scroll_id = &json_hashmap["_scroll_id"];
let scroll_id_str = scroll_id.to_string();
let scroll_id_str = rem_first_and_last(&scroll_id_str); // strip "\"" either side
info!("|{:#?}| type {}", scroll_id_str, utilities::str_type_of(&scroll_id_str));

...这显示返回的&str“滚动 ID”。一个典型的例子是:

“==”

...这很奇怪,因为这个id字符串大约是我使用Python时得到的ID字符串的4倍长。但上面的字符串虽然看起来有一定的重复字符,但实际上并不是较小字符串的重复。

之后,为了重复请求以获得更多批次的 100 个点击,我这样做:

let mut n_loops = 0;
loop {
    n_loops += 1;
    let data = json!({
        "scroll": "1m",
        "scroll_id": scroll_id_str,
    });
    let data_string = serde_json::to_string(&data).unwrap();
    let url = format!("{es_url}/_search?scroll");
    let send_result = reqwest_client
    .post(url)
    .header("Content-type", "application/json")
    .body(data_string)
    .basic_auth("mike12", Some("mike12"))
    .send(); 
    let response = match send_result {
        Ok(response) => response,
        Err(e) => {
            error!("SEND error was {}", e);
            return false
        },
    };
    info!("response {:?} type {}", response, utilities::str_type_of(&response));
    let response_status = response.status();

...这是我收到 400“错误请求”的地方。我最初怀疑是滚动 ID 错误。在Python中,等效的“后续”“滚动”请求工作正常。

elasticsearch
  • 1 个回答
  • 13 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve