-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterator-based SeqObject #416
Comments
To add some context to what @bertptrs said:
generated_stream = jj2_env.from_string(some_template).stream(
items=item_generator, **new_template_context
) This is very useful when working with a large number of items that need to be rendered, since they don't all need to be loaded into memory first. Is there a way to achieve this in We looked at SeqObject to replicate this, however it requires random access in its fn get_item(&self, idx: usize) -> Option<Value>; |
This seems to conflate two very different problems. One is iterating over something that has no known length, the other is streaming a large template. The latter (eg: what Iterators in MiniJinja don't really exist, they are just vectors or sequences internally. Since there is no other loop equivalent it's quite likely it won't be possible to accomplish this without abusing recursion. I will look into what it takes to add iterator support but it's not something that is super trivial. |
I looked into this a bit now. Today the iterator types you can get from Maybe this is something to look at together with #400 |
In order for this to be changed it most likely will either require some hacks or at least to do the changes here: #418 |
Turns out part of this is fixable even without a major. I hacked something up in #426. Would be curious to know if this works for you. You can basically now pass |
Great, thanks for looking into this so quickly. We'll give this a try next week and will let you know if it works for us. |
We have now tried this for one use case and I'm happy to report that it's working for us. Thanks for adding support for this! |
Since version 0.27 this crate provides a method to adapt an arbitrary object into a conceptual sequence, using the
SeqObject
trait which can then be used to iterate over. This can then be turned into aValue
and used in the template.One thing it doesn't allow you to do, is iterate over objects in a streaming fashion. The requirement of random access, baked into the trait, invalidates doing as much. You have to know ahead of time how many objects you have, and you have to have random access on the underlying object.
In Python on the other hand, it's possible to use generators to lazily load values and iterate over them as you go.
This mainly comes into play when writing multi-gigabyte files based on millions of iterable items.
Something like
Value::from_iterator(val: impl<Iterator> + 'static)
would solve this. Not to be confused with the currentFromIterator
impl, which materializes the list into a Vec. This is explicitly undesirable.The text was updated successfully, but these errors were encountered: