더미 데이터를 생성하는 과정중에서 다음과 같은 문제점이 발생했다.
해당 문제점들을 각각 해결해보자..!
문제가 되는(?) 코드
for i in range(1, num + 1):
user_class = fake.set_user_in_order()
user_id = user_class.user_id
product_class = fake.set_product_in_order()
product_id = product_class.product_id
amount = fake.pyint(min_value=1, max_value=10)
product_price = product_class.price
totalPrice = amount * product_price
createdAt = fake.iso8601()
faker 패키지 까보기
def iso8601(
self,
tzinfo: Optional[TzInfo] = None,
end_datetime: Optional[DateParseType] = None,
sep: str = "T",
timespec: str = "auto",
) -> str:
"""
Get a timestamp in ISO 8601 format (or one of its profiles).
:param tzinfo: timezone, instance of datetime.tzinfo subclass
:param sep: separator between date and time, defaults to 'T'
:param timespec: format specifier for the time part, defaults to 'auto' - see datetime.isoformat() documentation
:example: '2003-10-21T16:05:52+0000'
"""
return self.date_time(tzinfo, end_datetime=end_datetime).isoformat(sep, timespec)
date_time 메서드 까보기
def date_time(
self,
tzinfo: Optional[TzInfo] = None,
end_datetime: Optional[DateParseType] = None,
) -> datetime:
"""
Get a datetime object for a date between January 1, 1970 and now
:param tzinfo: timezone, instance of datetime.tzinfo subclass
:example: datetime('2005-08-16 20:39:21')
:return: datetime
"""
# NOTE: On windows, the lowest value you can get from windows is 86400
# on the first day. Known python issue:
# https://bugs.python.org/issue30684
return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=self.unix_time(end_datetime=end_datetime))
date_time_between 사용법
def date_time_between(
self,
start_date: DateParseType = "-30y",
end_date: DateParseType = "now",
tzinfo: Optional[TzInfo] = None,
) -> datetime:
"""
Get a datetime object based on a random date between two given dates.
Accepts date strings that can be recognized by strtotime().
:param start_date: Defaults to 30 years ago
:param end_date: Defaults to "now"
:param tzinfo: timezone, instance of datetime.tzinfo subclass
:example: datetime('1999-02-02 11:42:52')
:return: datetime
"""
start_date = self._parse_date_time(start_date, tzinfo=tzinfo)
end_date = self._parse_date_time(end_date, tzinfo=tzinfo)
if end_date - start_date <= 1:
ts = start_date + self.generator.random.random()
else:
ts = self.generator.random.randint(start_date, end_date)
if tzinfo is None:
return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=ts)
else:
return (datetime(1970, 1, 1, tzinfo=tzutc()) + timedelta(seconds=ts)).astimezone(tzinfo)
fake.iso8601()
에서 fake.date_time_between(start_date="-2y").isoformat("T", "auto")
로 변경해줌으로써 해결!