How to use the dungeonsheets.stats.findattr function in dungeonsheets

To help you get started, we’ve selected a few dungeonsheets examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github canismarko / dungeon-sheets / tests / test_stats.py View on Github external
def test_findattr(self):
        """Check if the function can find attributes."""
        class TestClass():
            my_attr = 47
            YourAttr = 53
        test_class = TestClass()
        # Direct access
        self.assertEqual(stats.findattr(test_class, 'my_attr'),
                         test_class.my_attr)
        self.assertEqual(stats.findattr(test_class, 'YourAttr'),
                         test_class.YourAttr)
        # Swapping spaces for capitalization
        self.assertEqual(stats.findattr(test_class, 'my attr'),
                         test_class.my_attr)
        self.assertEqual(stats.findattr(test_class, 'your attr'),
                         test_class.YourAttr)
github canismarko / dungeon-sheets / dungeonsheets / classes.py View on Github external
def wild_shapes(self, new_shapes):
        actual_shapes = []
        # Retrieve the actual monster classes if possible
        for shape in new_shapes:
            if isinstance(shape, monsters.Monster):
                # Already a monster shape so just add it as is
                new_shape = shape
            else:
                # Not already a monster so see if we can find one
                try:
                    NewMonster = findattr(monsters, shape)
                    new_shape = NewMonster()
                except AttributeError:
                    msg = f'Wild shape "{shape}" not found. Please add it to ``monsters.py``'
                    raise exceptions.MonsterError(msg)
            actual_shapes.append(new_shape)
        # Save the updated list for later
        self._wild_shapes = actual_shapes
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
def wear_armor(self, new_armor):
        """Accepts a string or Armor class and replaces the current armor.

        If a string is given, then a subclass of
        :py:class:`~dungeonsheets.armor.Armor` is retrived from the
        ``armor.py`` file. Otherwise, an subclass of
        :py:class:`~dungeonsheets.armor.Armor` can be provided
        directly.

        """
        if new_armor not in ('', 'None', None):
            if isinstance(new_armor, armor.Armor):
                new_armor = new_armor
            else:
                NewArmor = findattr(armor, new_armor)
                new_armor = NewArmor()
            self.armor = new_armor
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
# Treat weapons specially
                for weap in val:
                    self.wield_weapon(weap)
            elif attr == 'magic_items':
                if isinstance(val, str):
                    val = [val]
                for mitem in val:
                    try:
                        self.magic_items.append(findattr(magic_items, mitem)(owner=self))
                    except (AttributeError):
                        msg = (f'Magic Item "{mitem}" not defined. '
                               f'Please add it to ``magic_items.py``')
                        warnings.warn(msg)
            elif attr == 'weapon_proficiencies':
                self.other_weapon_proficiencies = ()
                wps = set([findattr(weapons, w) for w in val])
                wps -= set(self.weapon_proficiencies)
                self.other_weapon_proficiencies = list(wps)
            elif attr == 'armor':
                self.wear_armor(val)
            elif attr == 'shield':
                self.wield_shield(val)
            elif attr == 'circle':
                if hasattr(self, 'Druid'):
                    self.Druid.circle = val
            elif attr == 'features':
                if isinstance(val, str):
                    val = [val]
                _features = []
                for f in val:
                    try:
                        _features.append(findattr(features, f))
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
_features.append(findattr(features, f))
                    except AttributeError:
                        msg = (f'Feature "{f}" not defined. '
                               f'Please add it to ``features.py``')
                        # create temporary feature
                        _features.append(features.create_feature(
                            name=f, source='Unknown',
                            __doc__="""Unknown Feature. Add to features.py"""))
                        warnings.warn(msg)
                self.custom_features += tuple(F(owner=self) for F in _features)
            elif (attr == 'spells') or (attr == 'spells_prepared'):
                # Create a list of actual spell objects
                _spells = []
                for spell_name in val:
                    try:
                        _spells.append(findattr(spells, spell_name))
                    except AttributeError:
                        msg = (f'Spell "{spell_name}" not defined. '
                               f'Please add it to ``spells.py``')
                        warnings.warn(msg)
                        # Create temporary spell
                        _spells.append(spells.create_spell(name=spell_name, level=9))
                        # raise AttributeError(msg)
                # Sort by name
                _spells.sort(key=lambda spell: spell.name)
                # Save list of spells to character atribute
                if attr == 'spells':
                    # Instantiate them all for the spells list
                    self._spells = tuple(S() for S in _spells)
                else:
                    # Instantiate them all for the spells list
                    self._spells_prepared = tuple(S() for S in _spells)
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
# raise AttributeError(msg)
                # Sort by name
                _spells.sort(key=lambda spell: spell.name)
                # Save list of spells to character atribute
                if attr == 'spells':
                    # Instantiate them all for the spells list
                    self._spells = tuple(S() for S in _spells)
                else:
                    # Instantiate them all for the spells list
                    self._spells_prepared = tuple(S() for S in _spells)
            elif attr == 'infusions':
                if hasattr(self, 'Artificer'):
                    _infusions = []
                    for infusion_name in val:
                        try:
                            _infusions.append(findattr(infusions, infusion_name))
                        except AttributeError:
                            msg = (f'Infusion "{infusion_name}" not defined. '
                                   f'Please add it to ``infusions.py``')
                            warnings.warn(msg)
                    _infusions.sort(key=lambda infusion: infusion.name)
                    self.infusions = tuple(i() for i in _infusions)
            else:
                if not hasattr(self, attr):
                    warnings.warn(f"Setting unknown character attribute {attr}",
                                  RuntimeWarning)
                # Lookup general attributes
                setattr(self, attr, val)
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
"""
        for attr, val in attrs.items():
            if attr == 'dungeonsheets_version':
                pass # Maybe we'll verify this later?
            elif attr == 'weapons':
                if isinstance(val, str):
                    val = [val]
                # Treat weapons specially
                for weap in val:
                    self.wield_weapon(weap)
            elif attr == 'magic_items':
                if isinstance(val, str):
                    val = [val]
                for mitem in val:
                    try:
                        self.magic_items.append(findattr(magic_items, mitem)(owner=self))
                    except (AttributeError):
                        msg = (f'Magic Item "{mitem}" not defined. '
                               f'Please add it to ``magic_items.py``')
                        warnings.warn(msg)
            elif attr == 'weapon_proficiencies':
                self.other_weapon_proficiencies = ()
                wps = set([findattr(weapons, w) for w in val])
                wps -= set(self.weapon_proficiencies)
                self.other_weapon_proficiencies = list(wps)
            elif attr == 'armor':
                self.wear_armor(val)
            elif attr == 'shield':
                self.wield_shield(val)
            elif attr == 'circle':
                if hasattr(self, 'Druid'):
                    self.Druid.circle = val
github canismarko / dungeon-sheets / dungeonsheets / character.py View on Github external
def race(self, newrace):
        if isinstance(newrace, race.Race):
            self._race = newrace
            self._race.owner = self
        elif isinstance(newrace, type) and issubclass(newrace, race.Race):
            self._race = newrace(owner=self)
        elif isinstance(newrace, str):
            try:
                self._race = findattr(race, newrace)(owner=self)
            except AttributeError:
                msg = (f'Race "{newrace}" not defined. '
                       f'Please add it to ``race.py``')
                self._race = race.Race(owner=self)
                warnings.warn(msg)
        elif newrace is None:
            self._race = race.Race(owner=self)