WhyNotHugo / python-barcode

㊙️ Create standard barcodes with Python. No external dependencies. 100% Organic Python.
http://python-barcode.rtfd.io/
MIT License
558 stars 123 forks source link

Cleanup of barcode.codex._convert #234

Open maresb opened 2 months ago

maresb commented 2 months ago

Most invocations of _convert are guaranteed to not handle digits in charset C. Handling extra digits in charset C introduces extra complexity due to grouping of pairs of digits, which is handled by _buffer. (I rename _buffer to _digit_buffer for clarity.)

In order to isolate the complexity and ease type hinting, I restrict _convert to return only int and raise an informative exception whenever the buffer is triggered. Correspondingly I introduce a function _convert_or_buffer returning int | None to handle the full case when _digit_buffer might be used.

There is just one single place (_build) where _convert_or_buffer is necessary rather than _convert.

Reasons why _convert is safe to use in all other invocations:

This makes it clear why only the invocation in _build needs to handle the None return type. (IMO it's pretty tricky to deduce this if you're not already familiar with the implementation.)

maresb commented 2 months ago

I'd prefer merging this after #230, but either way works.

Rebasing after #230 will likely require resolving a trivial merge conflict.

maresb commented 1 month ago

@WhyNotHugo, I rebased to resolve the merge conflicts. Zero urgency on this.