Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ source/
_*/
.idea/*
svg-inkscape/
*.minted
30 changes: 0 additions & 30 deletions Pipfile

This file was deleted.

902 changes: 0 additions & 902 deletions Pipfile.lock

This file was deleted.

5 changes: 4 additions & 1 deletion core/builder/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

from typing import Any, Self, Optional
from pathlib import Path
from enschema import Schema, SchemaError
from enschema import Schema, SchemaError, Regex

from core.utilities import colour as c

log = logging.getLogger('dgs')


ValidIdentifier = Regex(r'^[A-Za-z_][A-Za-z_0-9]*$')


class Context(abc.ABC):
_defaults: dict[str, Any] = {} # Defaults for every instance
_schema: Optional[Schema] = None # Validation schema for the context, or None if it is not to be validated
Expand Down
8 changes: 4 additions & 4 deletions core/builder/context/quantities/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def format(self, fmt: str = None):
elif fmt is None:
fmt = f'.{self.digits}g'

return self._format(fmt)
return format(self, fmt)

@property
def approx(self):
Expand All @@ -42,7 +42,7 @@ def approx(self):
def _full(self, kind: str, precision: int = None) -> str:
if precision is None:
precision = self.digits
return self._format(f'.{precision}{kind}')
return f'{self:.{precision}{kind}}'

def fullf(self, precision: int = None) -> str:
""" Full, with f formatting """
Expand All @@ -54,11 +54,11 @@ def fullg(self, precision: int = None) -> str:

@property
def full_exact(self):
return self._format('99g')
return f'{self:99g}'

@property
def full_approx(self):
return self._format(self.approximate(self.digits))
return f'{self.approximate(self.digits):{self.digits}g}'

def __str__(self):
return self.full
Expand Down
47 changes: 40 additions & 7 deletions core/builder/context/quantities/math.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

import regex as re


Expand All @@ -19,13 +17,48 @@ def __str__(self):
def __repr__(self):
return repr(self.__str__())

def __format__(self, spec: Optional[str] = None):
_INTERPUNCTION = '.,;?!'
_BASE_SPECS = {'', 'disp', 'align'}
_SPECS_ACCEPTING_PUNCTUATION = {'disp', 'align'}

def __format__(self, spec: str = ''):
interpunction = ''
if len(spec) > 0 and spec[-1] in self._INTERPUNCTION:
interpunction = spec[-1]
spec = spec[:-1]

# Distinguish "unknown base spec" from "valid base spec with invalid
# trailing character," because the latter is the much more common
# author mistake.
if spec not in self._BASE_SPECS:
if len(spec) > 1 and spec[:-1] in self._BASE_SPECS:
raise ValueError(
f"Invalid trailing character {spec[-1]!r} in MathObject "
f"format spec; expected one of {''.join(self._INTERPUNCTION)} "
f"or no trailing character"
)
raise NotImplementedError(
f"Unknown format spec {spec!r} for MathObject; "
f"expected one of {sorted(self._BASE_SPECS - {''})} or empty"
)

# Inline math doesn't need in-math punctuation — authors can simply
# type the punctuation outside, after the closing $.
if interpunction and spec not in self._SPECS_ACCEPTING_PUNCTUATION:
raise ValueError(
f"Inline math does not accept trailing punctuation; "
f"write the punctuation outside the math instead: "
f"`(* eq | inline *){interpunction}`"
)

match spec:
case None:
return self.__str__()
case '':
return f"${self.content}$"
case 'disp':
content = re.sub(r'^(?!\Z)', ' ', self.content, flags=re.MULTILINE)
return f"""$$\n{content}\n$$ {{#eq:{self.id}}}"""
return f"$$\n{content}{interpunction}\n$$ {{#eq:{self.id}}}"
case 'align':
content = re.sub(r'^(?!\Z)', ' ', self.content, flags=re.MULTILINE)
return f"""$${{\n{content}\n}}$$ {{#eq:{self.id}}}"""
return f"$${{\n{content}{interpunction}\n}}$$ {{#eq:{self.id}}}"
case _:
raise NotImplementedError(f"Unknown format spec {spec!r} for MathObject")
Loading
Loading