Skip to content

Commit

Permalink
Annotating README's sample code as Python
Browse files Browse the repository at this point in the history
  • Loading branch information
benhodgson committed Jan 12, 2013
1 parent 0dd60c9 commit 14be263
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions README.md
Expand Up @@ -25,16 +25,18 @@ the given base. This character should be appended to the input string to
produce a valid Luhn string. `verify` tests whether or not a string is a valid
Luhn string in the given base. By default, Baluhn operates in base 10:

>>> from baluhn import generate, verify
>>> verify('5105105105105100') # MasterCard test number
True
>>> value = '510510510510510' # note the missing check digit
>>> generate(value)
'0'
>>> verify(value + '0')
True
>>> verify(value + '7')
False
```python
>>> from baluhn import generate, verify
>>> verify('5105105105105100') # MasterCard test number
True
>>> value = '510510510510510' # note the missing check digit
>>> generate(value)
'0'
>>> verify(value + '0')
True
>>> verify(value + '7')
False
```

When operating in a base other than decimal, encoder and decoder callables
should be supplied. The encoder should take a single argument, an integer, and
Expand All @@ -44,17 +46,19 @@ return its integer value in the operating base. Note that the mapping between
values and characters defined by the encoder and decoder should be one-to-one.

For example, when working in hexadecimal:

>>> hex_alphabet = '0123456789abcdef'
>>> hex_encoder = lambda i: hex_alphabet[i]
>>> hex_decoder = lambda s: hex_alphabet.index(s)
>>> value = 'a8b56f'
>>> generate(value, base=16, encoder=hex_encoder, decoder=hex_decoder)
'b'
>>> verify('a8b56fb', base=16, decoder=hex_decoder)
True
>>> verify('a8b56fc', base=16, decoder=hex_decoder)
False

```python
>>> hex_alphabet = '0123456789abcdef'
>>> hex_encoder = lambda i: hex_alphabet[i]
>>> hex_decoder = lambda s: hex_alphabet.index(s)
>>> value = 'a8b56f'
>>> generate(value, base=16, encoder=hex_encoder, decoder=hex_decoder)
'b'
>>> verify('a8b56fb', base=16, decoder=hex_decoder)
True
>>> verify('a8b56fc', base=16, decoder=hex_decoder)
False
```

## Author

Expand Down Expand Up @@ -82,4 +86,4 @@ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
For more information, please refer to <http://unlicense.org/>

0 comments on commit 14be263

Please sign in to comment.