File: faker.py

package info (click to toggle)
python-flask-seeder 1.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: python: 1,062; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
""" Faker module """

from flask_seeder.generator import Generator

# pylint: disable=too-few-public-methods
class Faker:
    """ Base Faker class

    The `init` attribute is a dictionary that tells Faker how to
    initialize the classes, for example:
        {
            "name": generator.Name()
        }

    Attributes:
        cls: The type of class to be created
        init: Dictionary with initialization data
    """

    def __init__(self, cls=None, init=None):
        """ Initialize faker """
        self.cls = cls
        self.init = init

    def _init_args(self):
        args = {}
        if self.init is None:
            return args

        for arg, value in self.init.items():
            if isinstance(value, Generator):
                args[arg] = value.generate()
            else:
                args[arg] = value

        return args

    def create(self, limit=1):
        """ Create objects

        Create a number of instance of `cls`,
        all initialized with data from `init`.

        Arguments:
            limit: How many objects to create, default 1.

        Returns:
            List of `cls` instances initialized with data from `init`.
        """
        instances = []
        for _ in range(limit):
            args = self._init_args()
            instances.append(self.cls(**args))

        return instances