Skip to content

Commit 3075e25

Browse files
authored
PEP8 (andiwand#4)
* PEP8 linting * add a travis ci linting * Drop Python 3.4, add 3.8 support This lxml version requires Python 2.7, 3.5 or later.
1 parent 926e839 commit 3075e25

File tree

4 files changed

+116
-72
lines changed

4 files changed

+116
-72
lines changed

.travis.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
language: python
3+
4+
python:
5+
- "2.7"
6+
- "3.5"
7+
- "3.6"
8+
- "3.7"
9+
- "3.8"
10+
11+
cache:
12+
pip: true
13+
14+
before_install:
15+
- python --version
16+
- pip install -U pip
17+
18+
install:
19+
- pip install ".[test]" .
20+
21+
script:
22+
- pycodestyle .
23+
24+
after_success:
25+
- codecov
26+
27+
# vim: set ts=2 sw=2 :

leo/leo.py

+47-38
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010
from bs4 import BeautifulSoup
1111

1212
COUNTRY_CODES = {
13-
'en',
14-
'de',
15-
'fr',
16-
'es',
13+
"en",
14+
"de",
15+
"fr",
16+
"es",
1717
}
1818
LANGUAGES = {
19-
'en': 'English',
20-
'de': 'German',
21-
'fr': 'French',
22-
'es': 'Spanish',
19+
"en": "English",
20+
"de": "German",
21+
"fr": "French",
22+
"es": "Spanish",
2323
}
2424

25-
URL = "http://dict.leo.org/dictQuery/m-vocab/{0}/query.xml" \
26-
"?tolerMode=nof&lp={0}&lang=de&rmWords=off&rmSearch=on" \
27-
"&search={1}&searchLoc=0&resultOrder=basic" \
28-
"&multiwordShowSingle=on"
29-
URL_PRONOUNCE = 'http://dict.leo.org/media/audio/{0}.mp3'
25+
URL = (
26+
"http://dict.leo.org/dictQuery/m-vocab/{0}/query.xml"
27+
"?tolerMode=nof&lp={0}&lang=de&rmWords=off&rmSearch=on"
28+
"&search={1}&searchLoc=0&resultOrder=basic"
29+
"&multiwordShowSingle=on"
30+
)
31+
URL_PRONOUNCE = "http://dict.leo.org/media/audio/{0}.mp3"
3032

3133

3234
def similarity(s1, s2):
@@ -36,8 +38,8 @@ def similarity(s1, s2):
3638
def fetch(phrase, lang):
3739
url = URL.format(lang, phrase)
3840
r = requests.get(url)
39-
soup = BeautifulSoup(r.text, 'xml')
40-
return soup.find_all('entry')
41+
soup = BeautifulSoup(r.text, "xml")
42+
return soup.find_all("entry")
4143

4244

4345
def pronounce(phrase, lang):
@@ -46,11 +48,11 @@ def pronounce(phrase, lang):
4648
return None
4749
result = []
4850
for entry in entries:
49-
for side in entry.find_all('side', lang='en'):
50-
word = side.find('word').get_text()
51-
if not side.find('pron'):
51+
for side in entry.find_all("side", lang="en"):
52+
word = side.find("word").get_text()
53+
if not side.find("pron"):
5254
continue
53-
url = side.find('pron')['url']
55+
url = side.find("pron")["url"]
5456
result.append((word, url))
5557
if not result:
5658
return False
@@ -67,20 +69,24 @@ def translate(phrase, lang):
6769
if not entries:
6870
return None
6971

70-
return [tuple([side.find('word').get_text()
71-
for side in entry.find_all('side')])
72-
for entry in entries]
72+
return [
73+
tuple([
74+
side.find("word").get_text() for side in entry.find_all("side")
75+
])
76+
for entry in entries
77+
]
7378

7479

7580
def play(url):
7681
try:
7782
import vlc
83+
7884
player = vlc.MediaPlayer(url)
7985
player.play()
8086
while player.get_state() not in (
81-
vlc.State.Stopped,
82-
vlc.State.Ended,
83-
vlc.State.Error,
87+
vlc.State.Stopped,
88+
vlc.State.Ended,
89+
vlc.State.Error,
8490
):
8591
time.sleep(0.1)
8692
player.stop()
@@ -90,41 +96,44 @@ def play(url):
9096

9197
def main(argv=None):
9298
parser = argparse.ArgumentParser(
93-
description='leo dict cli',
99+
description="leo dict cli",
94100
)
95101
parser.add_argument(
96-
'-l',
97-
'--language',
102+
"-l",
103+
"--language",
98104
help='destination language (default is "%(default)s")',
99-
default='en',
105+
default="en",
106+
)
107+
parser.add_argument(
108+
"-p",
109+
"--pronounce",
110+
help="pronounce phrase",
111+
action="store_true",
100112
)
101113
parser.add_argument(
102-
'-p',
103-
'--pronounce',
104-
help='pronounce phrase',
105-
action='store_true',
114+
"phrase",
115+
help="phrase to translate",
106116
)
107-
parser.add_argument('phrase', help='phrase to translate')
108117
args = parser.parse_args(argv)
109118

110-
phrase, lang = args.phrase, args.language + 'de'
119+
phrase, lang = args.phrase, args.language + "de"
111120

112121
if args.pronounce:
113122
result = pronounce(phrase, lang)
114123
return 0 if result else 1
115124

116125
translations = translate(phrase, lang)
117126
if not translations:
118-
print('no translation found')
127+
print("no translation found")
119128
return 1
120129

121130
for trans in translations:
122-
print('%s\t\t%s' % trans)
131+
print("%s\t\t%s" % trans)
123132

124133
return 0
125134

126135

127-
if __name__ == '__main__':
136+
if __name__ == "__main__":
128137
rcode = main()
129138
sys.exit(rcode)
130139

leo/train.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@
1111

1212
def main(argv=None):
1313
parser = argparse.ArgumentParser(
14-
description='pronunciation training',
14+
description="pronunciation training",
1515
)
1616
parser.add_argument(
17-
'-s',
18-
'--sequential',
19-
help='sequential order',
20-
action='store_true',
17+
"-s",
18+
"--sequential",
19+
help="sequential order",
20+
action="store_true",
2121
)
2222
parser.add_argument(
23-
'-p',
24-
'--pronounce',
25-
help='pronounce word',
26-
action='store_true',
23+
"-p",
24+
"--pronounce",
25+
help="pronounce word",
26+
action="store_true",
27+
)
28+
parser.add_argument(
29+
"file",
30+
help="word list",
2731
)
28-
parser.add_argument('file', help='word list')
2932
args = parser.parse_args(argv)
3033

31-
lang = 'ende'
34+
lang = "ende"
3235

33-
with open(args.file, 'r') as f:
36+
with open(args.file, "r") as f:
3437
words = [line.strip() for line in f.readlines() if line.strip()]
3538
if not args.sequential:
3639
random.shuffle(words)
@@ -39,7 +42,7 @@ def main(argv=None):
3942
sys.stdout.write(word)
4043
input()
4144

42-
subs = [sub.strip() for sub in word.split('/')]
45+
subs = [sub.strip() for sub in word.split("/")]
4346
for sub in subs:
4447
subs_len = len(subs)
4548
if subs_len > 1:
@@ -59,7 +62,7 @@ def main(argv=None):
5962
return 0
6063

6164

62-
if __name__ == '__main__':
65+
if __name__ == "__main__":
6366
exit = main()
6467
sys.exit(exit)
6568

setup.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
from setuptools import find_packages, setup
22

33
setup(
4-
name='leo',
5-
version='1.1',
6-
description='dict.leo.org',
7-
long_description='Leo Dictionary',
4+
name="leo",
5+
version="1.1",
6+
description="dict.leo.org",
7+
long_description="Leo Dictionary",
88
classifiers=[
9-
'Development Status :: 4 - Beta',
10-
'License :: OSI Approved :: Apache Software License',
11-
'Operating System :: POSIX :: Linux',
12-
'Programming Language :: Python :: 2.7',
13-
'Programming Language :: Python :: 3.4',
14-
'Programming Language :: Python :: 3.5',
15-
'Programming Language :: Python :: 3.6'
9+
"Development Status :: 4 - Beta",
10+
"License :: OSI Approved :: Apache Software License",
11+
"Operating System :: POSIX :: Linux",
12+
"Programming Language :: Python :: 2.7",
13+
"Programming Language :: Python :: 3.5",
14+
"Programming Language :: Python :: 3.6",
15+
"Programming Language :: Python :: 3.7",
16+
"Programming Language :: Python :: 3.8",
1617
],
17-
keywords='leo',
18-
author='Andreas Stefl',
19-
author_email='[email protected]',
20-
url='https://github.com/andiwand/pyleo',
21-
license='GPLv3',
18+
keywords="leo",
19+
author="Andreas Stefl",
20+
author_email="[email protected]",
21+
url="https://github.com/andiwand/pyleo",
22+
license="GPLv3",
2223
packages=find_packages(),
2324
install_requires=[
24-
'beautifulsoup4>=4.3.0',
25-
'requests>=1.2.3',
26-
'lxml>=4.1.1',
25+
"beautifulsoup4>=4.3.0",
26+
"requests>=1.2.3",
27+
"lxml>=4.1.1",
2728
],
2829
extras_require={
30+
"test": [
31+
"pycodestyle",
32+
"codecov",
33+
]
2934
},
3035
entry_points="""
3136
[console_scripts]
3237
leo=leo.leo:main
3338
train=leo.train:main
34-
"""
39+
""",
3540
)

0 commit comments

Comments
 (0)