979 lines
36 KiB
HTML
979 lines
36 KiB
HTML
|
|
<!doctype html>
|
|
<html lang="en" class="no-js">
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
|
|
|
|
|
|
<link rel="canonical" href="https://preserves.dev/python/latest/path/">
|
|
|
|
|
|
<link rel="prev" href="../merge/">
|
|
|
|
|
|
<link rel="next" href="../schema/">
|
|
|
|
<link rel="icon" href="../assets/images/favicon.png">
|
|
<meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.3">
|
|
|
|
|
|
|
|
<title>Preserves Path - Python Preserves</title>
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../assets/stylesheets/main.c4a75a56.min.css">
|
|
|
|
|
|
<link rel="stylesheet" href="../assets/stylesheets/palette.a0c5b2b5.min.css">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
|
|
<style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../assets/_mkdocstrings.css">
|
|
|
|
<script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<body dir="ltr" data-md-color-scheme="default" data-md-color-primary="" data-md-color-accent="">
|
|
|
|
|
|
|
|
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
|
|
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
|
|
<label class="md-overlay" for="__drawer"></label>
|
|
<div data-md-component="skip">
|
|
|
|
|
|
<a href="#preserves-path" class="md-skip">
|
|
Skip to content
|
|
</a>
|
|
|
|
</div>
|
|
<div data-md-component="announce">
|
|
|
|
</div>
|
|
|
|
<div data-md-color-scheme="default" data-md-component="outdated" hidden>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<header class="md-header md-header--shadow" data-md-component="header">
|
|
<nav class="md-header__inner md-grid" aria-label="Header">
|
|
<a href=".." title="Python Preserves" class="md-header__button md-logo" aria-label="Python Preserves" data-md-component="logo">
|
|
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>
|
|
|
|
</a>
|
|
<label class="md-header__button md-icon" for="__drawer">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
|
|
</label>
|
|
<div class="md-header__title" data-md-component="header-title">
|
|
<div class="md-header__ellipsis">
|
|
<div class="md-header__topic">
|
|
<span class="md-ellipsis">
|
|
Python Preserves
|
|
</span>
|
|
</div>
|
|
<div class="md-header__topic" data-md-component="header-topic">
|
|
<span class="md-ellipsis">
|
|
|
|
Preserves Path
|
|
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<label class="md-header__button md-icon" for="__search">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
|
|
</label>
|
|
<div class="md-search" data-md-component="search" role="dialog">
|
|
<label class="md-search__overlay" for="__search"></label>
|
|
<div class="md-search__inner" role="search">
|
|
<form class="md-search__form" name="search">
|
|
<input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
|
|
<label class="md-search__icon md-icon" for="__search">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
|
|
</label>
|
|
<nav class="md-search__options" aria-label="Search">
|
|
|
|
<button type="reset" class="md-search__icon md-icon" title="Clear" aria-label="Clear" tabindex="-1">
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
|
|
</button>
|
|
</nav>
|
|
|
|
</form>
|
|
<div class="md-search__output">
|
|
<div class="md-search__scrollwrap" data-md-scrollfix>
|
|
<div class="md-search-result" data-md-component="search-result">
|
|
<div class="md-search-result__meta">
|
|
Initializing search
|
|
</div>
|
|
<ol class="md-search-result__list" role="presentation"></ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="md-header__source">
|
|
<a href="https://gitlab.com/preserves/preserves" title="Go to repository" class="md-source" data-md-component="source">
|
|
<div class="md-source__icon md-icon">
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
|
|
</div>
|
|
<div class="md-source__repository">
|
|
GitLab
|
|
</div>
|
|
</a>
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
</header>
|
|
|
|
<div class="md-container" data-md-component="container">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<main class="md-main" data-md-component="main">
|
|
<div class="md-main__inner md-grid">
|
|
|
|
|
|
|
|
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
|
|
|
|
|
|
<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
|
|
<label class="md-nav__title" for="__drawer">
|
|
<a href=".." title="Python Preserves" class="md-nav__button md-logo" aria-label="Python Preserves" data-md-component="logo">
|
|
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z"/></svg>
|
|
|
|
</a>
|
|
Python Preserves
|
|
</label>
|
|
|
|
<div class="md-nav__source">
|
|
<a href="https://gitlab.com/preserves/preserves" title="Go to repository" class="md-source" data-md-component="source">
|
|
<div class="md-source__icon md-icon">
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
|
|
</div>
|
|
<div class="md-source__repository">
|
|
GitLab
|
|
</div>
|
|
</a>
|
|
</div>
|
|
|
|
<ul class="md-nav__list" data-md-scrollfix>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href=".." class="md-nav__link">
|
|
Overview
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../api/" class="md-nav__link">
|
|
The top-level preserves package
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../binary/" class="md-nav__link">
|
|
Machine-oriented binary syntax
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../compare/" class="md-nav__link">
|
|
Comparing Values
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../error/" class="md-nav__link">
|
|
Codec errors
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../fold/" class="md-nav__link">
|
|
Traversing values
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../merge/" class="md-nav__link">
|
|
Merging values
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item md-nav__item--active">
|
|
|
|
<input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
|
|
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__link md-nav__link--active" for="__toc">
|
|
Preserves Path
|
|
<span class="md-nav__icon md-icon"></span>
|
|
</label>
|
|
|
|
<a href="./" class="md-nav__link md-nav__link--active">
|
|
Preserves Path
|
|
</a>
|
|
|
|
|
|
|
|
<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__title" for="__toc">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Table of contents
|
|
</label>
|
|
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path" class="md-nav__link">
|
|
preserves.path
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="preserves.path">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--command-line-usage" class="md-nav__link">
|
|
Command-line usage
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--examples" class="md-nav__link">
|
|
Examples
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="Examples">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--setup-loading-test-data" class="md-nav__link">
|
|
Setup: Loading test data
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--example-1-selecting-string-valued-documentation-annotations" class="md-nav__link">
|
|
Example 1: Selecting string-valued documentation annotations
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--example-2-selecting-tests-with-records-as-their-annotatedvalues" class="md-nav__link">
|
|
Example 2: Selecting tests with Records as their annotatedValues
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.Predicate" class="md-nav__link">
|
|
Predicate
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.Selector" class="md-nav__link">
|
|
Selector
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.syntax" class="md-nav__link">
|
|
syntax
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.exec" class="md-nav__link">
|
|
exec()
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.parse" class="md-nav__link">
|
|
parse()
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../schema/" class="md-nav__link">
|
|
Preserves Schema
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../text/" class="md-nav__link">
|
|
Human-readable text syntax
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li class="md-nav__item">
|
|
<a href="../values/" class="md-nav__link">
|
|
Representations of Values
|
|
</a>
|
|
</li>
|
|
|
|
|
|
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
|
|
<div class="md-sidebar__scrollwrap">
|
|
<div class="md-sidebar__inner">
|
|
|
|
|
|
<nav class="md-nav md-nav--secondary" aria-label="Table of contents">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<label class="md-nav__title" for="__toc">
|
|
<span class="md-nav__icon md-icon"></span>
|
|
Table of contents
|
|
</label>
|
|
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path" class="md-nav__link">
|
|
preserves.path
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="preserves.path">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--command-line-usage" class="md-nav__link">
|
|
Command-line usage
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--examples" class="md-nav__link">
|
|
Examples
|
|
</a>
|
|
|
|
<nav class="md-nav" aria-label="Examples">
|
|
<ul class="md-nav__list">
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--setup-loading-test-data" class="md-nav__link">
|
|
Setup: Loading test data
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--example-1-selecting-string-valued-documentation-annotations" class="md-nav__link">
|
|
Example 1: Selecting string-valued documentation annotations
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path--example-2-selecting-tests-with-records-as-their-annotatedvalues" class="md-nav__link">
|
|
Example 2: Selecting tests with Records as their annotatedValues
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</nav>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.Predicate" class="md-nav__link">
|
|
Predicate
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.Selector" class="md-nav__link">
|
|
Selector
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.syntax" class="md-nav__link">
|
|
syntax
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.exec" class="md-nav__link">
|
|
exec()
|
|
</a>
|
|
|
|
</li>
|
|
|
|
<li class="md-nav__item">
|
|
<a href="#preserves.path.parse" class="md-nav__link">
|
|
parse()
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="md-content" data-md-component="content">
|
|
<article class="md-content__inner md-typeset">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h1 id="preserves-path">Preserves Path</h1>
|
|
|
|
|
|
<div class="doc doc-object doc-module">
|
|
|
|
|
|
<a id="preserves.path"></a>
|
|
<div class="doc doc-contents first">
|
|
|
|
<p>The <a class="autorefs autorefs-internal" href="#preserves.path">preserves.path</a> module implements <a href="https://preserves.dev/preserves-path.html">Preserves
|
|
Path</a>.</p>
|
|
<p>Preserves Path is roughly analogous to
|
|
<a href="https://www.w3.org/TR/2017/REC-xpath-31-20170321/">XPath</a>, but for Preserves values: just as
|
|
XPath selects portions of an XML document, a Preserves Path uses <em>path expressions</em> to select
|
|
portions of a <code>Value</code>.</p>
|
|
<p>Use <a class="autorefs autorefs-internal" href="#preserves.path.parse">parse</a> to compile a path expression, and then use the
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.exec">exec</a> method on the result to apply it to a given input:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="n">parse</span><span class="p">(</span><span class="n">PATH_EXPRESSION_STRING</span><span class="p">)</span><span class="o">.</span><span class="n">exec</span><span class="p">(</span><span class="n">PRESERVES_VALUE</span><span class="p">)</span>
|
|
<span class="o">-></span> <span class="n">SEQUENCE_OF_PRESERVES_VALUES</span>
|
|
</code></pre></div>
|
|
<h3 id="preserves.path--command-line-usage">Command-line usage</h3>
|
|
<p>When <a class="autorefs autorefs-internal" href="#preserves.path">preserves.path</a> is run as a <code>__main__</code> module, <code>sys.argv[1]</code> is
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.parse">parsed</a>, interpreted as a path expression, and
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.exec">run</a> against <a class="autorefs autorefs-internal" href="../text/#preserves.text">human-readable values</a> read from standard
|
|
input. Each matching result is passed to <a class="autorefs autorefs-internal" href="../text/#preserves.text.stringify">stringify</a> and printed to
|
|
standard output.</p>
|
|
<h3 id="preserves.path--examples">Examples</h3>
|
|
<h4 id="preserves.path--setup-loading-test-data">Setup: Loading test data</h4>
|
|
<p>The following examples use <code>testdata</code>:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="o">>>></span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'tests/samples.bin'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|
<span class="o">...</span> <span class="n">testdata</span> <span class="o">=</span> <span class="n">decode_with_annotations</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
|
|
</code></pre></div>
|
|
<p>Recall that <code>samples.bin</code> contains a binary-syntax form of the human-readable
|
|
[<code>samples.pr](https://preserves.dev/tests/samples.pr) test data file, intended to exercise most
|
|
of the features of Preserves. In particular, the root</code>Value` in the file has a number of
|
|
annotations (for documentation and other purposes).</p>
|
|
<h4 id="preserves.path--example-1-selecting-string-valued-documentation-annotations">Example 1: Selecting string-valued documentation annotations</h4>
|
|
<p>The path expression <code>.annotations ^ Documentation . 0 / string</code> proceeds in five steps:</p>
|
|
<ol>
|
|
<li><code>.annotations</code> selects each annotation on the root document</li>
|
|
<li><code>^ Documentation</code> retains only those values (each an annotation of the root) that are <code>Record</code>s with label equal to the symbol <code>Documentation</code></li>
|
|
<li><code>. 0</code> moves into the first child (the first field) of each such <code>Record</code>, which in our case is a list of other <code>Value</code>s</li>
|
|
<li><code>/</code> selects all immediate children of these lists</li>
|
|
<li><code>string</code> retains only those values that are strings</li>
|
|
</ol>
|
|
<p>The result of evaluating it on <code>testdata</code> is as follows:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="o">>>></span> <span class="n">selector</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="s1">'.annotations ^ Documentation . 0 / string'</span><span class="p">)</span>
|
|
<span class="o">>>></span> <span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">selector</span><span class="o">.</span><span class="n">exec</span><span class="p">(</span><span class="n">testdata</span><span class="p">):</span>
|
|
<span class="o">...</span> <span class="nb">print</span><span class="p">(</span><span class="n">stringify</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>
|
|
<span class="s2">"Individual test cases may be any of the following record types:"</span>
|
|
<span class="s2">"In each test, let stripped = strip(annotatedValue),"</span>
|
|
<span class="s2">" encodeBinary(·) produce canonical ordering and no annotations,"</span>
|
|
<span class="s2">" looseEncodeBinary(·) produce any ordering, but with annotations,"</span>
|
|
<span class="s2">" annotatedBinary(·) produce canonical ordering, but with annotations,"</span>
|
|
<span class="s2">" decodeBinary(·) include annotations,"</span>
|
|
<span class="s2">" encodeText(·) include annotations,"</span>
|
|
<span class="s2">" decodeText(·) include annotations,"</span>
|
|
<span class="s2">"and check the following numbered expectations according to the table above:"</span>
|
|
<span class="s2">"Implementations may vary in their treatment of the difference between expectations"</span>
|
|
<span class="s2">"21/22 and 31/32, depending on how they wish to treat end-of-stream conditions."</span>
|
|
</code></pre></div>
|
|
<h4 id="preserves.path--example-2-selecting-tests-with-records-as-their-annotatedvalues">Example 2: Selecting tests with Records as their annotatedValues</h4>
|
|
<p>The path expression <code>// [.^ [= Test + = NondeterministicTest]] [. 1 rec]</code> proceeds in three steps:</p>
|
|
<ol>
|
|
<li>
|
|
<p><code>//</code> recursively decomposes the input, yielding all direct and indirect descendants of each input value</p>
|
|
</li>
|
|
<li>
|
|
<p><code>[.^ [= Test + = NondeterministicTest]]</code> retains only those inputs (each a descendant of the root) that yield more than zero results when executed against the expression within the brackets:</p>
|
|
<ol>
|
|
<li><code>.^</code> selects only labels of values that are <code>Records</code>, filtering by type and transforming in a single step</li>
|
|
<li><code>[= Test + = NondeterministicTest]</code> again filters by a path expression:<ol>
|
|
<li>the infix <code>+</code> operator takes the <em>union</em> of matches of its arguments</li>
|
|
<li>the left-hand argument, <code>= Test</code> selects values (remember, record labels) equal to the symbol <code>Test</code></li>
|
|
<li>the right-hand argument <code>= NondeterministicTest</code> selects values equal to <code>NondeterministicTest</code></li>
|
|
</ol>
|
|
</li>
|
|
</ol>
|
|
<p>The result is thus all <code>Record</code>s anywhere inside <code>testdata</code> that have either <code>Test</code> or <code>NondeterministicTest</code> as their labels.</p>
|
|
</li>
|
|
<li>
|
|
<p><code>[. 1 rec]</code> filters these <code>Record</code>s by another path expression:</p>
|
|
<ol>
|
|
<li><code>. 1</code> selects their second field (fields are numbered from 0)</li>
|
|
<li><code>rec</code> retains only values that are <code>Record</code>s</li>
|
|
</ol>
|
|
</li>
|
|
</ol>
|
|
<p>Evaluating the expression against <code>testdata</code> yields the following:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="o">>>></span> <span class="n">selector</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="s1">'// [.^ [= Test + = NondeterministicTest]] [. 1 rec]'</span><span class="p">)</span>
|
|
<span class="o">>>></span> <span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">selector</span><span class="o">.</span><span class="n">exec</span><span class="p">(</span><span class="n">testdata</span><span class="p">):</span>
|
|
<span class="o">...</span> <span class="nb">print</span><span class="p">(</span><span class="n">stringify</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLMHY2FwdHVyZbSzB2Rpc2NhcmSEhA==] <capture <discard>>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLMHb2JzZXJ2ZbSzBXNwZWFrtLMHZGlzY2FyZIS0swdjYXB0dXJltLMHZGlzY2FyZISEhIQ=] <observe <speak <discard> <capture <discard>>>>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLWzBnRpdGxlZLMGcGVyc29usAECswV0aGluZ7ABAYSwAWWxCUJsYWNrd2VsbLSzBGRhdGWwAgcdsAECsAEDhLECRHKE] <[titled person 2 thing 1] 101 "Blackwell" <date 1821 2 3> "Dr">></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLMHZGlzY2FyZIQ=] <discard>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLABB7WEhA==] <7 []>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLMHZGlzY2FyZLMIc3VycHJpc2WE] <discard surprise>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLEHYVN0cmluZ7ABA7ABBIQ=] <"aString" 3 4>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tLSzB2Rpc2NhcmSEsAEDsAEEhA==] <<discard> 3 4>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[hbMCYXK0swFShbMCYWazAWaE] @ar <R @af f>></span>
|
|
<span class="o"><</span><span class="n">Test</span> <span class="c1">#[tIWzAmFyswFShbMCYWazAWaE] <@ar R @af f>></span>
|
|
</code></pre></div>
|
|
|
|
|
|
|
|
<div class="doc doc-children">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div class="doc doc-object doc-attribute">
|
|
|
|
|
|
|
|
<h2 id="preserves.path.Predicate" class="doc doc-heading">
|
|
<code class="highlight language-python"><span class="n">Predicate</span> <span class="o">=</span> <span class="n">syntax</span><span class="o">.</span><span class="n">Predicate</span></code>
|
|
|
|
<span class="doc doc-labels">
|
|
<small class="doc doc-label doc-label-module-attribute"><code>module-attribute</code></small>
|
|
</span>
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="doc doc-contents ">
|
|
|
|
<p>Schema definition for representing a Preserves Path <code>Predicate</code>.</p>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="doc doc-object doc-attribute">
|
|
|
|
|
|
|
|
<h2 id="preserves.path.Selector" class="doc doc-heading">
|
|
<code class="highlight language-python"><span class="n">Selector</span> <span class="o">=</span> <span class="n">syntax</span><span class="o">.</span><span class="n">Selector</span></code>
|
|
|
|
<span class="doc doc-labels">
|
|
<small class="doc doc-label doc-label-module-attribute"><code>module-attribute</code></small>
|
|
</span>
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="doc doc-contents ">
|
|
|
|
<p>Schema definition for representing a sequence of Preserves Path <code>Step</code>s.</p>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="doc doc-object doc-attribute">
|
|
|
|
|
|
|
|
<h2 id="preserves.path.syntax" class="doc doc-heading">
|
|
<code class="highlight language-python"><span class="n">syntax</span> <span class="o">=</span> <span class="n">load_schema_file</span><span class="p">(</span><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="s1">'path.prb'</span><span class="p">)</span><span class="o">.</span><span class="n">path</span></code>
|
|
|
|
<span class="doc doc-labels">
|
|
<small class="doc doc-label doc-label-module-attribute"><code>module-attribute</code></small>
|
|
</span>
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="doc doc-contents ">
|
|
|
|
<p>This value is a Python representation of a <a class="autorefs autorefs-internal" href="../schema/#preserves.schema">Preserves Schema</a> definition
|
|
for the Preserves Path expression language. The language is defined in the file
|
|
<a href="https://preserves.dev/path/path.prs">path.prs</a>.</p>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="doc doc-object doc-function">
|
|
|
|
|
|
|
|
<h2 id="preserves.path.exec" class="doc doc-heading">
|
|
<code class="highlight language-python"><span class="n">exec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span></code>
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="doc doc-contents ">
|
|
|
|
<p>WARNING: This is not a <em>function</em>: it is a <em>method</em> on
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.Selector">Selector</a>, <a class="autorefs autorefs-internal" href="#preserves.path.Predicate">Predicate</a>, and so on.</p>
|
|
<div class="highlight"><pre><span></span><code><span class="o">>>></span> <span class="n">sel</span> <span class="o">=</span> <span class="n">parse</span><span class="p">(</span><span class="s1">'/ [.length gt 1]'</span><span class="p">)</span>
|
|
<span class="o">>>></span> <span class="n">sel</span><span class="o">.</span><span class="n">exec</span><span class="p">([</span><span class="s1">''</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'ab'</span><span class="p">,</span> <span class="s1">'abc'</span><span class="p">,</span> <span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'bcd'</span><span class="p">,</span> <span class="s1">'cd'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">''</span><span class="p">])</span>
|
|
<span class="p">(</span><span class="s1">'ab'</span><span class="p">,</span> <span class="s1">'abc'</span><span class="p">,</span> <span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'bcd'</span><span class="p">,</span> <span class="s1">'cd'</span><span class="p">)</span>
|
|
</code></pre></div>
|
|
|
|
<details class="quote">
|
|
<summary>Source code in <code>preserves/path.py</code></summary>
|
|
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">523</span>
|
|
<span class="normal">524</span>
|
|
<span class="normal">525</span>
|
|
<span class="normal">526</span>
|
|
<span class="normal">527</span>
|
|
<span class="normal">528</span>
|
|
<span class="normal">529</span>
|
|
<span class="normal">530</span>
|
|
<span class="normal">531</span>
|
|
<span class="normal">532</span>
|
|
<span class="normal">533</span>
|
|
<span class="normal">534</span>
|
|
<span class="normal">535</span>
|
|
<span class="normal">536</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="nd">@extend</span><span class="p">(</span><span class="n">syntax</span><span class="o">.</span><span class="n">Function</span><span class="p">)</span>
|
|
<span class="k">def</span> <span class="nf">exec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""WARNING: This is not a *function*: it is a *method* on</span>
|
|
<span class="sd"> [Selector][preserves.path.Selector], [Predicate][preserves.path.Predicate], and so on.</span>
|
|
|
|
<span class="sd"> ```python</span>
|
|
<span class="sd"> >>> sel = parse('/ [.length gt 1]')</span>
|
|
<span class="sd"> >>> sel.exec(['', 'a', 'ab', 'abc', 'abcd', 'bcd', 'cd', 'd', ''])</span>
|
|
<span class="sd"> ('ab', 'abc', 'abcd', 'bcd', 'cd')</span>
|
|
|
|
<span class="sd"> ```</span>
|
|
|
|
<span class="sd"> """</span>
|
|
<span class="k">return</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">selector</span><span class="o">.</span><span class="n">exec</span><span class="p">(</span><span class="n">v</span><span class="p">)),)</span>
|
|
</code></pre></div></td></tr></table></div>
|
|
</details>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="doc doc-object doc-function">
|
|
|
|
|
|
|
|
<h2 id="preserves.path.parse" class="doc doc-heading">
|
|
<code class="highlight language-python"><span class="n">parse</span><span class="p">(</span><span class="n">s</span><span class="p">)</span></code>
|
|
|
|
</h2>
|
|
|
|
|
|
<div class="doc doc-contents ">
|
|
|
|
<p>Parse <code>s</code> as a Preserves Path path expression, yielding a
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.Selector">Selector</a> object. Selectors (and Predicates etc.) have an
|
|
<a class="autorefs autorefs-internal" href="#preserves.path.exec">exec</a> method defined on them.</p>
|
|
<p>Raises <code>ValueError</code> if <code>s</code> is not a valid path expression.</p>
|
|
|
|
<details class="quote">
|
|
<summary>Source code in <code>preserves/path.py</code></summary>
|
|
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">131</span>
|
|
<span class="normal">132</span>
|
|
<span class="normal">133</span>
|
|
<span class="normal">134</span>
|
|
<span class="normal">135</span>
|
|
<span class="normal">136</span>
|
|
<span class="normal">137</span>
|
|
<span class="normal">138</span>
|
|
<span class="normal">139</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Parse `s` as a Preserves Path path expression, yielding a</span>
|
|
<span class="sd"> [Selector][preserves.path.Selector] object. Selectors (and Predicates etc.) have an</span>
|
|
<span class="sd"> [exec][preserves.path.exec] method defined on them.</span>
|
|
|
|
<span class="sd"> Raises `ValueError` if `s` is not a valid path expression.</span>
|
|
|
|
<span class="sd"> """</span>
|
|
<span class="k">return</span> <span class="n">parse_selector</span><span class="p">(</span><span class="n">Parser</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>
|
|
</code></pre></div></td></tr></table></div>
|
|
</details>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<hr>
|
|
<div class="md-source-file">
|
|
<small>
|
|
|
|
Last update:
|
|
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">March 16, 2023</span>
|
|
|
|
<br>
|
|
Created:
|
|
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">March 16, 2023</span>
|
|
|
|
|
|
</small>
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</article>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
<footer class="md-footer">
|
|
|
|
<div class="md-footer-meta md-typeset">
|
|
<div class="md-footer-meta__inner md-grid">
|
|
<div class="md-copyright">
|
|
|
|
|
|
Made with
|
|
<a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
|
|
Material for MkDocs
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
|
|
</div>
|
|
<div class="md-dialog" data-md-component="dialog">
|
|
<div class="md-dialog__inner md-typeset"></div>
|
|
</div>
|
|
|
|
<script id="__config" type="application/json">{"base": "..", "features": [], "search": "../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}, "version": {"provider": "mike"}}</script>
|
|
|
|
|
|
<script src="../assets/javascripts/bundle.efa0ade1.min.js"></script>
|
|
|
|
|
|
</body>
|
|
</html> |