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-3387716

Fravadona's questions

Martin Hope
Fravadona
Asked: 2025-03-06 18:08:55 +0800 CST

PowerShell `-Path` cmdlet 的通配符/通配符问题

  • 7

我有一些文件名称中带有“元字符”,例如括号:

'test[txt].0',  'test[xml].0', 'test[xml].1' | % {New-Item $_} | Out-Null

我可以使用 glob 获取这些文件:

Resolve-Path -Path 'test*'

Path
----
C:\test[txt].0
C:\test[xml].0
C:\test[xml].1

或者使用反引号转义符:

Resolve-Path -Path 'test`[xml`].0'

Path
----
C:\test[xml].0

test[xml].但我无法获得以使用反引号转义符和“通配符”开头的那些:

Resolve-Path -Path 'test`[xml`].*'

-Path正确的用法是什么?

powershell
  • 3 个回答
  • 103 Views
Martin Hope
Fravadona
Asked: 2025-03-06 01:18:09 +0800 CST

如何在 PowerShell 中检查路径是否指向文件或目录?

  • 7

我想验证用户提供的字符串是否是文件或目录的路径。

$Path = 'some input string'

if (!(Test-Path -LiteralPath $Path)) {
  Write-Error "${Path}: No such file or directory"
  exit 1
}

问题是Test-Path无法区分例如目录和注册表项......

我该怎么做?

powershell
  • 2 个回答
  • 46 Views
Martin Hope
Fravadona
Asked: 2024-12-11 04:53:16 +0800 CST

如何选择结束标记之前的文本节点(但前提是有一个)

  • 6

我想提取元素结束标记之前的文本节点a。例如,对于此示例 XML:

<root>
  <a/>
  <a>1</a>
  <a>2<b/>3</a>
  <a>4<b/></a>
  <a><b/>5<c/></a>
</root>

预期结果将是序列( 1, 3 )

该表达式//a/text()[last()]是不够的,因为它返回( 1, 3, 4, 5 ),问题是我不知道如何添加约束:“就在”的结束标记之前a。

xpath
  • 2 个回答
  • 29 Views
Martin Hope
Fravadona
Asked: 2024-12-07 03:26:22 +0800 CST

用 jq 的 `IN` 替换选择条件

  • 5

给出简化的输入:

[
  [ "gpu" ],
  [ "disk" ]
]

我想选择第一个元素为的数组"gpu"。

我可以用一个简单的条件来做到这一点:

map(select(.[0] == "gpu"))
[
  [ "gpu" ]
]

但是我不明白当我尝试使用IN内置函数时出了什么问题:

map(select(.[0] | IN(["gpu"])))
[]

我用的是jq1.6

jq
  • 1 个回答
  • 33 Views
Martin Hope
Fravadona
Asked: 2024-11-24 06:46:55 +0800 CST

XPath/XQuery 过程分组

  • 5

我有一个 XML 文件:

<books>
  <title>Moby-Dick</title>
  <author>Herman Melville</author>
  <title>Sunrise Nights</title>
  <author>Jeff Zentner</author>
  <author>Brittany Cavallaro</author>
  <price>14.52€</price>
  <title>My Salty Mary</title>
  <author>Cynthia Hand</author>
  <author>Brodi Ashton</author>
  <author>Jodi Meadows</author>
</books>

我想将其转换为:

<books>
  <book>
    <title>Moby-Dick</title>
    <author>Herman Melville</author>
  </book>
  <book>
    <title>Sunrise Nights</title>
    <author>Jeff Zentner</author>
    <author>Brittany Cavallaro</author>
    <price>14.52€</price>
  </book>
  <book>
    <title>My Salty Mary</title>
    <author>Cynthia Hand</author>
    <author>Brodi Ashton</author>
    <author>Jodi Meadows</author>
  </book>
</books>

逻辑是,book每次我们遇到一个时就创建一个新的title,并将每个后续的“非标题”节点放入该书中。

以下是我迄今为止尝试过的:

