面向对象的 CLI:使用 Fire 简化类和对象的方法暴露 (中英双语)
面向对象的 CLI:使用 Fire 简化类和对象的方法暴露
在传统的命令行工具开发中,argparse 是最常用的库之一,用于处理命令行参数和配置。它通常用于函数式编程,但在处理类和对象时,使用起来可能不如 Fire 方便。Fire 是一个自动生成命令行接口的库,不仅支持函数,还能够支持类和对象。它通过动态地将 Python 类的属性和方法暴露给命令行,极大地简化了命令行工具的开发过程。
本文将探讨 Fire 如何支持类和对象,并通过一个示例来展示它的优势。我们将详细讲解代码,并分析 Fire 如何实现这一功能。
Fire 如何支持类和对象?
在使用 Fire 时,我们可以将类及其方法直接暴露为命令行工具,而无需手动设置参数解析。Fire 会自动分析类的构造函数(__init__)和方法(def),并生成对应的命令行接口。
关键原理:
- 自动映射:Fire 会扫描类的构造函数和方法,自动识别方法参数,并将它们映射为命令行参数。
- 递归支持:如果类方法中还有其他类的实例,Fire 会递归地将这些类和方法也暴露给命令行。
- 自动类型推断:Fire 会根据函数签名推断参数的类型,包括数字、布尔值和字符串等。
通过这种方式,Fire 可以非常方便地将类及其方法转化为命令行工具,并允许用户像调用普通命令行工具一样,传递参数给类的方法。
示例:使用 Fire 曝露类和对象
让我们通过一个简单的例子来演示 Fire 如何支持类和对象。
代码示例
import fireclass DatasetManager:def __init__(self, repo_id):self.repo_id = repo_iddef upload(self, version: str, overwrite=False):print(f"Uploading dataset version {version} to {self.repo_id}...")if overwrite:print(f"Overwriting existing version {version}.")else:print(f"Version {version} will be added without overwriting.")def list_tags(self):print(f"Listing tags for {self.repo_id}...")class CLI:def __init__(self, repo_id):self.manager = DatasetManager(repo_id)def upload(self, version: str, overwrite=False):self.manager.upload(version, overwrite)def list_tags(self):self.manager.list_tags()if __name__ == "__main__":fire.Fire(CLI)
代码解析
-
DatasetManager 类:
DatasetManager类包含两个方法:upload和list_tags。upload方法用于上传数据集并可选择是否覆盖现有版本,而list_tags用于列出数据集的标签。
-
CLI 类:
CLI类封装了DatasetManager类的实例,并将其方法暴露为命令行工具。用户可以通过CLI类的方法来操作DatasetManager。
-
fire.Fire(CLI):
fire.Fire(CLI)让CLI类的所有方法变成命令行工具,CLI的每个方法会成为一个命令。
运行命令行
假设我们保存了以上代码到一个文件 cli_tool.py,然后通过命令行运行:
- 上传数据集:
$ python cli_tool.py upload v1.0 --overwrite
Uploading dataset version v1.0 to None...
Overwriting existing version v1.0.
- 列出标签:
$ python cli_tool.py list_tags
Listing tags for None...
解析
- 在命令行中,
upload和list_tags方法分别映射为两个命令。用户只需要按照方法的签名提供相应的参数,Fire 会自动处理参数解析。 version和overwrite被视为upload方法的命令行参数,而repo_id是DatasetManager类的一个实例属性,默认在创建时传入。
自动生成 CLI 的工作原理
Fire 的核心机制是在后台使用 Python 的反射功能来检查类及其方法。当调用 fire.Fire(CLI) 时,Fire 会:
- 扫描
CLI类中的所有方法。 - 检查每个方法的参数类型,并生成相应的命令行选项。
- 如果方法中使用了其他类的实例,Fire 会递归地处理这些实例,使得整个类层级都能通过命令行操作。
Fire 的这个特性使得开发者能够快速为现有的类和对象生成命令行工具,而不需要编写冗长的命令行参数解析代码。
总结
Fire 的强大之处在于它能够简化命令行工具的开发,特别是对于包含多个类和对象的复杂项目。通过自动识别类的方法和参数,Fire 提供了一个简洁而强大的方式,将类和对象暴露为命令行接口。这种方式不仅减少了开发者的工作量,还提高了代码的可读性和可维护性。
对于那些需要在命令行中操作多个类或方法的情况,Fire 无疑是一个更合适的选择,特别是在需要快速原型开发时,它的面向对象支持使得代码更加简洁高效。
Object-Oriented CLI: How Fire Supports Classes and Objects
When developing command-line interfaces (CLI) in Python, libraries like argparse are commonly used for parsing command-line arguments. However, when it comes to dealing with classes and objects, Fire, a Python library developed by Google, offers significant advantages over argparse. Unlike argparse, which requires manually setting up argument parsing for each function, Fire can automatically expose Python classes, methods, and attributes as command-line commands, making it a more flexible and convenient tool for building CLIs.
In this blog post, we’ll explore how Fire supports classes and objects, demonstrate it with a code example, and dive into the internal workings of how Fire makes this possible.
How Fire Supports Classes and Objects
Fire simplifies the process of exposing Python classes and their methods as command-line interfaces by dynamically creating a CLI based on the structure of the class. It recursively discovers class methods and instance variables, making them available as command-line arguments.
Key Principles:
- Automatic Mapping: Fire scans classes and methods, automatically turning them into CLI commands.
- Recursive Support: If a method requires another class instance, Fire automatically makes that class and its methods available as subcommands.
- Type Inference: Fire can infer argument types based on the function signature, including strings, integers, booleans, etc.
This dynamic generation of CLI commands makes it incredibly easy to turn an object-oriented class into a functional command-line tool.
Example: Using Fire with Classes and Objects
Let’s walk through a simple example where we use Fire to expose a class and its methods via the command line.
Code Example
import fireclass DatasetManager:def __init__(self, repo_id):self.repo_id = repo_iddef upload(self, version: str, overwrite=False):print(f"Uploading dataset version {version} to {self.repo_id}...")if overwrite:print(f"Overwriting existing version {version}.")else:print(f"Version {version} will be added without overwriting.")def list_tags(self):print(f"Listing tags for {self.repo_id}...")class CLI:def __init__(self, repo_id):self.manager = DatasetManager(repo_id)def upload(self, version: str, overwrite=False):self.manager.upload(version, overwrite)def list_tags(self):self.manager.list_tags()if __name__ == "__main__":fire.Fire(CLI)
Explanation of the Code
-
DatasetManager Class:
- Contains methods for uploading datasets (
upload) and listing dataset tags (list_tags). - The
uploadmethod has parameters forversionandoverwrite.
- Contains methods for uploading datasets (
-
CLI Class:
- Encapsulates an instance of
DatasetManagerand exposes its methods (uploadandlist_tags) to the command line.
- Encapsulates an instance of
-
fire.Fire(CLI):
- This line exposes all the methods of the
CLIclass to the command line.CLImethods become commands that can be executed with arguments.
- This line exposes all the methods of the
Running the CLI
Assuming you saved the above code to a file named cli_tool.py, you can execute it from the command line like this:
-
Upload Dataset:
$ python cli_tool.py upload v1.0 --overwrite Uploading dataset version v1.0 to None... Overwriting existing version v1.0. -
List Tags:
$ python cli_tool.py list_tags Listing tags for None...
What Happens Behind the Scenes
- Fire automatically exposes the methods
uploadandlist_tagsas CLI commands. - It takes care of parsing the arguments (
version,overwrite) and passing them to the corresponding method ofDatasetManager. - The
repo_idis passed during the initialization of theCLIclass, making it accessible inDatasetManager.
How Fire Implements Object-Oriented CLI
Internally, Fire works by using Python’s reflection capabilities to inspect classes and methods. Here’s how it works step by step:
- Class Inspection: Fire inspects the class passed to
fire.Fire(), discovering all methods and attributes. - Argument Mapping: For each method, Fire maps function parameters to command-line arguments.
- Recursive Command Creation: If a method contains an object or class instance, Fire recursively creates commands for that class as well.
- Argument Parsing: Fire uses Python’s built-in
argparsemodule behind the scenes to handle argument parsing, but it generates the required configuration automatically based on the class signature.
This mechanism allows you to expose complex class hierarchies and objects without manually writing out argument parsing code.
Conclusion
Fire offers an elegant and efficient way to create CLIs from Python classes and objects. By automating the argument mapping and recursion, Fire eliminates much of the boilerplate code required by traditional CLI libraries like argparse. This makes Fire an excellent choice when working with complex object-oriented structures or when you need to quickly prototype command-line tools.
Whether you’re building a simple script or a large application with multiple classes, Fire provides a seamless interface to expose your code to the command line. Its automatic argument parsing, support for recursion, and object-oriented design make it a powerful tool for any developer looking to create a flexible and maintainable CLI.
后记
2024年12月15日19点40分于上海,在GPT4o mini大模型辅助下完成。
相关文章:
面向对象的 CLI:使用 Fire 简化类和对象的方法暴露 (中英双语)
面向对象的 CLI:使用 Fire 简化类和对象的方法暴露 在传统的命令行工具开发中,argparse 是最常用的库之一,用于处理命令行参数和配置。它通常用于函数式编程,但在处理类和对象时,使用起来可能不如 Fire 方便。Fire 是…...
flutter控件buildDragTargetWidget详解
文章目录 1. DragTarget 的核心概念基本属性 2. 基本用法3. 使用 buildDragTargetWidget4. 常见场景5. 注意事项 buildDragTargetWidget 不是 Flutter 中的内置 API 或方法,但根据命名习惯,它很可能是您正在实现或使用的一个方法,用于在 Flut…...
使用webrtc-streamer查看实时监控
摄像头配置(海康摄像头为例) 摄像头视频编码应改成H264格式 webrtc-streamer下载 webrtc-streamer下载地址 下载后解压出来双击运行,端口默认8000 VUE2项目引入文件 在项目静态文件“public”中需引入两个js文件“webrtcstreamer.js”与“…...
【数据分享】2014-2024年我国POI兴趣点数据(免费获取/来源于OSM地图)
POI是Point of Interest的简称,意为“兴趣点”,是互联网电子地图中用于表示特定位置的地理实体的核心数据类型。POI通常用于标注具体地点,例如餐厅、商场、学校、医院、景点等。这些数据以点的形式呈现,并附带详细属性信息&#x…...
Leetcode 3389. Minimum Operations to Make Character Frequencies Equal
Leetcode 3389. Minimum Operations to Make Character Frequencies Equal 1. 解题思路2. 代码实现 题目链接:3389. Minimum Operations to Make Character Frequencies Equal 1. 解题思路 这一题从答题从test的结果来说来说做出的人很少,主要确实有些…...
Vite 与 Webpack 的区别
在前端开发中,构建工具是不可或缺的,Webpack 和 Vite 是当前最流行的选择之一。尽管它们的目标相似,但在实现方式和开发体验上却有显著差异。本文将探讨 Vite 和 Webpack 的主要区别,以便于根据项目需求选择合适的工具。 1. 构建…...
基于32单片机的RS485综合土壤传感器检测土壤PH、氮磷钾的使用(超详细)
1-3为RS485综合土壤传感器的基本内容 4-5为基于STM32F103C8T6单片机使用RS485传感器检测土壤PH、氮磷钾并显示在OLED显示屏的相关配置内容 注意:本篇文件讲解使用的是PH、氮磷钾四合一RS485综合土壤传感器,但里面的讲解内容适配市面上的所有多合一的RS…...
【从零开始入门unity游戏开发之——C#篇11】一个标准 C# 程序介绍、新的值类型——枚举
文章目录 一、一个标准 C# 程序1、文件名(Program.cs):2、 using 语句:3、命名空间(namespace)4、类(class):4、入口函数(Main 方法)5、程序运行流…...
vue 签名校验 md5 uuid
import CryptoJS from crypto-js import uuid from /utils/uuid import { SECRET_KEY } from /utils/config // 签名校验 const nonceStr uuid.uuid() const timestamp new Date().getTime() // const sign CryptoJS.MD5(nonceStr nonceStr &secretKey SECRET_KEY …...
CSS系列(16)-- 架构与模式详解
前端技术探索系列:CSS 架构与模式详解 🏗️ 致读者:探索 CSS 架构的艺术 👋 前端开发者们, 今天我们将深入探讨 CSS 架构与设计模式,学习如何构建可维护的样式系统。 CSS 架构方法论 🚀 OO…...
【go语言】reflect包与类型推断
reflect 包的核心概念 Go 中的反射涉及两个核心概念: Type:表示一个类型的结构体,reflect.Type 是类型的描述。Value:表示一个值的结构体,reflect.Value 是一个具体值的包装。 反射让我们能够动态地访问对象的类型和…...
3.python运算符
Python 提供了多种运算符,用于执行算术、比较、逻辑等各种操作。以下是 Python 中常见的运算符类型及其用法: 文章目录 1. 算术运算符2. 比较运算符3. 逻辑运算符4. 赋值运算符5. 位运算符6. 成员运算符7. 身份运算符8. 运算符优先级 1. 算术运算符 算…...
【竞技宝】CS2-上海major:spirit力克MOUZ niko梦碎
北京时间2024年12月15日,CS2上海major正在如火如荼的进行中,昨日迎来两场半决赛MOUZ对阵spirit以及FAZE对阵G2。Spirit和MOUZ和各自赢下了自己的选图之后,spirit双子星在图三抗住压力帮助队伍杀入决赛。而G2和FAZE的比赛中,FAZE依然延续上一场的火热手感完全压制了G2,G2的明星选…...
【Leetcode 每日一题】3266. K 次乘运算后的最终数组 II
问题背景 给你一个整数数组 n u m s nums nums,一个整数 k k k 和一个整数 m u l t i p l i e r multiplier multiplier。 你需要对 n u m s nums nums 执行 k k k 次操作,每次操作中: 找到 n u m s nums nums 中的 最小 值 x x x&a…...
etcd集群常见日志
1、节点失去领导者 {"level":"info","ts":"2024-05-07T01:54:04.948Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"raft.node: 9afce9447872453 lost le…...
【漫话机器学习系列】005.神经网络的结构(architecture on the neural network)
神经网络(Neural Network)是一种模拟人脑神经系统的计算模型,由大量相互连接的神经元(节点)组成,广泛应用于深度学习和机器学习领域。以下是神经网络的基本结构及关键组成部分。 1. 神经网络的基本组成 一…...
基于 Couchbase 数据仓库元数据管理的可行性方案
在大数据体系中,元数据管理是数据治理的关键一环。以下是一套元数据管理的可行性方案,适合你的当前架构设计(基于 Couchbase 数据仓库)并支持高效管理数据的分层与结构。 1. 元数据管理的目标 统一数据管理:清晰描述 …...
SpringBoot:快速构建微服务应用
一、SpringBoot简介 什么是SpringBoot 是由Pivotal团队提供的快速开发框架。它基于Spring框架,可以用于快速构建微服务应用程序。SpringBoot提供了一种快速、便捷的方式来启动和配置一个基于Spring的应用程序,它封装了很多常用的配置,简化了开…...
汽车嵌入式软件构建高效技术团队的全面思考
在汽车嵌入式软件开发领域,构建一支高效的通用技术团队至关重要。这类团队负责为各种项目提供可复用、标准化的技术基石,从而提高开发效率、降低成本并确保产品质量。构建这样的团队需要从技术能力、角色分工、标准化与复用、流程管理与质量保证、工具和…...
【跨库查询、多库查询】.NET开源 ORM 框架 SqlSugar 系列
文章目录 一、跨库方式1:跨库导航二、手动跨库查询三、同服务器:自动查询跨库查询3.1 Mysql和SqlServer自动3.2 自动: PgSql跨Scheme查询3.3 其他库同服务器 四、跨服务器:自动跨库查询4.1 配置SqlServer dblink4.2 配置 Oracle dblink4.3 配…...
利用ngx_stream_return_module构建简易 TCP/UDP 响应网关
一、模块概述 ngx_stream_return_module 提供了一个极简的指令: return <value>;在收到客户端连接后,立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量(如 $time_iso8601、$remote_addr 等)&a…...
大话软工笔记—需求分析概述
需求分析,就是要对需求调研收集到的资料信息逐个地进行拆分、研究,从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要,后续设计的依据主要来自于需求分析的成果,包括: 项目的目的…...
抖音增长新引擎:品融电商,一站式全案代运营领跑者
抖音增长新引擎:品融电商,一站式全案代运营领跑者 在抖音这个日活超7亿的流量汪洋中,品牌如何破浪前行?自建团队成本高、效果难控;碎片化运营又难成合力——这正是许多企业面临的增长困局。品融电商以「抖音全案代运营…...
什么是库存周转?如何用进销存系统提高库存周转率?
你可能听说过这样一句话: “利润不是赚出来的,是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业,很多企业看着销售不错,账上却没钱、利润也不见了,一翻库存才发现: 一堆卖不动的旧货…...
在WSL2的Ubuntu镜像中安装Docker
Docker官网链接: https://docs.docker.com/engine/install/ubuntu/ 1、运行以下命令卸载所有冲突的软件包: for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done2、设置Docker…...
全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比
目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec? IPsec VPN 5.1 IPsec传输模式(Transport Mode) 5.2 IPsec隧道模式(Tunne…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
基于 TAPD 进行项目管理
起因 自己写了个小工具,仓库用的Github。之前在用markdown进行需求管理,现在随着功能的增加,感觉有点难以管理了,所以用TAPD这个工具进行需求、Bug管理。 操作流程 注册 TAPD,需要提供一个企业名新建一个项目&#…...
为什么要创建 Vue 实例
核心原因:Vue 需要一个「控制中心」来驱动整个应用 你可以把 Vue 实例想象成你应用的**「大脑」或「引擎」。它负责协调模板、数据、逻辑和行为,将它们变成一个活的、可交互的应用**。没有这个实例,你的代码只是一堆静态的 HTML、JavaScript 变量和函数,无法「活」起来。 …...
pikachu靶场通关笔记19 SQL注入02-字符型注入(GET)
目录 一、SQL注入 二、字符型SQL注入 三、字符型注入与数字型注入 四、源码分析 五、渗透实战 1、渗透准备 2、SQL注入探测 (1)输入单引号 (2)万能注入语句 3、获取回显列orderby 4、获取数据库名database 5、获取表名…...
