multiformats / multiformats/py-multihash

No `NewReader()`/`NewWriter()` stream wrapper classes

Open
#57 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17
Forks
17
Avg merge
1h 5m
Merged PRs (30d)
3

Description

Go provides NewReader(r io.Reader) Reader and NewWriter(w io.Writer) Writer wrapper classes with ReadMultihash() and WriteMultihash() methods. Python has Multihash.read()/write() static/instance methods but no dedicated wrapper classes.

Problem

Go's io.go:

type Reader interface {
    io.Reader
    ReadMultihash() (Multihash, error)
}

type Writer interface {
    io.Writer
    WriteMultihash(Multihash) error
}

func NewReader(r io.Reader) Reader { return &mhReader{r} }
func NewWriter(w io.Writer) Writer { return &mhWriter{w} }

The mhReader.ReadMultihash() method reads varints directly from the stream (not buffering the entire input), handles io.ByteReader for efficiency, and validates the result with Cast().

Python's Multihash.read() is a classmethod that works similarly but there's no MultihashReader class that wraps a stream and provides a clean interface for reading multiple multihashes.

Proposed Solution

Add wrapper classes:

class MultihashReader:
    """Wraps a binary stream with ReadMultihash() capability."""

    def __init__(self, stream: BinaryIO):
        self._stream = stream

    def read(self, buf: bytearray) -> int:
        """Standard read interface."""
        return self._stream.readinto(buf)

    def read_multihash(self) -> Multihash:
        """Read one multihash from the stream."""
        return Multihash.read(self._stream)


class MultihashWriter:
    """Wraps a binary stream with WriteMultihash() capability."""

    def __init__(self, stream: BinaryIO):
        self._stream = stream

    def write(self, buf: bytes) -> int:
        """Standard write interface."""
        return self._stream.write(buf)

    def write_multihash(self, mh: Multihash) -> int:
        """Write one multihash to the stream."""
        return mh.write(self._stream)

Export from __init__.py and add tests.

Related

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing Multihash.read() and write() implementations, then inspect init.py to understand public exports. Add the stream wrapper classes and tests for reading and writing multiple multihashes, including the standard stream methods; done means the classes are importable and the tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.