7
7
8
8
import re
9
9
from datetime import datetime
10
- from typing import Dict , Iterable , List , NamedTuple , Optional , Union
10
+ from typing import Any , Dict , Iterable , List , Optional , Union
11
11
12
12
13
13
STRTIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
@@ -316,25 +316,144 @@ def __repr__(self):
316
316
)
317
317
318
318
319
- class Creator ( NamedTuple ) :
319
+ class Creator :
320
320
"""Metadata for person who created an object.
321
321
322
322
Parameters
323
323
----------
324
- given_name: str
325
- Optional. Default None.
326
- family_name: str
324
+ name: str
327
325
Optional. Default None.
328
326
email: str
329
327
Optional. Default None.
330
328
organisation: str
331
329
Optional. Default None.
332
330
"""
333
331
334
- given_name : Optional [str ] = None
335
- family_name : Optional [str ] = None
336
- email : Optional [str ] = None
337
- organisation : Optional [str ] = None
332
+ def __init__ (
333
+ self ,
334
+ name : Optional [str ] = None ,
335
+ email : Optional [str ] = None ,
336
+ organisation : Optional [str ] = None ,
337
+ given_name : Optional [str ] = None ,
338
+ family_name : Optional [str ] = None ,
339
+ ):
340
+ """Create an Creator metadata object.
341
+
342
+ The creator has optional name, email and organisation properties.
343
+ Separate given and family name values will be combined to a single
344
+ value for name. (This prevents confusion due to different cultural
345
+ conventions, see for example:
346
+ https://uxmovement.com/forms/why-your-form-only-needs-one-name-field/)
347
+
348
+ Parameters
349
+ ----------
350
+ name: str
351
+ Full name of the creator. Optional, default None.
352
+ email: str
353
+ Email address of the creator. Optional, default None.
354
+ organisation: str
355
+ Name of the organisation of the creator, or the organisation that
356
+ created the model. Optional, default None.
357
+
358
+ """
359
+ self ._name : Optional [str ] = None
360
+ self ._email : Optional [str ] = None
361
+ self ._organisation : Optional [str ] = None
362
+
363
+ self .name = Creator ._fix_name (
364
+ name = name , given_name = given_name , family_name = family_name
365
+ )
366
+ self .email = email
367
+ self .organisation = organisation
368
+
369
+ @staticmethod
370
+ def _fix_name (
371
+ name : Optional [str ] = None ,
372
+ given_name : Optional [str ] = None ,
373
+ family_name : Optional [str ] = None ,
374
+ ):
375
+ if name is not None and name != family_name :
376
+ if given_name is not None or family_name is not None :
377
+ raise ValueError (
378
+ """Too many name values were provided. Either a name or a
379
+ given and/or family name should be provided."""
380
+ )
381
+ return name
382
+ if given_name is not None and family_name is not None :
383
+ # This probably does not convert all names correctly.
384
+ # Names should however preferentially be represented as a single value.
385
+ return f"{ given_name } { family_name } "
386
+ elif given_name is not None :
387
+ return given_name
388
+ else :
389
+ # This also covers the case wher all values are None
390
+ return family_name
391
+
392
+ @property
393
+ def name (self ) -> Optional [str ]:
394
+ """Get the model creator name.
395
+
396
+ Returns
397
+ -------
398
+ str
399
+ Creator name.
400
+ """
401
+ return self ._name
402
+
403
+ @name .setter
404
+ def name (self , value : Optional [str ]) -> None :
405
+ """Set the name of the model creator.
406
+
407
+ Parameters
408
+ ----------
409
+ value: str
410
+ Name of the creator.
411
+ """
412
+ self ._name = value
413
+
414
+ @property
415
+ def email (self ) -> Optional [str ]:
416
+ """Get the email address of the model creator.
417
+
418
+ Returns
419
+ -------
420
+ str
421
+ Email address.
422
+ """
423
+ return self ._email
424
+
425
+ @email .setter
426
+ def email (self , value : Optional [str ]) -> None :
427
+ """Set the email of the model creator.
428
+
429
+ Parameters
430
+ ----------
431
+ value: str
432
+ Email address of the creator.
433
+ """
434
+ self ._email = value
435
+
436
+ @property
437
+ def organisation (self ) -> Optional [str ]:
438
+ """Get the organisation of the model creator.
439
+
440
+ Returns
441
+ -------
442
+ str
443
+ Organisation.
444
+ """
445
+ return self ._organisation
446
+
447
+ @organisation .setter
448
+ def organisation (self , value : Optional [str ]) -> None :
449
+ """Set the organisation of the model creator.
450
+
451
+ Parameters
452
+ ----------
453
+ value: str
454
+ Organisation of the model creator.
455
+ """
456
+ self ._organisation = value
338
457
339
458
@staticmethod
340
459
def from_data (data : Union [Dict , "Creator" ]) -> "Creator" :
@@ -358,15 +477,24 @@ def from_data(data: Union[Dict, "Creator"]) -> "Creator":
358
477
else :
359
478
raise TypeError (f"Invalid format for Creator: { data } " )
360
479
480
+ def _asdict (self ) -> Dict :
481
+ d = {}
482
+ if self .name is not None :
483
+ d ["name" ] = self .name
484
+ if self .email is not None :
485
+ d ["email" ] = self .email
486
+ if self .organisation is not None :
487
+ d ["organisation" ] = self .organisation
488
+ return d
489
+
361
490
def to_dict (self ) -> Dict :
362
491
"""Convert Creator to dictionary.
363
492
364
493
Returns
365
494
-------
366
495
dict in this format
367
496
{
368
- "given_name": str,
369
- "family_name": str,
497
+ "name": str,
370
498
"email": str,
371
499
"organisation": str,
372
500
}
@@ -392,6 +520,24 @@ def __repr__(self):
392
520
"""
393
521
return (
394
522
f"{ self .__class__ .__module__ } .{ self .__class__ .__qualname__ } "
395
- f"('{ self .given_name } ', ' { self . family_name } ', '{ self .email } ', "
523
+ f"('{ self .name } ', '{ self .email } ', "
396
524
f"'{ self .organisation } ')"
397
525
)
526
+
527
+ def __eq__ (self , other : Any ) -> bool :
528
+ """Determine whether the Creator is equal to another Creator object.
529
+
530
+ Parameters
531
+ ----------
532
+ other: Creator
533
+ Creator object to compare to.
534
+ """
535
+ if not isinstance (other , __class__ ):
536
+ return False
537
+ if self .name != other .name :
538
+ return False
539
+ if self .email != other .email :
540
+ return False
541
+ if self .organisation != other .organisation :
542
+ return False
543
+ return True
0 commit comments