Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9719aa1

Browse files
author
yangwenqiang.ywq
committed
this and weakref
Change-Id: I3b4ce57023fa50abdcf0e83262a0e77424f30b75
1 parent 76be0bf commit 9719aa1

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
1919

2020
> Python无所不能
2121
22+
## [this](content/this.md)
23+
2224
## [MySQLdb](content/MySQLdb.md)
2325

2426
## [os](content/os.md)
@@ -279,6 +281,8 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
279281

280282
## [pyrasite](content/pyrasite.md)
281283

284+
## [weakref](content/weakref.md)
285+
282286
## [tools](content/tools.md)
283287

284288
## [Other_thing](content/other_thing.md)

‎code/marshmallow_demo.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding=utf-8
2+
3+
4+
from datetime import datetime
5+
from marshmallow import Schema, fields, pprint
6+
7+
8+
class ArtistSchema(Schema):
9+
name = fields.Str()
10+
location = fields.List(fields.Str())
11+
12+
13+
class AlbumSchema(Schema):
14+
title = fields.Str()
15+
release_date = fields.Date()
16+
artist = fields.Nested(ArtistSchema())
17+
18+
19+
bowie = dict(name='David Bowie', location=["Beijing", "Shanghai"])
20+
album = dict(artist=bowie, title="Hunky Dory", release_date=datetime.now())
21+
22+
schema = AlbumSchema()
23+
result = schema.dump(album)
24+
pprint(result, indent=2)
25+

‎code/marshmallow_load.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding=utf-8
2+
3+
from pprint import pprint
4+
from marshmallow import Schema, fields, pprint, post_load
5+
6+
7+
class User(object):
8+
"""docstring for User"""
9+
def __init__(self, created_at, email, name):
10+
self.created_at = created_at
11+
self.email = email
12+
self.name = name
13+
14+
15+
class UserSchema(Schema):
16+
created_at = fields.Date()
17+
email = fields.Str()
18+
name = fields.Str()
19+
age = fields.Int()
20+
21+
# @post_load
22+
# def make_user(self, data):
23+
# return User(**data)
24+
25+
user_data = {
26+
'created_at': '2014年08月11日T05:26:03.869245',
27+
# 'email': u'ken@yahoo.com',
28+
'name': u'Ken',
29+
'age': '20-'
30+
}
31+
schema = UserSchema()
32+
result = schema.load(user_data)
33+
pprint(result.data)
34+
print(type(result.data))
35+
print(result)
36+
print(type(result))
37+
# {'name': 'Ken',
38+
# 'email': 'ken@yahoo.com',
39+
# 'created_at': datetime.datetime(2014, 8, 11, 5, 26, 3, 869245)},
40+
41+
42+
print result.data['name']
43+
result.data['name'] = 'windard'
44+
print result
45+
print result.data.name
46+
47+
result.data['email'] = result.data.get('email', None)
48+
print type(result.data['age'])
49+

‎content/logging.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ if __name__ == '__main__':
138138

139139
所以可以使用 `logger.exception` 代替 `logger.error` ,日志等级也是 `error`, 但是会打印出异常的堆栈信息。
140140

141+
logging 如果不配置是没有数据输出的,但是如果不想写这么长的配置怎么办呢?直接使用 `logging.basicConfig()` 进行一个简单的基础配置。
141142

142143
示例代码
143144

‎content/this.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## this
2+
3+
```
4+
>>> import this
5+
The Zen of Python, by Tim Peters
6+
7+
Beautiful is better than ugly.
8+
Explicit is better than implicit.
9+
Simple is better than complex.
10+
Complex is better than complicated.
11+
Flat is better than nested.
12+
Sparse is better than dense.
13+
Readability counts.
14+
Special cases aren't special enough to break the rules.
15+
Although practicality beats purity.
16+
Errors should never pass silently.
17+
Unless explicitly silenced.
18+
In the face of ambiguity, refuse the temptation to guess.
19+
There should be one-- and preferably only one --obvious way to do it.
20+
Although that way may not be obvious at first unless you're Dutch.
21+
Now is better than never.
22+
Although never is often better than *right* now.
23+
If the implementation is hard to explain, it's a bad idea.
24+
If the implementation is easy to explain, it may be a good idea.
25+
Namespaces are one honking great idea -- let's do more of those!
26+
```
27+

‎content/weakref.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## weakref
2+
3+
正常的引用会将引用计数器加一,最后在引用计数为零的时候删除,回收内存。
4+
5+
使用弱引用类型不会将计数器加一,可以减少避免循环引用,长期占有内存的情况。
6+
7+
可以使用 `sys.getrefcount` 来查看一个对象的引用次数。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /