Git Product home page Git Product logo

Comments (3)

rtobar avatar rtobar commented on August 23, 2024 1

@Bubu thanks for the very good question. I have pondered about this myself in the past, I have also thought it would be nice to have -- so having someone else express interest in the idea is definitely good.

I think this should be possible, but needs some care to great care. The ijson functions actually already support generators as inputs, but those are assumed to be the lower-level generator functions of ijson itself (e.g., you can use ijson.parse() as the input to ijson.items, see the "Intercepting events" section of the README). It should still be possible to detect those separately from any other arbitrary generators and act differently though. After that it should all work, because funnily enough we internally turn file objects into generators!

def file_source(f, buf_size=64*1024):

The C backend might need some more extra care as well.

I can't promise anything in terms of deadlines. But like I said, I'm onboard with the idea, and if someone decides to step in and give it a crack in the meanwhile I'll be happy to review code and PRs.

from ijson.

rtobar avatar rtobar commented on August 23, 2024

For those coming in the future: see #58 (comment) for an (untested, personally) example of a simple file-like wrapper around a generator as a workaround.

from ijson.

MorningLightMountain713 avatar MorningLightMountain713 commented on August 23, 2024

For those coming in the future: see #58 (comment) for an (untested, personally) example of a simple file-like wrapper around a generator as a workaround.

Based on the above, here is what I'm using with httpx:

import httpx
import ijson
from contextlib import asynccontextmanager
from typing import AsyncIterator

class HttpxStreamAsFile:
    def __init__(self, url: str):
        self.url = url
        self.data = None
        self.client = httpx.AsyncClient()

    @asynccontextmanager
    async def create_stream(self) -> AsyncIterator:
        try:
            await self._create_stream()
            yield
        finally:
            await self.client.aclose()

    async def _create_stream(self) -> None:
        req = self.client.build_request("GET", self.url)
        res = await self.client.send(req, stream=True)
        self.data = res.aiter_bytes()

    async def read(self, n: int) -> None:
        if self.data is None or n == 0:
            return b""

        return await anext(self.data, b"")


async def main():
    url = "your-url"
    httpx_as_file = HttpxStreamAsFile(
        url
    )
    async with httpx_as_file.create_stream():
        async for prefix, event, name in ijson.items(httpx_as_file):
            print(prefix, event, name)

from ijson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.