Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a9349c

Browse files
committed
further piptree improvement from githug-copilote-gpt4o free
1 parent 8b3a617 commit 3a9349c

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

winpython/piptree.py

+36-52
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self, target=None):
5555
for package in packages:
5656
self._process_package(package)
5757

58-
# On a second pass, complement distro in reverse mode with 'wanted-per':
58+
# On a second pass, complement dependancies in reverse mode with 'wanted-per':
5959
self._populate_wanted_per()
6060

6161
def _get_environment(self):
@@ -93,8 +93,8 @@ def _process_package(self, package):
9393
"requires_dist": requires,
9494
"wanted_per": [],
9595
"description": meta.get("Description", ""),
96-
"provides": provides, # extras of the package: 'array' for dask because dask['array'] defines some extra
97-
"provided": provided, # extras from other package: 'test' for pytest because dask['test'] wants pytest
96+
"provides": provides,
97+
"provided": provided, # being extras from other packages: 'test' for pytest because dask['test'] wants pytest
9898
}
9999

100100
def _get_requires(self, package):
@@ -123,7 +123,8 @@ def _get_requires(self, package):
123123
return requires
124124

125125
def _get_provides(self, package):
126-
"""Get the provides of a package."""
126+
"""Get the extended list of dependant packages, from extra options."""
127+
# 'array' is an added dependancy package of dask, if you install dask['array']
127128
provides = {'': None}
128129
if package.requires:
129130
for req in package.requires:
@@ -140,6 +141,7 @@ def _populate_wanted_per(self):
140141
# contains =
141142
# req_key = upstream package_key
142143
# req_version = downstream package version wanted
144+
# req_extra = extra option of the demanding package that wants this dependancy
143145
# req_marker = marker of the downstream package requirement (if any)
144146
for p in self.distro:
145147
for r in self.distro[p]["requires_dist"]:
@@ -167,14 +169,11 @@ def _populate_wanted_per(self):
167169
def _downraw(self, pp, extra="", version_req="", depth=20, path=[], verbose=False):
168170
"""build a nested list of needed packages with given extra and depth"""
169171
envi = {"extra": extra, **self.environment}
170-
p = normalize(pp)
171-
172-
# handles several extras, example: dask[array,diagnostics]
173-
extras = extra.split(",")
174-
172+
p = normalize(pp)
173+
extras = extra.split(",") # to handle several extras, example: dask[array,diagnostics]
175174
ret_all = []
176-
if p+"["+extra+"]" in path: # for dask[complete]->dask[array,test,..]
177-
print("cycle!", "->".join(path + [p+"["+extra+"]"]))
175+
if p + "[" + extra + "]" in path: # for dask[complete]->dask[array,test,..]
176+
print("cycle!", "->".join(path + [p + "[" + extra + "]"]))
178177
elif p in self.distro and len(path) <= depth:
179178
for extra in extras: # several extras request management
180179
envi = {"extra": extra, **self.environment}
@@ -191,7 +190,7 @@ def _downraw(self, pp, extra="", version_req="", depth=20, path=[], verbose=Fals
191190
r["req_extra"],
192191
r["req_version"],
193192
depth,
194-
path + [p+"["+extra+"]"],
193+
path + [p + "[" +extra + "]"],
195194
verbose=verbose,
196195
)
197196
ret_all.append(ret)
@@ -219,7 +218,7 @@ def _upraw(self, pp, extra="", version_req="", depth=20, path=[], verbose=False)
219218
return []
220219
ret = []
221220
for r in self.distro[p]["wanted_per"]:
222-
up_req = (r["req_marker"].split('extra == ')+[""])[1].translate(remove_list) if "req_marker" in r else ""
221+
up_req = (r.get("req_marker", "").split('extra == ')+[""])[1].translate(remove_list)
223222
if r["req_key"] in self.distro and r["req_key"]+"["+up_req+"]" not in path: # avoids circular links on dask[array]
224223
# 2024-06-30 example of langchain <- numpy. pip.distro['numpy']['wanted_per'] has:
225224
# {'req_key': 'langchain', 'req_version': '(>=1,<2)', 'req_extra': '', 'req_marker': ' python_version < "3.12"'},
@@ -243,74 +242,59 @@ def _upraw(self, pp, extra="", version_req="", depth=20, path=[], verbose=False)
243242
verbose=verbose,
244243
)
245244
if not ret == []:
246-
ret_all += [ret]
245+
ret_all.append(ret)
247246
return ret_all
248247

