Ethan's Blog


  • Home

  • Archives

  • Tags

  • Search

Generate CLI from Swagger

Posted on 2022-04-08

It’s always good to start with a command like that:

1
$ autorest --version=3.0.6370 --az --use=@autorest/az@latest ../azure-rest-api-specs/specification/dnsresolver/resource-manager/readme.md --azure-cli-extension-folder=./azure-cli-extensions

Make sure you have these specific versions installed:

AutoRest Core AutoRest CLI Node.js
3.0.6370 3.5.1 12.20

Errors you might encounter nowadays:

  • ERROR: Schema violation: Additional properties not allowed: x-ms-identifiers
    1. Append --pass-thru:schema-validator-swagger to the command;
  • AttributeError: module ‘mistune’ has no attribute ‘BlockGrammar’
    1. Active the venv within ~/.autorest/@autorest_python@5.4.0/node_modules/@autorest/python;
    2. Execute pip install mistune==0.8.4;
  • ImportError: cannot import name ‘_unicodefun’ from ‘click’
    1. Active the venv within ~/.autorest/@autorest_az@1.8.0/node_modules/@autorest/az;
    2. Execute pip install click==8.0.2;

One more thing, it’s a good practice to clean up AutoRest extensions by:

1
$ autorest --reset
Read more »

Contribute to Azure CLI

Posted on 2022-04-04

What is Azure CLI?

Azure CLI is a command-line tool to create and manage resources. You interact with Azure by running commands in a terminal or writing scripts to automate tasks. Azure CLI interacts with the Azure Resource Manager (ARM) service, which is the management layer to interact with resources in your account.

Prerequisites

Fork and clone the repositories you wish to develop for:

1
2
$ git clone https://github.com/necusjz/azure-cli.git
$ git clone https://github.com/necusjz/azure-cli-extensions.git

Add or change the upstream:

1
2
$ git remote add upstream https://github.com/Azure/azure-cli.git
$ git remote set-url upstream https://github.com/Azure/azure-cli.git

Install and setup azdev:

1
2
$ pip install azdev
$ azdev setup -c -r ./azure-cli-extensions

List what extensions are currently visible to your development environment:

1
2
$ azdev extension list -o table
$ azdev extension add {}

Publish extensions:

1
$ azdev extension publish {} --storage-account {} --storage-container {} --storage-account-key {}

场景:处理旅游数据

Posted on 2022-03-26

Ethan 打开笔记本电脑,从电脑桌面上的文件夹里翻出两个 Excel 表格文件,在这两个文件里,分别存着最近去过普吉岛和新西兰旅游的旅客信息,需要从这两份数据里,找出那些去过普吉岛但没去过新西兰的人,再让销售人员向他们推销一些新西兰精品旅游路线。将文件转换为 JSON 格式后,里面的内容大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 去过普吉岛的人员数据
users_visited_phuket = [
{
"first_name": "Sirena",
"last_name": "Gross",
"phone_number": "650-568-0388",
"date_visited": "2018-03-14",
},
...
]

# 去过新西兰的人员数据
users_visited_nz = [
{
"first_name": "Justin",
"last_name": "Malcom",
"phone_number": "267-282-1964",
"date_visited": "2011-03-13",
},
...
]
Read more »

开发大型项目

Posted on 2022-03-22

数据模型与描述符

在 Python 中,数据模型(Data Model)是一个非常重要的概念,假如把 Python 语言看作一个框架,数据模型就描述了框架如何工作,创建怎样的对象才能更好地融入 Python 这个框架。所有与数据模型有关的方法,基本都是以双下划线 __ 开头和结尾,它们通常被称为魔法方法(Magic Method)。

字符串魔法方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Person:
def __init__(self, name, age, favorite_color):
self.name = name
self.age = age
self.favorite_color = favorite_color

def __str__(self):
return self.name

def __repr__(self):
# 变量名后的 !r 表示在渲染字符串模版时,程序会优先使用 repr() 而非 str() 的结果
return '{cls_name}(name={name!r}, age={age!r}, favorite_color={color!r})'.format(
cls_name=self.__class__.__name__,
name=self.name,
age=self.age,
color=self.favorite_color,
)

def __format__(self, format_spec):
if format_spec == 'verbose':
return f'{self.name}({self.age})[{self.favorite_color}]'
elif format_spec == 'simple':
return f'{self.name}({self.age})'
return self.name
Read more »

场景:寻找元类替代品

Posted on 2022-03-20

Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).
-- Tim Peters

元类是 Python 中的一种特殊对象,它控制着类的创建行为,就像普通类控制着实例的创建行为一样。type 是 Python 中最基本的元类,直接调用 type() 就可以创建一个类:

1
2
3
>>> Foo = type('Foo', (), {'bar': 3})
>>> Foo
<class '__main__.Foo'>

在调用 type() 创建类时,需要提供三个参数,它们的含义如下:

  • name: str,需要创建的类名;
  • bases: Tuple[Type],包含其他类的元组,代表类的所有基类;
  • attrs: Dict[str, Any],包含所有类成员(属性、方法)的字典;
Read more »
1…101112…55
necusjz

necusjz

271 posts
16 tags
© 2016 - 2026 necusjz
Powered by Hexo
Theme - NexT.Mist