Skip to content

Commit

Permalink
Added aastex.Variable class. (#4)
Browse files Browse the repository at this point in the history
* Added `aastex.Variable` class.

* black

* docs
  • Loading branch information
byrdie committed Jun 4, 2024
1 parent ac45804 commit d39dd6f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
34 changes: 34 additions & 0 deletions aastex/_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Affiliation",
"Author",
"Acronym",
"Variable",
"Abstract",
"Section",
"Subsection",
Expand Down Expand Up @@ -170,6 +171,39 @@ def dumps(self):
return command


@dataclasses.dataclass
class Variable(pylatex.base_classes.LatexObject):
"""
A wrapper around the ``\\newcommand`` LaTeX command.
"""

name: str
"""The name of the variable."""

value: float | u.Quantity
"""The value of the variable."""

@property
def _name(self) -> str:
return NoEscape(f"\\{self.name}")

@property
def _value(self) -> str:
v = self.value
if isinstance(v, u.Quantity):
v = f"{v:latex_inline}"
v = rf"\ensuremath{{{v[1:~0]}}}"
else:
v = str(v)
return NoEscape(v)

def dumps(self) -> str:
return Command(
command="newcommand",
arguments=[self._name, self._value],
).dumps()


class Abstract(pylatex.base_classes.Environment):
def __init__(
self,
Expand Down
19 changes: 19 additions & 0 deletions aastex/_tests/test_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ def test_dumps(self, a: aastex.Title):
assert isinstance(a.dumps(), str)


@pytest.mark.parametrize(
argnames="a",
argvalues=[
aastex.Variable("foo", 2),
aastex.Variable("bar", 3 * u.AA),
],
)
class TestVariable:

def test_name(self, a: aastex.Variable):
assert isinstance(a.name, str)

def test_value(self, a: aastex.Variable):
assert isinstance(a.value, (int, float, u.Quantity))

def test_dumps(self, a: aastex.Variable):
assert isinstance(a.dumps(), str)


@pytest.mark.parametrize(
argnames="a",
argvalues=[
Expand Down

0 comments on commit d39dd6f

Please sign in to comment.