249248
def down(self, pp="", extra="", depth=99, indent=5, version_req="", verbose=False):
250249
"""print the downward requirements for the package or all packages"""
251-
if not pp == ".":
252-
if not extra == ".":
250+
if pp != ".":
251+
if extra != ".":
253252
if pp in self.distro:
254253
extras = [s for s in extra.split(',') if s in sorted(self.distro[pp]["provides"])]
255254
if extras == []: return ''
256-
rawtext = json.dumps(
257-
self._downraw(pp, extra, version_req, depth, verbose=verbose), indent=indent
258-
)
255+
rawtext = json.dumps(self._downraw(pp, extra, version_req, depth, verbose=verbose), indent=indent)
259256
lines = [l for l in rawtext.split("\n") if len(l.strip()) > 2]
260257
return ("\n".join(lines).replace('"', ""))
261258
else:
262259
if pp in self.distro:
263-
r = []
264-
for one_extra in sorted(self.distro[pp]["provides"]):
265-
s = self.down(pp, one_extra, depth, indent, version_req, verbose=verbose)
266-
if s != '': r += [s]
267-
#print(r)
268-
return '\n'.join([i for i in r if i!= ''])
260+
results = [self.down(pp, one_extra, depth, indent, version_req, verbose=verbose)
261+
for one_extra in sorted(self.distro[pp]["provides"])]
262+
return '\n'.join(filter(None, results))
269263
else:
270-
r = []
271-
for one_pp in sorted(self.distro):
272-
s = self.down(one_pp, extra, depth, indent, version_req, verbose=verbose)
273-
if s != '': r += [s]
274-
return '\n'.join([i for i in r if i!= ''])
275-
264+
results = [self.down(one_pp, extra, depth, indent, version_req, verbose=verbose)
265+
for one_pp in sorted(self.distro)]
266+
return '\n'.join(filter(None, results))
267+
276268
def up(self, pp, extra="", depth=99, indent=5, version_req="", verbose=False):
277-
"""print the upward needs for the package"""
269+
"""Print the upward needs for the package."""
278270
r = []
279-
if not pp == ".":
280-
if not extra == ".":
281-
s = self._upraw(pp, extra, version_req, depth, verbose=verbose)
282-
if s == []: return ''
271+
if pp != ".":
272+
if extra != ".":
283273
rawtext = json.dumps(self._upraw(pp, extra, version_req, depth, verbose=verbose), indent=indent)
284274
lines = [l for l in rawtext.split("\n") if len(l.strip()) > 2]
285-
return ("\n".join(i for i in lines if i!= '').replace('"', "") )
275+
return ('\n'.join(filter(None, lines)).replace('"', "") )
286276
else:
287277
if pp in self.distro:
288-
r = []
289-
for one_extra in sorted(set(self.distro[pp]["provided"]).union(set(self.distro[pp]["provides"]))): #direct and from-upward tags
290-
s = self.up(pp, one_extra, depth, indent, version_req, verbose=verbose)
291-
if s != '': r += [s]
292-
return '\n'.join([i for i in r if i!= ''])
278+
# get 'extra' tags from direct and from upward packages
279+
results = [self.up(pp, one_extra, depth, indent, version_req, verbose=verbose)
280+
for one_extra in sorted(set(self.distro[pp]["provided"]).union(set(self.distro[pp]["provides"])))]
281+
return '\n'.join(filter(None, results))
293282
else:
294-
for one_pp in sorted(self.distro):
295-
s = self.up(one_pp, extra, depth, indent, version_req, verbose=verbose)
296-
if s != []: r += [s]
297-
if r !=[]:
298-
return '\n'.join([i for i in r if i!= ''])
299-
else:
300-
return
283+
results = [self.up(one_pp, extra, depth, indent, version_req, verbose=verbose) for one_pp in sorted(self.distro)]
284+
return '\n'.join(filter(None, results))
301285

302286
def description(self, pp):
303-
"return description of the package"
287+
"""Return description of the package."""
304288
if pp in self.distro:
305289
return print("\n".join(self.distro[pp]["description"].split(r"\n")))
306290

307291
def summary(self, pp):
308-
"return summary of the package"
292+
"""Return summary of the package."""
309293
if pp in self.distro:
310294
return self.distro[pp]["summary"]
311295

312296
def pip_list(self, full=False, max_length=144):
313-
"""do like pip list"""
297+
"""List installed packages similar to pip list."""
314298
if full:
315299
return [(p, self.distro[p]["version"], sum_up(self.distro[p]["summary"]), max_length) for p in sorted(self.distro)]
316300
else:

0 commit comments

Comments
 (0)