【Python接口测试】11_constant-获取Excel中各个坐标下接口参数值

本文介绍了一种使用Python实现的自动化测试框架,该框架能够从Excel文件中读取测试用例数据,包括接口字段、请求方式、URL等关键信息,并提供了遍历测试用例的方法。此外,还涵盖了如何处理测试用例的执行标志、请求数据、预期结果及实际结果的写入。

根据上一部分中,获取到了Excel中的第一行的接口字段列。下面可以遍历测试用例每一行,获取每一条测试用例中接口字段的值。

#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# FileName: get_excelData.py

from ApiTestFrame.util.operate_excel import OperateExcel
from ApiTestFrame.constant import get_constant
from ApiTestFrame.util.operate_json import OperateJson
from ApiTestFrame.util.connect_db import ConnectDB


class GetExcelData:

    def __init__(self):
        self.operate_excel = OperateExcel()

    # 获取excel行数,也就是case的个数
    def get_case_lines(self):
        return self.operate_excel.get_lines()

    # 是否携带header
    def is_header(self, row):
        col = int(get_constant.get_header())
        header = self.operate_excel.get_cell(row, col)
        if header == 'yes':
            return get_constant.get_header_value()
        else:
            return None

    # 获取是否执行
    def get_is_run(self, row):
        flag = None
        col = int(get_constant.get_run())
        run_model = self.operate_excel.get_cell(row, col)
        if run_model == 'yes':
            flag = True
        else:
            flag = False
        return flag

    # 获取请求方式
    def get_method(self, row):
        col = int(get_constant.get_method())
        method = self.operate_excel.get_cell(row, col)
        return method

    # 获取请求链接
    def get_url(self, row):
        col = int(get_constant.get_url())
        url = self.operate_excel.get_cell(row, col)
        return url

    # 获取请求数据
    def get_data(self, row):
        col = int(get_constant.get_requestData())
        data = self.operate_excel.get_cell(row, col)
        if data == '':
            return None
        return data

    # 通过获取关键字拿到data数据
    def get_data_from_json(self, row):
        operate_json = OperateJson()
        key_data = self.get_data(row)
        request_data = operate_json.get_keyData(key_data)
        return request_data

    # 获取预期结果
    def get_except_data(self, row):
        col = int(get_constant.get_expectedResult())
        except_data = self.operate_excel.get_cell(row, col)
        if except_data == '':
            return None
        return except_data

    # 通过SQl获取预期结果
    def get_except_data_form_mysql(self, row):
        op_mysql = ConnectDB()
        sql = self.get_except_data(row)
        print(sql)
        res = op_mysql.select_data(sql)
        return res.decode('unicode-escape')

    # 写入实际结果
    def write_result(self, row, value):
        col = int(get_constant.get_actualResult())
        self.operate_excel.write_value(row, col, value)
