我试图使用Jackson将类值写入JSON,其中包含Optional作为字段:
public class Test {
Optional<String> field = Optional.of("hello, world!");
public Optional<String> getField() {
return field;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Test()));
}
}
Run Code Online (Sandbox Code Playgroud)
执行时,此类生成以下输出:
{"field":{"present":true}}
Run Code Online (Sandbox Code Playgroud)
我理解当前/不存在的字段被包含在内并且可以在读取JSON数据时解决它,但是我无法解决可选的实际内容永远不会写入输出的事实.:(
除了不使用ObjectMapper之外,这里有任何变通方法吗?
所以我希望这个代码能够在新的Java 8日期/时间包下工作,因为它所做的只是将给定的ZonedDateTime转换为字符串并使用相同的内置DateTimeFormatter实例(ISO_INSTANT)返回:
ZonedDateTime now = ZonedDateTime.now();
System.out.println(ZonedDateTime.parse(
now.format(DateTimeFormatter.ISO_INSTANT),
DateTimeFormatter.ISO_INSTANT));
Run Code Online (Sandbox Code Playgroud)
但显然它没有:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=549, NanoOfSecond=549000000, MicroOfSecond=549000, InstantSeconds=1409600268},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
Run Code Online (Sandbox Code Playgroud)
我已经看过这个条目,但它没有帮助我,因为需要ZonedDateTime对象而不是本地对象,还因为我已经安装了8u20:无法使用Java8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime
任何人都知道这里发生了什么?
在旧boto库是很简单的使用proxy,proxy_port,proxy_user和proxy_pass当你打开一个连接参数.但是,我找不到在boto3上以编程方式定义代理参数的任何等效方法.:(
我在使用RCurl函数getURL访问使用自签名证书的服务器上的HTTPS URL 时遇到问题.我在Mac OS X 10.9.2上运行R 3.0.2.
我已经阅读了有关该主题的常见问题解答和卷曲页面.所以这就是我的立场:
RCurl顽固地拒绝使用以下代码连接到该网站:
getURL(" https://somesite.tld ",verbose = T,cainfo = normalizePath("〜/ cert.pem"))
这是我得到的输出:
* Adding handle: conn: 0x7f92771b0400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 38 (0x7f92771b0400) send_pipe: 1, recv_pipe: 0
* About to connect() to somesite.tld port 443 (#38)
* Trying 42.42.42.42...
* Connected to somesite.tld (42.42.42.42) port 443 …Run Code Online (Sandbox Code Playgroud) 我正在读取包含时间戳和时区规范的文件.我希望能够检测此文件上的给定时区是否被R识别,并提供我自己的默认值,如果不是.
但是,如果给出一个无效的时区,似乎as.POSIXct会无声地回退到UTC,没有错误或警告我可以捕获和处理:
> as.POSIXct("1970-01-01", tz="blah")
[1] "1970-01-01 UTC"
Run Code Online (Sandbox Code Playgroud)
在R中检查给定时区是否被识别的"适当"方式是什么?
我必须用R读取一个文件,其中可变数量的列由|字符分隔.但是,如果它前面有一个\它不应该被视为分隔符.
我首先想到的东西strsplit(x, "[^\\][|]")会起作用,但这里的问题是每个管道前的字符都被"消耗"了:
> strsplit("word1|word2|word3\\|aha!|word4", "[^\\][|]")
[[1]]
[1] "word" "word" "word3\\|aha" "word4"
Run Code Online (Sandbox Code Playgroud)
有谁能建议这样做的方法?理想情况下,它应该被矢量化,因为有问题的文件非常大.
我在 R 中使用 xgb.cv 和 xgboost。但是,它不能并行工作
我的示例代码如下
library(xgboost)
library(parallel)
param <- list("objective" = "reg:logistic"
, "eval_metric" = "logloss"
,"nthread" = 8
,"bst:eta" = .025
,"bst:max_depth" = 3
,"lambda" = 1
,"lambda_bias" = 0
,"alpha" = .8
,"min_child_weight" = 3
,"subsample" = .9
,"colsample_bytree" = .6)
bst.cv3 = xgb.cv(param=param, data = x, label = y,
nfold = 3, nrounds=cv.nround, missing = NA
,prediction = TRUE)
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码不起作用。我必须做些什么才能使它们平行?
我在 xgboost 网站和 github 上找到了这个
但是,我无法运行
brew install clang-omp
Run Code Online (Sandbox Code Playgroud)
或者
brew install gcc …Run Code Online (Sandbox Code Playgroud)