小编pou*_*def的帖子

如何获取 Pydantic 模型类型?

考虑以下模型:

from pydantic import BaseModel

class Cirle(BaseModel):
    radius: int
    pi = 3.14
Run Code Online (Sandbox Code Playgroud)

如果我运行以下代码,我可以看到该模型的字段:

print(Circle.__fields__)

# Output:

{
  'radius': ModelField(name='radius', type=int, required=True), 
  'pi': ModelField(name='pi', type=float, required=False, default=3.14)
}
Run Code Online (Sandbox Code Playgroud)

问题是:如何从类型中获取类型(或推断类型提示)ModelField?这些都会给出错误:

Circle.__fields__['pi'].type
# AttributeError: 'ModelField' object has no attribute 'type'

Circle.__fields__['pi'].__dict__
# AttributeError: 'ModelField' object has no attribute '__dict__'

type(Circle.__fields__['pi'])
# <class 'pydantic.fields.ModelField'>

import typing
typing.get_type_hints(Circle.__fields__['pi'])
# TypeError: ModelField(name='pi', type=float, required=False, default=3.14)
# is not a module, class, method, or function.

typing.get_type_hints(Circle)
# This does not include the "pi" field because …
Run Code Online (Sandbox Code Playgroud)

python pydantic

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

帮助解释gdb:segfault的功能

我正在尝试调试段错误,我从gdb输出:

(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x08048af9 in parse_option_list (ptr=0x6f72505f <Address 0x6f72505f out of bounds>, box_name=0x696d6978 <Address 0x696d6978 out of bounds>, option_list=0x313a7974, 
    num_elements=0x33313532) at submit.c:125
125                         memcpy(&(option_list[(*num_elements)].value), value, 24);
(gdb) p num_elements
$15 = (int *) 0x33313532
(gdb) p *num_elements
Cannot access memory at address 0x33313532
(gdb) 
Run Code Online (Sandbox Code Playgroud)

它看起来像memcpy()中的东西正在变得混乱.但我无法弄清楚究竟是什么问题,因为该行引用了这么多变量.

有人可以帮助弄清楚这0x8048af9 in parse_option_list...条线告诉我的是什么吗?

我的功能签名是:

int parse_option_list(char *ptr, char *box_name,
   struct option_list_values *option_list, int *num_elements)
Run Code Online (Sandbox Code Playgroud)

这可能有用:

struct option_list_values {
    char value[24];
    char name[24];
};
Run Code Online (Sandbox Code Playgroud)

此外,变量valuename …

c gdb segmentation-fault

3
推荐指数
1
解决办法
3974
查看次数

禁用浮动图像周围的文本环绕

我有一个看起来像这样的页面:

<div>
<p> This text appears above the image</p>
<!-- I know the <div><p> construction is superfluous. I was just trying things out -->
</div>

<div style="display: block; width: 100%; clear: both;">

   <img class="left" alt="Img1" src="img1.jpg" alt="" width="242" height="181" />
   <img class="right" alt="Img2" src="img2.jpg" alt="" width="242" height="181" />

</div>

<div>
<p> This text appears between those images. I want it to display below them.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

每个图像都有这个CSS:

.left {
   float: left;
   margin-right: 5px;
}
.right {
   float: right;
   margin-left: 5px;
} …
Run Code Online (Sandbox Code Playgroud)

css css-float

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

Overriding errors.Is() not working with custom errors

I am trying to create a custom error in Go such that I can create new instances of that error with a custom message, and then handle that specific type of error. However, when I try to do this, then my overridden errors.Is() is not being executed. Here is my code:

package main

import (
        "fmt"
        "errors"
)

type Error struct {
        Message string
        Code int
}

func (e *Error) Error() string {
        return e.Message
}

func (e Error) Is(target …
Run Code Online (Sandbox Code Playgroud)

go

3
推荐指数
1
解决办法
6588
查看次数

SCP不能用bash脚本中的空格路径?

我有一个bash脚本生成一个SCP命令来执行.代码的相关部分如下所示:

echo $COPY_CMD
$COPY_CMD
Run Code Online (Sandbox Code Playgroud)

我的输出如下:

rascher@localhost:~/Desktop/video_final$ ./xfervids.sh 
scp "/media/My Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD" rascher@192.168.1.101:./video_working/
rascher@192.168.1.101's password: 
"/media/My: No such file or directory
Book/PhotosVideos/Videos/18May2008Download/SD_VIDEO/PRG001/MOV056.MOD": No such file or directory

但是,当我选择输出的第2行:scp "/media/...复制它,并将其粘贴到终端中时,它可以工作.

我究竟做错了什么?我已经尝试在"My Book"("My\ Book")中转义空间,放置两个\字符而不是一个("My\\ Book")但是我不能让它表现得一致.救命?

linux bash shell scp

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

如何使用easy_install安装pysvg?

我是一名铁杆C程序员,我想学习python.我正在尝试编写一个脚本来生成SVG文件.如果我不能让它工作,那么我将杀死几只小猫并编写C程序来执行此任务.

我想使用pysvg库.实际上,我并没有和那个特定的图书馆结婚.我应该使用更好的SVG生成器(更好地记录,真的)lib吗?您使用哪个库来生成SVG?我从头开始创建简单的SVG(盒子和线条).

我尝试下载并运行python install.py install但我真的不想给它root访问权限.

然后我心软了,决定试试sudo easy_install pysvg,它不会起作用:

rascher@coltrane:~$ sudo easy_install pysvg
Searching for pysvg
Reading http://pypi.python.org/simple/pysvg/
Reading http://codeboje.de/pysvg/
No local packages or download links found for pysvg
error: Could not find suitable distribution for Requirement.parse('pysvg')

rascher@coltrane:~$ sudo easy_install pysvg==0.2.1
Searching for pysvg==0.2.1
Reading http://pypi.python.org/simple/pysvg/
Reading http://codeboje.de/pysvg/
No local packages or download links found for pysvg==0.2.1
error: Could not find suitable distribution for Requirement.parse('pysvg==0.2.1')
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我该如何安装这个库?

python svg easy-install

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

排序链接列表的最佳方法是什么?

在[C/C++]中对链接列表进行排序的最佳算法是什么?

c sorting algorithm list

-1
推荐指数
2
解决办法
3487
查看次数

标签 统计

c ×2

python ×2

algorithm ×1

bash ×1

css ×1

css-float ×1

easy-install ×1

gdb ×1

go ×1

linux ×1

list ×1

pydantic ×1

scp ×1

segmentation-fault ×1

shell ×1

sorting ×1

svg ×1