Scala - 重新格式化哈希映射

use*_*388 1 scala hashmap

我有一些Python代码处理相当复杂的hashmap(示例输入),重构它并创建(示例输出)它的简化版本.我正在寻找使用Scala解决这个问题的最佳方法,即内置或外部库?我是Scala的新手并且刚刚掌握它,所以这里的一些指导会非常有用.

这很容易使用Python,我希望将与Scala一起使用.

输入:

data_in = {
    'map': {
        'stats': {
            'uphosts': u'3',
            'timestr': u'Thu Mar 20 18:18:09 2014',
            'downhosts': u'0',
            'totalhosts': u'3',
            'elapsed': u'1.71'
        },
        'scaninfo': {
            u'tcp': {
                'services': u'80,443',
                'method': u'syn'
            }
        },
        'command_line': u'command goes here'
    },
    'scan': {
        u'2a00:2384:0:208f::15': {
            'status': {
                'state': u'up',
                'reason': u'nd-response'
            },
            'hostname': u'static.xyz.com',
            'vendor': {
                u'00:0C:67:99:6f:96': u'VMware'
            },
            'addresses': {
                u'mac': u'00:gf:29:99:6D:96',
                u'ipv6': u'a848:2384:0:3456::15'
            },
            u'tcp': {
                80: {
                    'product': '',
                    'state': u'open',
                    'version': '',
                    'name': u'http',
                    'conf': u'3',
                    'extrainfo': '',
                    'reason': u'syn-ack',
                    'cpe': ''
                },
                443: {
                    'product': '',
                    'state': u'open',
                    'version': '',
                    'name': u'https',
                    'conf': u'3',
                    'script': {
                        u'ssl-cert': u'place holder'
                    },
                    'extrainfo': '',
                    'reason': u'syn-ack',
                    'cpe': ''
                }
            }
        },
        u'2a00:2384:0:208f::16': {
            'status': {
                'state': u'up',
                'reason': u'nd-response'
            },
            'hostname': u'static.edf.com',
            'vendor': {
                u'00:0C:55:AE:33:ff': u'VMware'
            },
            'addresses': {
                u'mac': u'00:54:29:fg:55:0F',
                u'ipv6': u'8938:8584:0:8685::16'
            },
            u'tcp': {
                80: {
                    'product': '',
                    'state': u'open',
                    'version': '',
                    'name': u'http',
                    'conf': u'3',
                    'extrainfo': '',
                    'reason': u'syn-ack',
                    'cpe': ''
                },
                443: {
                    'product': '',
                    'state': u'open',
                    'version': '',
                    'name': u'https',
                    'conf': u'3',
                    'script': {
                        u'ssl-cert': u'place holder'
                    },
                    'extrainfo': '',
                    'reason': u'syn-ack',
                    'cpe': ''
                }
            }
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

所需输出:

data_out_1 = [
    {'address': u'2a00:2384:0:208f::15',
  'hostname': u'static.xyz.com',
  'ports': {80: {'reason': u'syn-ack', 'state': u'open'},
            443: {'reason': u'syn-ack',
                  'ssl_cert': u'place holder',
                  'state': u'open'}}},
    {'address': u'2a00:2384:0:208f::16',
  'hostname': u'static.edf.com',
  'ports': {80: {'reason': u'syn-ack', 'state': u'open'},
            443: {'reason': u'syn-ack',
                  'ssl_cert': u'place holder',
                  'state': u'open'}}}]       
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 8

这不是类型安全的.

Hashmaps无法存储不同类型的数据*.首先创建数据结构来保存输入数据(案例类在这里有帮助).

所以你的stats对象可能看起来像

case class Stats(uphosts: Int, timeStr: Datetime, downhosts: Int, totalHosts: Int, elapsed:Double)
Run Code Online (Sandbox Code Playgroud)
  • 暂时忽略这里的子类型.

  • 绝对+1.养成为处理的所有内容生成域抽象的习惯.字典对于构造数据是可怕的(除非它是字典,当然......). (4认同)
  • 值得注意的是,这个建议不是特定于scala的.无论你在哪种语言工作,巨型,疯狂的词典都是一个糟糕的想法...*包括*python. (2认同)