Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Python clumsy

    Scheduled Pinned Locked Moved MicroPython
    2 Posts 2 Posters 81 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      kjm
      last edited by kjm

      I'm down loading a compressed file into a variable hexstr on my gpy which I then write to flash as gzip.py so I can extract it with

      with open('gzip.py', 'r') as f: byts=uzlib.DecompIO(f, 31); f=open('main.py', 'w'); f.write(byts.read().decode())
      

      then put it back into flash as main.py. But it feels clumsy. Surely there is a way I can extract it from hexstr directly without having to save it to gzip.py first? I tried stuff like zlib.DecompIO(hexstr, 31) and zlib.DecompIO(hexstr.read(), 31) but wild stabs in the dark never work. I think I need a StringIO or something in there to change the string into an object the extractor can use?

      1 Reply Last reply Reply Quote 0
      • J Offline
        James Milllere
        last edited by James Milllere

        Hi,
        You can indeed achieve this without saving it as an intermediate file using io.BytesIO.
        Here's how you can do it:

        import uzlib # Assuming you have the uzlib library for MicroPython

        # Assuming 'hexstr' contains your compressed data
        # You may need to convert 'hexstr' to bytes if it's currently a string
        
        # Create a BytesIO object to work with in-memory data
        from io import BytesIO
        hexstr_bytes = bytes.fromhex(hexstr)  # Convert 'hexstr' to bytes
        hexstr_io = BytesIO(hexstr_bytes)
        
        # Decompress the data
        decompressed_bytes = uzlib.decomp(hexstr_io, 31)
        
        # Now, you can write the decompressed data to 'main.py' in the flash
        with open('main.py', 'wb') as f:
            f.write(decompressed_bytes)
        

        In this code, you can use BytesIO to create an in-memory buffer and then decompress the data from hexstr directly into that buffer. After decompression, you can write the decompressed data to 'main.py' without the need for an intermediate 'gzip.py' file. Resource- https://pypi.org/project/Clumsy/

        Thanks

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom