我有一个包含所有 AWS 区域的 Python 类。我编写了一个类方法,它将返回所有区域的列表。有没有更好的方法来返回所有类变量值,这样我就不必像下面的示例中那样对 return 语句中的所有值进行硬编码?
class AwsRegion():
'''
Class to define AWS Regions
'''
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
@classmethod
def all(cls, ):
return [AwsRegion.OHIO, AwsRegion.NORTH_VIRGINIA, AwsRegion.NORTH_CALIFORNIA, AwsRegion.OREGON, \
AwsRegion.MUMBAI, AwsRegion.SEOUL, AwsRegion.SINGAPORE, AwsRegion.SYDNEY, AwsRegion.TOKYO, \
AwsRegion.FRANKFURT, AwsRegion.IRELAND, AwsRegion.LONDON, AwsRegion.SAO_PAULO]
Run Code Online (Sandbox Code Playgroud) 我在某种程度上熟悉Python中的列表推导.但是在我需要检查列表是否为None的情况下,列表理解会失败.
例如
tags = v.tags
if tags:
for t in tags:
if t['Key'] == 'Name':
# Do something
Run Code Online (Sandbox Code Playgroud)
现在,如果标签为None,则以下列表推导失败.如果标签为空/ [],它可以正常工作.我想要一个列表理解,检查无.
[k for k,v in tags if tags]:
Run Code Online (Sandbox Code Playgroud) 我试图了解变量如何在python中工作。说我有一个对象存储在变量中a:
>>> a = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
如果我分配a给b,则它们都指向同一个对象:
>>> b = a
>>> b is a
True
Run Code Online (Sandbox Code Playgroud)
但是,如果我重新分配a或b,那就不再正确了:
>>> a = {'x': 'y'}
>>> a is b
False
Run Code Online (Sandbox Code Playgroud)
这两个变量现在具有不同的值:
>>> a
{'x': 'y'}
>>> b
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我不明白为什么变量现在不同。为什么a is b不再是真的?有人可以解释发生了什么吗?
我在 PHP 中运行以下代码。我的目的是在响应中获得“contact.html”,但我在输出中实际得到的是ntact.html
$str = 'http://localhost/contact.html';
echo $str . "<br>";
echo ltrim($str,'http://localhost');
Run Code Online (Sandbox Code Playgroud)
有什么想法为什么 PHP 会这样,我该怎么做才能解决这个问题?
我有 2 个数组。arr1 将是一个超集,而 arr2 将是一个子集,并且 arr2 的大小将始终小于 arr1。
我想找出 arr1 中哪些值不在 arr2 中。
例如
arr1 = [
"07d65369-78eb-4afb-aba8-710ac146b93f",
"15d65369-25eb-9u4b-6t7m-820ac145t8w1"
]
arr2 = [
"07d65369-78eb-4afb-aba8-710ac146b93f"
]
Run Code Online (Sandbox Code Playgroud)
预期结果应该是 ["15d65369-25eb-9u4b-6t7m-820ac145t8w1"]
我在模块中定义了一个函数,该函数应该执行获取并返回响应。我在从 fetch 返回响应时遇到问题。调用函数将返回值设为“未定义”。
我是 JavaScript 和 Node 的新手,所以如果你不介意的话,可能需要一点帮助。
调用函数
async function executeTest() {
try {
const response = await bpc.postLendingApplication(
blendConnection,
loanData
);
console.log("Response from POST Loan: ", response);
} catch (error) {
console.log(error);
}
}
Run Code Online (Sandbox Code Playgroud)
执行获取请求的模块函数
const fetch = require("node-fetch");
async function postLendingApplication(connection, data) {
console.log("Processing POST Loan.");
await fetch(connection.url, {
method: "POST",
headers: connection.headers,
body: data,
}).then(async res => {
console.log("Status: ", res.status);
console.log("StatusText: ", res.statusText);
console.log("OK: ", res.ok);
return await res;
});
}
Run Code Online (Sandbox Code Playgroud)
控制台输出是:
Processing POST Loan. …Run Code Online (Sandbox Code Playgroud) python ×3
async-await ×1
class ×1
class-method ×1
dataweave ×1
fetch ×1
javascript ×1
node-fetch ×1
node.js ×1
php ×1
reference ×1
substring ×1
texttrimming ×1
url ×1
url-parsing ×1
variables ×1