Some python tricks 2

Теги:

translate in Python to transliterate Cyrillic

source

How call ipython from virtualenv

python -m IPython

Proper way to declare custom exceptions in modern Python

class ValidationError(Exception):
    def __init__(self, message, errors):
        # Call the base class constructor with the parameters it needs
        super().__init__(message)

        # Now for your custom code...
        self.errors = errors

source

Accessing dict keys like an attribute

discussion

example:

class Map(dict):
    """
    Example:
    m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer'])
    """
    def __init__(self, *args, **kwargs):
        super(Map, self).__init__(*args, **kwargs)
        for arg in args:
            if isinstance(arg, dict):
                for k, v in arg.iteritems():
                    self[k] = v

        if kwargs:
            for k, v in kwargs.iteritems():
                self[k] = v

    def __getattr__(self, attr):
        return self.get(attr)

    def __setattr__(self, key, value):
        self.__setitem__(key, value)

    def __setitem__(self, key, value):
        super(Map, self).__setitem__(key, value)
        self.__dict__.update({key: value})

    def __delattr__(self, item):
        self.__delitem__(item)

    def __delitem__(self, key):
        super(Map, self).__delitem__(key)
        del self.__dict__[key]

source

dotmap Dot access dictionary with dynamic hierarchy creation and ordered iteration

How to annotate a type that’s a class object (instead of a class instance)

from typing import Type
class Foo: ...
class Bar(Foo): ...
class Baz: ...
some_class: Type[Foo]
some_class = Foo # ok
some_class = Bar # ok
some_class = Baz # error
some_class = Foo() # error

source

Types annotations for classes with dynamic attribute names

example that not solve problem

How to return dictionary keys as a list in Python

>>> newdict = {1:0, 2:0, 3:0}
>>> [*newdict]
[1, 2, 3]

source

Смотри еще: