Skip to content

Commit

Permalink
Merge pull request #48 from LemonLzy/feature/card
Browse files Browse the repository at this point in the history
Feature/card
  • Loading branch information
LemonLzy authored Jul 19, 2023
2 parents 49e71eb + 5594baf commit c48f870
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,25 @@ f.gender()

f.industry()
# Software & Services

f.CVD()
# 252

f.CVV()
# 748

f.CID()
# 207

f.CAV()
# 459

f.CVC()
# 767

f.CSC()
# 3473

f.CVN()
# 541
```
8 changes: 8 additions & 0 deletions lfinancial/factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from lfinancial.generators.bank import BankGenerator
from lfinancial.generators.card import CardGenerator
from lfinancial.generators.contry import CountryGenerator
from lfinancial.generators.currency import CurrencyGenerator
from lfinancial.generators.document import IDCodeGenerator
Expand Down Expand Up @@ -40,6 +41,13 @@ def __init__(self):
"product": StockGenerator(),
"gender": GenderGenerator(),
"industry": IndustryGenerator(),
"CSC": CardGenerator(),
"CID": CardGenerator(),
"CVC": CardGenerator(),
"CAV": CardGenerator(),
"CVV": CardGenerator(),
"CVD": CardGenerator(),
"CVN": CardGenerator(),
}

def register_generator(self, name, generator):
Expand Down
29 changes: 29 additions & 0 deletions lfinancial/financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ def gender(self, country=None):
def industry(self, country=None):
return self._generate("industry", country)

def CSC(self, country=None):
return self._generate("CSC", country)

def CID(self, country=None):
return self._generate("CID", country)

def CVC(self, country=None):
return self._generate("CVC", country)

def CAV(self, country=None):
return self._generate("CAV", country)

def CVV(self, country=None):
return self._generate("CVV", country)

def CVD(self, country=None):
return self._generate("CVD", country)

def CVN(self, country=None):
return self._generate("CVN", country)


if __name__ == '__main__':
f = Financial()
Expand Down Expand Up @@ -108,3 +129,11 @@ def industry(self, country=None):
print(f.product())
print(f.gender())
print(f.industry())
print(f.CVD())
print(f.CVV())
print(f.CID())
print(f.CAV())
print(f.CVC())
print(f.CSC())
print(f.CVN())

113 changes: 113 additions & 0 deletions lfinancial/generators/card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import random


class CardGenerator:

def __init__(self):
self.default_country = {
"card": "US",
"CSC": "US"
}

def gen(self, card_type, country=None):
if country is None:
country = self.default_country.get(card_type)

match card_type:
case "bankcard":
doc_type = BankCard(country)
case "CSC":
doc_type = CSC("CSC")
case "CID":
doc_type = CSC("CID")
case "CVC":
doc_type = CSC("CVC")
case "CAV":
doc_type = CSC("CAV")
case "CVV":
doc_type = CSC("CVV")
case "CVD":
doc_type = CSC("CVD")
case "CVN":
doc_type = CSC("CVN")
case _:
raise ValueError(f"Unsupported card type: {card_type}.")
return doc_type.gen()


class CardType:
def __init__(self, name):
self.name = name

def gen(self):
raise NotImplementedError("Subclasses must implement gen method.")


class BankCard(CardType):
"""
https://en.wikipedia.org/wiki/Payment_card_number
"""
IIN = {
"American Express": [5610] + [i for i in range(560221, 560225)],
"Bankcard": [5610, 560221 - 560225],
"China T-Union": [5610, 560221 - 560225],
"China UnionPay": [5610, 560221 - 560225],
"Diners Club International": [5610, 560221 - 560225],
"Diners Club United States & Canada": [5610, 560221 - 560225],
"Discover Card": [5610, 560221 - 560225],
"UkrCard": [5610, 560221 - 560225],
"RuPay": [5610, 560221 - 560225],
"InterPayment": [5610, 560221 - 560225],
"JCB": [5610, 560221 - 560225],
"Lasen": [5610, 560221 - 560225],
"Maestro UK": [5610, 560221 - 560225],
"Maestro": [5610, 560221 - 560225],
"Dankort": [5610, 560221 - 560225],
"Mir": [5610, 560221 - 560225],
"NPS Pridnestrovie": [5610, 560221 - 560225],
"Mastercard": [5610, 560221 - 560225],
"Solo": [5610, 560221 - 560225],
"Switch": [5610, 560221 - 560225],
"Troy": [5610, 560221 - 560225],
"Visa": [4, 5610, 560221 - 560225],
"Visa Electron": [5610, 560221 - 560225],
"UATP": [5610, 560221 - 560225],
"Verve": [5610, 560221 - 560225],
"LankaPay": [5610, 560221 - 560225],
"UzCard": [5610, 560221 - 560225],
"Humo": [5610, 560221 - 560225],
"GPN": [5610, 560221 - 560225],
"Napas": [5610, 560221 - 560225],
}

def gen(self):
return random.choice(self.IIN)


class CSC(CardType):
"""
https://zh.wikipedia.org/wiki/%E9%93%B6%E8%A1%8C%E5%8D%A1%E5%AE%89%E5%85%A8%E7%A0%81
"""
CSC = {
"CSC": 4,
"CID": 3,
"CVC": 3,
"CVV": 3,
"CAV": 3,
"CVD": 3,
"CVN": 3
}

def __init__(self, csc_name):
super().__init__(csc_name)

def gen(self):
length = self.CSC.get(self.name)
range_start = 10 ** (length - 1)
range_end = (10 ** length) - 1
return random.randint(range_start, range_end)


if __name__ == '__main__':
c = CardGenerator()
print(c.gen("CID"))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='lfinancial',
version='0.3.0',
version='0.3.1',
author='zaneliu',
description='Generate financial test data',
long_description=long_description,
Expand Down

0 comments on commit c48f870

Please sign in to comment.