Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

python - Using a dataclass to read a text file setting an attribute to be a datetime object

I'm trying to force myself to get into the habit of using dataclasses. I'm reading a text file with rows of data, two of the columns contain a date. When I read the file into the class, why is the date not being converted to a datetime object?

import datetime
from dataclasses import dataclass

@dataclass
class AC07:
    NAME: str
    MADE_FROM_DATE: datetime.datetime
    MADE_UPTO_DATE: datetime.datetime
    
counter = 0
deck = []
with open(filename, encoding="latin-1") as file:
    for line in file:
        args = line.strip().split("|")[1:]  # not interested in first column
        data = AC07(*args)
        deck.append(data)
        counter += 1
        if counter == 10:
            break
        
print (type(deck[0].MADE_FROM_DATE))
<class 'str'>
# Expected behavior <class datetime.datetime> 
question from:https://stackoverflow.com/questions/65917205/using-a-dataclass-to-read-a-text-file-setting-an-attribute-to-be-a-datetime-obje

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please someone correct me if am wrong, but I believe dataclasses does not force type. datetime.datetime in your example is (almost) merely an annotation, apart from two implementation details. Check the documentation. Specifically,

The dataclass() decorator examines the class to find fields. A field is defined as class variable that has a type annotation. With two exceptions described below, nothing in dataclass() examines the type specified in the variable annotation.

The two exceptions have nothing to do with forcing data types:

Class variables One of two places where dataclass() actually inspects the type of a field is to determine if a field is a class variable as defined in PEP 526. It does this by checking if the type of the field is typing.ClassVar. If a field is a ClassVar, it is excluded from consideration as a field and is ignored by the dataclass mechanisms. Such ClassVar pseudo-fields are not returned by the module-level fields() function.

Init-only variables The other place where dataclass() inspects a type annotation is to determine if a field is an init-only variable. It does this by seeing if the type of a field is of type dataclasses.InitVar. If a field is an InitVar, it is considered a pseudo-field called an init-only field. As it is not a true field, it is not returned by the module-level fields() function. Init-only fields are added as parameters to the generated __init__() method, and are passed to the optional __post_init__() method. They are not otherwise used by dataclasses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...