chemql-python 1.6.4__tar.gz → 1.6.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chemql_python-1.6.6/PKG-INFO +677 -0
- chemql_python-1.6.6/README.md +663 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/pyproject.toml +1 -1
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/__init__.py +290 -56
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/codecql.py +315 -47
- chemql_python-1.6.6/src/chemql/highlighting.py +502 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/math.cql +2 -0
- chemql_python-1.6.6/src/chemql_python.egg-info/PKG-INFO +677 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_public_api.py +15 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_reactions.py +15 -0
- chemql_python-1.6.4/PKG-INFO +0 -1341
- chemql_python-1.6.4/README.md +0 -1327
- chemql_python-1.6.4/src/chemql/highlighting.py +0 -255
- chemql_python-1.6.4/src/chemql_python.egg-info/PKG-INFO +0 -1341
- {chemql_python-1.6.4 → chemql_python-1.6.6}/LICENSE +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/setup.cfg +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/__main__.py +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/build.py +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/colors.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/BondEnergies.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/MoleculeBonds.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/Molecules.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/PeriodicTableJSON.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/Reactions.json +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/THIRD_PARTY_LICENSES/PeriodicTableJSON.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/data/THIRD_PARTY_LICENSES/RHEA.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/html/periodic_table.html +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/README.md +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/collections.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/datetime.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/decimal.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/fractions.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/json.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/math/statistics.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/pathlib.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/random.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql/stdlib/text.cql +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql_python.egg-info/SOURCES.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql_python.egg-info/dependency_links.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql_python.egg-info/entry_points.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql_python.egg-info/requires.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/src/chemql_python.egg-info/top_level.txt +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_codecql.py +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_external_molecule_lookup.py +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_stdlib.py +0 -0
- {chemql_python-1.6.4 → chemql_python-1.6.6}/tests/test_structured_types.py +0 -0
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chemql-python
|
|
3
|
+
Version: 1.6.6
|
|
4
|
+
Summary: A chemistry-focused query language and REPL
|
|
5
|
+
Author-email: Mohammad Rakibul Islam <rakibulofficial1017@gmail.com>
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: prompt_toolkit>=3.0
|
|
10
|
+
Requires-Dist: rich>=13.7
|
|
11
|
+
Provides-Extra: test
|
|
12
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# ChemQL
|
|
16
|
+
|
|
17
|
+
**The Python chemistry query language.** Query chemistry, automate calculations, and build reusable programs, from an interactive prompt, from script files, or from Python.
|
|
18
|
+
|
|
19
|
+
| | |
|
|
20
|
+
|---|---|
|
|
21
|
+
| Distribution name | `chemql-python` |
|
|
22
|
+
| Import / CLI name | `chemql` |
|
|
23
|
+
| Current version | 1.6.6 |
|
|
24
|
+
| Python | 3.12+ |
|
|
25
|
+
| Editor | [CodecQL](CODECQL.md), a terminal editor for `.cql` files, bundled (`chemql edit`) |
|
|
26
|
+
|
|
27
|
+
```chemql
|
|
28
|
+
search elements number > 10 and number < 20 return name symbol
|
|
29
|
+
search molecules molecular_weight < 50 sort molecular_weight limit 10
|
|
30
|
+
search reactions reactants has "O2" and products has "H2O"
|
|
31
|
+
balance H2 + O2 -> H2O
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Contents
|
|
37
|
+
|
|
38
|
+
- [Features](#features)
|
|
39
|
+
- [Installation](#installation)
|
|
40
|
+
- [Quick start](#quick-start)
|
|
41
|
+
- [Querying data](#querying-data)
|
|
42
|
+
- [Chemistry tools](#chemistry-tools)
|
|
43
|
+
- [The language](#the-language)
|
|
44
|
+
- [Standard library and packages](#standard-library-and-packages)
|
|
45
|
+
- [Python API](#python-api)
|
|
46
|
+
- [Command-line reference](#command-line-reference)
|
|
47
|
+
- [Configuration and environment](#configuration-and-environment)
|
|
48
|
+
- [Data sources](#data-sources)
|
|
49
|
+
- [Safety](#safety)
|
|
50
|
+
- [CodecQL editor](#codecql-editor)
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- **SQL-like queries** over three bundled datasets: periodic-table **elements**, **molecules** and **reactions**, with comparison operators, wildcard and regex matching, `and`/`or`/`not`, grouping, sorting, limiting, counting, projection and set operations.
|
|
57
|
+
- **Online lookups** when the bundled data has no answer: PubChem, ChemSpider (with an API key) and NIST WebBook for molecules; Chempirical and Rhea for reactions.
|
|
58
|
+
- **Equation balancing** (`balance H2 + O2 -> H2O`) with exact rational arithmetic, and **reaction lookup** under a given temperature, pressure and set of catalysts.
|
|
59
|
+
- **A complete scripting language**: variables, user-defined functions, macros and definitions, `if`/`for`/`while`, structured error handling (`risk`/`fix`/`cleanup`), enums, structs, records, namespaces with public/private members.
|
|
60
|
+
- **Python everywhere**: embed Python expressions in `[ ... ]`, multi-line Python in `[[[ ... ]]]`, and queries inside Python with `{{ ... }}`.
|
|
61
|
+
- **Shell integration**: run system commands from backticks or fenced blocks in a persistent shell session.
|
|
62
|
+
- **Built-in functions and a standard library** (`math`, `text`, `datetime`, `json`, `random`, ...).
|
|
63
|
+
- **A package manager and registry**: `chemql install`, `chemql init`, `chemql build`, `chemql upload`.
|
|
64
|
+
- **A Python API** (`execute_query_text`, a fluent `search(...)` builder, and plain data classes).
|
|
65
|
+
- **An interactive prompt** with syntax highlighting, context-aware completion and automatic multi-line block detection.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install chemql-python
|
|
73
|
+
chemql --version
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Requirements**
|
|
77
|
+
|
|
78
|
+
| Requirement | Notes |
|
|
79
|
+
|---|---|
|
|
80
|
+
| Python 3.12 or newer | The source uses PEP 701 f-string syntax. |
|
|
81
|
+
| `prompt_toolkit` 3.x | Interactive prompt. Use a recent 3.0 release. |
|
|
82
|
+
| `rich` *(optional)* | Renders Markdown help for installed packages. |
|
|
83
|
+
| `colorama` *(optional, Windows)* | ANSI colour support in [CodecQL](CODECQL.md); falls back to enabling virtual-terminal mode directly. |
|
|
84
|
+
|
|
85
|
+
**Upgrading**
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
chemql --upgrade # latest release
|
|
89
|
+
chemql --upgrade 1.6.6 # a specific version
|
|
90
|
+
chemql --upgrade --force # force a reinstall, overriding system-package protections
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Quick start
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
chemql # interactive prompt
|
|
99
|
+
chemql -c 'search elements symbol = "O"' # run one command
|
|
100
|
+
chemql -f script.cql # run a file
|
|
101
|
+
chemql -f script.cql -o out.txt # write output to a file (ANSI stripped)
|
|
102
|
+
echo 'findel "He"' | chemql # piped input is executed as a script
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
At the prompt, type `help` (or `?`) for the full reference, `help{function}` for details on a built-in, and `exit` (or Ctrl+C) to leave. Blocks such as `func ... endfunc`, `if ... endif` and `[[[ ... ]]]` are detected automatically: pressing Enter inserts a new line until the block is closed.
|
|
106
|
+
|
|
107
|
+
```chemql
|
|
108
|
+
>>> findel "Fe" return name number
|
|
109
|
+
>>> let h = {{ search elements symbol = "H" }}
|
|
110
|
+
>>> [h.atomic_mass]
|
|
111
|
+
>>> balance N2 + H2 <-> NH3
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
> **Comments** use `//` for a line and `/// ... ///` for a block. **Several commands** can share a line, separated by `;`. A trailing `\` continues a command on the next line.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Querying data
|
|
119
|
+
|
|
120
|
+
### Sources
|
|
121
|
+
|
|
122
|
+
| Source | Contents | Fields |
|
|
123
|
+
|---|---|---|
|
|
124
|
+
| `elements` | The periodic table | `name symbol number atomic_mass category phase period group block density melt boil molar_heat appearance electron_configuration electron_configuration_semantic electronegativity_pauling electron_affinity ionization_energies shells discovered_by named_by summary source image bohr_model_image bohr_model_3d spectral_img xpos ypos wxpos wypos` |
|
|
125
|
+
| `molecules` | Common molecules with bonding data | `name formula molecular_weight elements iupac_name smiles cid state melting_point boiling_point density bonds total_bond_energy` |
|
|
126
|
+
| `reactions` | Stored reactions | `id name equation reaction_type reversible reactants products conditions notes source` |
|
|
127
|
+
|
|
128
|
+
```chemql
|
|
129
|
+
source molecules // make molecules the default source
|
|
130
|
+
list all // list the fields of the current source (or the sources if none is set)
|
|
131
|
+
search molecular_weight < 20
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Anatomy of a query
|
|
135
|
+
|
|
136
|
+
```text
|
|
137
|
+
search [first|last|all] [SOURCE] CONDITIONS [sort FIELD [asc|desc]] [limit N] [count] [return FIELD ...]
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```chemql
|
|
141
|
+
search elements symbol = "Fe" return name symbol number
|
|
142
|
+
search molecules molecular_weight < 20 sort molecular_weight asc limit 5
|
|
143
|
+
search reactions reversible = true return name equation source
|
|
144
|
+
search first molecules name like "*acid" sort molecular_weight desc
|
|
145
|
+
search elements number > 100 count
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
- With no `first`/`last`, a single match returns the object itself and several matches return a list. No match prints `No matching item`.
|
|
149
|
+
- `return` projects fields **after** filtering, sorting and limiting. One field gives a list; several give a table.
|
|
150
|
+
- `count` must be the last operation and cannot be combined with `first`, `last` or `return`.
|
|
151
|
+
- A query may begin directly with an operation to act on the whole source: `search molecules count`.
|
|
152
|
+
|
|
153
|
+
### Operators
|
|
154
|
+
|
|
155
|
+
| Operator | Meaning |
|
|
156
|
+
|---|---|
|
|
157
|
+
| `=` `!=` `>` `<` `>=` `<=` | Comparison. Numeric and boolean values are converted from the query text (`reversible = true`). |
|
|
158
|
+
| `like` | Wildcard match, **case-insensitive**, against the *entire* value. |
|
|
159
|
+
| `like!` | Wildcard match, **case-sensitive**. |
|
|
160
|
+
| `likep` | Regular-expression search (`re.search`) on string fields. |
|
|
161
|
+
| `has` | Containment for lists and dictionaries. Molecule identifiers match exactly (`O2` does not match `CO2`); other strings match as case-insensitive substrings. |
|
|
162
|
+
| `not` | Negation: `name not like "H*"`, or `not (number > 10)`. |
|
|
163
|
+
| `and` `or` | Combine conditions. `and` binds tighter than `or`; use parentheses to group. |
|
|
164
|
+
| `union` `intersection` | Combine whole queries, evaluated left to right. |
|
|
165
|
+
|
|
166
|
+
```chemql
|
|
167
|
+
search elements category = "noble gas" and (period = 2 or period = 3)
|
|
168
|
+
search reactions reactants has "O2" and products has "H2O"
|
|
169
|
+
search elements symbol likep "^(He|Ne|Ar)$" return name
|
|
170
|
+
search molecules name like "*water*" union search molecules name like "*acid*"
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### `like` wildcards
|
|
174
|
+
|
|
175
|
+
| Wildcard | Matches |
|
|
176
|
+
|---|---|
|
|
177
|
+
| `*` | Any run of characters |
|
|
178
|
+
| `_` | Any single character |
|
|
179
|
+
| `#` | One digit |
|
|
180
|
+
| `?` | One letter |
|
|
181
|
+
| `%` | Zero or more digits |
|
|
182
|
+
| `&` | Zero or more letters |
|
|
183
|
+
| `\` | Escapes the next character |
|
|
184
|
+
|
|
185
|
+
### Shortcuts
|
|
186
|
+
|
|
187
|
+
```chemql
|
|
188
|
+
findel "He" // name or symbol
|
|
189
|
+
findel "Fe" return name number
|
|
190
|
+
|
|
191
|
+
findmol "Water" // name or formula; bundled data first, then online sources
|
|
192
|
+
findmol "aspirin" return formula molecular_weight
|
|
193
|
+
|
|
194
|
+
findre "Haber process" // name, id or equation; then Chempirical and Rhea (RHEA:nnnnn ids work)
|
|
195
|
+
findre "Haber" return name equation // project fields, as with search
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Using variables in queries
|
|
199
|
+
|
|
200
|
+
```chemql
|
|
201
|
+
define MAX 9
|
|
202
|
+
search elements number <= @MAX // literal text substitution
|
|
203
|
+
|
|
204
|
+
let limit_value = 20
|
|
205
|
+
search elements number < [limit_value] // Python expression evaluated inside the query
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Chemistry tools
|
|
211
|
+
|
|
212
|
+
### Balancing equations
|
|
213
|
+
|
|
214
|
+
```chemql
|
|
215
|
+
balance H2 + O2 -> H2O
|
|
216
|
+
balance N2 + H2 <-> NH3 // <-> marks the reaction as reversible
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
The result is a reaction with integer coefficients. When a reversibility flag is set, the equation also shows an estimated ΔH computed from the bundled bond energies. Formulas may contain nested parentheses, e.g. `Fe2(SO4)3`. Equations without a unique positive solution are rejected with an error.
|
|
220
|
+
|
|
221
|
+
### Finding a stored reaction
|
|
222
|
+
|
|
223
|
+
Reactions in the bundled dataset are only matched when their conditions match the current state.
|
|
224
|
+
|
|
225
|
+
```chemql
|
|
226
|
+
set temperature 150C // C, F or K; or `standard` (273.15 K) / `room` (298.15 K)
|
|
227
|
+
set pressure 1atm // Pa kPa MPa hPa mbar bar atm torr mmHg psi kpsi N/m^2; or standard / room
|
|
228
|
+
set catalysts iron zinc // replace the catalyst list; `set catalysts` clears it
|
|
229
|
+
add catalyst "Iron"
|
|
230
|
+
remove catalyst "Zinc"
|
|
231
|
+
conditions // show the current state
|
|
232
|
+
|
|
233
|
+
react N2 3H2 // coefficients can be written as 3H2 or 3 H2
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
A reaction matches when its reactants are exactly those given and its conditions are met: if it specifies a temperature, the current one must be within 5 K; if it specifies a pressure, within 5 %; and every catalyst it requires must be in the catalyst list. Reversible reactions can also be matched from their products. One product returns a molecule; several return a table.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## The language
|
|
241
|
+
|
|
242
|
+
### Variables, Python and embedded queries
|
|
243
|
+
|
|
244
|
+
```chemql
|
|
245
|
+
let answer = 42
|
|
246
|
+
[answer * 2] // evaluate a Python expression
|
|
247
|
+
[x = 10] // run a Python statement
|
|
248
|
+
let h = {{ search elements symbol = "H" }} // a query as a value
|
|
249
|
+
[h.atomic_mass]
|
|
250
|
+
variables // list variables
|
|
251
|
+
unset answer
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
```chemql
|
|
255
|
+
[[[
|
|
256
|
+
for n in range(3):
|
|
257
|
+
print(n)
|
|
258
|
+
]]]
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Variables, Python names and query results all live in one shared session. The value of the last expression is available as `_`.
|
|
262
|
+
|
|
263
|
+
### Definitions and macros
|
|
264
|
+
|
|
265
|
+
```chemql
|
|
266
|
+
define MAX 9 // literal text replacement, also inside strings
|
|
267
|
+
search elements number <= @MAX
|
|
268
|
+
definitions // list definitions
|
|
269
|
+
remove definition MAX
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Definitions are expanded in source order, once, and are not retroactive. **Macros** generate source text:
|
|
273
|
+
|
|
274
|
+
```chemql
|
|
275
|
+
define element_by_number{}
|
|
276
|
+
search elements number = \1
|
|
277
|
+
enddefine
|
|
278
|
+
|
|
279
|
+
@element_by_number{8}
|
|
280
|
+
macros
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Functions and built-ins
|
|
284
|
+
|
|
285
|
+
```chemql
|
|
286
|
+
func square
|
|
287
|
+
return [\1 * \1]
|
|
288
|
+
endfunc
|
|
289
|
+
|
|
290
|
+
square{5} // 25
|
|
291
|
+
let total = int{1} + square{3} // calls compose with Python expressions
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Function calls use braces and commas: `name{arg1, arg2}`, and arguments are available in the body as `\1`, `\2`, and so on. Quote text that contains spaces. Keep comments outside function bodies. `static func` and `func Type.method` (with `this`) define static and instance methods on structs; operator methods such as `func Type.__add__` enable overloading. User-defined functions take precedence over built-ins with the same name.
|
|
295
|
+
|
|
296
|
+
| Category | Built-ins |
|
|
297
|
+
|---|---|
|
|
298
|
+
| Numbers | `abs` `round` `min` `max` `sum` |
|
|
299
|
+
| Core | `len` `type` |
|
|
300
|
+
| Conversion | `str` `int` `float` `bool` |
|
|
301
|
+
| Strings | `upper` `lower` `contains` `replace` `split` `join` |
|
|
302
|
+
| Collections | `first` `last` `unique` `sort` `reversed` |
|
|
303
|
+
| Files | `read_text` `read_lines` `write_text` `file_exists` `list_files` |
|
|
304
|
+
| Data files | `read_csv` `write_csv` `read_json` `write_json` |
|
|
305
|
+
| Chemistry | `element` `count_atoms` `molar_mass` `empirical_formula` |
|
|
306
|
+
| Help | `help` |
|
|
307
|
+
|
|
308
|
+
```chemql
|
|
309
|
+
molar_mass{"H2SO4"}
|
|
310
|
+
count_atoms{"H2SO4", "O"} // 4
|
|
311
|
+
empirical_formula{"C6H12O6"} // CH2O
|
|
312
|
+
element{"Na", "atomic_mass"}
|
|
313
|
+
let rows = read_csv{"data.csv"}
|
|
314
|
+
write_json{"out.json", rows}
|
|
315
|
+
help{read_csv} // detailed help for any built-in
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### Control flow
|
|
319
|
+
|
|
320
|
+
```chemql
|
|
321
|
+
for i in range(5)
|
|
322
|
+
let value = i
|
|
323
|
+
endfor
|
|
324
|
+
|
|
325
|
+
if answer > 10
|
|
326
|
+
[print("big")]
|
|
327
|
+
elif answer > 5
|
|
328
|
+
[print("medium")]
|
|
329
|
+
else
|
|
330
|
+
[print("small")]
|
|
331
|
+
endif
|
|
332
|
+
|
|
333
|
+
[x = 0]
|
|
334
|
+
while x < 3
|
|
335
|
+
[x += 1]
|
|
336
|
+
endwhile
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
`break`, `continue` and `skip` control loops. The Python-style bracket form `[for ...]` ... `[endblock]` is also accepted.
|
|
340
|
+
|
|
341
|
+
### Error handling
|
|
342
|
+
|
|
343
|
+
```chemql
|
|
344
|
+
risk
|
|
345
|
+
signal "Something went wrong" // raise a recoverable error
|
|
346
|
+
fix
|
|
347
|
+
let handled = error // `error` holds the error type, or None after success
|
|
348
|
+
cleanup
|
|
349
|
+
[print("always runs")]
|
|
350
|
+
endrisk
|
|
351
|
+
|
|
352
|
+
warn "careful" // prints a warning; the latest is stored in `warnings`
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Inside a `risk` block, errors are raised rather than printed, so `fix` can handle them. `--suppress-warnings` silences `warn` output. Built-in functions raise `TooFewArguments`, `TooManyArguments`, `InvalidArgumentType`, `InvalidArgumentValue`, `FileReadError` and `FileWriteError`; `help{errors}` lists all error signals.
|
|
356
|
+
|
|
357
|
+
### Enums, structs and records
|
|
358
|
+
|
|
359
|
+
```chemql
|
|
360
|
+
enum Levels
|
|
361
|
+
LOW = 50
|
|
362
|
+
MEDIUM // omitted values are generated automatically
|
|
363
|
+
HIGH
|
|
364
|
+
endenum
|
|
365
|
+
|
|
366
|
+
struct Animal
|
|
367
|
+
name! // required field (also written `required name`)
|
|
368
|
+
sound = "..." // field with a default
|
|
369
|
+
endstruct
|
|
370
|
+
|
|
371
|
+
struct Dog from Animal // structs inherit from structs, records or Python classes
|
|
372
|
+
color!
|
|
373
|
+
endstruct
|
|
374
|
+
|
|
375
|
+
let rex = Dog{name = "Rex", color = "brown"}
|
|
376
|
+
|
|
377
|
+
func Dog.describe
|
|
378
|
+
return ["Dog named " + this.name]
|
|
379
|
+
endfunc
|
|
380
|
+
[rex.describe()]
|
|
381
|
+
|
|
382
|
+
record Point // immutable; supports arithmetic between records
|
|
383
|
+
x
|
|
384
|
+
y = 0 // fields with defaults must come last
|
|
385
|
+
endrecord
|
|
386
|
+
let p = Point{1, 2}
|
|
387
|
+
[p + p]
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
Records inherit only from records. Structs cannot inherit from enums.
|
|
391
|
+
|
|
392
|
+
### Namespaces and visibility
|
|
393
|
+
|
|
394
|
+
Members are **private** by default; mark them `public` to expose them.
|
|
395
|
+
|
|
396
|
+
```chemql
|
|
397
|
+
namespace Geometry
|
|
398
|
+
public let PI = 3.14159
|
|
399
|
+
public func area
|
|
400
|
+
return [\1 * \2]
|
|
401
|
+
endfunc
|
|
402
|
+
private let secret = 42
|
|
403
|
+
endnamespace
|
|
404
|
+
|
|
405
|
+
Geometry::area{3, 4}
|
|
406
|
+
Geometry::PI
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Calling a private member from outside raises `PermissionError`. Namespaces nest.
|
|
410
|
+
|
|
411
|
+
### Shell commands
|
|
412
|
+
|
|
413
|
+
````chemql
|
|
414
|
+
`ls -la` // one command
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
cd /tmp
|
|
418
|
+
echo "state persists between commands"
|
|
419
|
+
```
|
|
420
|
+
````
|
|
421
|
+
|
|
422
|
+
The shell is persistent: `cd` and shell variables carry over. ChemQL uses `bash` on Linux and macOS and PowerShell on Windows. Output is streamed live, and interactive programs that read the keyboard work.
|
|
423
|
+
|
|
424
|
+
### History
|
|
425
|
+
|
|
426
|
+
```chemql
|
|
427
|
+
dump session.txt // queries and results
|
|
428
|
+
dump good queries ok.cql // only successful queries
|
|
429
|
+
dump bad results errors.txt // only the output of failed commands
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
Syntax: `dump [all|good|bad] [queries|results] FILE`.
|
|
433
|
+
|
|
434
|
+
### Other commands
|
|
435
|
+
|
|
436
|
+
| Command | Purpose |
|
|
437
|
+
|---|---|
|
|
438
|
+
| `view` | Open the interactive periodic table in your browser |
|
|
439
|
+
| `clear` | Clear the terminal |
|
|
440
|
+
| `version` | Print the version |
|
|
441
|
+
| `help`, `?`, `help{topic}` | Help; topics include `syntax`, `errors`, `files`, `chemistry`, `stdlib`, built-in names, modules and installed packages |
|
|
442
|
+
| `exit` | Quit |
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
## Standard library and packages
|
|
447
|
+
|
|
448
|
+
### Loading modules
|
|
449
|
+
|
|
450
|
+
```chemql
|
|
451
|
+
load math // then call math::function{...}
|
|
452
|
+
math::sqrt{81} // 9.0
|
|
453
|
+
|
|
454
|
+
load math::statistics
|
|
455
|
+
math::statistics::mean{2, 4, 6} // 4
|
|
456
|
+
|
|
457
|
+
load text as t // alias
|
|
458
|
+
t::upper{"hello"}
|
|
459
|
+
|
|
460
|
+
load sqrt from math // import one public member
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
Qualified calls require an explicit `load`. Use `help{stdlib}` to list modules and `help{math::sqrt}` for individual functions.
|
|
464
|
+
|
|
465
|
+
| Module | Highlights |
|
|
466
|
+
|---|---|
|
|
467
|
+
| `math` | `PI` `E` `TAU`, `sqrt` `cbrt` `floor` `ceil` `factorial` `gcd` `lcm` `hypot` `pow`, trigonometry, `log` `log10` `log2` `exp`, `isfinite` `isinf` `isnan` `isclose` |
|
|
468
|
+
| `math::statistics` | `mean` `median` `mode` `multimode` `quantiles` `variance` `stdev` `pvariance` `pstdev` `harmonic_mean` `geometric_mean` |
|
|
469
|
+
| `text` | case conversion, `strip`, `startswith`, `endswith`, `find`, `count`, `isalpha` and friends, `regex_search` `regex_findall` `regex_replace` |
|
|
470
|
+
| `collections` | `unique` `flatten` `zip` `pairwise` `frequencies` `group_count` `product` `sorted` `reversed` |
|
|
471
|
+
| `datetime` | `today` `now` `utcnow` `date` `time` `timestamp` `from_timestamp` `weekday` `date_add_days` `date_difference_days` |
|
|
472
|
+
| `json` | `dumps` `dumps_pretty` `loads` `json_type` |
|
|
473
|
+
| `random` | `seed` `random` `randint` `randrange` `choice` `sample` `shuffle` `uniform` |
|
|
474
|
+
| `decimal` | `create` `add` `subtract` `multiply` `divide` `quantize` `to_string` |
|
|
475
|
+
| `fractions` | `create` `add` `subtract` `multiply` `divide` `numerator` `denominator` `to_float` |
|
|
476
|
+
| `pathlib` | `name` `stem` `suffix` `parent` `join` `exists` `is_file` `is_dir` `read_text` `write_text` |
|
|
477
|
+
|
|
478
|
+
### Package manager
|
|
479
|
+
|
|
480
|
+
```bash
|
|
481
|
+
chemql search "periodic" # search the registry
|
|
482
|
+
chemql info NAME # registry metadata
|
|
483
|
+
chemql install NAME # latest release
|
|
484
|
+
chemql install NAME --version 1.2.0
|
|
485
|
+
chemql upgrade NAME # re-download, replacing the installed copy
|
|
486
|
+
chemql list # installed packages
|
|
487
|
+
chemql uninstall NAME
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
Inside the interactive prompt, use `install PACKAGE [--version VERSION]`. Packages install to `~/.chemql/packages/NAME/VERSION` and are then loaded with `load NAME`. When resolving `load a::b`, ChemQL looks for `a/b.cql` in the current directory, then in installed packages (newest version first, honouring the manifest's `main` entry), then in the bundled `stdlib`. Releases that the registry has not marked as safe are refused.
|
|
491
|
+
|
|
492
|
+
### Creating a package
|
|
493
|
+
|
|
494
|
+
```bash
|
|
495
|
+
chemql init mypkg # scaffolds cqlpackage.json, mypkg.cql and README.md
|
|
496
|
+
cd mypkg
|
|
497
|
+
chemql build # dist/mypkg-1.0.0.tar.gz
|
|
498
|
+
chemql upload # to the registry (10 MB limit)
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
`cqlpackage.json` (a `cqlpackage.toml` with the same keys is also accepted):
|
|
502
|
+
|
|
503
|
+
```json
|
|
504
|
+
{
|
|
505
|
+
"name": "mypkg",
|
|
506
|
+
"author": "",
|
|
507
|
+
"version": "1.0.0",
|
|
508
|
+
"description": "A ChemQL package named mypkg.",
|
|
509
|
+
"help": "README.md",
|
|
510
|
+
"main": "mypkg.cql",
|
|
511
|
+
"submodules": [],
|
|
512
|
+
"buildsystem": "chemql-build",
|
|
513
|
+
"chemql-dependencies": [],
|
|
514
|
+
"python-dependencies": [],
|
|
515
|
+
"readme": "README.md",
|
|
516
|
+
"license": "MIT",
|
|
517
|
+
"source-code": {},
|
|
518
|
+
"help-individual": {}
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
```chemql
|
|
523
|
+
// mypkg.cql
|
|
524
|
+
public func hello
|
|
525
|
+
return "Hello from mypkg!"
|
|
526
|
+
endfunc
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
```chemql
|
|
530
|
+
load mypkg
|
|
531
|
+
mypkg::hello{}
|
|
532
|
+
help{mypkg} // renders the manifest's `help` Markdown file
|
|
533
|
+
help{mypkg::hello} // uses the `help-individual` entry for `hello`
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
A build needs a manifest and at least one `.cql` file. Version control folders, virtual environments and `dist` are excluded from the archive.
|
|
537
|
+
|
|
538
|
+
---
|
|
539
|
+
|
|
540
|
+
## Python API
|
|
541
|
+
|
|
542
|
+
```python
|
|
543
|
+
from chemql import execute_query_text
|
|
544
|
+
|
|
545
|
+
oxygen = execute_query_text('search elements symbol = "O"')
|
|
546
|
+
print(oxygen.name, oxygen.atomic_mass)
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
`execute_query_text(text)` returns the raw result: an `Element`, `Molecule` or `Reaction`, a list, a table (`ReturnTable`), a count or a string. Query-level mistakes (a typo in a key, a bad operator) are usually returned as a formatted `Error: ...` string rather than raised.
|
|
550
|
+
|
|
551
|
+
`execute_query(text)` is a typed variant: it returns `str`, `Molecule`, `Element`, `Reaction` or `Unknown`, and wraps anything else (lists, tables, counts) in `Unknown`, whose `.value` holds the raw result.
|
|
552
|
+
|
|
553
|
+
### Fluent query builder
|
|
554
|
+
|
|
555
|
+
```python
|
|
556
|
+
from chemql import search, union, intersection
|
|
557
|
+
|
|
558
|
+
noble = search("elements").category.condition(like="noble gas").return_("name", "symbol")
|
|
559
|
+
print(noble.execute())
|
|
560
|
+
|
|
561
|
+
light = search("elements").atomic_mass.condition(lt=10)
|
|
562
|
+
metals = search("elements").category.condition(like="*metal")
|
|
563
|
+
print((light & metals).execute()) # intersection; `|` is union
|
|
564
|
+
print(union(light, metals).execute())
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
`condition()` accepts exactly one keyword: `eq ne lt lte gt gte like likep has`.
|
|
568
|
+
|
|
569
|
+
### Lookups and chemistry helpers
|
|
570
|
+
|
|
571
|
+
```python
|
|
572
|
+
from chemql import (
|
|
573
|
+
ELEMENTS, MOLECULES, REACTIONS,
|
|
574
|
+
find_molecule, find_reaction, search_reactions, balance_stoichiometry,
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
find_molecule("water") # bundled data only; matches name, formula or IUPAC name
|
|
578
|
+
find_reaction("Haber", external=False) # local data only; external=True also tries Chempirical and Rhea
|
|
579
|
+
search_reactions(reversible=True, name="acid") # strings are case-insensitive substrings; other values are compared exactly
|
|
580
|
+
|
|
581
|
+
reaction = balance_stoichiometry("H2 + O2", "H2O")
|
|
582
|
+
print(reaction)
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
`balance_stoichiometry(reactants, products, reversible=None)` accepts `"A + B"` strings or iterables of formulas and raises `ValueError` if the reaction cannot be balanced uniquely.
|
|
586
|
+
|
|
587
|
+
**Exported names** (`chemql.__all__`): `Element`, `Molecule`, `Reaction`, `MOLECULES`, `REACTIONS`, `find_molecule`, `find_reaction`, `search_reactions`, `balance_stoichiometry`, `execute_query`, `execute_query_text`, `ChemQLQuery`, `search`, `union`, `intersection`, `ReturnTable`, `Unknown`, `__version__`, and the exceptions `ChemQLError`, `ChemQLBuiltinError`, `TooFewArguments`, `TooManyArguments`, `InvalidArgumentType`, `InvalidArgumentValue`, `FileReadError`, `FileWriteError`. (`ELEMENTS` is importable but not listed in `__all__`.)
|
|
588
|
+
|
|
589
|
+
---
|
|
590
|
+
|
|
591
|
+
## Command-line reference
|
|
592
|
+
|
|
593
|
+
```text
|
|
594
|
+
chemql [options]
|
|
595
|
+
chemql SUBCOMMAND ...
|
|
596
|
+
chemql edit [codecql options] [FILE]
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
| Option | Description |
|
|
600
|
+
|---|---|
|
|
601
|
+
| *(none)* | Start the interactive prompt (or run piped input) |
|
|
602
|
+
| `-c`, `--command CODE` | Execute one command |
|
|
603
|
+
| `-f`, `--file FILE` | Execute a file |
|
|
604
|
+
| `-o`, `--output FILE` | Write `-c`/`-f` output to a file, without ANSI sequences |
|
|
605
|
+
| `--no-ansi` | Strip ANSI styling from output |
|
|
606
|
+
| `--suppress-warnings` | Hide `warn` messages |
|
|
607
|
+
| `-v`, `--version` | Print the version |
|
|
608
|
+
| `--upgrade [VERSION]` | Upgrade ChemQL with pip |
|
|
609
|
+
| `--force`, `--dangerously` | With `--upgrade`: force a reinstall, overriding system-package protections |
|
|
610
|
+
|
|
611
|
+
| Subcommand | Description |
|
|
612
|
+
|---|---|
|
|
613
|
+
| `install NAME [--version V]` | Install a package from the registry |
|
|
614
|
+
| `upgrade NAME [--version V]` | Re-download a package, replacing the installed copy |
|
|
615
|
+
| `search QUERY` | Search the registry |
|
|
616
|
+
| `info NAME` | Show registry metadata as JSON |
|
|
617
|
+
| `list` | List installed packages |
|
|
618
|
+
| `uninstall NAME` | Remove an installed package |
|
|
619
|
+
| `init [NAME] [-d DIR]` | Scaffold a package project |
|
|
620
|
+
| `build` | Build `dist/NAME-VERSION.tar.gz` from the current directory |
|
|
621
|
+
| `upload` | Upload the built archive to the registry |
|
|
622
|
+
| `edit [FILE]` | Open the [CodecQL](CODECQL.md) editor |
|
|
623
|
+
|
|
624
|
+
Subcommands cannot be combined with `-c`, `-f` or `-o`.
|
|
625
|
+
|
|
626
|
+
---
|
|
627
|
+
|
|
628
|
+
## Configuration and environment
|
|
629
|
+
|
|
630
|
+
| Variable | Purpose | Default |
|
|
631
|
+
|---|---|---|
|
|
632
|
+
| `CHEMQL_PACKAGE_DIR` | Where packages are installed and searched | `~/.chemql/packages` |
|
|
633
|
+
| `CHEMQL_INSTALLED_FILE` | Installed-package record | `~/.chemql/installed.json` |
|
|
634
|
+
| `CHEMSPIDER_API_KEY` | Enables ChemSpider as a molecule source | *(unset)* |
|
|
635
|
+
|
|
636
|
+
---
|
|
637
|
+
|
|
638
|
+
## Data sources
|
|
639
|
+
|
|
640
|
+
ChemQL always consults its bundled data first, so offline queries over the bundled data keep working.
|
|
641
|
+
|
|
642
|
+
| Data | Source |
|
|
643
|
+
|---|---|
|
|
644
|
+
| Elements | `data/PeriodicTableJSON.json` |
|
|
645
|
+
| Molecules, bonds | `data/Molecules.json`, `data/MoleculeBonds.json` |
|
|
646
|
+
| Bond energies (used for ΔH estimates) | `data/BondEnergies.json` |
|
|
647
|
+
| Reactions | `data/Reactions.json` |
|
|
648
|
+
| Molecules, online fallback | PubChem (PUG REST and PUG-View), ChemSpider, NIST WebBook |
|
|
649
|
+
| Reactions, online fallback | Chempirical, Rhea |
|
|
650
|
+
|
|
651
|
+
The package also bundles `html/periodic_table.html` (used by `view`) and a `stdlib/` directory of `.cql` modules.
|
|
652
|
+
|
|
653
|
+
<!-- TODO: add attribution and licence notes for the bundled datasets and the services above. -->
|
|
654
|
+
|
|
655
|
+
---
|
|
656
|
+
|
|
657
|
+
## Safety
|
|
658
|
+
|
|
659
|
+
ChemQL runs real code with your permissions: `[ ... ]` and `[[[ ... ]]]` execute Python, backticks and fenced blocks execute shell commands, and `load` runs the source of a package. Only run scripts and install packages you trust. Online lookups need network access and send your query text to the services listed above.
|
|
660
|
+
|
|
661
|
+
---
|
|
662
|
+
|
|
663
|
+
## CodecQL editor
|
|
664
|
+
|
|
665
|
+
CodecQL is a small terminal editor built for ChemQL, with syntax highlighting, inline suggestions and one-key execution.
|
|
666
|
+
|
|
667
|
+
```bash
|
|
668
|
+
chemql edit script.cql
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
See [CODECQL.md](CODECQL.md) for key bindings, configuration and the suggestion-rule format.
|
|
672
|
+
|
|
673
|
+
---
|
|
674
|
+
|
|
675
|
+
## License
|
|
676
|
+
|
|
677
|
+
MIT. See [LICENSE](LICENSE)
|