我正在尝试使用lxml
模块解析Web上的各种页面,例如:
def dom(self):
return lxml.html.fromstring(self.content)
Run Code Online (Sandbox Code Playgroud)
但似乎我必须在html5页面的情况下切换lxml.html
到这一点lxml.html.html5parser
.
http://lxml.de/html5parser.html
那么如何确定页面是否基于html5?DOCTYPE
在解析之前我是否必须通过char 检查char?
编辑:我做了一个简单的正则表达式来处理这个问题.它似乎有效,但是,我仍然在寻找一些巧妙的方法.该解决方案打破sourceline
方法.
import lxml.html
from lxml.html import html5parser
def dom(self):
content = self.content
if self._is_html5():
elm = html5parser.fromstring(content)
content = lxml.html.tostring(elm, method='html')
return lxml.html.fromstring(content)
def _is_html5(self):
return bool(re.match(r'^<!doctype html>', self.content, re.I))
Run Code Online (Sandbox Code Playgroud) 今天我遇到一个Twitter帖子告诉我另一个神秘的Perl行为.有人可以告诉我以下脚本中的第3个语句有什么问题吗?我正在寻找perldoc中相关的文档部分.
#!/usr/bin/perl
$x[1] = "foo"; $_ = "foo"; s/$x[1]/bar/; print "$_\n";
$x[10] = "foo"; $_ = "foo"; s/$x[10]/bar/; print "$_\n";
$x[100] = "foo"; $_ = "foo"; s/$x[100]/bar/; print "$_\n";
$x[1000] = "foo"; $_ = "foo"; s/$x[1000]/bar/; print "$_\n";
__END__
bar
bar
foo
bar
Run Code Online (Sandbox Code Playgroud)
它看起来像Perl解释器趋向于分开$x
的[100]
.
$x[100] = 'foo';
$_ = 'foo';
s/${x}[100]/bar/;
print "$_\n";
Run Code Online (Sandbox Code Playgroud)
谢谢你们.我在Camel Book中找到了一个文档,它建议与@ fred-gannet完全相同.启发式的因素是字符出现次数和括号中的修剪策略.
https://books.google.com/books?id=xx5JBSqcQzIC&lpg=PR1&pg=PA65#v=onepage&q&f=false
在搜索模式中,也经历双引号插值,存在一个不幸的模糊:
/$foo[bar]/
要插值为/${foo}[bar]/
(其中[bar]是正则表达式的字符类)或as/${foo[bar]}/
(其中[bar]是数组@foo的下标)?如果@foo
不存在,那显然是一个字符类.如果@foo
存在,Perl会对[bar]进行很好的猜测,并且几乎总是正确的.†如果猜错了,或者你只是偏执狂,你可以强制使用大括号进行正确的插值,如前所示.即使你只是谨慎,也可能不是一个坏主意.
https://rt.perl.org/Public/Bug/Display.html?id=133027#txn-1542459
代码在S_intuit_more()中.
https://github.com/Perl/perl5/blob/823ba440369100de3f2693420a3887a645a57d28/toke.c#L4207-L4217
if (*s …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 mypy 与 SQLAlchemy 一起使用。为了验证/修改特定列值(email
在本例中),SQLAlchemy官方文档提供了hybrid_property
装饰器。
问题是,mypy
无法EmailAddress
正确识别类构造函数,它给出:
email_address.py:31: error: Unexpected keyword argument "email" for "EmailAddress"; did you mean "_email"?
Run Code Online (Sandbox Code Playgroud)
我怎样才能告诉 mypy 识别这些列?
email_address.py:31: error: Unexpected keyword argument "email" for "EmailAddress"; did you mean "_email"?
Run Code Online (Sandbox Code Playgroud)
我正在使用以下软件包:
SQLAlchemy==1.4.35
mypy==0.942
mypy-extensions==0.4.3
sqlalchemy2-stubs==0.0.2a22
Run Code Online (Sandbox Code Playgroud) 让我们假设我已经拥有了包含几个子程序的Child
包和Parent
包.这两个包通过聚合组合在一起,就像在perltoot
:
use warnings;
use strict;
package Child;
sub new {
my ($class, %arg) = @_;
return bless { %arg }, $class;
}
sub method_x {
warn 'call method x';
}
sub method_y {
warn 'call method y';
}
sub method_z {
warn 'call method z';
}
1;
package Parent;
sub new {
my ($class, %arg) = @_;
return bless {
child => undef,
%arg,
}, $class;
}
sub child { shift->{child} }
sub x …
Run Code Online (Sandbox Code Playgroud) 我要重构大量旧的perl脚本.(超过150k行,没有测试,没有严格,没有包,没有提交日志,没有评论)
当我开始在t
目录下编写一些测试时.我发现几乎所有文件require
都是绝对路径.所以我尝试模拟内置require
函数使它们可移植,但没有运气:
use strict;
use warnings;
use Test::More;
use FindBin;
BEGIN {
my $root = "$FindBin::RealBin/../";
sub require {
$_[0] = $root . $_[0];
CORE::require(@_);
}
}
require_ok "foo.pl";
done_testing();
Run Code Online (Sandbox Code Playgroud)
上面的脚本给了我: Error: Can't locate foo.pl in @INC...
如何在Perl需要之前添加根路径?
谢谢Axeman,我使用以下钩子修改了绝对路径.
my $root = "$RealBin/../";
unshift @INC, sub {
my ($coderef, $filename) = @_;
$filename =~ s/^\///;
open(FILE, '<', $root . $filename);
return *FILE;
};
Run Code Online (Sandbox Code Playgroud) perl ×3
python ×2
aggregation ×1
callback ×1
html5 ×1
lxml ×1
mocking ×1
mypy ×1
oop ×1
refactoring ×1
regex ×1
require ×1
sqlalchemy ×1