Finology 大数据金融

通过大数据以量化金融

日志

日志是跟踪软件运行时所发生的事件的一种方法。软件开发者在代码中调用日志函数,表明发生了特定的事件。事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数据)。事件还具有开发者归因于事件的重要性;重要性也可以称为级别或严重性。

什么时候使用Logging

logging提供了一组便利的函数,用来做简单的日志。它们是 debug()、 info()、 warning()、 error() 和 critical()。

logging函数根据它们用来跟踪的事件的级别或严重程度来命名。标准级别及其适用性描述如下(以严重程度递增排序):

级别 何时使用
DEBUG 详细信息,一般只在调试问题时使用。
INFO 证明事情按预期工作。
WARNING 某些没有预料到的事件的提示,或者在将来可能会出现的问题提示。例如:磁盘空间不足。但是软件还是会照常运行。
ERROR 由于更严重的问题,软件已不能执行一些功能了。
CRITICAL 严重错误,表明软件已不能继续运行了。

打印日志到控制台

由于默认设置的等级是warning,所有只有warning的信息会输出到控制台。

1
2
3
4
5
6
import logging

logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything

WARNING:root:Watch out!

打印日志到文件

1
2
3
4
5
6
7
8
9
10
import logging

logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too

我们设置了logging的级别为DEBUG,所以所有信息都将被写入到example.log文件中。

如果想要每次启动时,原来的打印日志都被清空的话,则需要把filemode由默认的a改为w

1
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)

日志的格式

日志级别

1
2
3
4
5
6
7
8
9
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')

DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too

日期

1
2
3
4
5
import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')

2019-02-07 22:21:20,993 is when this event was logged.

本章节中我们将向大家介绍如何将数据插入到MongoDB的集合中。

文档的数据结构和JSON基本一样。

所有存储在集合中的数据都是BSON格式。

BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON。

插入文档

MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:

1
db.COLLECTION_NAME.insert(document)
1
2
3
4
5
6
7
8
9
10
11
12
> document = ({"name": "simon", "age": 20, "interests": ["badminton", "swimming", "music"]})
{
"name" : "simon",
"age" : 20,
"interests" : [
"badminton",
"swimming",
"music"
]
}
> db.col.insert(document)
WriteResult({ "nInserted" : 1 })

插入文档你也可以使用 db.col.save(document) 命令。如果不指定 _id 字段 save() 方法类似于 insert() 方法。如果指定 _id 字段,则会更新该 _id 的数据。

指定_id字段是{"_id": ObjectId("5c5be011dd377f0cd58338ae")}

更新文档

update() 方法

update() 方法用于更新已存在的文档。语法格式如下:

1
2
3
4
5
6
7
8
9
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)

参数说明:

  • query : update的查询条件,类似sql update查询内where后面的。

  • update : update的对象和一些更新的操作符(如$,$inc…)等,也可以理解为sql update查询内set后面的

  • upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

  • multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

  • writeConcern :可选,抛出异常的级别。

save() 方法

save() 方法通过传入的文档来替换已有文档。语法格式如下:

1
2
3
4
5
6
db.collection.save(
<document>,
{
writeConcern: <document>
}
)

参数说明:

document : 文档数据。

writeConcern :可选,抛出异常的级别。

删除文档

1
2
3
4
5
6
7
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)

参数说明:

  • query :(可选)删除的文档的条件。

  • justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。

  • writeConcern :(可选)抛出异常的级别。

remove() 方法已经过时了,现在官方推荐使用 deleteOne() 和 deleteMany() 方法。

如删除集合下全部文档:

1
db.inventory.deleteMany({})

删除 status 等于 A 的全部文档:

1
db.inventory.deleteMany({ status : "A" })

删除 status 等于 D 的一个文档:

1
db.inventory.deleteOne( { status: "D" } )

MongoDB创建数据库

use DATABASE_NAME

如果数据库不存在,则创建数据库,否则切换到指定数据库。

1
2
3
4
> use test2
switched to db test2
> db
test2

查看所有数据库,使用show dbs命令

1
2
3
4
5
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB

可以看到,我们刚创建的数据库 test2 并不在数据库的列表中, 要显示它,我们需要向 runoob 数据库插入一些数据。

1
2
3
4
5
6
7
8
9
db.test2.insert({"name":"simon"})
WriteResult({ "nInserted" : 1 })

> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
test2 0.000GB

MongoDB删除数据库

1
2
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }

删除集合collection

show collectionsshow tables都可以查看集合列表。

1
2
3
4
5
6
> show collections
collection1
collection2

> db.collection1.drop()
true

MongoDB创建集合

1
db.createCollection(name, options)

name: 要创建的集合名称

options: 可选参数, 指定有关内存大小及索引的选项

options 可以是如下参数:

字段 类型 描述
capped 布尔 (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当 该值为 true 时,必须指定 size 参数。
autoIndexId 布尔 (可选)如为 true,自动在 _id 字段创建索引。默认为 false。
size 数值 (可选)为固定集合指定一个最大值(以字节计)。如果 capped 为 true,也需要指定该字段。
max 数值 (可选)指定固定集合中包含文档的最大数量。

在插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段。

test数据库中创建coll1集合

1
2
3
4
> use test
switched to db test
> db.createCollection('coll1')
{ "ok" : 1 }

创建固定集合 mycol,整个集合空间大小 6142800 KB, 文档最大个数为 10000 个。

1
2
3
4
5
> db.createCollection("mycol", {capped: true, autoIndexId: true, size: 6142800, max: 10000})
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}

在 MongoDB 中,你不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合。

1
2
3
4
db.mycol2.insert({"name": "simon"})
WriteResult({ "nInserted" : 1 })
show collections
mycol2

MongoDB删除集合

db.collection.drop()

1
2
3
4
> show collections
mycol2
> db.mycol2.drop()
true

启动mongodb服务

并把服务暴露给任何ip, –fork是在后台运行。

1
mongod --bind_ip 0.0.0.0 --dbpath /path/to/your/db --fork --logpath /path/to/logfile.log
0%