Skip to content

Commit 464efa4

Browse files
authored
Merge pull request #8 from lsgrep/expand-conventions
Expand conventions
2 parents 7b7a4b9 + 5d6f714 commit 464efa4

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
Checkout the [Releases](https://github.com/lsgrep/mldocs/releases), download the latest `mldocs.alfredworkflow`,
1818
then double click it(You have to have Alfred + Powerpack License).
1919

20+
## Conventions
21+
For convenience, a few prefixes are automatically expanded(see [PR](https://github.com/lsgrep/mldocs/compare/expand-conventions?expand=1) for more).
22+
- `np` => `numpy`
23+
- `pd` => `pandas`
24+
- `plt` => `pyplot`
25+
- `sns` => `seaborn`
26+
2027
## How does it work
2128
- `mldocs` fetches the doc data from Github(`data/ml.json`), then caches the data for a few days
2229
- The first query will be slow then it will be pretty fast afterwards

mldocs.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,63 @@ def parse_domain(link):
4141
return link.split("//")[-1].split("/")[0]
4242

4343

44+
# expand commonly used prefixes
45+
# plt => pyplot
46+
# sns => seaborn
47+
# np => numpy
48+
# pd => pandas
49+
def expand_args(args):
50+
for i, arg in enumerate(args):
51+
arg = str(arg).lower()
52+
args[i] = arg
53+
if arg == 'plt':
54+
args[i] = 'pyplot'
55+
continue
56+
57+
if arg.startswith('plt.'):
58+
_, rem = arg.split('.')
59+
args[i] = 'pyplot.' + rem.lower()
60+
continue
61+
62+
if arg == 'sns':
63+
args[i] = 'seaborn'
64+
continue
65+
66+
if arg.startswith('sns.'):
67+
_, rem = arg.split('.')
68+
args[i] = 'seaborn.' + rem
69+
continue
70+
71+
if arg.lower() == 'np':
72+
args[i] = 'numpy'
73+
continue
74+
75+
if arg.startswith('np.'):
76+
_, rem = arg.split('.')
77+
args[i] = 'numpy.' + rem
78+
continue
79+
80+
if arg.lower() == 'pd':
81+
args[i] = 'pandas'
82+
continue
83+
84+
if arg.startswith('pd.'):
85+
_, rem = arg.split('.')
86+
args[i] = 'pandas.' + rem
87+
continue
88+
89+
return args
90+
91+
92+
def search(args, keywords):
93+
# args is lower case already
94+
args = expand_args(args)
95+
for k in args:
96+
keywords = [i for i in keywords if k in i.lower()]
97+
result = sorted(keywords, key=len)
98+
return result
99+
100+
44101
def main(wf):
45102
# The Workflow3 instance will be passed to the function
46103
# you call from `Workflow3.run`.
@@ -67,15 +124,9 @@ def main(wf):
67124
ml_data = wf.cached_data('keywords', get_ml_docs, max_age=3600 * 24 * 3)
68125
assets = dict(wf.cached_data('assets', get_assets, max_age=3600 * 24 * 7))
69126
asset_keywords = sorted(assets.keys(), key=len)
127+
result = search(args, ml_data.keys())
70128

71-
keywords = ml_data.keys()
72-
73-
for k in args:
74-
keywords = [i for i in keywords if k.lower() in i.lower()]
75-
76-
result = sorted(keywords, key=len)
77-
78-
for ml_keyword in result[:20]:
129+
for ml_keyword in result[:30]:
79130
doc_link = ml_data[ml_keyword]['url']
80131
doc_desc = doc_link # default value
81132

0 commit comments

Comments
 (0)