Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析Python';Perl中的序列化对象_Python_Perl_Parsing - Fatal编程技术网

解析Python';Perl中的序列化对象

解析Python';Perl中的序列化对象,python,perl,parsing,Python,Perl,Parsing,我需要让我的Perl代码读取一些Python的序列化对象,以便以后处理。我提供了基于Parse::MGC的解析器,但速度很慢。可能是我做错了,或者是有人知道用某种Perl结构转换Python序列化对象的更好方法 以下是我的解析代码: package Room::HandParser; use base qw( Parser::MGC ); my @poker_cards_string = ( '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', 'Th

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

以下是我的解析代码:

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,这在我的例子中是不可接受的


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

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

如果有人感兴趣的话:我最后使用了一些正则表达式将此字符串转换为JSON(因为它们看起来非常接近),然后用JSON::XS解析它__parse_hands()子例程。

repr(ob)
的输出不是序列化格式,而是为方便程序员而制作的表示。即使在Python中,使用
repr
/
eval
组合也不是一个好主意。使用Python和Perl都支持的真正的序列化格式(例如JSON),这就是我所拥有的。我不是在做Python部分,但需要处理用它生成的数据。我考虑过这一点,但我不开发Python部分,我想避免这种情况。@Pavel Karoukin:你可以[要求某人]编写一个Python脚本,只反序列化并打印到JSON或YAML。这将不需要干预Python软件,并将节省大量的麻烦。我已经使用perl的
可存储
格式完成了这项工作。你所需要的只是一层很薄的python胶水来让它工作。