在Python 3中实现类型检查器的简单方法

示例函数

为了开发类型检查器,我们需要一个简单的函数对其进行实验。欧几里得算法就是一个完美的例子:

Python

 

1

2

3

4

5

6

7

8

9

def gcd(a, b):

    '''Return the greatest common divisor of a and b.'''

    a = abs(a)

    b = abs(b)

    if a < b:

        a, b = b, a

    while b != 0:

        a, b = b, a % b

    return a

在上面的示例中,参数 a 和 b 以及返回值应该是 int 类型的。预期的类型将会以函数注解的形式来表达,函数注解是 Python 3 的一个新特性。接下来,类型检查机制将会以一个装饰器的形式实现,注解版本的第一行代码是:

Python

 

1

def gcd(a: int, b: int) -> int:

使用“gcd.__annotations__”可以获得一个包含注解的字典:

Python

 

1

2

3

4

>>> gcd.__annotations__

{'return': <class 'int'>, 'b': <class 'int'>, 'a': <class 'int'>}

>>> gcd.__annotations__['a']

<class 'int'>

需要注意的是,返回值的注解存储在键“return”下。这是有可能的,因为“return”是一个关键字,所以不能用作一个有效的参数名。

检查返回值类型

返回值注解存储在字典“__annotations__”中的“return”键下。我们将使用这个值来检查返回值(假设注解存在)。我们将参数传递给原始函数,如果存在注解,我们将通过注解中的值来验证其类型:

Python

 

1

2

3

4

5

6

7

8

def typecheck(f):

    def wrapper(*args, **kwargs):

        result = f(*args, **kwargs)

        return_type = f.__annotations__.get('return', None)

        if return_type and not isinstance(result, return_type):

            raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))

        return result

    return wrapper

我们可以用“a”替换函数gcd的返回值来测试上面的代码:

Python

 

1

2

3

4

5

6

Traceback (most recent call last):

  File "typechecker.py", line 9, in <module>

    gcd(1, 2)

  File "typechecker.py", line 5, in wrapper

    raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))

RuntimeError: gcd should return int

由上面的结果可知,确实检查了返回值的类型。

检查参数类型

函数的参数存在于关联代码对象的“co_varnames”属性中,在我们的例子中是“gcd.__code__.co_varnames”。元组包含了所有局部变量的名称,并且该元组以参数开始,参数数量存储在“co_nlocals”中。我们需要遍历包括索引在内的所有变量,并从参数“args”中获取参数值,最后对其进行类型检查。

得到了下面的代码:

Python

 

1

2

3

4

5

6

7

8

9

10

11

12

13

def typecheck(f):

    def wrapper(*args, **kwargs):

        for i, arg in enumerate(args[:f.__code__.co_nlocals]):

            name = f.__code__.co_varnames[i]

            expected_type = f.__annotations__.get(name, None)

            if expected_type and not isinstance(arg, expected_type):

                raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))

        result = f(*args, **kwargs)

        return_type = f.__annotations__.get('return', None)

        if return_type and not isinstance(result, return_type):

            raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))

        return result

    return wrapper

在上面的循环中,i是数组args中参数的以0起始的索引,arg是包含其值的字符串。可以利用“f.__code__.co_varnames[i]”读取到参数的名称。类型检查代码与返回值类型检查完全一样(包括错误消息的异常)。

为了对关键字参数进行类型检查,我们需要遍历参数kwargs。此时的类型检查几乎与第一个循环中相同:

Python

 

1

2

3

4

for name, arg in kwargs.items():

    expected_type = f.__annotations__.get(name, None)

    if expected_type and not isinstance(arg, expected_type):

        raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))

得到的装饰器代码如下:

Python

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

def typecheck(f):

    def wrapper(*args, **kwargs):

        for i, arg in enumerate(args[:f.__code__.co_nlocals]):

            name = f.__code__.co_varnames[i]

            expected_type = f.__annotations__.get(name, None)

            if expected_type and not isinstance(arg, expected_type):

                raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))

        for name, arg in kwargs.items():

            expected_type = f.__annotations__.get(name, None)

            if expected_type and not isinstance(arg, expected_type):

                raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))

        result = f(*args, **kwargs)

        return_type = f.__annotations__.get('return', None)

        if return_type and not isinstance(result, return_type):

            raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))

        return result

    return wrapper

将类型检查代码写成一个函数将会使代码更加清晰。为了简化代码,我们修改错误信息,而当返回值是无效的类型时,将会使用到这些错误信息。我们也可以利用 functools 模块中的 wraps 方法,将包装函数的一些属性复制到 wrapper 中(这使得 wrapper 看起来更像原来的函数):

Python

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

def typecheck(f):

    def do_typecheck(name, arg):

        expected_type = f.__annotations__.get(name, None)

        if expected_type and not isinstance(arg, expected_type):

            raise RuntimeError("{} should be of type {} instead of {}".format(name, expected_type.__name__, type(arg).__name__))

 

    @functools.wraps(f)

    def wrapper(*args, **kwargs):

        for i, arg in enumerate(args[:f.__code__.co_nlocals]):

            do_typecheck(f.__code__.co_varnames[i], arg)

        for name, arg in kwargs.items():

            do_typecheck(name, arg)

 

        result = f(*args, **kwargs)

 

        do_typecheck('return', result)

        return result

    return wrapper

 

