runtime
297 / 500
question 297 Packaging & Databases · medium

Expand ~= and wildcard specifiers

A simple version checker only understands >=, <, == and != with plain versions. Write expand_spec(spec) to rewrite the two shorthand forms into conditions it understands. ~=X.Y (compatible release) means >=X.Y and below the next release of the part before the last: ~=2.2 → >=2.2,<3, ~=1.4.5 → >=1.4.5,<1.5. ==X.Y.* means any version starting X.Y: >=X.Y,<X.(Y+1).

Functionexpand_spec(spec)
specconditions joined by commas, with or without spaces, e.g. "~=1.4.5, !=1.4.7"
Returnsthe conditions joined by , with no spaces, shorthand expanded in place, others unchanged; "" for ""
RaisesValueError for ~= with a one-part version (~=2), which PEP 440 does not allow
Examples
callexpand_spec('~=2.2')returns'>=2.2,<3'
callexpand_spec('~=1.4.5, !=1.4.7')returns'>=1.4.5,<1.5,!=1.4.7'
callexpand_spec('==3.1.*')returns'>=3.1,<3.2'

Loading your progress…

Expand ~= and wildcard specifiers — Python practice · Runtime