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

Gordon's questions

Martin Hope
Gordon
Asked: 2025-04-02 21:14:57 +0800 CST

使用前导 0 和尾随 0 格式化小数

  • 4

ChatGPT 和 Copilot 都让我失望了。我试图为时间添加填充,以便为整秒提供两位数,为小数提供三位数。因此,给定$time由

$timer = [Stopwatch]::StartNew()
$timer.Stop()
$time = $timer.Elapsed.TotalSeconds

人工智能建议如下

$time = [Math]::Round($time, 3)
$time = "{0:000.000}" -f $time

当我尝试像这样使用结果时间时

"$($time): $function"

小数点左边有一个 0,并且没有添加尾随零。

我(和人工智能)哪里做错了?

powershell
  • 1 个回答
  • 39 Views
Martin Hope
Gordon
Asked: 2024-11-04 16:25:56 +0800 CST

强制同步处理

  • 6

我之前有使用 PowerShell 的经验,现在正在尝试理解 Swift 并发。我的第一个与并发相关的项目是一个用于批量转录音频文件的实用程序,我正在努力理解一些基础知识。

我在这里尝试做的只是获取音频文件列表,然后循环显示正在处理的音频文件,并在 UI 中使用一个简单的计时器来模拟当前的转录工作。我发现 [这个][1] 允许我使用典型的asyncAfter计时器,但它不起作用。

我的期望是 UI 显示当前文件名,然后过一段时间控制台显示相同的文件名,而 UI 显示下一个文件名,但 UI 仅显示最后一个文件名,并且所有写入控制台的内容都表明基本上每个计时器都在并行运行。

在理想情况下,我希望并行转录所有内容,但这是另一个问题,它似乎SFSpeechURLRecognitionRequest不支持这一点。事实上,即使只是按顺序转录也给我带来了问题,但我甚至不能 100% 确定我是按顺序进行的,所以我试图强迫这样做,因此这里的代码失败了。

struct ContentView: View {
    @State private var fileName: String?
    @State private var files: [URL] = []
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("\(fileName ?? "-")")
        }
        .padding()
        .onAppear {
            files = getFiles()
            processFiles()
        }
    }
    
    
    private func getFiles() -> [URL] {
        do {
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let path = documentsURL.appendingPathComponent("Voice Memos").absoluteURL
            
            let contents = try FileManager.default.contentsOfDirectory(at: path, includingPropertiesForKeys: nil, options: [])
            
            let files = (contents.filter {$0.pathExtension == "m4a"}).sorted { url1, url2 in
                url1.path > url2.path
            }
            
            return files
        }
        catch {
            print(error.localizedDescription)
            return []
        }
    }
    
    private func processFiles() {
        for file in files {
            Task {
                await processFile(file)
            }
        }
    }
    
    private func processFile(_ url: URL) async {
        fileName = url.lastPathComponent
        let seconds = Double.random(in: 10.0...20.0)
        Task.synchronous {
            DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
                print("\(url.lastPathComponent) \(seconds)")
            }
        }
    }
}

extension Task where Failure == Error {
    /// Performs an async task in a sync context.
    ///
    /// - Note: This function blocks the thread until the given operation is finished. The caller is responsible for managing multithreading.
    static func synchronous(priority: TaskPriority? = nil, operation: @escaping @Sendable () async throws -> Success) {
        let semaphore = DispatchSemaphore(value: 0)

        Task(priority: priority) {
            defer { semaphore.signal() }
            return try await operation()
        }

        semaphore.wait()
    }
}

编辑:我也尝试过

let duration: UInt64 = 2_000_000_000 /* 2 seconds */
var seconds: UInt64 { duration/1_000_000_000 }
Task.synchronous { try await Task.sleep(nanoseconds: duration) }

作为计时器功能,它只是挂起了。至于那些立即投反对票的人,你能至少解释一下为什么这是一个如此令人反感的问题吗?

swift
  • 1 个回答
  • 39 Views
Martin Hope
Gordon
Asked: 2024-06-25 13:50:27 +0800 CST

字符串变量中 .Where() 的 Powershell 闭包

  • 5

我有一个从注册表中的卸载键派生的哈希表通用列表,我需要搜索它。每个列表成员都是包含这些键的有序字典。每个键的实际值是单独定义的,因为有些是直接来自注册表的值,有些是计算出来的。

[Ordered]@{
        displayName = 'DisplayName'
        displayVersion = 'DisplayVersion'
        installSource = 'InstallSource'
        publisher = 'Publisher'
        quietUninstallString = 'QuietUninstallString'
        uninstallParam = 'UninstallParam'
        uninstallPath = 'UninstallPath'
        uninstallString = 'UninstallString'
        installDate = 'InstallDate'
        keyID = '$keyID'
        keyName = '$keyName'
        keyGUID = '$keyGUID'
        keyPath = '$keyPath'
        architecture = '$architecture'
    } 

一旦我将这个集合放在变量中,$rawUninstallKeys我就需要搜索它,而搜索是复杂的,因为 Autodesk 往往有多个具有相同或相似数据的卸载键,但只有一个会起作用。例如,将有一个带有DisplayNameof 的GUID 键Autodesk Revit 2025,它提供实际有效的卸载字符串,而另一个带有 of 的 GUIDDisplayName什么Revit 2025也不做。因此,为了提取正确的 uninstallString,我需要搜索与第一个模式匹配的键,并且具有与第二个模式匹配的第二个键。实际上存在大量的变化,因为 Autodesk 在任何一致性方面都很差。所以,我现在可以正常工作,我只需对原始键执行 foreach 来寻找第一个模式,如果找到它,我会派生第二个模式(在本例中,我将“Autodesk”替换为$null),然后使用另一个 foreach 进行搜索。它确实有效,但天哪,它很慢。所以,我想尝试使用.Where()列表中的方法来获得 .NET 优化。但这意味着要构建一个要应用的闭包集合。

所以这会起作用

$rawUninstallKeys.Where({$_.publisher -like "Autodesk*"})

但我正在努力将闭包放入仍允许$_适当扩展的变量中。这进一步复杂化了以下事实:{$_.DisplayName}当我只需要查看该值是否存在时,或者{-not $_.DisplayName}当我只想要一个不包含该属性的键时,或者当第一个例子需要将属性值与另一个值进行比较时,这可能是一个通配符。实际上我需要做的是在闭包字符串中扩展变量,.Where()但这不起作用

.Where({$($ExecutionContext.InvokeCommand.ExpandString($closure))})
powershell
  • 1 个回答
  • 52 Views
Martin Hope
Gordon
Asked: 2024-06-01 19:24:13 +0800 CST

带有可选绑定参数的 SwiftUI 视图

  • 5

我正在尝试修改斯图尔特林奇的评级视图以支持框静态表示和设置评级的“控件”。为此,我需要一些可选属性来解决这两种不同的情况。当前评级也是如此 currentRating,以创建该数量符号的视图。 maxRating是最大可能额定值 & mutableRating是绑定电流额定值,因此我可以将短号值显示为填充符号,其余符号最多为maxRating未填充,以及未填充和删除符号以将额定值设置为 0。

我遇到问题的地方是使绑定参数也成为可选的。我发现这表明它应该是可能的,但这也相当古老。无论如何,我尝试了注释行中所示的各种排列,试图使初始化工作,但出现了各种不同的失败。

struct RatingView: View {
    var currentRating: Int?
    
    var maxRating: Int?
    @Binding var mutableRating: Int?
    
    var width:Int
    var color: UIColor
    var sfSymbol: String
    
