本文整理汇总了Python中faker.providers.date_time.Provider类的典型用法代码示例。如果您正苦于以下问题:Python Provider类的具体用法?Python Provider怎么用?Python Provider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Provider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: firefox
def firefox(cls):
ver = (
'Gecko/{0} Firefox/{1}.0'.format(
DatetimeProvider.date_time_between(
datetime(2011, 1, 1)), random.randint(4, 15)),
'Gecko/{0} Firefox/3.6.{1}'.format(
DatetimeProvider.date_time_between(
datetime(2010, 1, 1)), random.randint(1, 20)),
'Gecko/{0} Firefox/3.8'.format(
DatetimeProvider.date_time_between(
datetime(2010, 1, 1)), ),
)
tmplt_win = '({0}; {1}; rv:1.9.{2}.20) {3}'
tmplt_lin = '({0}; rv:1.9.{1}.20) {2}'
tmplt_mac = '({0}; rv:1.9.{1}.20) {2}'
platforms = (
tmplt_win.format(cls.windows_platform_token(),
cls.random_element(cls.langs),
random.randint(0, 2),
random.choice(ver)),
tmplt_lin.format(cls.linux_platform_token(),
random.randint(5, 7),
random.choice(ver)),
tmplt_mac.format(cls.mac_platform_token(),
random.randint(2, 6),
random.choice(ver)),
)
return 'Mozilla/5.0 ' + cls.random_element(platforms)
示例2: test_nl_BE_ssn_valid
def test_nl_BE_ssn_valid(self):
from faker.providers.ssn.nl_BE import Provider
provider = Provider(None)
for i in range (1000):
ssn = provider.ssn()
self.assertEqual(len(ssn), 11)
gen_ssn_base = ssn[0:6]
gen_seq = ssn[6:9]
gen_chksum = ssn[9:11]
gen_ssn_base_as_int = int(gen_ssn_base)
gen_seq_as_int = int(gen_seq)
gen_chksum_as_int = int(gen_chksum)
# Check that the sequence nr is between 1 inclusive and 998 inclusive
self.assertGreater(gen_seq_as_int,0)
self.assertLessEqual(gen_seq_as_int, 998)
# validate checksum calculation
# Since the century is not part of ssn, try both below and above year 2000
ssn_below = int(ssn[0:9])
chksum_below = 97 - (ssn_below % 97)
ssn_above = ssn_below + 2000000000
chksum_above = 97 - (ssn_above % 97)
results = [ chksum_above, chksum_below ]
self.assertIn(gen_chksum_as_int,results)
示例3: test_no_words_sentence
def test_no_words_sentence(self):
from faker.providers.lorem import Provider
provider = Provider(None)
paragraph = provider.paragraph(0)
self.assertEqual(paragraph, '')
示例4: test_no_words_paragraph
def test_no_words_paragraph(self):
from faker.providers.lorem import Provider
provider = Provider(None)
sentence = provider.sentence(0)
self.assertEqual(sentence, '')
示例5: test_random_pyfloat
def test_random_pyfloat(self):
from faker.providers.python import Provider
provider = Provider(None)
self.assertTrue(0 <= abs(provider.pyfloat(left_digits=1)) < 10)
self.assertTrue(0 <= abs(provider.pyfloat(left_digits=0)) < 1)
x=abs(provider.pyfloat(right_digits=0))
self.assertTrue(x-int(x) == 0)
with self.assertRaises(ValueError,
msg='A float number cannot have 0 digits '
'in total'):
provider.pyfloat(left_digits=0, right_digits=0)
示例6: test_random_pystr_characters
def test_random_pystr_characters(self):
from faker.providers.python import Provider
provider = Provider(None)
characters = provider.pystr()
self.assertEqual(len(characters), 20)
characters = provider.pystr(max_chars=255)
self.assertEqual(len(characters), 255)
characters = provider.pystr(max_chars=0)
self.assertEqual(characters, '')
characters = provider.pystr(max_chars=-10)
self.assertEqual(characters, '')
示例7: test_us_ssn_valid
def test_us_ssn_valid(self):
from faker.providers.ssn.en_US import Provider
provider = Provider(None)
for i in range(1000):
ssn = provider.ssn()
self.assertEqual(len(ssn), 11)
self.assertNotEqual(ssn[0], '9')
self.assertNotEqual(ssn[0:3], '666')
self.assertNotEqual(ssn[0:3], '000')
self.assertNotEqual(ssn[4:6], '00')
self.assertNotEqual(ssn[7:11], '0000')
示例8: test_parse_date_time
def test_parse_date_time(self):
timestamp = DatetimeProvider._parse_date_time('+30d')
now = DatetimeProvider._parse_date_time('now')
assert timestamp > now
delta = timedelta(days=30)
from_delta = DatetimeProvider._parse_date_time(delta)
from_int = DatetimeProvider._parse_date_time(30)
assert datetime.fromtimestamp(from_delta).date() == (
datetime.fromtimestamp(timestamp).date())
assert datetime.fromtimestamp(from_int).date() == (
datetime.fromtimestamp(timestamp).date())
示例9: test_parse_date_time
def test_parse_date_time(self):
timestamp = DatetimeProvider._parse_date_time('+30d')
now = DatetimeProvider._parse_date_time('now')
self.assertTrue(timestamp > now)
delta = timedelta(days=-30)
from_delta = DatetimeProvider._parse_date_time(delta)
from_int = DatetimeProvider._parse_date_time(30)
self.assertEqual(datetime.fromtimestamp(from_delta).date(),
datetime.fromtimestamp(timestamp).date())
self.assertEqual(datetime.fromtimestamp(from_int).date(),
datetime.fromtimestamp(timestamp).date())
示例10: test_parse_timedelta
def test_parse_timedelta(self):
from faker.providers.date_time import Provider
td = timedelta(days=7)
seconds = Provider._parse_timedelta(td)
assert seconds == 604800.0
seconds = Provider._parse_timedelta('+1w')
assert seconds == 604800.0
seconds = Provider._parse_timedelta('+1y')
assert seconds == 31556736.0
with pytest.raises(ValueError):
Provider._parse_timedelta('foobar')
示例11: test_parse_timedelta
def test_parse_timedelta(self):
from faker.providers.date_time import Provider
td = timedelta(days=7)
seconds = Provider._parse_timedelta(td)
self.assertEqual(seconds, 604800.0)
seconds = Provider._parse_timedelta('+1w')
self.assertEqual(seconds, 604800.0)
seconds = Provider._parse_timedelta('+1y')
self.assertEqual(seconds, 31556736.0)
with self.assertRaises(ValueError):
Provider._parse_timedelta('foobar')
示例12: test_date_time_between_dates
def test_date_time_between_dates(self):
from faker.providers.date_time import Provider
import random as r
random = r.Random()
provider = Provider(None)
timestamp_start = random.randint(0, 10000000000)
timestamp_end = timestamp_start + 1
datetime_start = datetime.datetime.fromtimestamp(timestamp_start)
datetime_end = datetime.datetime.fromtimestamp(timestamp_end)
random_date = provider.date_time_between_dates(datetime_start, datetime_end)
self.assertTrue(datetime_start <= random_date)
self.assertTrue(datetime_end >= random_date)
示例13: test_language_code
def test_language_code(self):
from faker.providers.misc import Provider
for _ in range(99):
language_code = Provider.language_code()
self.assertTrue(isinstance(language_code, string_types))
Factory.create(locale=language_code)
示例14: ssn
def ssn(cls):
"""
Returns 11 character Polish national identity code (Public Electronic Census System,
Polish: Powszechny Elektroniczny System Ewidencji Ludności - PESEL).
It has the form YYMMDDZZZXQ, where YYMMDD is the date of birth (with century
encoded in month field), ZZZ is the personal identification number, X denotes sex
(even for females, odd for males) and Q is a parity number.
https://en.wikipedia.org/wiki/National_identification_number#Poland
"""
birth_date = DateTimeProvider.date_time()
year_without_century = int(birth_date.strftime('%y'))
month = calculate_month(birth_date)
day = int(birth_date.strftime('%d'))
pesel_digits = [
int(year_without_century / 10),
year_without_century % 10,
int(month / 10),
month % 10,
int(day / 10), day % 10
]
for _ in range(4):
pesel_digits.append(cls.random_digit())
pesel_digits.append(checksum(pesel_digits))
return ''.join(str(digit) for digit in pesel_digits)
示例15: test_password
def test_password(self):
from faker.providers.misc import Provider
def in_string(char, _str):
return char in _str
for _ in range(999):
password = Provider.password()
self.assertTrue(any([in_string(char, password) for char in "[email protected]#$%^&*()_+"]))
self.assertTrue(any([in_string(char, password) for char in string.digits]))
self.assertTrue(any([in_string(char, password) for char in string.ascii_uppercase]))
self.assertTrue(any([in_string(char, password) for char in string.ascii_lowercase]))
with self.assertRaises(AssertionError):
Provider.password(length=2)