TypeError:列表索引必须是整数或切片,而不是在单元测试中使用str [英] TypeError: list indices must be integers or slices, not str on Unit Testing

查看:179
本文介绍了TypeError:列表索引必须是整数或切片,而不是在单元测试中使用str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下功能上运行单元测试时,出现错误"TypeError: list indices must be integers or slices, not str ".以下函数用于调用保存在Google AI平台中的模型.但是,数据输入必须进行base64序列化,因此,我在tfx_test(request)中包含了get_serialized_example(raw)函数.任何帮助和建议,我们感激不尽.预先感谢!

I have the error as "TypeError: list indices must be integers or slices, not str " while running a unit test on the following function. The following function is used to call the model that is saved in Google AI Platform. However, the data input has to be base64 serialized, hence, I include the get_serialized_example(raw) function within the tfx_test(request). Any help and advice are appreciated. Thanks in advance!

def tfx_test(request):
    
    #User Inputs
    project = request.project
    model = request.model
    signature = request.signature
    version = request.version
    
    #Data inputs Base64 encoder 
    
    def get_serialized_example(raw):
        return tf.train.Example(
                features=tf.train.Features(
                  feature={"value":
                              tf.train.Feature(bytes_list=tf.train.BytesList(value=[raw]))
                          }
                    )
                ).SerializeToString()


    b64_country_code = base64.b64encode(get_serialized_example(request.country_code)).decode('utf-8')
    
    b64_project_type = base64.b64encode(get_serialized_example(request.project_type)).decode('utf-8')
    
    # ml.googleapis.com
    service = googleapiclient.discovery.build('ml', 'v1')
    name = 'projects/{}/models/{}'.format(project, model)

    if version is not None:
        name += '/versions/{}'.format(version)

    response = service.projects().predict(
        name=name,
        body={
            'signature_name': signature,
            'instances': [
                    {
                       "examples":{"b64": b64_country_code,
                                   "b64": b64_project_type}
                    }]
        }
    ).execute()

    if 'error' in response:
        raise RuntimeError(response['error'])

    return response['predictions']

单元测试

def test_safety_kfp_custom(self):

def test_safety_kfp_custom(self):

self.request = TestScenario.populated_request(Test.test1)
self.response = tfx_test(self.request)

self.assertEqual(0.35915321111679077, self.response["predictions"])

tfx_test函数的数据输入

class request():
    project = "xxx"
    model = "xxx"
    signature = "xxx"
    country_code = b"UK"
    project_type = b"PT"
    version = 'xxx'

推荐答案

正如Zenith在评论中提到的那样,我应该在函数返回时返回响应而不是response ['predictions'].谢谢,

As Zenith mentioned in the comment, I should return the response instead of response['predictions'] on the return of the function. Thanks,

这篇关于TypeError:列表索引必须是整数或切片,而不是在单元测试中使用str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