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

Skip to content

Commit 80f7102

Browse files
committed
Improve packaging.database documentation
1 parent bf6c7ec commit 80f7102

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

Doc/library/packaging.database.rst

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Installed Python distributions are represented by instances of
1515
Most functions also provide an extra argument ``use_egg_info`` to take legacy
1616
distributions into account.
1717

18+
For the purpose of this module, "installed" means that the distribution's
19+
:file:`.dist-info`, :file:`.egg-info` or :file:`egg` directory or file is found
20+
on :data:`sys.path`. For example, if the parent directory of a
21+
:file:`dist-info` directory is added to :envvar:`PYTHONPATH`, then it will be
22+
available in the database.
1823

1924
Classes representing installed distributions
2025
--------------------------------------------
@@ -128,7 +133,7 @@ Functions to work with the database
128133
for the first installed distribution matching *name*. Egg distributions are
129134
considered only if *use_egg_info* is true; if both a dist-info and an egg
130135
file are found, the dist-info prevails. The directories to be searched are
131-
given in *paths*, which defaults to :data:`sys.path`. Return ``None`` if no
136+
given in *paths*, which defaults to :data:`sys.path`. Returns ``None`` if no
132137
matching distribution is found.
133138

134139
.. FIXME param should be named use_egg
@@ -200,20 +205,23 @@ functions:
200205
Examples
201206
--------
202207

203-
Print all information about a distribution
204-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
208+
Printing all information about a distribution
209+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
205210

206-
Given a path to a ``.dist-info`` distribution, we shall print out all
211+
Given the name of an installed distribution, we shall print out all
207212
information that can be obtained using functions provided in this module::
208213

209214
import sys
210215
import packaging.database
211216

212-
path = input()
213-
# first create the Distribution instance
214217
try:
215-
dist = packaging.database.Distribution(path)
216-
except FileNotFoundError:
218+
name = sys.argv[1]
219+
except ValueError:
220+
sys.exit('Not enough arguments')
221+
222+
# first create the Distribution instance
223+
dist = packaging.database.Distribution(path)
224+
if dist is None:
217225
sys.exit('No such distribution')
218226

219227
print('Information about %r' % dist.name)
@@ -244,7 +252,7 @@ information from a :file:`.dist-info` directory. By typing in the console:
244252

245253
.. code-block:: sh
246254
247-
$ echo /tmp/choxie/choxie-2.0.0.9.dist-info | python3 print_info.py
255+
python print_info.py choxie
248256
249257
we get the following output:
250258

@@ -299,10 +307,23 @@ we get the following output:
299307
* It was installed as a dependency
300308
301309
302-
Find out obsoleted distributions
303-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310+
Getting metadata about a distribution
311+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
312+
313+
Sometimes you're not interested about the packaging information contained in a
314+
full :class:`Distribution` object but just want to do something with its
315+
:attr:`~Distribution.metadata`::
316+
317+
>>> from packaging.database import get_distribution
318+
>>> info = get_distribution('chocolate').metadata
319+
>>> info['Keywords']
320+
['cooking', 'happiness']
321+
322+
323+
Finding out obsoleted distributions
324+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304325

305-
Now, we take tackle a different problem, we are interested in finding out
326+
Now, we tackle a different problem, we are interested in finding out
306327
which distributions have been obsoleted. This can be easily done as follows::
307328

308329
import packaging.database

Lib/packaging/database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'get_distributions', 'get_distribution', 'get_file_users',
2020
'provides_distribution', 'obsoletes_distribution',
2121
'enable_cache', 'disable_cache', 'clear_cache',
22+
# XXX these functions' names look like get_file_users but are not related
2223
'get_file_path', 'get_file']
2324

2425

0 commit comments

Comments
 (0)