Skip to content

Commit

Permalink
feat: support full names and mistaken usage for QuickConstruct (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii authored Jul 7, 2021
1 parent e0482bf commit c8a7f44
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def __init__(
storage = args[-1]
args = args[:-1]

# Support raw Quick Construct being accidentally passed in
args = [
a.axes[0] # type: ignore
if isinstance(a, hist.quick_construct.ConstructProxy) and len(a.axes) == 1
else a
for a in args
]

if args:
if isinstance(storage, str):
storage_str = storage.title()
Expand Down
22 changes: 17 additions & 5 deletions src/hist/quick_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, hist_class: "Type[BaseHist]", *axes: AxisProtocol) -> None:
self.hist_class = hist_class
self.axes = axes

def Reg(
def Regular(
self,
bins: int,
start: float,
Expand Down Expand Up @@ -63,6 +63,8 @@ def Reg(
),
)

Reg = Regular

def Sqrt(
self,
bins: int,
Expand Down Expand Up @@ -170,7 +172,7 @@ def Func(
),
)

def Bool(
def Boolean(
self,
name: str = "",
label: str = "",
Expand All @@ -188,7 +190,9 @@ def Bool(
),
)

def Var(
Bool = Boolean

def Variable(
self,
edges: Iterable[float],
*,
Expand Down Expand Up @@ -219,7 +223,9 @@ def Var(
),
)

def Int(
Var = Variable

def Integer(
self,
start: int,
stop: int,
Expand Down Expand Up @@ -252,7 +258,9 @@ def Int(
),
)

def IntCat(
Int = Integer

def IntCategory(
self,
categories: Iterable[int],
*,
Expand All @@ -275,6 +283,8 @@ def IntCat(
),
)

IntCat = IntCategory

def StrCat(
self,
categories: Iterable[str],
Expand All @@ -298,6 +308,8 @@ def StrCat(
),
)

StrCategory = StrCat


class ConstructProxy(QuickConstruct):
__slots__ = ()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ def test_hist_proxy():
assert h["T", "F"] == 1


def test_hist_proxy_mistake():
h = Hist(Hist.new.IntCat(range(10)))
h2 = Hist.new.IntCategory(range(10)).Double()

assert h == h2


def test_general_density():
"""
Test general density -- whether Hist density work properly.
Expand Down

0 comments on commit c8a7f44

Please sign in to comment.