Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Python
Commits
f06d49fd
Commit
f06d49fd
authored
4 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
New Python types hints
parent
4aa4946d
No related branches found
No related tags found
1 merge request
!1
First try of type hints
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
libs/lib/xaal/lib/bindings.py
+14
-15
14 additions, 15 deletions
libs/lib/xaal/lib/bindings.py
libs/lib/xaal/lib/devices.py
+43
-20
43 additions, 20 deletions
libs/lib/xaal/lib/devices.py
libs/lib/xaal/lib/types.py
+17
-0
17 additions, 0 deletions
libs/lib/xaal/lib/types.py
with
74 additions
and
35 deletions
libs/lib/xaal/lib/bindings.py
+
14
−
15
View file @
f06d49fd
...
...
@@ -2,13 +2,12 @@ import uuid
from
.exceptions
import
UUIDError
class
UUID
:
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
__uuid
=
uuid
.
UUID
(
*
args
,
**
kwargs
)
@staticmethod
def
random_base
(
digit
=
2
):
def
random_base
(
digit
=
2
)
->
'
UUID
'
:
"""
zeros the last digits of a random uuid, usefull w/ you want to forge some addresses
two digit is great.
"""
...
...
@@ -20,48 +19,48 @@ class UUID:
raise
UUIDError
@staticmethod
def
random
():
def
random
()
->
'
UUID
'
:
tmp
=
uuid
.
uuid1
().
int
return
UUID
(
int
=
tmp
)
def
__add__
(
self
,
value
)
:
def
__add__
(
self
,
value
:
int
)
->
'
UUID
'
:
tmp
=
self
.
__uuid
.
int
+
value
return
UUID
(
int
=
tmp
)
def
__sub__
(
self
,
value
)
:
def
__sub__
(
self
,
value
:
int
)
->
'
UUID
'
:
tmp
=
self
.
__uuid
.
int
-
value
return
UUID
(
int
=
tmp
)
def
__eq__
(
self
,
value
):
def
__eq__
(
self
,
value
)
->
bool
:
return
self
.
__uuid
==
value
def
__lt__
(
self
,
value
):
def
__lt__
(
self
,
value
)
->
bool
:
return
self
.
__uuid
.
int
<
value
def
__gt__
(
self
,
value
):
def
__gt__
(
self
,
value
)
->
bool
:
return
self
.
__uuid
.
int
>
value
def
__str__
(
self
):
def
__str__
(
self
)
->
str
:
return
str
(
self
.
__uuid
)
def
__repr__
(
self
):
# pragma: no cover
def
__repr__
(
self
)
->
str
:
# pragma: no cover
return
f
"
UUID(
'
{
self
.
__uuid
}
'
)
"
def
__hash__
(
self
):
def
__hash__
(
self
)
->
int
:
return
self
.
__uuid
.
__hash__
()
def
get
(
self
):
def
get
(
self
)
->
uuid
.
UUID
:
return
self
.
__uuid
def
set
(
self
,
value
):
def
set
(
self
,
value
:
uuid
.
UUID
):
self
.
__uuid
=
value
@property
def
str
(
self
):
def
str
(
self
)
->
str
:
return
str
(
self
)
@property
def
bytes
(
self
):
def
bytes
(
self
)
->
bytes
:
return
self
.
__uuid
.
bytes
...
...
This diff is collapsed.
Click to expand it.
libs/lib/xaal/lib/devices.py
+
43
−
20
View file @
f06d49fd
...
...
@@ -19,20 +19,24 @@
#
import
time
import
logging
from
tabulate
import
tabulate
from
typing
import
Optional
,
Union
,
Any
from
.
import
config
from
.
import
tools
from
.
import
bindings
from
.exceptions
import
DeviceError
from
.types
import
UUIDT
,
DeviceT
,
EngineT
,
AsyncEngineT
from
tabulate
import
tabulate
import
logging
logger
=
logging
.
getLogger
(
__name__
)
class
Attribute
(
object
):
def
__init__
(
self
,
name
,
dev
=
None
,
default
=
None
):
def
__init__
(
self
,
name
,
dev
:
Optional
[
DeviceT
]
=
None
,
default
:
Any
=
None
):
self
.
name
=
name
self
.
default
=
default
self
.
device
=
dev
...
...
@@ -76,15 +80,34 @@ class Attributes(list):
raise
KeyError
(
name
)
class
Device
(
object
):
__slots__
=
[
"
__dev_type
"
,
"
__address
"
,
"
group_id
"
,
"
vendor_id
"
,
"
product_id
"
,
"
hw_id
"
,
"
__version
"
,
"
__url
"
,
"
schema
"
,
"
info
"
,
"
unsupported_attributes
"
,
"
unsupported_methods
"
,
"
unsupported_notifications
"
,
"
alive_period
"
,
"
next_alive
"
,
"
__attributes
"
,
"
methods
"
,
"
engine
"
,
]
def
__init__
(
self
,
dev_type
:
str
,
addr
:
Optional
[
UUIDT
]
=
None
,
engine
:
Union
[
EngineT
,
AsyncEngineT
,
None
]
=
None
,
):
__slots__
=
[
'
__dev_type
'
,
'
__address
'
,
'
group_id
'
,
'
vendor_id
'
,
'
product_id
'
,
'
hw_id
'
,
'
__version
'
,
'
__url
'
,
'
schema
'
,
'
info
'
,
'
unsupported_attributes
'
,
'
unsupported_methods
'
,
'
unsupported_notifications
'
,
'
alive_period
'
,
'
next_alive
'
,
'
__attributes
'
,
'
methods
'
,
'
engine
'
]
def
__init__
(
self
,
dev_type
,
addr
=
None
,
engine
=
None
):
# xAAL internal attributes for a device
self
.
dev_type
=
dev_type
# xaal dev_type
self
.
address
=
addr
# xaal addr
...
...
@@ -92,8 +115,8 @@ class Device(object):
self
.
vendor_id
=
None
# vendor ID ie : ACME
self
.
product_id
=
None
# product ID
self
.
hw_id
=
None
# hardware info
self
.
__
version
=
None
# product release
self
.
__
url
=
None
# product URL
self
.
version
=
None
# product release
self
.
url
=
None
# product URL
self
.
schema
=
None
# schema URL
self
.
info
=
None
# additionnal info
...
...
@@ -111,21 +134,21 @@ class Device(object):
self
.
engine
=
engine
@property
def
dev_type
(
self
):
def
dev_type
(
self
)
->
str
:
return
self
.
__dev_type
@dev_type.setter
def
dev_type
(
self
,
value
):
def
dev_type
(
self
,
value
:
str
):
if
not
tools
.
is_valid_dev_type
(
value
):
raise
DeviceError
(
f
"
The dev_type
{
value
}
is not valid
"
)
self
.
__dev_type
=
value
@property
def
version
(
self
):
def
version
(
self
)
->
Optional
[
str
]
:
return
self
.
__version
@version.setter
def
version
(
self
,
value
):
def
version
(
self
,
value
:
Any
):
# version must be a string
if
value
:
self
.
__version
=
"
%s
"
%
value
...
...
@@ -137,7 +160,7 @@ class Device(object):
return
self
.
__address
@address.setter
def
address
(
self
,
value
):
def
address
(
self
,
value
:
Optional
[
UUIDT
]
):
if
value
is
None
:
self
.
__address
=
None
return
...
...
@@ -231,13 +254,13 @@ class Device(object):
print
(
tabulate
(
r
,
tablefmt
=
"
fancy_grid
"
))
def
__repr__
(
self
):
def
__repr__
(
self
)
->
str
:
return
f
"
<xaal.Device
{
id
(
self
)
:
x
}
{
self
.
address
}
{
self
.
dev_type
}
>
"
#####################################################
# default public methods
#####################################################
def
_get_description
(
self
):
def
_get_description
(
self
)
->
dict
:
result
=
{}
if
self
.
vendor_id
:
result
[
'
vendor_id
'
]
=
self
.
vendor_id
...
...
This diff is collapsed.
Click to expand it.
libs/lib/xaal/lib/types.py
0 → 100644
+
17
−
0
View file @
f06d49fd
# Python type hints for the xAAL library
from
typing
import
TYPE_CHECKING
,
TypeVar
if
TYPE_CHECKING
:
from
.devices
import
Device
,
Attributes
,
Attribute
from
.engine
import
Engine
from
.aioengine
import
AsyncEngine
from
.bindings
import
UUID
DeviceT
=
TypeVar
(
"
DeviceT
"
,
bound
=
"
Device
"
)
AttributeT
=
TypeVar
(
"
AttributeT
"
,
bound
=
"
Attribute
"
)
AttributesT
=
TypeVar
(
"
AttributesT
"
,
bound
=
"
Attributes
"
)
EngineT
=
TypeVar
(
"
EngineT
"
,
bound
=
"
Engine
"
)
AsyncEngineT
=
TypeVar
(
"
AsyncEngineT
"
,
bound
=
"
AsyncEngine
"
)
UUIDT
=
TypeVar
(
"
UUIDT
"
,
bound
=
"
UUID
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment