Skip to content

Commit

Permalink
Merge pull request #225 from yandy/master
Browse files Browse the repository at this point in the history
use official cached_property when possible
  • Loading branch information
fenngwd authored May 25, 2022
2 parents 91e23b3 + 697d657 commit 37eff53
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ python:
- '3.6'
- '3.7'
- '3.8'
- '3.9'
install:
- pip install -U importlib-metadata
- pip list
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## [Unreleased]
### Changed
- Use cached_property from standard lib when possible

## [2.3.3] - 2022-04-22
### Added
Expand Down Expand Up @@ -49,4 +51,4 @@
### Changed
- update travis ci

## [0.3.0] - 2017-08-16
## [0.3.0] - 2017-08-16
3 changes: 3 additions & 0 deletions docs/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ utils.import_string('sea.app:BaseApp')

## `cached_property`

> 如果你使用的是 python 3.8 以上版本,标准库中已经提供 cached_property 装饰器,推荐使用官方版本
> `from functools import cached_property`
property 的值初次计算后会缓存在 instance 上,重复调用不会重复计算

```python
Expand Down
14 changes: 10 additions & 4 deletions sea/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import inspect
import logging
import os.path
import sys

from sea import exceptions, utils
from sea.config import Config, ConfigAttribute
from sea.datatypes import ImmutableDict, ConstantsObject

if sys.version_info.minor >= 8:
from functools import cached_property
else:
from sea.utils import cached_property


class BaseApp:
"""The BaseApp object implements grpc application
Expand Down Expand Up @@ -46,7 +52,7 @@ def __init__(self, root_path, env):
self._extensions = {}
self._middlewares = []

@utils.cached_property
@cached_property
def logger(self):
logger = logging.getLogger('sea.app')
if self.debug and logger.level == logging.NOTSET:
Expand All @@ -57,19 +63,19 @@ def logger(self):
logger.addHandler(h)
return logger

@utils.cached_property
@cached_property
def servicers(self):
rv = ConstantsObject(self._servicers)
del self._servicers
return rv

@utils.cached_property
@cached_property
def extensions(self):
rv = ConstantsObject(self._extensions)
del self._extensions
return rv

@utils.cached_property
@cached_property
def middlewares(self):
rv = tuple(self._middlewares)
del self._middlewares
Expand Down

0 comments on commit 37eff53

Please sign in to comment.