    public init(
        currentRating: Int? = nil,
        maxRating: Int? = nil,
        mutableRating: Binding<Int?>,
//        mutableRating: Binding<Int>?,
//        mutableRating: (Binding<Int>)?,
        width: Int = 20,
        color: UIColor = .systemYellow,
        sfSymbol: String = "star"
    ) {
        self.currentRating = currentRating
        self.maxRating = maxRating
        self._mutableRating = mutableRating
//        self._mutableRating = mutableRating ?? Binding.constant(nil)
        self.width = width
        self.color = color
        self.sfSymbol = sfSymbol
    }
    
    public var body: some View {
        Text("")
    }
}

#Preview ("mutating") {
    struct PreviewWrapper: View {
        @State var rating: Int? = 3
        
        var body: some View {
            RatingView(
                maxRating: 5,
                mutableRating: $rating,
                width: 30,
                color: .red,
                sfSymbol: "heart"
            )
        }
    }
    return PreviewWrapper()
}

#Preview ("non mutating") {
    struct PreviewWrapper: View {
        
        var body: some View {
            RatingView(
                currentRating: 3,
                width: 20,
                color: .red,
                sfSymbol: "heart"
            )
        }
    }
    return PreviewWrapper()
}

所以,我的第一个问题是,我仍然可以在 iOS 17 中执行此操作吗?如果可以,如何执行?其次,我很好奇这三种 Init 参数方法之间有什么区别?

mutableRating: Binding<Int?>
mutableRating: Binding<Int>?
mutableRating: (Binding<Int>)?

编辑:故障澄清当我使用 mutableRating: Binding<Int?> & self._mutableRating = mutableRating 我进入Missing argument for parameter 'mutableRating' in call预览时non mutating。我认为这意味着该参数实际上不是可选的,考虑到所提到的差异,这是有道理的。如果我将参数行更改为 mutableRating: Binding<Int>?使参数本身可选,那么我会得到Cannot assign value of type 'Binding<Int>' to type 'Binding<Int?>' on the initialization line. One of the fix options offered suggests that this might work, self._mutableRating = mutableRating ?? nilbut that produces无法分配“Binding”类型的值?输入 'Binding<Int?>'` 这让我们回到了参数的变化。

swift
  • 2 个回答
  • 54 Views
Martin Hope
Gordon
Asked: 2024-04-07 18:55:04 +0800 CST

使用 StringWriter 扩展变量而不是逐字写入字符串的 XML 控制台

  • 5

当我处理代码以进行一些广泛的 XML 操作时,我一直在使用此函数将 XML 转储到控制台进行审查,并且在大多数情况下它按预期工作。

function Write-PxXmlToConsole {
    param (
        [Xml.XmlNode]$Xml
    )
    
    if ($Xml -is [Xml.XmlDocument]) {
        $xmlDocument = $Xml
    } else {
        $xmlDocument = [Xml.XmlDocument]::new()
        $newElement = $xmlDocument.ImportNode($Xml, $True)
        [Void]$xmlDocument.AppendChild($newElement)
    }

    $stringWriter = [System.IO.StringWriter]::new()
    $xmlWriter = [System.Xml.XmlTextWriter]::new($stringWriter)
    $xmlWriter.Formatting = "indented"

    $xmlDocument.WriteTo($xmlWriter)
    $xmlWriter.Flush()
    $stringWriter.Flush()

    Write-Host $stringWriter.ToString()
    Write-Host
}

然而,似乎某个地方的[System.IO.StringWriter]变数正在扩大。因为当我的 XML 看起来像这样时

$xml = [Xml]@"
<Pattern>$key.Value</Pattern>
"@
Write-PxXmlToConsole $xml

我得到<Pattern>Autodesk Revit 2019.Value</Pattern>输出是因为在这个会话的某个地方变量$key被填充了。如果我开始一个新的会话,我会得到<Pattern>.Value</Pattern>. 有点痛苦,我在这里似乎找不到任何表明可以抑制这种行为的设置。

powershell
  • 1 个回答
  • 32 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