let $books := (
  doc("books.xml")/books/* =>
    fold-left((array{}, 0), function($acc, $node) {
      let
        $arr := $acc[1],
        $idx := $acc[2]
      return
        if (name($node) = "title")
        then ($arr => array:append($node), $idx+1)
        else ($arr => array:put($idx, ($arr($idx), $node)), $idx)
    })
  )[1]
return
  <books>{
    for $book in $books
    return <book>{$book}</book>
  }</books>

但我明白

<books>
  <book>
    <title>Moby-Dick</title>
    <author>Herman Melville</author>
    <title>Sunrise Nights</title>
    <author>Jeff Zentner</author>
    <author>Brittany Cavallaro</author>
    <price>14.52€</price>
    <title>My Salty Mary</title>
    <author>Cynthia Hand</author>
    <author>Brodi Ashton</author>
    <author>Jodi Meadows</author>
  </book>
</books>

旁白: group by这似乎对解决当前问题没有用,所以我尝试将书籍分组到一个数组中,但我不知道这是否是正确的方法;欢迎任何建议。

xpath
  • 1 个回答
  • 46 Views
Martin Hope
Fravadona
Asked: 2024-10-06 07:13:16 +0800 CST

是否可以插入 shadowRoot 的样式?

  • 6

我正在尝试使用 Vanilla 自定义元素,但似乎无法插入样式标签:

class MyElement extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode: "open"});
  }
  connectedCallback() {
    this.shadowRoot.append(document.getElementById('my-element-template').content.cloneNode(true));
  }
}
window.customElements.define("my-element", MyElement);
<template id="my-element-template">
  <slot name="my-style">
    <style>
      p { color: red; }
    </style>
  </slot>
  <p>Why am I still red instead of green?</p>
</template>

<my-element>
  <style slot="my-style">
    p { color: green; }
  </style>
</my-element>

javascript
  • 1 个回答
  • 66 Views
Martin Hope
Fravadona
Asked: 2024-09-22 21:44:25 +0800 CST

使用 Select2 组件时如何定位浏览器验证消息

  • 5

我正在尝试<select>用现有表单中的 Select2 替换标准。表单逻辑是用 Vanilla JS 编写的,客户端验证留给浏览器。

问题是当我使用 Select2 组件时浏览器生成的错误消息的位置:

document.querySelectorAll("[data-is=select2]").forEach(el => {
  // the Select2 component only works with JQuery objects
  let $jqo = $(el);
  $jqo.select2({
    width: "auto",
    placeholder: el.firstElementChild.textContent
  });
  // making sure select2 works with the existing handlers
  $jqo.on("select2:select", ev => el.dispatchEvent(new Event("change")));
});
form {
  width: 20rem;
}
label {
  display: flex;
  align-items: center;
  column-gap: 1ch;
}
/* making select and Select2 occupy the remaining space */
select, .select2-container {
  flex-grow: 1;
}
<script src="https://code.jquery.com/jquery-3.7.1.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />

<!-- Standard select example --!>
<form>
  <p>
    <label>
      Greetings:
      <select name="greetings" required>
        <option hidden disabled value="" selected>Select a Greetings</option>
      </select>
    </label>
  <p>
    <input type="submit" value="Standard select">
</form>

<hr>

<!-- Standard select replaced with a Select2 component !-->
<form>
  <p>
    <label>
      Greetings:
      <select data-is="select2" name="greetings" required>
        <option hidden disabled value="" selected>Select2 a Greetings</option>
      </select>
    </label>
  <p>
    <input type="submit" value="Select2 select">
</form>

如您所见,单击按钮后,Select2 的验证消息被放置在<label>而不是 下方<select>。

我该如何修复它?最好不使用 JQuery。

javascript
  • 1 个回答
  • 52 Views
Martin Hope
Fravadona
Asked: 2024-09-06 05:21:30 +0800 CST

使用 JavaScript 解析嵌入的 XML

  • 5

script我无法使用Vanilla JavaScript解析标签中嵌入的 XML 文档:

document.addEventListener('DOMContentLoaded', function() {
  const xmlStr = document.getElementById('xml').innerText;
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlStr,'text/xml');
  const barText = xmlDoc.getElementById('bar').textContent;
  alert(barText)
});
<body>

  <h1>Parsing &lt;script type="text/xml"&gt with JavaScript</h1>

  <script id="xml" type="text/xml">
<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar id="bar">Hello world!</bar>
</foo>
  </script>

</body>

这里有什么问题?

javascript
  • 1 个回答
  • 23 Views
Martin Hope
Fravadona
Asked: 2024-06-10 08:43:55 +0800 CST

如何使用 XPath 3.1 生成 JSON 字符串

  • 4

我在节点中有一些文本,例如:

<xml>&lt;Hello world "&amp;" foo bar&gt;</xml>

我想string(/xml)对其进行处理和 JSON 转义,以便结果类似于:

"<Hello world \"&\" foo bar>"

我怎样才能用 XPath 3.1 做到这一点?

xpath
  • 1 个回答
  • 31 Views
Martin Hope
Fravadona
Asked: 2024-06-10 05:16:13 +0800 CST

