38
38
39
39
40
40
class File :
41
+ _lines_of_code : int
42
+ _character_count : int
43
+
41
44
"""Represents a single file within a mystb.in paste.
42
45
43
46
Attributes
@@ -46,44 +49,42 @@ class File:
46
49
The file's name.
47
50
content: :class:`str`
48
51
The file's contents.
49
- lines_of_code: Optional[:class:`int`]
50
- The lines of code within the file.
51
- character_count: Optional[:class:`int`]
52
- The character count of the file.
53
-
54
-
55
- .. note::
56
- The ``lines_of_code`` and ``character_count`` come from the API and should not be provided by the user.
57
52
"""
58
53
59
54
__slots__ = (
60
55
"filename" ,
61
56
"content" ,
62
- "lines_of_code " ,
63
- "character_count " ,
57
+ "_lines_of_code " ,
58
+ "_character_count " ,
64
59
)
65
60
66
61
def __init__ (
67
62
self ,
68
63
* ,
69
64
filename : str ,
70
65
content : str ,
71
- lines_of_code : Optional [int ] = None ,
72
- character_count : Optional [int ] = None ,
73
66
) -> None :
74
67
self .filename : str = filename
75
68
self .content : str = content
76
- self .lines_of_code : int = lines_of_code or content .count ("\n " )
77
- self .character_count : int = character_count or len (content )
69
+
70
+ @property
71
+ def lines_of_code (self ) -> int :
72
+ return self ._lines_of_code
73
+
74
+ @property
75
+ def character_count (self ) -> int :
76
+ return self ._character_count
78
77
79
78
@classmethod
80
79
def _from_data (cls , payload : FileResponse , / ) -> Self :
81
- return cls (
80
+ self = cls (
82
81
content = payload ["content" ],
83
82
filename = payload ["filename" ],
84
- lines_of_code = payload ["loc" ],
85
- character_count = payload ["charcount" ],
86
83
)
84
+ self ._lines_of_code = payload ["loc" ]
85
+ self ._character_count = payload ["charcount" ]
86
+
87
+ return self
87
88
88
89
def _to_dict (self ) -> dict [str , Any ]:
89
90
ret : dict [str , Any ] = {"content" : self .content , "filename" : self .filename }
@@ -92,6 +93,10 @@ def _to_dict(self) -> dict[str, Any]:
92
93
93
94
94
95
class Paste :
96
+ _last_edited : Optional [datetime .datetime ]
97
+ _expires : Optional [datetime .datetime ]
98
+ _views : Optional [int ]
99
+
95
100
"""Represents a Paste object from mystb.in.
96
101
97
102
Attributes
@@ -100,47 +105,31 @@ class Paste:
100
105
The ID of this paste.
101
106
created_at: :class:`datetime.datetime`
102
107
When this paste was created in UTC.
103
- expires: Optional[:class:`datetime.datetime`]
104
- When this paste expires, if at all.
105
- last_edited: Optional[:class:`datetime.datetime`]
106
- When this paste was last edited, if at all.
107
108
files: list[:class:`mystbin.File`]
108
109
The list of files within this Paste.
109
- views: Optional[:class:`int`]
110
- How many views this paste has had, if any.
111
-
112
-
113
- .. note::
114
- The ``last_edited``, ``expires`` and ``views`` attributes come from the API and are not user provided.
115
110
"""
116
111
117
112
__slots__ = (
118
113
"id" ,
119
114
"author_id" ,
120
115
"created_at" ,
121
- "expires" ,
122
116
"files" ,
123
117
"notice" ,
124
- "views" ,
125
- "last_edited" ,
118
+ "_expires" ,
119
+ "_views" ,
120
+ "_last_edited" ,
126
121
)
127
122
128
123
def __init__ (
129
124
self ,
130
125
* ,
131
126
id : str ,
132
127
created_at : str ,
133
- expires : Optional [str ] = None ,
134
- last_edited : Optional [str ] = None ,
135
128
files : list [File ],
136
- views : Optional [int ] = None ,
137
129
) -> None :
138
130
self .id : str = id
139
131
self .created_at : datetime .datetime = datetime .datetime .fromisoformat (created_at )
140
- self .expires : Optional [datetime .datetime ] = datetime .datetime .fromisoformat (expires ) if expires else None
141
- self .last_edited : Optional [datetime .datetime ] = datetime .datetime .fromisoformat (last_edited ) if last_edited else None
142
132
self .files : list [File ] = files
143
- self .views : Optional [int ] = views
144
133
145
134
def __str__ (self ) -> str :
146
135
return self .url
@@ -152,14 +141,37 @@ def __repr__(self) -> str:
152
141
def url (self ) -> str :
153
142
return f"https://mystb.in/{ self .id } "
154
143
144
+ @property
145
+ def last_edited (self ) -> Optional [datetime .datetime ]:
146
+ return self ._last_edited
147
+
148
+ @property
149
+ def expires (self ) -> Optional [datetime .datetime ]:
150
+ return self ._expires
151
+
152
+ @property
153
+ def views (self ) -> Optional [int ]:
154
+ return self ._views
155
+
155
156
@classmethod
156
157
def _from_data (cls , payload : PasteResponse , / ) -> Self :
157
158
files = [File ._from_data (data ) for data in payload ["files" ]]
158
- return cls (
159
+ self = cls (
159
160
id = payload ["id" ],
160
161
created_at = payload ["created_at" ],
161
- expires = payload ["expires" ],
162
162
files = files ,
163
- views = payload .get ("views" ),
164
- last_edited = payload .get ("last_edited" ),
165
163
)
164
+ self ._views = payload .get ("views" )
165
+ last_edited = payload .get ("last_edited" )
166
+ if last_edited :
167
+ self ._last_edited = datetime .datetime .fromisoformat (last_edited )
168
+ else :
169
+ self ._last_edited = None
170
+
171
+ expires = payload .get ("expires" )
172
+ if expires :
173
+ self ._expires = datetime .datetime .fromisoformat (expires )
174
+ else :
175
+ self ._expires = None
176
+
177
+ return self
0 commit comments