diff --git a/README.md b/README.md index 7de30fb..5304793 100644 --- a/README.md +++ b/README.md @@ -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 ``` \ No newline at end of file diff --git a/lfinancial/factory.py b/lfinancial/factory.py index 74afe97..0764150 100644 --- a/lfinancial/factory.py +++ b/lfinancial/factory.py @@ -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 @@ -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): diff --git a/lfinancial/financial.py b/lfinancial/financial.py index 679e6f9..bdeb5dd 100644 --- a/lfinancial/financial.py +++ b/lfinancial/financial.py @@ -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() @@ -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()) + diff --git a/lfinancial/generators/card.py b/lfinancial/generators/card.py index 29d531d..25e1b42 100644 --- a/lfinancial/generators/card.py +++ b/lfinancial/generators/card.py @@ -1,9 +1,54 @@ +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)]], + "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], @@ -35,6 +80,11 @@ class CardGenerator: "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 """ @@ -48,7 +98,16 @@ class CardGenerator: "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.IIN) + print(c.gen("CID")) diff --git a/setup.py b/setup.py index 0d1b9ac..1f14186 100644 --- a/setup.py +++ b/setup.py @@ -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,