Z
Z
zlodiak2019-04-26 16:45:26
Python
zlodiak, 2019-04-26 16:45:26

How to populate a table with test data?

Please help me understand what is wrong.
I am using the nimesis package . With it, I'm trying to generate data to fill a table in the database with it.
Here is my model:

from app import db
import datetime


class User(db.Model):
    __tablename__ = 'users'

    id =            db.Column(db.Integer, primary_key=True)
    username =      db.Column(db.String(40), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    reg_date_unix = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
    upd_date_unix = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
    email =         db.Column(db.String(40))
    desc =          db.Column(db.Text)

    def __repr__(self):
        return '<User {}>'.format(self.username)

    @staticmethod
    def _bootstrap(count=10, locale='ru'):
        from mimesis import Generic

        generic = Generic(locale)

        for _ in range(count):
            user = User(
                username=generic.full_name(gender='female'),
                password_hash=generic.text(quantity=100),
                reg_date_unix=generic.datetime.timestamp(posix=False),
                upd_date_unix=generic.datetime.timestamp(posix=False),
                email=generic.email(),
                desc=generic.numbers.gender()
            )

            db.session.add(user)
            try:
                db.session.commit()
            except Exception:
                db.session.rollback()

As you can see, the model class has a static method that generates values ​​for the table. Thus in a DB the table is already created.
The problem appears when I try to run the _bootstrap() method from the shell like this:
(venv) [email protected] ~/python/crazy_run $ flask shell
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
App: app [production]
Instance: /home/kalinin/python/crazy_run/instance
>>> User()._bootstrap()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/kalinin/python/crazy_run/app/models.py", line 21, in _bootstrap
    from mimesis import Generic
  File "/home/kalinin/python/crazy_run/venv/lib/python3.5/site-packages/mimesis/__init__.py", line 10, in <module>
    from mimesis.providers import *
  File "/home/kalinin/python/crazy_run/venv/lib/python3.5/site-packages/mimesis/providers/__init__.py", line 5, in <module>
    from mimesis.providers.base import BaseProvider, BaseDataProvider
  File "/home/kalinin/python/crazy_run/venv/lib/python3.5/site-packages/mimesis/providers/base.py", line 84
    self._data: Dict[str, Any] = {}
              ^
SyntaxError: invalid syntax

That is, the system tells me that the error is in But what exactly is the error and how to fix it is not clear to me.
from mimesis import Generic

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qlkvg, 2019-04-26
@zlodiak

Python is old. Type annotation has appeared in Python since version 3.6.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question