Comments (5)
I've wanted this feature for a long time, but it's pretty hard to implement for all these languages supported by Mimesis.
from mimesis.
I created a proof of concept for this. In the below example you can get a sense for the API.
The example is a modification of https://mimesis.name/en/master/schema.html#schema-definition where we create the email based on the name.
def custom_email_handler(
random: Random,
*,
domains: list[str],
_eager_data: dict[str, Any],
**_kwargs: Any,
) -> str:
name: str = _eager_data["owner"]["creator"]
username = name.replace(" ", ".").lower()
return f"{username}@{random.choice(domains)}"
fieldset = Fieldset()
field = Field()
lazy_field = LazyField()
lazy_field.register_handler("custom_email", custom_email_handler)
schema = EvaluatedSchema(
{
"pk": (field, "increment", {}),
"uid": (field, "uuid", {}),
"name": (field, "text.word", {}),
"version": (field, "version", {}),
"timestamp": (field, "timestamp", {"fmt": TimestampFormat.POSIX}),
"owner": {
"email": (lazy_field, "custom_email", {"domains": ["mimesis.name"]}),
"creator": (field, "full_name", {"gender": Gender.FEMALE}),
},
"apiKeys": (fieldset, "token_hex", {"key": lambda s: s[:16], "i": 3}),
}
)
print(schema.create())
Example output:
{
"pk": 1,
"uid": "7c731bdf-656e-4266-aefe-3af7af05dcbf",
"name": "doc",
"version": "92.18.33",
"timestamp": 1726148859,
"owner": {
"creator": "Madelaine Price",
"email": "[email protected]"
},
"apiKeys": [
"b404af366115c2fe",
"d16e1fb9bbd3c354",
"a63237fed56da108"
]
}
@lk-geimfari: If you have an idea of how to implement this with the current Schema class, I would love to hear about it. I couldn't see a way of implementing it with the current API (CallableSchema is essentially a black box).
from mimesis.
I created a proof of concept for this. In the below example you can get a sense for the API.
The example is a modification of https://mimesis.name/en/master/schema.html#schema-definition where we create the email based on the name.
def custom_email_handler( random: Random, *, domains: list[str], _eager_data: dict[str, Any], **_kwargs: Any, ) -> str: name: str = _eager_data["owner"]["creator"] username = name.replace(" ", ".").lower() return f"{username}@{random.choice(domains)}" fieldset = Fieldset() field = Field() lazy_field = LazyField() lazy_field.register_handler("custom_email", custom_email_handler) schema = EvaluatedSchema( { "pk": (field, "increment", {}), "uid": (field, "uuid", {}), "name": (field, "text.word", {}), "version": (field, "version", {}), "timestamp": (field, "timestamp", {"fmt": TimestampFormat.POSIX}), "owner": { "email": (lazy_field, "custom_email", {"domains": ["mimesis.name"]}), "creator": (field, "full_name", {"gender": Gender.FEMALE}), }, "apiKeys": (fieldset, "token_hex", {"key": lambda s: s[:16], "i": 3}), } ) print(schema.create())Example output:
{ "pk": 1, "uid": "7c731bdf-656e-4266-aefe-3af7af05dcbf", "name": "doc", "version": "92.18.33", "timestamp": 1726148859, "owner": { "creator": "Madelaine Price", "email": "[email protected]" }, "apiKeys": [ "b404af366115c2fe", "d16e1fb9bbd3c354", "a63237fed56da108" ] }@lk-geimfari: If you have an idea of how to implement this with the current Schema class, I would love to hear about it. I couldn't see a way of implementing it with the current API (CallableSchema is essentially a black box).
Alas, it's too complicated to implement this feature at this moment.
from mimesis.
I agree it's not viable with the Schema API as-is.
Would you be open to a breaking change of the Schema API and/or a LazySchema class?
The API I demonstrated above isn't theoretical. It's the API from working code. The question is simply if you want it as part of mimesis and if so, how you want to integrate it.
from mimesis.
I'd prefer not to break the existing API, but having a LazyField would be useful.
from mimesis.
Related Issues (20)
- Add support for additional locales
- Use better color scheme for docs
- Add croatian locale HOT 5
- Person date of birth HOT 5
- generic.pyi has incorrect attribute for providers.Cryptographic HOT 1
- Support for secondary addresses
- [Integration] Pytest plugin is outdated HOT 4
- Suppor Fieldset fixture for pytest plugin.
- Codecov reporting wrong coverage information. HOT 8
- factory_boy integration out of the box. HOT 1
- Support python < 3.10 HOT 1
- Remove inappropriate words from your random text selections HOT 3
- A custom handler with an alias returns an error when using the alias HOT 4
- Tests that write to files fail in windows
- Duplicate/overwritten test
- 15.1.0: pytest fails onscanning units HOT 5
- Use Markdown for Readme
- Mimesis fails on FreeBSD HOT 1
- Location aware test data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mimesis.