Beispiel #1
0
 def test_fields(self):
     """Test fields()."""
     self.assertEqual(fields(self.e),
                      dict(g=Tuple[self.NT], h=List[ForwardRef('Epsilon')]))
     self.assertEqual(
         fields(self.e, True),
         dict(g=Tuple[self.NT], h=List[ForwardRef('Epsilon')], _i=int))
Beispiel #2
0
 def test_fields(self):
     """Test fields()."""
     self.assertEqual(
         repr(fields(self.e)),
         "{'g': typing.Tuple[tests.NT], 'h': typing.List[ForwardRef('Epsilon')]}"
     )
     self.assertEqual(
         repr(fields(self.e, True)),
         "{'g': typing.Tuple[tests.NT], 'h': typing.List[ForwardRef('Epsilon')], '_i': <class 'int'>}"
     )
Beispiel #3
0
 def domain(self) -> dict:
     """The EIP-712 domain fields (built using ``HEADER_FIELDS``)."""
     return {
         field.replace("_", ""): getattr(self, field)
         for field in fields(self.__class__, internals=True)
         if field in HEADER_FIELDS
     }
    def transforminitargs(cls, *args, root=None, samp=None, **kwargs):
        if samp is not None:
            if isinstance(samp, str):
                if "SlideID" in kwargs:
                    raise TypeError("Provided both samp and SlideID")
                else:
                    kwargs["SlideID"] = samp
            else:
                if args or kwargs:
                    raise TypeError(
                        "Have to give either a sample or other arguments, not both."
                    )
                return super().transforminitargs(
                    *args, **kwargs, **{
                        field: getattr(samp, field)
                        for field in dataclassy.fields(SampleDef)
                    })

        if "SlideID" in kwargs and root is not None:
            root = pathlib.Path(root)
            try:
                cohorttable = readtable(root / "sampledef.csv", SampleDef)
            except IOError:
                pass
            else:
                for row in cohorttable:
                    if row.SlideID == kwargs["SlideID"]:
                        return cls.transforminitargs(root=root, samp=row)

            if "Scan" not in kwargs:
                try:
                    kwargs["Scan"] = max(
                        int(folder.name.replace("Scan", ""))
                        for folder in (root / kwargs["SlideID"] /
                                       "im3").glob("Scan*/"))
                except ValueError:
                    pass
            if "BatchID" not in kwargs and kwargs.get("Scan",
                                                      None) is not None:
                try:
                    with open(root / kwargs["SlideID"] / "im3" /
                              f"Scan{kwargs['Scan']}" / "BatchID.txt") as f:
                        kwargs["BatchID"] = int(f.read())
                except FileNotFoundError:
                    pass

        if "SampleID" not in kwargs: kwargs["SampleID"] = 0

        return super().transforminitargs(*args, **kwargs)
Beispiel #5
0
 def transforminitargs(cls,
                       *args,
                       pscale=None,
                       apscale=None,
                       im3x=None,
                       im3y=None,
                       im3xvec=None,
                       xvec=None,
                       vertex=None,
                       **kwargs):
     xveckwargs = {}
     vertexkwargs = {}
     im3xykwargs = {}
     im3xveckwargs = {}
     if xvec is not None:
         xveckwargs["x"], xveckwargs["y"] = xvec
     if vertex is not None:
         vertexkwargs = {
             field: getattr(vertex, field)
             for field in dataclassy.fields(type(vertex))
         }
         if apscale is None: apscale = vertex.apscale
         if apscale != vertex.apscale:
             raise ValueError(
                 f"Inconsistent apscales {apscale} {vertex.apscale}")
         if pscale is None: pscale = vertex.pscale
         if pscale != vertex.pscale is not None:
             raise ValueError(
                 f"Inconsistent pscales {pscale} {vertex.pscale}")
         del vertexkwargs["pscale"], vertexkwargs["apscale"]
     if im3x is not None:
         im3xykwargs["x"] = units.convertpscale(im3x, pscale, apscale)
     if im3y is not None:
         im3xykwargs["y"] = units.convertpscale(im3y, pscale, apscale)
     if im3xvec is not None:
         im3xveckwargs["x"], im3xveckwargs["y"] = units.convertpscale(
             im3xvec, pscale, apscale)
     return super().transforminitargs(
         *args,
         pscale=pscale,
         apscale=apscale,
         **kwargs,
         **xveckwargs,
         **vertexkwargs,
         **im3xykwargs,
         **im3xveckwargs,
     )
 def transforminitargs(cls, *args, rectangle=None, **kwargs):
     """
 If you give an existing Rectangle to init, the current rectangle
 will be identical to that one.  This is useful for subclasses.
 """
     rectanglekwargs = {}
     if rectangle is not None:
         rectanglekwargs = {
             **{
                 field: getattr(rectangle, field)
                 for field in dataclassy.fields(type(rectangle))
             }
         }
     return super().transforminitargs(
         *args,
         **rectanglekwargs,
         **kwargs,
     )
Beispiel #7
0
    def types(self) -> dict:
        """
        Recursively built ``dict`` (name of type ``->`` list of subtypes) of
        the underlying fields' types.
        """
        types: Dict[str, list] = {}
        types[self.type] = []

        for field in fields(self.__class__):
            value = getattr(self, field)
            if isinstance(value, EIP712Type):
                types[self.type].append({"name": field, "type": value.type})
                types.update(value.types())
            else:
                types[self.type].append({
                    "name": field,
                    "type": self.field_type(field)
                })

        return types
Beispiel #8
0
 def polygonfields(cls):
     return [
         field for field in dataclassy.fields(cls)
         if cls.metadata(field).get("ispolygonfield", False)
     ]