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

OrenIshShalom's questions

Martin Hope
OrenIshShalom
Asked: 2024-11-12 21:08:26 +0800 CST

使用 capsys 选择性调用测试函数

  • 5

我可以选择性地运行test_function_1,覆盖我的conftest.py装置中的仪表1

def test_function_1(instrumentation: dict[str, float]) -> None:
    assert instrumentation['a'] > instrumentation['b']

def test_function_2(capsys) -> None:
    print("Hello, pytest!")
    captured = capsys.readouterr()
    assert captured.out == "Hello, pytest!\n"

当我尝试调用时test_function_2,我不知道如何传递capsys给它2:

import tests
import pytest # <--- doesn't help ...

def test_callee_with_instrumentation():
    tests.test_function_1({'a': 110, 'b': 55, 'g': 6000})

def test_callee_with_capsys():
    # tests.test_function_2()              # <--- TypeError: test_function_2() missing 1 required positional argument: 'capsys'
    # tests.test_function_2(capsys)        # <--- NameError: name 'capsys' is not defined 
    # tests.test_function_2(pytest.capsys) # <--- AttributeError: module 'pytest' has no attribute 'capsys'
    pass

test_callee_with_instrumentation()
test_callee_with_capsys()

我很确定这些conftest.py装置是无关紧要的,但为了完整起见:

import pytest

@pytest.fixture(scope='function')
def instrumentation():
    return { 'a': 800, 'b': 620, 'c': 44 }

1在我的实际代码中,是众多capsys参数之一。

2这里有类似的问题。在我看来,这不是重复的,因为我问的是编程运行测试,而不是的正确含义capsys。

python
  • 2 个回答
  • 40 Views
Martin Hope
OrenIshShalom
Asked: 2024-10-26 13:58:25 +0800 CST

为什么 elf strip 不能跨平台

  • 7

我在我的树莓派上编译了一个 aarch64 elf 可执行文件:

$ file kbgen.elf.aarch64
kbgen.elf.aarch64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=4cf36e84c30ea8e53073bb64cf99df1d82084702, with debug_info, not stripped

当我尝试在我的 x64 机器(wsl)上剥离它的符号时失败了:

