@@ -9,8 +9,13 @@ def _keep(a: T, b: T) -> T:
99 return a if a is not None else b
1010
1111
12+ def _keep_longest (a : str , b : str ) -> str :
13+ return a if len (a ) > len (b ) else b
14+
15+
1216@dataclass
1317class Article :
18+ label : str
1419 ids : set [str ]
1520 authors : list [str ] = field (default_factory = list )
1621 year : Optional [int ] = None
@@ -20,7 +25,6 @@ class Article:
2025 issue : Optional [str ] = None
2126 page : Optional [str ] = None
2227 doi : Optional [str ] = None
23- _label : Optional [str ] = None
2428 _permalink : Optional [str ] = None
2529 times_cited : Optional [int ] = None
2630 references : list ["Article" ] = field (default_factory = list )
@@ -31,6 +35,7 @@ class Article:
3135 def merge (self , other : "Article" ) -> "Article" :
3236 """Merge two articles into a new one."""
3337 return Article (
38+ label = _keep_longest (self .label , other .label ),
3439 ids = self .ids .union (other .ids ),
3540 authors = self .authors if self .authors else other .authors ,
3641 year = _keep (self .year , other .year ),
@@ -40,7 +45,6 @@ def merge(self, other: "Article") -> "Article":
4045 issue = _keep (self .issue , other .issue ),
4146 page = _keep (self .page , other .page ),
4247 doi = _keep (self .doi , other .doi ),
43- _label = _keep (self ._label , other ._label ),
4448 _permalink = _keep (self ._permalink , other ._permalink ),
4549 times_cited = _keep (self .times_cited , other .times_cited ),
4650 references = self .references or other .references ,
@@ -54,17 +58,17 @@ def key(self) -> str:
5458 return next (iter (sorted (self .ids )))
5559
5660 @property
57- def label (self ) -> str :
58- if self ._label is not None :
59- return self ._label
61+ def simple_label (self ) -> Optional [str ]:
6062 pieces = {
61- "AU" : self .authors [0 ].replace ("," , "" ) if self .authors else "anonymous" ,
63+ "AU" : self .authors [0 ].replace ("," , "" ) if self .authors else None ,
6264 "PY" : str (self .year ) if self .year else None ,
6365 "J9" : str (self .journal ) if self .journal else None ,
6466 "VL" : f"V{ self .volume } " if self .volume else None ,
6567 "BP" : f"P{ self .page } " if self .page else None ,
6668 "DI" : f"DOI { self .doi } " if self .doi else None ,
6769 }
70+ if not any (pieces .values ()):
71+ return None
6872 return ", " .join (value for value in pieces .values () if value )
6973
7074 @property
@@ -85,10 +89,17 @@ def simple_id(self) -> Optional[str]:
8589 def __repr__ (self ) -> str :
8690 return f"Article(ids={ self .ids !r} , authors={ self .authors !r} )"
8791
88- def add_simple_id (self ) -> None :
92+ def add_simple_id (self ) -> "Article" :
8993 if self .simple_id is None :
90- return
94+ return self
9195 self .ids .add (f"simple:{ self .simple_id } " )
96+ return self
97+
98+ def set_simple_label (self ) -> "Article" :
99+ if self .simple_label is None :
100+ return self
101+ self .label = self .simple_label
102+ return self
92103
93104 def info (
94105 self ,
0 commit comments