gpt4 book ai didi

python - 在 Perl 中解析 Python 的序列化对象

转载 作者:行者123 更新时间:2023-11-28 21:29:11 25 4
gpt4 key购买 nike

我需要让我的 Perl 代码读取一些 Python 的序列化对象以供以后处理。我使用了基于 Parse::MGC 的解析器,但它很慢。可能是我做错了,或者可能有人知道更好的方法将 Python 的序列化对象转换为某种 Perl 结构?

这是我的解析代码:

package Room::HandParser;
use base qw( Parser::MGC );

my @poker_cards_string = ( '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th', 'Jh', 'Qh', 'Kh', 'Ah', '2d', '3d', '4d', '5d', '6d', '7d', '8d', '9d', 'Td', 'Jd', 'Qd', 'Kd', 'Ax', '2c', '3c', '4c', '5c', '6c', '7c', '8c', '9c', 'Tc', 'Jc', 'Qc', 'Kc', 'Ac', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', 'Ts', 'Js', 'Qs', 'Ks', 'As' );

sub parse_declaration {
my $self = shift;

[
$self->any_of(
sub { $self->token_int },
sub { $self->token_string },
),
$self->expect(":"),
$self->parse,
]
}

sub parse_hash {
my $self = shift;

my %ret;
$self->list_of(",", sub {
my $res = $self->parse_declaration;
$ret{$res->[0]} = $res->[2];
});

return \%ret;
}


sub parse_cards {
my $self = shift;
my $card = $self->token_int;
return $poker_cards_string[$card & 0x3F];
}

sub parse {
my $self = shift;

$self->any_of(
sub { $self->scope_of( "[", sub { $self->list_of(",", \&parse) }, "]" ) },
sub { $self->scope_of( "(", sub { $self->list_of(",", \&parse) }, ")" ) },
sub { $self->scope_of( "{", sub { $self->parse_hash }, "}" ) },
sub { $self->scope_of( "PokerCards([", sub { $self->list_of(",", \&parse_cards) }, "])" ) },
sub { $self->token_float },
sub { $self->token_int },
sub { $self->token_string },
sub { $self->token_kw( qw(None True False) ) },
);
}

1;

这是我需要解析的序列化 Python 对象的示例:

[('game', 0, 195, 0, 0.0, 'holdem', '100-200-no-limit', [50312, 50313, 50314, 50315, 50316, 50317, 2], 0, {2: 1000000, 50312: 200000, 50313: 200000, 50314: 200000, 50315: 200000, 50316: 200000, 50317: 200000}), ('position', 1), ('blind', 50313, 10000, 0), ('position', 2), ('blind', 50314, 20000, 0), ('position', -1), ('round', 'pre-flop', PokerCards([]), {2: PokerCards([226, 208]), 50312: PokerCards([223, 206]), 50313: PokerCards([221, 233]), 50314: PokerCards([222, 211]), 50315: PokerCards([235, 216]), 50316: PokerCards([209, 236]), 50317: PokerCards([237, 243])}), ('position', 3), ('call', 50315, 20000), ('position', 4), ('call', 50316, 20000), ('position', 5), ('call', 50317, 20000), ('position', 6), ('call', 2, 20000), ('position', 0), ('fold', 50312), ('position', 1), ('call', 50313, 10000), ('position', 2), ('check', 50314), ('position', -1), ('round', 'flop', PokerCards([7, 21, 46]), {2: PokerCards([226, 208]), 50313: PokerCards([221, 233]), 50314: PokerCards([222, 211]), 50315: PokerCards([235, 216]), 50316: PokerCards([209, 236]), 50317: PokerCards([237, 243])}), ('position', 1), ('check', 50313), ('position', 2), ('check', 50314), ('position', 3), ('check', 50315), ('position', 4), ('check', 50316), ('position', 5), ('check', 50317), ('position', 6), ('check', 2), ('position', -1), ('round', 'turn', PokerCards([7, 21, 46, 38]), None), ('position', 1), ('check', 50313), ('position', 2), ('check', 50314), ('position', 3), ('check', 50315), ('position', 4), ('check', 50316), ('position', 5), ('check', 50317), ('position', 6), ('check', 2), ('position', -1), ('round', 'river', PokerCards([7, 21, 46, 38, 20]), None), ('position', 1), ('check', 50313), ('position', 2), ('check', 50314), ('position', 3), ('check', 50315), ('position', 4), ('check', 50316), ('position', 5), ('check', 50317), ('position', 6), ('check', 2), ('position', -1), ('showdown', None, {2: PokerCards([226, 208]), 50313: PokerCards([29, 41]), 50314: PokerCards([222, 211]), 50315: PokerCards([43, 24]), 50316: PokerCards([209, 236]), 50317: PokerCards([45, 51])}), ('end', [50317], [{'serial2delta': {2: -20000, 50313: -20000, 50314: -20000, 50315: -20000, 50316: -20000, 50317: 100000}, 'player_list': [50312, 50313, 50314, 50315, 50316, 50317, 2], 'serial2rake': {50317: 0}, 'serial2share': {50317: 120000}, 'pot': 120000, 'serial2best': {2: {'hi': [101154816, ['FlHouse', 46, 20, 7, 34, 21]]}, 50313: {'hi': [50841600, ['Trips', 46, 20, 7, 38, 21]]}, 50314: {'hi': [50841600, ['Trips', 46, 20, 7, 38, 21]]}, 50315: {'hi': [50842368, ['Trips', 46, 20, 7, 38, 24]]}, 50316: {'hi': [50841600, ['Trips', 46, 20, 7, 38, 21]]}, 50317: {'hi': [101171200, ['FlHouse', 46, 20, 7, 51, 38]]}}, 'type': 'game_state', 'side_pots': {'building': 0, 'pots': [[120000, 120000]], 'last_round': 3, 'contributions': {0: {0: {2: 20000, 50313: 20000, 50314: 20000, 50315: 20000, 50316: 20000, 50317: 20000}}, 1: {}, 2: {}, 'total': {2: 20000, 50313: 20000, 50314: 20000, 50315: 20000, 50316: 20000, 50317: 20000}, 3: {}}}}, {'serials': [50313, 50314, 50315, 50316, 50317, 2], 'pot': 120000, 'hi': [50317], 'chips_left': 0, 'type': 'resolve', 'serial2share': {50317: 120000}}])]

对于这样的结构,需要几秒钟和 100% CPU 来解析这个对象,这在我的情况下是 Not Acceptable 。

编辑:在这里,我不是在寻找解决方法,例如编写 python 脚本来评估此结构并将其输出为 JSON,或者使用添加的函数重写原始 Python 应用程序以将数据存储为 JSON。我正在考虑使用 Perl 解析这些数据,并具有合理的性能,因为这种格式非常接近 JSON,并且应该可以在相似的时间内解析它。

最佳答案

使用不同的格式怎么样?例如,JSON 相当容易解析,并且 Perl 中的实现应该是开箱即用的。 Python 内置了 JSON 序列化和反序列化,因此您也不必在那里重新发明任何轮子。

关于python - 在 Perl 中解析 Python 的序列化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com