结论

注解是 Python 3 中的一个新元素,本文例子中的使用方法很普通,你也可以想象很多特定领域的应用。虽然上面的实现代码并不能满足实际产品要求,但它的目的本来就是用作概念验证。可以对其进行以下改善:

  • 处理额外的参数( args 中意想不到的项目)
  • 默认值类型检查
  • 支持多个类型
  • 支持模板类型(例如,int 型列表)

无意中浏览到这篇文章,感觉博主写的不错,这里转载一下https://www.jb51.net/article/68899.htm

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要使用 Python 在 ArcGIS 对指定图层实现增加多个指定字段,可以使用 `AddField_management` 工具。该工具用于向指定的要素类或表添加新字段。 以下是一个示例代码,可以向名为 `my_layer` 的图层添加名为 `field1`、`field2` 和 `field3` 的三个新字段: ```python import arcpy # 设置工作空间 arcpy.env.workspace = "C:/data" # 要添加字段的图层名称 layer_name = "my_layer" # 添加字段列表 field_list = ["field1", "field2", "field3"] # 循环遍历字段列表,并向图层添加每个字段 for field in field_list: arcpy.AddField_management(layer_name, field, "TEXT", "", "", 50) ``` 在上面的代码,首先将工作空间设置为数据所在的文件夹。然后,定义了要添加字段的图层名称和一个包含要添加的字段名称的列表。最后,使用 `AddField_management` 工具的 `for` 循环遍历列表,并向图层添加每个字段。在此示例,所有新字段的数据类型都设置为“文本”,并且字符长度为50。根据需要,可以调整这些参数。 注意,为了使用此代码,需要安装并配置 ArcPy 模块,并且必须具有适当的 ArcGIS 许可证。 ### 回答2: 要在ArcGIS使用Python对指定图层实现增加多个指定字段,可以使用arcpy模块和相应的函数。以下是一个简单的代码示例: ```python import arcpy # 设置工作空间 workspace = r"C:\path\to\your\workspace.gdb" arcpy.env.workspace = workspace # 指定要操作的图层和要添加的字段 layer_name = "your_layer" field_names = ["field1", "field2", "field3"] # 检查图层是否存在 if arcpy.Exists(layer_name): # 使用ListFields函数查看当前图层的字段 fields = arcpy.ListFields(layer_name) # 检查要添加的字段是否已经存在于图层 existing_fields = [field.name for field in fields] new_fields = [field_name for field_name in field_names if field_name not in existing_fields] if new_fields: # 使用AddField函数逐个在图层添加新字段 for new_field in new_fields: arcpy.AddField_management(layer_name, new_field, "TEXT", field_length=50) print("字段添加成功。") else: print("所有要添加的字段已经存在于图层。") else: print("指定的图层不存在。请检查图层名称和工作空间路径是否正确。") ``` 以上代码首先设置了工作空间,然后指定了要操作的图层名称和要添加的字段列表。 然后,代码检查指定的图层是否存在,并使用ListFields函数获得当前图层的所有字段。 接着,代码检查要添加的字段是否已经存在于图层,将不存在的字段加入到一个新列表。 最后,代码使用AddField函数逐个在图层添加新字段,字段类型设置为“TEXT”,字段长度设置为50。 如果所有要添加的字段都已经存在于图层,则输出提示信息。 如果指定的图层不存在,则输出错误信息。 请注意,在运行代码之前,请将示例的工作空间路径、图层名称和字段列表更改为您实际的路径和名称。此外,还可以根据需要修改字段类型和长度。 ### 回答3: 在ArcGIS,可以使用Python的ArcPy模块来实现对指定图层增加多个指定字段的操作。下面是一个示例代码: ```python import arcpy # 指定要添加字段的图层路径 layer_path = r"C:\path\to\your\layer.shp" # 指定要添加的字段列表 fields = [ {'name': 'field1', 'type': 'TEXT'}, {'name': 'field2', 'type': 'DOUBLE'}, {'name': 'field3', 'type': 'LONG'} ] # 打开编辑会话 with arcpy.da.Editor(arcpy.env.workspace) as edit_session: # 打开图层 with arcpy.da.UpdateCursor(layer_path, ['shape@']) as cursor: # 在每个要素上迭代 for row in cursor: # 获取要素的几何信息 geometry = row[0] # 创建一个新的要素对象,并将几何信息赋值给新要素 new_feature = arcpy.Feature() # 将新要素添加到图层 cursor.insertRow([new_feature]) # 在图层添加指定的字段 arcpy.management.AddFields(layer_path, fields) print("字段添加完成。") ``` 以上代码首先指定了要添加字段的图层路径和要添加的字段列表。然后,在编辑会话打开图层,并在每个要素上迭代,创建一个新的要素对象,并将其添加到图层。最后,使用`arcpy.management.AddFields()`函数在图层添加指定的字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值