API Reference¶
Core Functions¶
parse_cdl¶
Parse a CDL string into a structured description.
cdl_parser.parse_cdl(text)
¶
Parse a CDL string to CrystalDescription or AmorphousDescription.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
CDL string like "cubic[m3m]:{111}@1.0 + {100}@0.3" or "amorphous[opalescent]:{massive, botryoidal}" |
required |
Returns:
| Type | Description |
|---|---|
CrystalDescription | AmorphousDescription
|
CrystalDescription or AmorphousDescription object |
Raises:
| Type | Description |
|---|---|
ParseError
|
If parsing fails due to syntax error |
ValidationError
|
If validation fails (e.g., invalid point group) |
Examples:
>>> desc = parse_cdl("cubic[m3m]:{111}")
>>> desc.system
'cubic'
>>> desc.forms[0].miller.as_tuple()
(1, 1, 1)
Source code in src/cdl_parser/parser.py
validate_cdl¶
Validate a CDL string without parsing.
from cdl_parser import validate_cdl
is_valid, error = validate_cdl("cubic[m3m]:{111}")
if not is_valid:
print(f"Error: {error}")
cdl_parser.validate_cdl(text)
¶
Validate a CDL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
CDL string to validate |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (is_valid, error_message) |
Examples:
Source code in src/cdl_parser/parser.py
Data Classes¶
CrystalDescription¶
Main output of CDL parsing.
@dataclass
class CrystalDescription:
system: str # Crystal system
point_group: str # Point group symbol
forms: List[CrystalForm] # Crystal forms
modifications: List[Modification] # Morphological mods
twin: Optional[TwinSpec] # Twin specification
cdl_parser.CrystalDescription
dataclass
¶
Complete crystal description parsed from CDL.
The main output of CDL parsing, containing all information needed to generate a crystal visualization.
Attributes:
| Name | Type | Description |
|---|---|---|
system |
str
|
Crystal system ('cubic', 'hexagonal', etc.) |
point_group |
str
|
Hermann-Mauguin point group symbol ('m3m', '6/mmm', etc.) |
forms |
list[FormNode]
|
List of form nodes (CrystalForm or FormGroup) |
modifications |
list[Modification]
|
List of morphological modifications |
twin |
TwinSpec | None
|
Optional twin specification |
definitions |
list[Definition] | None
|
Optional list of named definitions |
Examples:
>>> desc = parse_cdl("cubic[m3m]:{111}@1.0 + {100}@1.3")
>>> desc.system
'cubic'
>>> len(desc.forms)
2
Source code in src/cdl_parser/models.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
flat_forms()
¶
Get a flat list of all CrystalForm objects (backwards compat).
Recursively traverses FormGroup nodes to extract all CrystalForm leaves. Features from parent FormGroups are merged into child forms.
Source code in src/cdl_parser/models.py
to_dict()
¶
Convert to dictionary representation.
Source code in src/cdl_parser/models.py
MillerIndex¶
Miller index representation.
@dataclass
class MillerIndex:
h: int
k: int
l: int
i: Optional[int] = None # For 4-index notation
def as_tuple(self) -> tuple[int, ...]
def as_3index(self) -> tuple[int, int, int]
cdl_parser.MillerIndex
dataclass
¶
Miller index representation.
Represents crystal face orientations using Miller or Miller-Bravais notation.
Attributes:
| Name | Type | Description |
|---|---|---|
h |
int
|
First Miller index |
k |
int
|
Second Miller index |
l |
int
|
Third Miller index (fourth in Miller-Bravais) |
i |
int | None
|
Third index for Miller-Bravais notation (hexagonal/trigonal) Calculated as -(h+k), only used for 4-index notation |
Examples:
>>> MillerIndex(1, 1, 1) # Octahedron face
>>> MillerIndex(1, 0, 0) # Cube face
>>> MillerIndex(1, 0, 1, i=-1) # Hexagonal {10-11}
Source code in src/cdl_parser/models.py
as_3index()
¶
CrystalForm¶
A crystal form with scale.
cdl_parser.CrystalForm
dataclass
¶
A crystal form with Miller index and scale.
Represents a single crystal form (set of symmetry-equivalent faces) with an optional distance scale for truncation.
Attributes:
| Name | Type | Description |
|---|---|---|
miller |
MillerIndex
|
The Miller index defining the form |
scale |
float
|
Distance scale (default 1.0, larger = more truncated) |
name |
str | None
|
Original name if using named form (e.g., 'octahedron') |
features |
list[Feature] | None
|
Optional list of feature annotations |
label |
str | None
|
Optional label for the form (e.g., 'prism' in prism:{10-10}) |
Examples:
>>> CrystalForm(MillerIndex(1, 1, 1), scale=1.0)
>>> CrystalForm(MillerIndex(1, 0, 0), scale=1.3, name='cube')
Source code in src/cdl_parser/models.py
Constants¶
from cdl_parser import (
CRYSTAL_SYSTEMS, # Set of system names
POINT_GROUPS, # Dict[system, Set[groups]]
DEFAULT_POINT_GROUPS, # Dict[system, default_group]
NAMED_FORMS, # Dict[name, (h, k, l)]
TWIN_LAWS, # Set of twin law names
)
CRYSTAL_SYSTEMS¶
Set of valid crystal system names:
cubictetragonalorthorhombichexagonaltrigonalmonoclinictriclinic
POINT_GROUPS¶
Dictionary mapping each crystal system to its valid point groups.
DEFAULT_POINT_GROUPS¶
Dictionary mapping each crystal system to its default (highest symmetry) point group.
NAMED_FORMS¶
Dictionary mapping common form names to their Miller indices:
| Name | Miller Index |
|---|---|
octahedron |
{111} |
cube |
{100} |
dodecahedron |
{110} |
trapezohedron |
{211} |
prism |
{10-10} |
basal |
{0001} |
rhombohedron |
{10-11} |
TWIN_LAWS¶
Set of recognized twin law names:
spinel- Spinel law (111) twinbrazil- Brazil law quartz twinjapan- Japan law quartz twinfluorite- Fluorite interpenetration twiniron_cross- Iron cross pyrite twin
Exceptions¶
ParseError¶
Raised when parsing fails due to syntax errors.
from cdl_parser import ParseError
try:
desc = parse_cdl("invalid{{{")
except ParseError as e:
print(f"Syntax error at position {e.position}: {e.message}")
cdl_parser.ParseError
¶
Bases: CDLError
Raised when CDL parsing fails.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable error description |
|
position |
Character position in the input string where error occurred |
|
line |
Optional line number (for multi-line inputs) |
|
column |
Optional column number |
Source code in src/cdl_parser/exceptions.py
ValidationError¶
Raised when validation fails due to invalid values.
from cdl_parser import ValidationError
try:
desc = parse_cdl("invalid[xxx]:{111}")
except ValidationError as e:
print(f"Validation error: {e.message}")
cdl_parser.ValidationError
¶
Bases: CDLError
Raised when CDL validation fails.
This is raised when the CDL syntax is correct but the content is semantically invalid (e.g., invalid point group for system).
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable error description |
|
field |
The field or component that failed validation |
|
value |
The invalid value |