8000 add an ISMN (International Standard Music Number) module · odony/python-stdnum@8bdb6ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bdb6ef

Browse files
committed
add an ISMN (International Standard Music Number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@33 9dea7c4f-944c-4273-ac1a-574ede026edc
1 parent 97d5280 commit 8bdb6ef

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Currently this package supports the following formats:
1111

1212
- ISBN (International Standard Book Number)
1313
- ISSN (International Standard Serial Number)
14+
- ISMN (International Standard Music Number)
1415
- BSN (Burgerservicenummer, the Dutch national identification number)
1516
- IMEI (International Mobile Equipment Identity)
1617
- MEID (Mobile Equipment Identifier)

stdnum/ismn.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# __init__.py - functions for handling ISMNs
2+
#
3+
# Copyright (C) 2010 Arthur de Jong
4+
#
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
#
10+
# This library is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with this library; if not, write to the Free Software
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
# 02110-1301 USA
19+
20+
"""Module for handling ISMNs (International Standard Music Number). This
21+
module handles both numbers in the 10-digit 13-digit format.
22+
23+
>>> is_valid('979-0-3452-4680-5')
24+
True
25+
>>> is_valid('9790060115615')
26+
True
27+
>>> is_valid(' M-2306-7118-7')
28+
True
29+
>>> is_valid('9790060115614') # incorrect check digit
30+
False
31+
>>> compact(' 979-0-3452-4680-5')
32+
'9790345246805'
33+
>>> format('9790060115615')
34+
'979-0-060-11561-5'
35+
>>> format('M230671187')
36+
'979-0-2306-7118-7'
37+
>>> to_ismn13('M230671187')
38+
'9790230671187'
39+
"""
40+
41+
42+
def compact(number):
43+
"""Convert the ISMN to the minimal representation. This strips the number
44+
of any valid ISMN separators and removes surrounding whitespace."""
45+
number = number.replace(' ' 10000 ,'').replace('-','').replace('.','').strip().upper()
46+
return number
47+
48+
def _calc_check_digit(number):
49+
"""Calculate the ISMN check digit. The number passed should not have
50+
the check bit included and should be in the 13-digit form."""
51+
return str((10 - sum( (2 * (i % 2) + 1) * int(number[i]) for i in range(len(number)))) % 10)
52+
53+
def is_valid(number):
54+
"""Checks to see if the number provided is a valid ISMN (either a legacy
55+
10-digit one or a 13-digit one). This checks the length and the check
56+
bit but does not check if the publisher is valid."""
57+
try:
58+
number = compact(number)
59+
except:
60+
return False
61+
if len(number) == 10 and number[0] == 'M' and number[1:].isdigit():
62+
if _calc_check_digit('9790' + number[1:-1]) == number[-1]:
63+
return True
64+
elif len(number) == 13 and number.isdigit():
65+
if _calc_check_digit(number[:-1]) == number[-1]:
66+
return True
67+
return False
68+
69+
def to_ismn13(number):
70+
"""Convert the number to ISMN13 format."""
71+
number = number.strip()
72+
min_number = compact(number)
73+
if len(min_number) == 13:
74+
return number # nothing to do, already 13 digit format
75+
# add prefix and strip the M
76+
if ' ' in number:
77+
return '979 0' + number[1:]
78+
elif '-' in number:
79+
return '979-0' + number[1:]
80+
else:
81+
return '9790' + number[1:]
82+
83+
# these are the ranges allocated to publisher codes
84+
_ranges = ( (3, '000', '099'), (4, '1000', '3999'), (5, '40000', '69999'),
85+
(6, '700000', '899999'), (7, '9000000', '9999999') )
86+
87+
def split(number):
88+
"""Split the specified ISMN into a bookland prefix (979), an ISMN
89+
prefix (0), a publisher element (3 to 7 digits), an item element (2 to
90+
6 digits) and a check digit."""
91+
# clean up number
92+
number = to_ismn13(compact(number))
93+
# rind the correct range and split the number
94+
for length, low, high in _ranges:
95+
if low <= number[4:4+length] <= high:
96+
return number[:3], number[3], number[4:4+length], number[4+length:-1], number[-1]
97+
98+
def format(number, separator='-'):
99+
"""Reformat the passed number to the standard format with the
100+
prefixes, the publisher element, the item element and the check-digit
101+
separated by the specified separator. The number is converted to the
102+
13-digit format silently."""
103+
return separator.join(x for x in split(number) if x)

tests/test_ismn.doctest

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
test_ismn.doctest - more detailed doctests for stdnum.ismn module
2+
3+
Copyright (C) 2010 Arthur de Jong
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
02110-1301 USA
19+
20+
21+
This file contains more detailed doctests for the stdnum.ismn module. It
22+
tries to test more corner cases and detailed functionality that is not
23+
really useful as module documentation.
24+
25+
>>> from stdnum import ismn
26+
27+
28+
These are normal variations that should just work.
29+
30+
>>> ismn.is_valid('979-0-3217-6543-6')
31+
True
32+
>>> ismn.is_valid(u'979-0-3217-6544-3')
33+
True
34+
>>> ismn.is_valid('9790321765450')
35+
True
36+
>>> ismn.is_valid('M-3217-6546-7')
37+
True
38+
>>> ismn.is_valid('M321765474')
39+
True
40+
>>> ismn.is_valid('979-0-260000438')
41+
True
42+
43+
44+
These are tests to check what happes when a wrong type is passed.
45+
46+
>>> ismn.is_valid(1857982185) # integer, not string
47+
False
48+
>>> ismn.is_valid(None)
49+
False
50+
>>> ismn.is_valid('')
51+
False
52+
53+
54+
Tests for mangling and incorect check digits.
55+
56+
>>> ismn.is_valid('979-0-3217-6543-x')
57+
False
58+
>>> ismn.is_valid('M-3217-6546-8')
59+
False
60+
>>> ismn.is_valid('979M321765450')
61+
False
62+
63+
64+
See if 10 to 13 digit conversion works.
65+
66+
>>> ismn.to_ismn13('979-0-32176544-3') # ismn13 should stay ismn13
67+
'979-0-32176544-3'
68+
>>> ismn.to_ismn13('M-32176546-7')
69+
'979-0-32176546-7'
70+
>>> ismn.to_ismn13('M 3217 65504')
71+
'979 0 3217 65504'
72+
73+
74+
Regrouping tests.
75+
76+
>>> ismn.format('M-3217-6546-7')
77+
'979-0-3217-6546-7'
78+
>>> ismn.format('9790321765450')
79+
'979-0-3217-6545-0'

0 commit comments

Comments
 (0)
0