如何区分 XPath 3.1 中的 Map、Array、String 等?

  • 5

是否有一个函数可以测试(或返回) 的类型.?例如:

( root(), (), "a", 1, map{}, array{} ) ! unknown:type-of(.)
node
sequence
string
number
map
array
xpath
  • 1 个回答
  • 35 Views
Martin Hope
Fravadona
Asked: 2024-05-26 16:00:49 +0800 CST

AWK 相当于 `read -r __ 余数`

  • 8

假设您有一个文件,其中包含 N 个以空格分隔的列和一个附加列,其中包含您想要保留的空格。

N = 2 的示例:

1.1 1.2 data for row1
  2.1   2.2    data   for    row2
?  ?   data   for   row3
 \ * data for   row4

我想输出:

data for row1
data   for    row2
data   for   row3
data for   row4

在 shell 中,您可以使用以下命令轻松完成此操作:

while read -r _ _ data
do
    printf "%s\n" "$data"
done < data.txt

但awk这样做有点困难。有没有一种方法awk可以只拆分前 N 列?

bash
  • 3 个回答
  • 85 Views
Martin Hope
Fravadona
Asked: 2023-09-22 04:05:39 +0800 CST

如何撤销2阶组合?

  • 7

我有一组对:

inputs = {
    ('id1', 'id2'), ('id1', 'id3'), ('id1', 'id4'),
    ('id2', 'id3'), ('id2', 'id4'),
    ('id3', 'id4'), ('id3', 'id5'),
    ('id4', 'id5'),
    ('id5', 'id6'),
}

我想反转 2 阶的组合,如下所示:

recombinations = [
    ('id1', 'id2', 'id3', 'id4'),
    ('id3', 'id4', 'id5'),
    ('id5', 'id6'),
]

我设法使用暴力来做到这一点:

ids = list(sorted( {i for i in itertools.chain(*inputs)} ))

excludes = set()
recombinations = {tuple(i) for i in map(sorted, inputs)}

for i in range(3, len(ids)+1):
    for subset in itertools.combinations(ids, i):
        for j in range(i-1, len(subset)):
            combs = set(itertools.combinations(subset, j))
            if all(tup in recombinations for tup in combs):
                recombinations.add(subset)
                excludes = excludes.union(combs)

for tup in excludes:
    recombinations.remove(tup)

print(recombinations)
{('id1', 'id2', 'id3', 'id4'), ('id3', 'id4', 'id5'), ('id5', 'id6')}

我的问题是:是否有更智能的方法来做到这一点,或者我可以在代码中实现一些优化?

python
  • 1 个回答
  • 80 Views
Martin Hope
Fravadona
Asked: 2023-09-06 22:02:20 +0800 CST

python列表理解:字典列表到具有键交集的列表字典

  • 6

我有一个包含可变数量字典的列表,例如:

var = [ {'a': 1, 'b': 2}, {'b': 20, 'a': 10, 'c': 30}, {'c': 300, 'a': 100} ]

我需要提取所有字典共有的键,列出它们的关联值,从中创建一个新字典,并将其存储在同一个变量中:

预期结果是:

var = { 'a': [1, 10, 100] }

我可以找到键的交集:

[k for k in var[0] if all(k in d for d in var[1:])]

但是如何完成其​​余的转换呢?

python
  • 2 个回答
  • 56 Views
Martin Hope
Fravadona
Asked: 2023-08-27 08:03:33 +0800 CST

如何使用仅输出顺序的模糊程序对大量文件进行排序

  • 7

我的一位同事想要运行一个 FORTRAN 程序,该程序接受文件参数并根据一些模糊的生物物理化学标准输出它们的顺序(最好是第一个)。他需要的是最好的10个结果。

虽然文件不大,但问题是他有一个bash: /home/progs/bin/ardock: Argument list too long,所以我创建了文件的 6 位长符号链接并将它们作为参数,这有效;-)

现在,如果文件数量确实太大,上述技巧无法发挥作用,那么您可以采取什么措施来从所有文件中获得 10 个最好的文件呢?您是否必须按块对文件进行排序,并使用类似的方法将最好的与最好的进行比较?

#!/bin/bash

best10() { ardock "$@" | head -n 10; }
export -f best10

find . -name '*.dat' -exec bash -c 'best10 "$@"' _ {} + |
xargs bash -c 'best10 "$@"' _ |
xargs bash -c 'best10 "$@"' _ |
xargs bash -c ... | ... | ...

这里的问题是,需要的数量xargs是事先不知道的,那么如何使其成为一个循环呢?

bash
  • 2 个回答
  • 82 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