$ strip kbgen.elf.aarch64
strip: Unable to recognise the format of the input file `kbgen.elf.aarch64'

这很奇怪,对吧?我的意思是精灵部分是“跨平台的”,不是吗?

# get inside raspberry pi to make sure stripping is ok there
$ ls -l kbgen.elf.aarch64
-rwxr-xr-x 1 oren oren 73257408 Oct 26 08:40 kbgen.elf.aarch64
$ sudo strip kbgen.elf.aarch64
$ ls -l kbgen.elf.aarch64 # <----- size reduced by a half, good !
-rwxr-xr-x 1 root root 36358680 Oct 26 08:42 kbgen.elf.aarch64

# make sure elf was stripped with file utility
$ file kbgen.elf.aarch64
kbgen.elf.aarch64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=4cf36e84c30ea8e53073bb64cf99df1d82084702, stripped
x86-64
  • 1 个回答
  • 46 Views
Martin Hope
OrenIshShalom
Asked: 2024-10-23 17:54:30 +0800 CST

控制 Wai 记录器消息

  • 6

我有一个 Haskellyesod网络服务器,它运行良好1,2,并且日志记录良好3:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeFamilies      #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE DeriveAnyClass    #-}
{-# LANGUAGE OverloadedStrings #-}

import Yesod
import Prelude
import Data.Aeson()
import GHC.Generics
import Data.Text
import Data.Time
import Yesod.Core.Types
import System.Log.FastLogger
import Network.Wai.Handler.Warp

-- Wai stuff
import qualified Network.Wai
import qualified Network.Wai.Logger
import qualified Network.HTTP.Types.Status
import qualified Network.Wai.Middleware.RequestLogger as Wai

data Healthy = Healthy Bool deriving ( Generic )

-- | This is just for the health check ...
instance ToJSON Healthy where toJSON (Healthy status) = object [ "healthy" .= status ]

data App = App

mkYesod "App" [parseRoutes|
/healthcheck HealthcheckR GET
|]

instance Yesod App where
    makeLogger = \_app -> myLogger

getHealthcheckR :: Handler Value
getHealthcheckR = do
    $logInfoS "(HealthCheck)" "Good !"
    returnJson $ Healthy True

myLogger :: IO Logger
myLogger = do
    _loggerSet <- newStdoutLoggerSet defaultBufSize
    formatter <- newTimeCache "[%d/%m/%Y ( %H:%M:%S )]"
    return $ Logger _loggerSet formatter

dateFormatter :: String -> String
dateFormatter date = let
    date' = parseTimeOrError True defaultTimeLocale "%d/%b/%Y:%T %Z" date :: UTCTime
    in formatTime defaultTimeLocale "[%Y/%m/%d ( %H:%M:%S )]" date'

formatter :: Network.Wai.Logger.ZonedDate -> Network.Wai.Request -> Network.HTTP.Types.Status.Status -> Maybe Integer -> LogStr
formatter zonedDate req status responseSize = "[ 17/17/2017 ]\n"

main :: IO ()
main = do
    waiApp <- toWaiApp App
    middleware <- Wai.mkRequestLogger (Wai.defaultRequestLoggerSettings { Wai.outputFormat = Wai.CustomOutputFormat formatter })
    run 3000 $ middleware waiApp

当我检查日志时,我看到三条消息,其中两条是我的

[23/10/2024 ( 15:07:06 )] [Info#(HealthCheck)] Good ! @(main:Main src/Main.hs:41:6)
172.17.0.1 - - [23/Oct/2024:15:07:06 +0000] "GET /healthcheck HTTP/1.1" 200 16 "" "curl/8.9.1"
[ 17/17/2017 ]

消息从哪里来172.17.0.1 - - ...的?!看来它一定来自 Wai 层(对吧?)但话说回来,我以为我配置了 Wai 日志


1,2完整的源代码在这里,根据评论进行了编辑以简化

3嗯,差不多了!

haskell
  • 1 个回答
  • 28 Views
Martin Hope
OrenIshShalom
Asked: 2024-09-22 17:59:00 +0800 CST

确定已加载哪些系统模块

  • 5

我正在尝试确定加载了哪些 python 系统模块:

In [1]: import sys
In [2]: l = sorted(list(sys.modules.keys()))
In [3]: 'json' in l
Out[3]: True

根据文档:

这是一个将模块名称映射到已加载模块的字典。

所以我认为也许是 sys进口的json,但结果却是错误的:

In [4]: json.dumps({'oren': 12})
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 json.dumps({'oren': 12})

NameError: name 'json' is not defined

我错过了什么?

python
  • 1 个回答
  • 43 Views
Martin Hope
OrenIshShalom
Asked: 2023-08-25 20:45:35 +0800 CST

泛型 T 扩展了 U 但 Readonly<T> 没有

  • 5

恕我直言,这不是这个问题的重复。这是一个最小的例子:

class Indexer {
  private readonly someIndex = 200;
  public getIndex() { return this.someIndex; }
}

class Person extends Indexer {}
class Something<T extends Indexer> {
  s = new Map<number, T>();
  insert(t: T){ /* ... */ }
}

const c1 = new Something<         Person >();
const c2 = new Something<Readonly<Person>>();
  • 一方面我的类需要子类化Indexer
  • 另一方面,它需要Readonly

我无法同时实现这两个属性:

$ npx tsc --target es2020 main.ts
main.ts:13:26 - error TS2344: Type 'Readonly<Person>' does not satisfy the constraint 'Indexer'.
  Property 'someIndex' is missing in type 'Readonly<Person>' but required in type 'Indexer'.

13 const c2 = new Something<Readonly<Person>>();
                            ~~~~~~~~~~~~~~~~

  main.ts:2:20
    2   private readonly someIndex = 200;
                         ~~~~~~~~~
    'someIndex' is declared here.
typescript
  • 1 个回答
  • 21 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