小编Vic*_*son的帖子

在JSONObject中添加JSONArray

我正在创建一个使用OneSignal发送通知的应用程序,并且必须以JSON格式执行POST请求.

要向用户发送通知,我必须使用include_player_ids必须是数组的参数,因为可以向多个用户发送相同的通知(在我的情况下,我只向一个用户发送通知).我使用JSONArray创建此数组,但在将其添加到JSONObject时,该字段还有额外的引号include_player_ids.

是)我有的:

{
  "headings": {"en":"my_title"},
  "contents": {"en":"my_text"},
  "include_player_ids": "[\"my-player-id\"]",
  "app_id":"my-app-id"
}
Run Code Online (Sandbox Code Playgroud)

如您所见,数组[]周围有一些引用.

我猜这是OneSignal的响应错误: errors":["include_player_ids must be an array"]

我想要的是 :

...
"include_player_ids": ["my-player-id"] 
...
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为将JSONObject添加到JSONObject不会这样做,即使它与标题/内容字段中看到的非常相似

我的代码:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.alt.JSONArray;

JSONObject headings = new JSONObject();
JSONObject contents = new JSONObject();
JSONArray player_id = new JSONArray();
JSONObject notification = new JSONObject();
try {
    notification.put("app_id", appId);
    notification.put("include_player_ids", player_id);
    player_id.put(idUser);
    headings.put("en", "my_title");
    contents.put("en", "my_text");
    notification.put("headings", headings);
    notification.put("contents", contents);             

} catch (JSONException e) {
    System.out.println("JSONException :" + …
Run Code Online (Sandbox Code Playgroud)

java json onesignal

5
推荐指数
1
解决办法
3877
查看次数

删除启动消息以更改 Spark 日志级别

我正在使用 PySpark 运行 Python 脚本(和测试),并希望从日志中删除不相关的信息。

每次我启动它们时,控制台中都会显示以下消息:

将默认日志级别设置为“WARN”。

要调整日志记录级别,请使用 sc.setLogLevel(newLevel)。对于 SparkR,请使用 setLogLevel(newLevel)。

我怎样才能完全删除它?(最好在log4j.properties中)


我已经尝试过的:

我已经log4j.rootCategory=ERROR, console在log4j.properties中设置了。

sc.setLogLevel(newLevel)按照消息所述执行的操作仅适用于以下日志,而不适用于脚本的开头。

log4j.propertieslog4j.logger.org.apache.spark=ERROR中的设置不会删除该消息。

我对此进行了很多搜索,但找不到相关的配置。


Spark Github(在 Logging.scala 中),我可以看到有一个silent用于显示消息的变量,但我找不到它的更改位置:

if (!silent) {
  System.err.printf("Setting default log level to \"%s\".\n", replLevel)
  System.err.println("To adjust logging level use sc.setLogLevel(newLevel). " +
    "For SparkR, use setLogLevel(newLevel).")
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您的任何帮助,

log4j python-3.x apache-spark pyspark

2
推荐指数
1
解决办法
1091
查看次数

使用** kwargs向后兼容

我曾经有一个带有一些关键字参数的函数: def foo(a=None, b=None)

在我的代码的新版本中,我不再需要关键字(b)之一,而是想要将其删除。

我应该**kwargs像这样def foo(a=None, **kwargs)向后添加功能以实现向后兼容吗?

在这种情况下,最佳做法是什么?

如果用户升级到我的软件包的新版本,我不想破坏他们的代码。

python python-3.x

2
推荐指数
1
解决办法
48
查看次数

从内部删除 bash 脚本的颜色并重定向输出

我的问题很简单,如何将 bash 脚本的所有输出重定向到文件和终端,并脚本本身中删除颜色字符?

我找不到适合我所有需求的答案。

到目前为止,我尝试tee输出到文件和终端,结合2>&1getstederrstdoutsed删除颜色字符,所有这些都exec在我的脚本中完成,但它不起作用,我只将彩色日志输入终端,什么也没有在文件中。

#!/usr/bin/env bash
exec 2>&1 | sed -r 's/\x1b\[[0-9;]*m//g' | tee script.logs

python somepython.py
python someotherpython.py
Run Code Online (Sandbox Code Playgroud)

这里 python 脚本产生彩色输出。

我想将它们记录到终端(未触及)和文件中(没有颜色)。实际上,我的 bash 脚本中发生的事情比这两个 python 脚本要多得多,这就是为什么我想全局重定向脚本 bash 的输出,而不仅仅是在每个 python 脚本之后进行管道传输。

因此我使用它是exec因为我认为它允许重定向脚本产生的所有输出。

预先感谢您的任何建议和帮助,

PS:我不想在文件中显示彩色日志,但我不在乎终端中是否需要这样做才能使日志在文件中不被着色。

bash stdout stderr

1
推荐指数
1
解决办法
3056
查看次数

标签 统计

python-3.x ×2

apache-spark ×1

bash ×1

java ×1

json ×1

log4j ×1

onesignal ×1

pyspark ×1

python ×1

stderr ×1

stdout ×1