2323from datetime import datetime
2424from typing import (
2525 Sequence ,
26- overload ,
2726 Any ,
2827 Union ,
2928 List ,
@@ -186,15 +185,24 @@ class Entity:
186185 >>> e.rm("NO2.accuracy")
187186 """
188187
189- @overload
190- def __init__ (self , type : str , id : str , * , ctx : List = None ):
188+ def __init__ (
189+ self ,
190+ * args : str ,
191+ ctx : List = None ,
192+ payload : dict = None ,
193+ autoprefix : Optional [bool ] = None ,
194+ ):
191195 """Create a NGSI-LD compliant entity
192196
193- One can omit the urn and namespace, "urn:ngsi-ld:" will be added automatically.
194- One can omit the type inside the identifier.
197+ Expected args are : the Entity's type and identifier.
198+ Example : Entity("AirQualityObserved", "RZ:Obsv4567")
199+ The fully qualified identifier is built following the naming convention : "urn:ngsi-ld:{type}:{id}'.
200+ "urn:ngsi-ld:" is added if not specified.
201+
202+ Alernatively a single arg is allowed : the fully qualified Entity's identifier.
203+ Example : Entity("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567")
204+ Type is inferred from the fully qualified identifier.
195205
196- By default, the constructor assumes the identifier naming convention "urn:ngsi-ld:<type>:<remainder>" and
197- automatically insert the type into the identifier.
198206 The default behaviour can be disabled : Entity.globalsettings.autoprefix = False.
199207
200208
@@ -224,60 +232,16 @@ def __init__(self, type: str, id: str, *, ctx: List = None):
224232 "type": "AirQualityObserved"
225233 }
226234 """
227- ...
228-
229- @overload
230- def __init__ (self , id : str , * , ctx : List = None ):
231- """Create a NGSI-LD compliant entity.
232-
233- Type is inferred from the fully qualified identifier.
234- Works only if your identifiers follow the naming convention "urn:ngsi-ld:<type>:<remainder>"
235-
236- Parameters
237- ----------
238- id : str
239- entity identifier (fully qualified urn)
240- context : list, optional
241- the NGSI-LD context, by default the NGSI-LD Core Context
242-
243- Example
244- -------
245- >>> from ngsildclient.model.entity import Entity
246- >>> e = Entity("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567")
247- >>> e.pprint()
248- {
249- "@context": [
250- "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
251- ],
252- "id": "urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567",
253- "type": "AirQualityObserved"
254- }
255- """
256- ...
257-
258- def __init__ (
259- self ,
260- arg1 : str = None ,
261- arg2 : str = None ,
262- * ,
263- ctx : List = None ,
264- payload : dict = None ,
265- autoprefix : Optional [bool ] = None ,
266- ):
267- logger .debug (f"{ arg1 = } { arg2 = } " )
268-
269- if not ctx :
270- ctx = [CORE_CONTEXT ]
271-
272- if autoprefix is None :
273- autoprefix = globalsettings .autoprefix
274-
275235 self .root : NgsiDict = None
276236 self ._lastprop : NgsiDict = None
277237 self ._anchored : bool = False
278238 self ._lastwasmulti : bool = False
279239
280- if payload is not None : # create Entity from a dictionary
240+ if len (args ) == 2 :
241+ type , id = args
242+ elif len (args ) == 1 :
243+ id , type = args [0 ], None
244+ elif len (args ) == 0 and payload is not None : # create Entity from a dictionary
281245 if not payload .get ("id" , None ):
282246 raise NgsiMissingIdError ()
283247 if not payload .get ("type" , None ):
@@ -286,14 +250,15 @@ def __init__(
286250 raise NgsiMissingContextError ()
287251 self ._lastprop = self .root = NgsiDict (payload )
288252 return
289-
290- # create a new Entity using its id and type
291-
292- if arg2 :
293- type , id = arg1 , arg2
294253 else :
295- type , id = None , arg1
254+ raise ValueError ( "Expecteds args : type, id (alt. fully qualified id)" )
296255
256+ if autoprefix is None :
257+ autoprefix = globalsettings .autoprefix
258+ if not ctx :
259+ ctx = [CORE_CONTEXT ]
260+
261+ # create a new Entity using its id and type
297262 if type is None : # try to infer type from the fully qualified identifier
298263 id = Urn .prefix (id )
299264 urn = Urn (id )
0 commit comments