-3

What field type would you use in a dataclass where the value provided is in a time format?

The JSON file I'm parsing returns a value like this: 2.00:00:15.0830000

In a class like this below, what type should I set?

from dataclasses import dataclass

@dataclass
class Values:
    duration: ???

date type seems not to be on the list.

The reason I require a non-str here is to calculate if the value is older than x days/hours then do something.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
elcade
  • 1
  • 3
  • 1
    If you want a non-`str` you should _parse_ it, use e.g. `datetime`. – jonrsharpe Jan 30 '23 at 19:48
  • `dataclass` doesn't convert things for you. It declares what they already are. Passing a string value to a non-`str` dataclass field doesn't do any actual work. It just lies to you and then lets things fail awkwardly later on. – Silvio Mayolo Jan 30 '23 at 19:50

1 Answers1

0

You can save it as a str (string), and then have a member function to convert it to decimal hours, mins, etc. In that case, maybe a dataclass is not appropriate (dataclasses tend to only hold data).

Alternatively, you may want to explore the package time, with its own datetime class and member functions.

JustLearning
  • 1,435
  • 1
  • 1
  • 9