我想导入一堆库并捕获异常.
如果我只有1个尝试catch块,我会得到1个异常(第一个).是否存在迭代所有库的模式,并且每个缺少的lib都有一个单独的例外?
#!/usr/bin/env python
try: import sys
except: print sys.exc_info()
try: import numpy as np
except: print sys.exc_info()
try: import scipy as sp
except: print sys.exc_info()
try: import os as os
except: print sys.exc_info()
try: from operator import itemgetter
except: print sys.exc_info()
try: import socket
except: print sys.exc_info()
try: import logging
except: print sys.exc_info()
try: from time import gmtime, strftime
except: print sys.exc_info()
Run Code Online (Sandbox Code Playgroud) 这是代码的当前版本,它可以完成非常简单的工作.它启动10个例程,每个例程向通道添加10条消息.另一端是一个真正的循环,读取通道并每500毫秒超时.
我在想有更好的东西.我认为while true循环可以替换为recur,其中它读取通道,并且在每次成功读取之后它会再次读取它.如果发生超时,它只会终止执行.
我有两个问题: - 这是正确的方法吗? - 如何使用惯用的Clojure实现它
(defn -main [& args]
(let [c (async/chan)]
(doseq [i (range 10)]
(async/go
(doseq [j (range 10)]
(Thread/sleep (rand-int 1000))
(async/>! c (str i " :: " j)))))
(while true
(async/<!!
(async/go
(let [[result source] (async/alts! [c (async/timeout 500)])]
(if (= source c)
(println "Got a value!" result)
(println "Timeout!"))))))))
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来列出可以在AWS IAM策略中使用的所有操作.
这是使用IAM操作的示例策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1457442845000",
"Effect": "Allow",
"Action": [
"iam:CreatePolicy",
"iam:CreatePolicyVersion",
"iam:GetGroupPolicy",
"iam:CreateGroup",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetRolePolicy",
"iam:ListAttachedGroupPolicies"
],
"Resource": [
"*"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想搜索文件中的操作,为此我想拥有所有可用的操作.我找不到一种方法来获得该列表.任何方向表示赞赏.
我有一堆 Avro 文件,我想从 S3 中逐个读取它们。我以字节形式读取文件没有问题,但我想知道之后如何迭代整个文件。当前代码:
conn = boto.s3.connect_to_region("us-east-1")
my_bucket=boto.s3.bucket.Bucket(conn, "my_bucket")
my_key = my_bucket.get_key("folder/file.avro")
raw_bytes = my_key.read()
test_schema = '''
{
"namespace": "com.company",
"type": "record",
"name": "MimeMessage_v2",
"fields": [
{
"name": "record_timestamp",
"type": "long"
},
{
"name": "contents",
"type": "bytes"
}
],
"message_id": 2
}
'''
schema = avro.schema.Parse(test_schema)
#this is the problematic section
dreader = DatumReader(schema, schema)
v = dreader.read(raw_bytes)
Run Code Online (Sandbox Code Playgroud)
我想知道如何正确读取包含 Avro 文件字节的变量。
我试图用Bokeh绘制一个简单的图表但是当x值是基于文本时它无法显示任何内容:
x=['-', 'AF', 'AS', 'EU', 'NA', 'OC', 'SA']
y=[8, 7621750, 33785311, 31486697, 38006434, 7312002, 7284879]
p = figure(plot_width=480, plot_height=300,title='test')
p.vbar(x=x, width=0.5, bottom=0, top=y, color="navy", alpha=0.5)
p.toolbar.logo = None
p.toolbar_location = None
v = gridplot([[p]])
show(v)
Run Code Online (Sandbox Code Playgroud)
我想知道这是不是一个bug.版本:0.13.0
应用建议的修复后,它可以工作:
for i in range(4):
ind=i+offset
rez[ind].sort(key=lambda tup: tup[0])
x = [x[0] for x in rez[ind]]
y = [x[1] for x in rez[ind]]
if type(x[0]) == str:
charts[i] = figure(
plot_width=480,
plot_height=300,
title=columns_being_investigated[ind],
x_range=x)
else:
charts[i] = figure(
plot_width=480,
plot_height=300,
title=columns_being_investigated[ind])
charts[i].vbar(x=x, width=0.5, bottom=0, …Run Code Online (Sandbox Code Playgroud) 我使用的是 Mac,新的 .NET 5.0 刚刚发布。我已经用 dotnet-install.sh 安装了它
dotnet-install.sh --version 5.0.100
dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.
dotnet-install: .NET Core …Run Code Online (Sandbox Code Playgroud) 我有一个 api.py 文件,其中包含以下内容:
from fastapi import FastAPI
import logging
import uvicorn
app = FastAPI(title="api")
LOG = logging.getLogger(__name__)
LOG.info("API is starting up")
LOG.info(uvicorn.Config.asgi_version)
@app.get("/")
async def get_index():
LOG.info("GET /"
return {"Hello": "Api"}
Run Code Online (Sandbox Code Playgroud)
该应用程序在本地运行:
uvicorn api:app --reload
Run Code Online (Sandbox Code Playgroud)
INFO: Will watch for changes in these directories: ['/Users/user/code/backend/api']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [44258] using StatReload
INFO: Started server process [44260]
INFO: Waiting for application startup.
INFO: Application startup complete.
Run Code Online (Sandbox Code Playgroud)
它不记录任何启动消息。
稍后向 api 发送 http 请求时: …
目前我有以下使用生成器:
(for i <- 999..100, j <- i..100, into: [], do: i * j)
|> Stream.filter(&(palindromic?(&1)))
|> Enum.sort
|> List.last
Run Code Online (Sandbox Code Playgroud)
有没有办法生成产品流?
我试图在OCaml中实现一个简单的范围函数,我想知道如何为不同的arity调用做到这一点.
let range_aux ~start ~stop ~step =
let rec aux start stop step acc =
match (start, stop, step, acc) with
| (start,stop,step,acc) when start = stop -> List.rev acc
| (start,stop,step,acc) -> aux (start + step) stop step (start :: acc) in
aux start stop step []
let range ~start ~stop ~step = range_aux ~start ~stop ~step
let range ~stop ~step = range_aux ~start:0 ~stop ~step
let range ~stop = range_aux ~start:0 ~stop ~step:1
Run Code Online (Sandbox Code Playgroud)
这显然不适用于最后一个定义胜利.有没有办法定义多个arity函数?
http::HeaderMap在 Rust 中将HTTP 请求标头 ( )序列化为 JSON的正确方法是什么?
我正在实现一个 AWS Lambda 函数,我想要一个简单的回声函数来进行调试。
use lambda_http::{lambda, IntoResponse, Request};
use lambda_runtime::{error::HandlerError, Context};
use log::{self, info};
use simple_logger;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_level(log::Level::Debug)?;
info!("Starting up...");
lambda!(handler);
return Ok(());
}
fn handler(req: Request, ctx: Context) -> Result<impl IntoResponse, HandlerError> {
Ok(format!("{}", req.headers()).into_response())
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以转换req.headers()为 JSON 并返回?