<think>我们被要求比较Python 2和Python 3中TensorFlow坐标变换是否一致。坐标变换在TensorFlow中可能涉及多种操作,例如仿射变换、旋转、缩放等。由于TensorFlow的API在Python 2和Python 3中设计上是一致的,因此理论上相同版本的TensorFlow在两种Python环境下的坐标变换结果应该一致。 但是,需要注意以下几点: 1. TensorFlow版本:相同版本的TensorFlow在Python 2和Python 3中的行为应该一致,因为TensorFlow的核心是C++编写的,Python只是封装。但不同版本的TensorFlow可能有不同的API和行为。 2. Python 2和Python 3的整数除法:在Python 2中,整数除法是整除(例如,3/2=1),而在Python 3中,3/2=1.5。如果坐标变换中涉及除法操作,并且使用了整数,那么结果可能不同。因此,在编写代码时,应确保使用浮点数或显式使用除法运算符(在Python 3中,整数除法用//)。 3. 其他库的依赖:坐标变换可能依赖NumPy等库,而NumPy在Python 2和Python 3中的行为基本一致,但也要注意一些细微差别。 为了验证,我们可以写一个简单的坐标变换例子(比如仿射变换)在Python 2和Python 3中运行,使用相同的TensorFlow版本,并比较结果。 示例:使用tf.keras.preprocessing.image.apply_affine_transform(这是一个常用的图像坐标变换函数)或者使用tf.image中的变换函数(如旋转、平移等)。 但是,请注意,从TensorFlow 2.0开始,官方不再支持Python 2。因此,如果我们使用TensorFlow 2.x,那么只能在Python 3中使用。如果要比较,必须使用同时支持Python 2和Python 3的TensorFlow版本(如1.x版本)。 因此,我们假设使用TensorFlow 1.x版本(例如1.15)进行测试。 步骤: 1. 在Python 2和Python 3环境中安装相同版本的TensorFlow(如1.15.0)。 2. 编写一个简单的坐标变换代码,例如对一组点进行仿射变换。 3. 比较输出结果。 示例代码(仿射变换): 我们使用一个简单的仿射变换矩阵来变换一个点(1,1)。 注意:仿射变换矩阵通常是2x3的矩阵,我们使用tf.contrib.image.transform(在TensorFlow 1.x中)或者使用tf.keras.preprocessing.image.apply_affine_transform(但注意这个函数是针对图像的,不是直接针对点的)。 这里我们使用tf.keras.preprocessing.image.apply_affine_transform来变换整个图像,然后观察同一个像素点的变换情况。但更直接的方式是使用仿射变换矩阵乘以点的齐次坐标。 我们可以自己写一个仿射变换函数,使用矩阵乘法。 然而,为了确保使用TensorFlow内置操作,我们使用tf.matmul。 假设我们有一个点(x, y),我们将其转换为齐次坐标[ x, y, 1 ],然后乘以仿射变换矩阵(2x3矩阵,但实际上我们需要3x3矩阵,因为齐次坐标是三维的。通常,仿射变换矩阵是2x3,我们将其扩展为3x3,第三行为[0,0,1])。但注意,我们只需要变换后的前两个坐标。 我们构建一个仿射变换矩阵,例如: 矩阵M = [ [a, b, c], [d, e, f] ] 然后,变换后的点为: x' = a*x + b*y + c y' = d*x + e*y + f 在TensorFlow中,我们可以用矩阵乘法来实现,但注意矩阵的形状。 由于我们只变换一个点,我们可以直接计算。 但是,为了使用TensorFlow,我们这样写: 代码示例(在TensorFlow 1.15中): import tensorflow as tf # 仿射变换矩阵,我们一个简单的平移矩阵:x方向平移10,y方向平移20 matrix = tf.constant([[1.0, 0.0, 10.0], [0.0, 1.0, 20.0]]) # 点 (1,1) point = tf.constant([1.0, 1.0]) # 扩展为齐次坐标 point_homo = tf.concat([point, [1.0]], axis=0) # 将矩阵扩展为3x3,第三行为[0,0,1](但实际我们只需要前两行,所以也可以不扩展,直接前两行与齐次坐标点乘) # 我们使用矩阵乘法:矩阵是2x3,点齐次坐标是3x1,得到2x1 # 注意:矩阵乘法要求第一个矩阵的列数等于第二个矩阵的行数,所以matrix(2x3)乘以point_homo(3x1) -> (2x1) point_transformed = tf.matmul(matrix, tf.reshape(point_homo, [3,1])) with tf.Session() as sess: result = sess.run(point_transformed) print(result) 在Python2和Python3中运行上述代码,预期结果应该是[[11.0], [21.0]]。 由于我们使用的是浮点数,并且TensorFlow核心是C++,所以结果应该一致。 但是,如果我们在代码中使用了整数,那么由于Python2和Python3的整数除法不同,可能会导致不同的结果。例如,如果我们计算中间量时使用了整数除法,那么结果可能不同。因此,在编写TensorFlow代码时,建议使用浮点数,并注意除法操作。 结论:在相同的TensorFlow版本下,使用相同的代码(注意避免整数除法的差异),坐标变换的结果在Python2和Python3中应该是一致的。 但是,由于TensorFlow 2.x不再支持Python2,所以如果使用TensorFlow 2.x,则无法在Python2中使用。因此,如果需要在Python2中使用TensorFlow,只能使用1.x版本,并且注意代码中不要有Python2和Python3不兼容的语法(如print语句在Python2中是print ...,在Python3中是print(...))。 因此,回答用户的问题:在相同TensorFlow版本(1.x)下,只要代码本身避免了Python2和Python3的不兼容性(特别是整数除法),坐标变换的结果应该是一致的。 生成相关问题: 1. TensorFlow 1.x和2.x在坐标变换API上有何不同? 2. 如何解决Python2和Python3中整数除法的差异问题? 3. TensorFlow中常用的坐标变换函数有哪些?</think>### Python 2 vs Python 3 中 TensorFlow 坐标变换的一致性分析 在 TensorFlow 中进行坐标变换时,**Python 2 和 Python 3 的核心计算结果是一致的**,因为 TensorFlow 的底层计算由 C++ 实现,Python 只是接口层。但需注意以下关键差异点: --- #### 1. **整数除法行为差异**(主要影响因素) - **Python 2**:整数除法默认向下整 ```python 3 / 2 = 1 # 整数结果 ``` - **Python 3**:整数除法返回浮点数 ```python 3 / 2 = 1.5 # 浮点数结果 ``` **对坐标变换的影响**: 若代码中包含整数除法(如缩放比例计算),在 Python 2 中可能导致精度损失,而 Python 3 会保留小数。例如: ```python # 缩放变换示例 scale_factor = 3 / 2 # Python 2 得 1,Python 3 得 1.5 tf.image.resize(images, [int(h * scale_factor), int(w * scale_factor)]) ``` **解决方案**: 显式使用浮点数或 `//` 明确整除: ```python scale_factor = 3.0 / 2.0 # 两者均得 1.5 scale_factor = 3 // 2 # 两者均得 1(明确整除) ``` --- #### 2. **TensorFlow API 版本兼容性** - **TensorFlow 1.x**:同时支持 Python 2/3,但需确保 API 调用方式一致。 - **TensorFlow 2.x+**:仅支持 Python 3,官方已停止 Python 2 支持[^1]。 若需跨版本兼容,应固定使用 **TensorFlow 1.15**(最后一个支持 Python 2 的版本)。 --- #### 3. **数据类型处理差异** - **Python 2**:`int` 和 `long` 是分离类型 - **Python 3**:只有 `int`(支持大整数) **影响场景**: 处理超大坐标(如 $10^{18}$)时,Python 2 需显式声明 `long`,否则可能溢出。 --- #### 4. **验证实验** 以下代码在 Python 2/3 + TensorFlow 1.15 中输出相同结果: ```python import tensorflow as tf # 定义坐标点 (2, 3) 的齐次坐标 point = tf.constant([2.0, 3.0, 1.0], dtype=tf.float32) # 平移变换矩阵 (x+10, y+20) matrix = tf.constant([[1, 0, 10], [0, 1, 20]], dtype=tf.float32) # 执行变换: [x'] = [1 0 10] [x] # [y'] [0 1 20] [y] # [1] transformed = tf.matmul(matrix, tf.reshape(point, (3, 1))) with tf.Session() as sess: result = sess.run(transformed) print(result) # 输出: [[12.], [23.]] ``` --- #### 结论 | 项目 | Python 2 | Python 3 | 一致性 | |--------------------|------------------------------|------------------------------|--------| | **底层计算** | TensorFlow C++ 内核 | TensorFlow C++ 内核 | ✅ 一致 | | **整数除法** | 整除 (`3/2=1`) | 真除 (`3/2=1.5`) | ❌ 需调整 | | **TensorFlow 2+** | ❌ 不支持 | ✅ 支持 | - | | **大整数处理** | 需显式 `long` | 统一 `int` | ❌ 需调整 | **最佳实践**: 1. 使用 **TensorFlow 1.15** 确保双版本兼容 2. 避免整数除法,改用浮点数或 `//` 3. 对坐标显式指定 `dtype=tf.float32` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值