Get a ``dict`` of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects. The TraitTypes returned don't know anything about the values that the various HasTrait's instances are holding. The metadata kwargs allow f
(self, **metadata: t.Any)
| 1917 | return list(self.traits(**metadata)) |
| 1918 | |
| 1919 | def traits(self, **metadata: t.Any) -> dict[str, TraitType[t.Any, t.Any]]: |
| 1920 | """Get a ``dict`` of all the traits of this class. The dictionary |
| 1921 | is keyed on the name and the values are the TraitType objects. |
| 1922 | |
| 1923 | The TraitTypes returned don't know anything about the values |
| 1924 | that the various HasTrait's instances are holding. |
| 1925 | |
| 1926 | The metadata kwargs allow functions to be passed in which |
| 1927 | filter traits based on metadata values. The functions should |
| 1928 | take a single value as an argument and return a boolean. If |
| 1929 | any function returns False, then the trait is not included in |
| 1930 | the output. If a metadata key doesn't exist, None will be passed |
| 1931 | to the function. |
| 1932 | """ |
| 1933 | traits = self._traits.copy() |
| 1934 | |
| 1935 | if len(metadata) == 0: |
| 1936 | return traits |
| 1937 | |
| 1938 | result = {} |
| 1939 | for name, trait in traits.items(): |
| 1940 | for meta_name, meta_eval in metadata.items(): |
| 1941 | if not callable(meta_eval): |
| 1942 | meta_eval = _SimpleTest(meta_eval) |
| 1943 | if not meta_eval(trait.metadata.get(meta_name, None)): |
| 1944 | break |
| 1945 | else: |
| 1946 | result[name] = trait |
| 1947 | |
| 1948 | return result |
| 1949 | |
| 1950 | def trait_metadata(self, traitname: str, key: str, default: t.Any = None) -> t.Any: |
| 1951 | """Get metadata values for trait by key.""" |