我需要在脚本中获取"当前用户" 的帐户IDboto3
.到目前为止,我最好的解决方案是解析当前用户arn:
>>> import boto3
>>> account_id = boto3.resource('iam').CurrentUser().arn.split(':')[4]
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更"轻量级"的方法.事实上
>>> timeit.timeit("boto3.resource('iam').CurrentUser().arn",
... 'import boto3', number=10)
4.8895583080002325
Run Code Online (Sandbox Code Playgroud)
我实际上不需要CurrentUser
脚本中的资源.
python3 套接字编程如何呈现此代码片段
class MySocket:
"""demonstration class only
- coded for clarity, not efficiency
"""
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(self, host, port):
self.sock.connect((host, port))
def mysend(self, msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def myreceive(self):
chunks = []
bytes_recd = 0
while bytes_recd < MSGLEN:
chunk = self.sock.recv(min(MSGLEN - bytes_recd, …
Run Code Online (Sandbox Code Playgroud) 我的切片numpy数组的代码(通过花哨的索引)非常慢.它目前是计划的瓶颈.
a.shape
(3218, 6)
ts = time.time(); a[rows][:, cols]; te = time.time(); print('%.8f' % (te-ts));
0.00200009
Run Code Online (Sandbox Code Playgroud)
获取由矩阵a的行'rows'和列'col'的子集组成的数组的正确numpy调用是什么?(事实上,我需要这个结果的转置)
在一个bash
脚本中我想将times
内置的输出分配给一个数组变量,但我发现没有比这更好的方法了
tempnam=/tmp/aaa_$$_$RANDOM
times > ${tempnam}
mapfile -t times_a < ${tempnam}
Run Code Online (Sandbox Code Playgroud)
我将输出写入临时文件并在数组times_a中读回,因为管道或$(times)
将在子shell中执行并返回错误的值.
没有临时文件的更好的解决方案?