question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating data by schema.

See original GitHub issue

We have implemented very primitive generator by schema:

from mimesis.schema import Schema
from mimesis import constants as c

schema = Schema(locale=c.EN)

# We will use format 'provider.method'
# Param 'schema' should be dict or path to json file.
result = schema.load(schema={
    'username': 'personal.username',
    'password': 'personal.password',
    'full_name': 'datetime.full_name',
}).create()

But is not better solutions. @sobolevn suggest much better solutions using Lazy objects`, which looks like that:

>>> from custom.utils import format_phone
>>> from mimesis.schema import Schema, fields
>>> schema = Schema('en')

>>> schema.load(schema={
...     "id": fields.cryptographic.uuid(version=4),
...     "name":  fields.personal.full_name(gender='female'),
...     "version": fields.development.version(semantic=True),
...     "phone": format_phone(fields.personal.phone),
... }).create(iterations=2)

I like second solution too.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
sobolevncommented, Sep 1, 2017

Sure!

2reactions
sobolevncommented, Aug 10, 2017

I don’t like current implementation. It breaks one major rule: “everything is an object”. Our fields right now are not objects in general case. They are strings.

So it could break a lot of things for the end user. Imagine a user has some sort of logic to reformat phone numbers to his specific needs. Like: reformat_phone(value). How is it possible with the current implementation? Or any other functions/classes/etc which wraps values.

What do I suggest?

Lazy objects (or generator)

In my opinion, we should create LazyField wrapper to wrap any other existing field. And a special fields container with all the existing fields wrapped into LazyField. So, how would it work?

>>> from custom.utils import format_phone
>>> from mimesis.schema import Schema, fields
>>> schema = Schema('en')

>>> schema.load(schema={
...     "id": fields.cryptographic.uuid(version=4),
...     "name":  fields.personal.full_name(gender='female'),
...     "version": fields.development.version(semantic=True),
...     "phone": format_phone(fields.personal.phone),
... }).create(iterations=2)

On each iteration lazy objects (or generator) generates new value. User has all the control, code is more pythonic.

Considerations

Do you have any ideas? Am I missing something?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Database Data Generator | DbSchema
Generate Random Data for MySql, PostgreSQL, Redshift, Cassandra, MSSql, Oracle, MariaDb and all relational and NoSQL databases.
Read more >
Three Best Tools For Generating Fake Data | by Mala Deep
Here I will share the top 3 DIY dataset generators. 1. Mockaroo. Mockaroo Schema UI. Screenshot from ...
Read more >
Mockaroo - Random Data Generator and API Mocking Tool ...
A free test data generator and API mocking tool - Mockaroo lets you create custom CSV, JSON, SQL, and Excel datasets to test...
Read more >
Free Resources for Generating Realistic Fake Data
Free Resources for Generating Realistic Fake Data · (1) Faker · (2) Mockaroo · (3) GenerateData · (4) JSON Schema Faker · (5)...
Read more >
Schema Generator
The Schema Generator processor generates a schema based on the structure of a record and writes the schema into a record header attribute....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found