Skip to content

Commit

Permalink
Auto merge of #115 - daya0576:PYPINYIN_NO_DICT_COPY, r=mozillazg
Browse files Browse the repository at this point in the history
feat: 通过环境变量, 禁用 copy 操作.

## PR 描述
通过环境变量, 禁用 copy 操作.

## 待办事项

* [x] 符合代码规范
* [x] 单元测试
* [x] 文档

<!--
感谢你的贡献!❤️
-->
  • Loading branch information
bors-homu committed Jan 12, 2018
2 parents 974350d + e3c8f84 commit eb9bd7d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ FAQ
设置环境变量 ``PYPINYIN_NO_PHRASES=true`` 即可


如何禁用默认的“拼音库”copy 操作
++++++++++++++++++++++++++++++++

设置环境变量 ``PYPINYIN_NO_DICT_COPY=true`` 即可.


副作用: 用户的自定义拼音库出现问题时, 无法回退到自带的拼音库.


``INITIALS`` 声母风格下,以 ``y``, ``w``, ``yu`` 开头的汉字返回空字符串
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand Down
10 changes: 8 additions & 2 deletions pypinyin/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
PHRASES_DICT = {}
else:
from pypinyin import phrases_dict
PHRASES_DICT = phrases_dict.phrases_dict.copy()
PHRASES_DICT = phrases_dict.phrases_dict

# 单字拼音库
PINYIN_DICT = pinyin_dict.pinyin_dict.copy()
PINYIN_DICT = pinyin_dict.pinyin_dict

# 利用环境变量控制不做copy操作(无自定义拼音库的情况), 以减少内存使用
if not os.environ.get('PYPINYIN_NO_DICT_COPY'):
PINYIN_DICT = PINYIN_DICT.copy()
PHRASES_DICT = PHRASES_DICT.copy()

# 匹配使用数字标识声调的字符的正则表达式
RE_TONE2 = re.compile(r'([aeoiuvnm])([1-4])$')

Expand Down
11 changes: 11 additions & 0 deletions tests/_test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from importlib import reload
import os

os.environ['PYPINYIN_NO_PHRASES'] = 'true'

import pypinyin.core # noqa
Expand All @@ -11,3 +13,12 @@
def test_env():
assert pypinyin.core.PHRASES_DICT == {}
assert pypinyin.core.seg('北京') == ['北京']


def test_no_copy():
""" 禁用copy操作的测试 """
assert pypinyin.core.PINYIN_DICT is not pypinyin.pinyin_dict.pinyin_dict

os.environ['PYPINYIN_NO_DICT_COPY'] = 'true'
reload(pypinyin.constants)
assert pypinyin.constants.PINYIN_DICT is pypinyin.pinyin_dict.pinyin_dict

0 comments on commit eb9bd7d

Please sign in to comment.