Skip to content

Commit 5c48ebe

Browse files
committed
🎨 refactoring the code
1 parent c65c835 commit 5c48ebe

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ before_install:
1616
script:
1717
- make tests
1818
after_success:
19-
codecov
19+
- cd test
20+
- codecov
2021

pyecharts/charts/map.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pyecharts.base import Base
66
from pyecharts.option import get_all_options
7-
from pyecharts.template import CITY_NAME_PINYIN_MAP
7+
from pyecharts.constants import CITY_NAME_PINYIN_MAP
88

99
PY2 = sys.version_info[0] == 2
1010

@@ -63,8 +63,6 @@ def __add(self, name, attr, value,
6363
"data": _data,
6464
"roam": is_roam
6565
})
66-
if PY2:
67-
maptype = maptype.decode('utf-8')
6866
name_in_pinyin = CITY_NAME_PINYIN_MAP.get(maptype, maptype)
6967
self._js_dependencies.add(name_in_pinyin)
7068
self._config_components(**kwargs)

pyecharts/constants.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22
# coding=utf-8
33
from __future__ import unicode_literals
44

5+
import os
6+
import json
7+
import codecs
8+
from pyecharts.utils import get_resource_dir
9+
10+
511
DEFAULT_HOST = 'https://chfw.github.io/jupyter-echarts/echarts'
612

713
CONFIGURATION = dict(
814
HOST='/nbextensions/echarts'
915
)
1016

17+
DEFAULT_ECHARTS_REGISTRY = os.path.join(
18+
get_resource_dir('templates'), 'js', 'echarts', 'registry.json')
19+
20+
with codecs.open(DEFAULT_ECHARTS_REGISTRY, 'r', 'utf-8') as f:
21+
content = f.read()
22+
CONFIG = json.loads(content)
23+
24+
DEFAULT_JS_LIBRARIES = CONFIG['FILE_MAP']
25+
26+
CITY_NAME_PINYIN_MAP = CONFIG['PINYIN_MAP']
27+
28+
1129
CITY_GEO_COORDS = {
1230
'阿城': [126.58, 45.32],
1331
'阿克苏': [80.19, 41.09],

pyecharts/template.py

+3-23
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,9 @@
88
import codecs
99
from jinja2 import Environment, FileSystemLoader
1010
import pyecharts.constants as constants
11-
import json
11+
from pyecharts.utils import get_resource_dir
1212

1313

14-
def get_resource_dir(folder):
15-
"""
16-
17-
:param folder:
18-
:return:
19-
"""
20-
current_path = os.path.dirname(__file__)
21-
resource_path = os.path.join(current_path, folder)
22-
return resource_path
23-
24-
25-
#with codecs.open(os.path.join(get_resource_dir('templates'), 'js', 'echarts', 'registry.json'), 'r', 'utf-8') as f:
26-
with open(os.path.join(get_resource_dir('templates'), 'js', 'echarts', 'registry.json'), 'rb') as f:
27-
content = f.read().decode('utf-8')
28-
CONFIG = json.loads(content)
29-
30-
DEFAULT_JS_LIBRARIES = CONFIG['FILE_MAP']
31-
32-
CITY_NAME_PINYIN_MAP = CONFIG['PINYIN_MAP']
33-
3414
PY2 = sys.version_info[0] == 2
3515

3616
JS_PATTERN = re.compile(r'<!-- build -->(.*)<!-- endbuild -->',
@@ -107,7 +87,7 @@ def produce_require_configuration(dependencies, jshost):
10787
require_conf_items = [
10888
"'%s': '%s/%s'" % (key,
10989
jshost,
110-
DEFAULT_JS_LIBRARIES.get(key, key))
90+
constants.DEFAULT_JS_LIBRARIES.get(key, key))
11191
for key in _d]
11292
require_libraries = ["'%s'" % key for key in _d]
11393
return dict(
@@ -124,7 +104,7 @@ def produce_html_script_list(dependencies):
124104
"""
125105
_d = ensure_echarts_is_in_the_front(dependencies)
126106
script_list = [
127-
'%s' % DEFAULT_JS_LIBRARIES.get(key, key)
107+
'%s' % constants.DEFAULT_JS_LIBRARIES.get(key, key)
128108
for key in _d]
129109
return script_list
130110

pyecharts/utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
3+
4+
def get_resource_dir(folder):
5+
"""
6+
7+
:param folder:
8+
:return:
9+
"""
10+
current_path = os.path.dirname(__file__)
11+
resource_path = os.path.join(current_path, folder)
12+
return resource_path

test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd test
22
nosetests --with-coverage --cover-package pyecharts --cover-package test
3-
mv .coverage ../
3+

test/test_constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import unicode_literals
44

5-
import pyecharts.template as constants
5+
import pyecharts.constants as constants
66
from nose.tools import eq_
77

88

0 commit comments

Comments
 (0)