Skip to content

Commit

Permalink
Merge pull request #157 from chinapnr/yijun_20181203
Browse files Browse the repository at this point in the history
Yijun 20181203
  • Loading branch information
itaa committed Dec 18, 2018
2 parents 4010a6a + d669c6b commit a159dd0
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
21 changes: 19 additions & 2 deletions demo/demo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

# id number false
id1 = '320124198701010012'
print(id1, is_valid_id_number(id1)[0])
print(id1, check_id_number(id1)[0])

# id number true
id2 = '130522198407316471'
print(id2, is_valid_id_number(id2)[0])
print(id2, check_id_number(id2)[0])

print('---')

Expand Down Expand Up @@ -91,3 +91,20 @@
print(result)

print('---')


# 2018.12.18 edit by David Yi
print('--- fish_data get_bankcard_checkcode demo ---')

# 不能放真的卡信息,有风险
print(get_bankcard_checkcode('439188000699010'))

print('---')

# 2018.12.18 edit by David Yi
print('--- fish_data check_bank_card demo ---')

# 不能放真的卡信息,有风险
print(check_bankcard('4391880006990100'))

print('---')
93 changes: 92 additions & 1 deletion fishbase/fish_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_idcard_checkcode(id_number_str):
# 2018.12.9 create by David Yi, add in v1.1.3, github issue #137
# 2018.12.13 edit, v1.1.4 github issue #145
# original source: https://zhuanlan.zhihu.com/p/24449773
def is_valid_id_number(id_number):
def check_id_number(id_number):
"""
检查身份证号码是否符合校验规则;
Expand Down Expand Up @@ -181,6 +181,8 @@ def get_zonecode_by_area(area_str, match_type='EXACT', result_type='LIST'):
result = get_zonecode_by_area(area_str='西安', match_type='FUZZY', result_type='SINGLE_STR')
print(result)
print('---')
输出结果::
--- fish_data get_zonecode_by_area demo ---
Expand Down Expand Up @@ -253,6 +255,7 @@ def get_cardbin_by_bank(bank, card_type):
result = get_cardbin_by_bank('CMB', 'DC')
print(result)
print('---')
输出结果::
Expand All @@ -277,3 +280,91 @@ def get_cardbin_by_bank(bank, card_type):
conn.close()

return values


# 计算银行卡校验位
# ---
# 2018.12.18 create by David Yi, v1.1.4, github issue #154
def get_bankcard_checkcode(card_number_str):
"""
计算银行卡校验位;
:param:
* card_number_str: (string) 要查询的银行卡号
:returns:
checkcode: (string) 银行卡的校验位
举例如下::
from fishbase.fish_data import *
print('--- fish_data get_bankcard_checkcode demo ---')
# 不能放真的卡信息,有风险
print(get_bankcard_checkcode('439188000699010'))
print('---')
输出结果::
--- fish_data get_bankcard_checkcode demo ---
9
---
"""
total = 0
even = True

for item in card_number_str[-1::-1]:
item = int(item)
if even:
item <<= 1
if item > 9:
item -= 9
total += item
even = not even

checkcode = (10 - (total % 10)) % 10

return str(checkcode)


# 检查银行卡校验位是否正确
# ---
# 2018.12.18 create by David Yi, v1.1.4, github issue #155
def check_bankcard(card_number_str):
"""
检查银行卡校验位是否正确;
:param:
* card_number_str: (string) 要查询的银行卡号
:returns:
返回结果:(bool) True or False
举例如下::
from fishbase.fish_data import *
print('--- fish_data check_bank_card demo ---')
# 不能放真的卡信息,有风险
print(check_bankcard('4391880006990100'))
print('---')
输出结果::
--- fish_data check_bank_card demo ---
False
---
"""

if isinstance(card_number_str, int):
card_number_str = str(card_number_str)

checkcode = card_number_str[-1]

result = get_bankcard_checkcode(card_number_str[0:-1])

return checkcode == result
29 changes: 24 additions & 5 deletions test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# 2018.12.10 v1.1.3 create by Jia ChunYing
# 2018.12.13 12.16 12.17v1.1.4 edit by David Yi
# 2018.12.13 12.16 12.17 12.18 v1.1.4 edit by David Yi
class TestData(object):

def test_get_idcard_checkcode(self):
Expand All @@ -23,19 +23,19 @@ def test_get_idcard_checkcode(self):
def test_is_valid_id_number(self):
# id number false
id1 = '320124198701010012'
assert is_valid_id_number(id1)[0] is False
assert check_id_number(id1)[0] is False

# id number true
id2 = '130522198407316471'
assert is_valid_id_number(id2)[0] is True
assert check_id_number(id2)[0] is True

# id number irregularity
id3 = '030522198407316471'
assert is_valid_id_number(id3)[0] is False
assert check_id_number(id3)[0] is False

# id number is int
id3 = 130522198407316471
assert is_valid_id_number(id3)[0] is True
assert check_id_number(id3)[0] is True

# 2018.12.16 edit by David Yi
def test_get_zonecode_by_area(self):
Expand Down Expand Up @@ -94,6 +94,25 @@ def test_get_cardbin_by_bank(self):
result = get_cardbin_by_bank('CMB', 'CC')
assert result == values

# 2018.12.18 edit by David Yi
def test_get_bankcard_checkcode(self):
# 测试校验码不正确
values = '0'
result = get_bankcard_checkcode('439188000699010')
assert result != values

# 测试校验码正确
values = '9'
result = get_bankcard_checkcode('439188000699010')
assert result == values

# 2018.12.18 edit by David Yi
def test_check_bankcard(self):
# 测试银行卡校验码是否正确
result = check_bankcard('4391880006990100')
assert result is False

result = check_bankcard('4391880006990109')
assert result is True


0 comments on commit a159dd0

Please sign in to comment.