Skip to content

Commit 28e57e8

Browse files
Documentation enhancement
1 parent fc3cfd4 commit 28e57e8

File tree

1 file changed

+292
-16
lines changed

1 file changed

+292
-16
lines changed

pyaml/yellow_pages.py

Lines changed: 292 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
4040
Examples
4141
--------
42-
>>> yp["BPM"]
43-
>>> yp["HCORR|VCORR"]
44-
>>> yp["BPM - re{BPM_C01-01}"]
45-
>>> yp["re{^BPM_C..-..$}"]
46-
>>> yp["(HCORR|VCORR) - re{CH_.*}"]
42+
.. code-block:: python
43+
44+
>>> yp["BPM"]
45+
>>> yp["HCORR|VCORR"]
46+
>>> yp["BPM - re{BPM_C01-01}"]
47+
>>> yp["re{^BPM_C..-..$}"]
48+
>>> yp["(HCORR|VCORR) - re{CH_.*}"]
4749
4850
Notes
4951
-----
@@ -187,20 +189,65 @@ def _extract_regex(tok: str) -> str:
187189

188190

189191
class YellowPages:
190-
"""
191-
Fully dynamic YellowPages service attached to Accelerator.
192+
r"""
193+
Dynamic discovery service for accelerator objects.
194+
195+
:class:`YellowPages` provides a unified access layer to arrays,
196+
tuning tools and diagnostics available in an
197+
:class:`~pyaml.accelerator.Accelerator`.
198+
199+
Entries are discovered dynamically by scanning all
200+
:class:`~pyaml.element_holder.ElementHolder` instances
201+
associated with the accelerator control and simulation modes.
202+
203+
Notes
204+
-----
205+
The :class:`~pyaml.accelerator.Accelerator` must provide:
206+
207+
- ``controls() -> dict[str, ElementHolder]``
208+
- ``simulators() -> dict[str, ElementHolder]``
209+
- ``modes() -> dict[str, ElementHolder]``
210+
211+
Each :class:`~pyaml.element_holder.ElementHolder` must implement:
212+
213+
- ``list_arrays()`` / ``get_array(name)``
214+
- ``list_tools()`` / ``get_tool(name)``
215+
- ``list_diagnostics()`` / ``get_diagnostic(name)``
216+
217+
Examples
218+
--------
219+
220+
Print the global overview:
221+
222+
.. code-block:: python
223+
224+
print(sr.yellow_pages)
225+
226+
Resolve an entry across all modes:
227+
228+
.. code-block:: python
192229
193-
Discovery:
194-
- keys/categories are derived by scanning all modes at runtime.
230+
sr.yellow_pages.get("BPM")
195231
196-
Resolution:
197-
- get(key, mode="...") resolves in a specific mode.
198-
- get(key) returns dict[mode_name, obj] for all modes where available.
232+
Resolve in a specific mode:
199233
200-
Query language (arrays/IDs):
201-
- yp["BPM"]
202-
- yp["HCORR|VCORR"]
203-
- yp["BPM - re{BPM_C01-01}"]
234+
.. code-block:: python
235+
236+
sr.yellow_pages.get("BPM", mode="live")
237+
238+
Query arrays using set expressions:
239+
240+
.. code-block:: python
241+
242+
sr.yellow_pages["BPM"]
243+
sr.yellow_pages["HCORR|VCORR"]
244+
sr.yellow_pages["BPM - re{BPM_C01-01}"]
245+
246+
Regex filtering:
247+
248+
.. code-block:: python
249+
250+
sr.yellow_pages["re{^BPM_C..-..$}"]
204251
"""
205252

206253
def __init__(self, accelerator: Any):
@@ -211,14 +258,93 @@ def __init__(self, accelerator: Any):
211258
# ---------------------------
212259

213260
def has(self, key: str) -> bool:
261+
"""
262+
Check whether a YellowPages key exists.
263+
264+
Parameters
265+
----------
266+
key : str
267+
Name of the entry.
268+
269+
Returns
270+
-------
271+
bool
272+
True if the key exists.
273+
274+
Examples
275+
--------
276+
277+
.. code-block:: python
278+
279+
>>> sr.yellow_pages.has("BPM")
280+
True
281+
282+
.. code-block:: python
283+
284+
>>> sr.yellow_pages.has("UNKNOWN")
285+
False
286+
287+
"""
214288
return key in self._all_keys()
215289

216290
def categories(self) -> list[str]:
291+
"""
292+
Return the list of available categories.
293+
294+
Only categories that contain elements are returned.
295+
296+
Returns
297+
-------
298+
list[str]
299+
300+
Examples
301+
--------
302+
303+
.. code-block:: python
304+
305+
>>> sr.yellow_pages.categories()
306+
['Arrays', 'Tools', 'Diagnostics']
307+
308+
"""
217309
discovered = self._discover()
218310
present = {cat for cat, keys in discovered.items() if keys}
219311
return [c.value for c in YellowPagesCategory if c in present]
220312

221313
def keys(self, category: str | YellowPagesCategory | None = None) -> list[str]:
314+
"""
315+
Return available YellowPages keys.
316+
317+
Parameters
318+
----------
319+
category : str or YellowPagesCategory, optional
320+
Restrict results to a specific category.
321+
322+
Returns
323+
-------
324+
list[str]
325+
326+
Examples
327+
--------
328+
329+
All entries:
330+
331+
.. code-block:: python
332+
333+
>>> sr.yellow_pages.keys()
334+
335+
Only arrays:
336+
337+
.. code-block:: python
338+
339+
>>> sr.yellow_pages.keys("Arrays")
340+
341+
Using enum:
342+
343+
.. code-block:: python
344+
345+
>>> sr.yellow_pages.keys(YellowPagesCategory.ARRAYS)
346+
347+
"""
222348
discovered = self._discover()
223349

224350
if category is None:
@@ -247,6 +373,31 @@ def __getattr__(self, name):
247373
# ---------------------------
248374

249375
def availability(self, key: str) -> set[str]:
376+
"""
377+
Return the list of modes where a key is available.
378+
379+
Parameters
380+
----------
381+
key : str
382+
383+
Returns
384+
-------
385+
set[str]
386+
387+
Examples
388+
--------
389+
390+
.. code-block:: python
391+
392+
>>> sr.yellow_pages.availability("BPM")
393+
{'live', 'design'}
394+
395+
.. code-block:: python
396+
397+
>>> sr.yellow_pages.availability("DEFAULT_ORBIT_CORRECTION")
398+
{'live'}
399+
400+
"""
250401
self._require_key(key)
251402
avail: set[str] = set()
252403
for mode_name, holder in self._acc.modes().items():
@@ -255,6 +406,42 @@ def availability(self, key: str) -> set[str]:
255406
return avail
256407

257408
def get(self, key: str, *, mode: str | None = None):
409+
"""
410+
Resolve a YellowPages entry.
411+
412+
Parameters
413+
----------
414+
key : str
415+
Entry name.
416+
mode : str, optional
417+
Specific mode.
418+
419+
Returns
420+
-------
421+
object or dict[str, object]
422+
423+
Examples
424+
--------
425+
426+
Resolve in all modes:
427+
428+
.. code-block:: python
429+
430+
>>> sr.yellow_pages.get("BPM")
431+
432+
Resolve in a specific mode:
433+
434+
.. code-block:: python
435+
436+
>>> sr.yellow_pages.get("BPM", mode="live")
437+
438+
Using attribute access:
439+
440+
.. code-block:: python
441+
442+
>>> sr.yellow_pages.BPM
443+
444+
"""
258445
self._require_key(key)
259446

260447
if mode is not None:
@@ -280,6 +467,61 @@ def get(self, key: str, *, mode: str | None = None):
280467
# ---------------------------
281468

282469
def __getitem__(self, query: str) -> list[str]:
470+
"""
471+
Evaluate a YellowPages query expression.
472+
473+
The query language allows set operations and regex filtering.
474+
475+
Operators
476+
---------
477+
478+
| : union
479+
480+
& : intersection
481+
482+
- : difference
483+
484+
Parentheses are supported.
485+
486+
Regex
487+
-----
488+
489+
Use the form ``re{pattern}``.
490+
491+
Examples
492+
--------
493+
494+
All BPMs:
495+
496+
.. code-block:: python
497+
498+
>>> sr.yellow_pages["BPM"]
499+
500+
Union:
501+
502+
.. code-block:: python
503+
504+
>>> sr.yellow_pages["HCORR|VCORR"]
505+
506+
Difference:
507+
508+
.. code-block:: python
509+
510+
>>> sr.yellow_pages["BPM - re{BPM_C01-01}"]
511+
512+
Regex filter:
513+
514+
.. code-block:: python
515+
516+
>>> sr.yellow_pages["re{^BPM_C..-..$}"]
517+
518+
Combined expressions:
519+
520+
.. code-block:: python
521+
522+
>>> sr.yellow_pages["(HCORR|VCORR) - re{CH_.*}"]
523+
524+
"""
283525
ids = self._eval_query_to_ids(query)
284526
return sorted(ids)
285527

@@ -288,6 +530,40 @@ def __getitem__(self, query: str) -> list[str]:
288530
# ---------------------------
289531

290532
def __repr__(self) -> str:
533+
"""
534+
Return a human-readable overview of the YellowPages content.
535+
536+
The output lists controls, simulators and discovered objects
537+
grouped by category.
538+
539+
Examples
540+
--------
541+
542+
.. code-block:: python
543+
544+
>>> print(sr.yellow_pages)
545+
546+
Controls:
547+
live
548+
.
549+
550+
Simulators:
551+
design
552+
.
553+
554+
Arrays:
555+
BPM (pyaml.arrays.bpm_array) size=224
556+
HCORR (pyaml.arrays.magnet_array) size=96
557+
.
558+
559+
Tools:
560+
DEFAULT_ORBIT_CORRECTION (pyaml.tuning_tools.orbit)
561+
.
562+
563+
Diagnostics:
564+
BETATRON_TUNE (pyaml.diagnostics.tune_monitor)
565+
.
566+
"""
291567
lines: list[str] = []
292568

293569
lines.append("Controls:")

0 commit comments

Comments
 (0)