Skip to content

Commit

Permalink
Restore python2.7 (#17)
Browse files Browse the repository at this point in the history
* Restore python 2.7 compatibility

* Add test for findSubclassesRecursively

* Fix types for auto step time
  • Loading branch information
thomas-phillips-nz authored Sep 29, 2023
1 parent 954ec36 commit 2211727
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
14 changes: 8 additions & 6 deletions tenma/tenmaDcLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def findSubclassesRecursively(cls):
"""
Finds all subclasses of a given class recursively
"""
all_subclasses = []
for subclass in cls.__subclasses__():
yield from findSubclassesRecursively(subclass)
yield subclass
all_subclasses.append(subclass)
all_subclasses.extend(findSubclassesRecursively(subclass))
return all_subclasses


class TenmaSerialHandler(object):
Expand Down Expand Up @@ -617,7 +619,7 @@ def startAutoVoltageStep(self, channel, startMillivolts,
:param stepMillivolts: Amount to increase voltage by in mV
:type stepMillivolts: int
:param stepTime: Time to wait before each increase, in Seconds
:type stepTime: int
:type stepTime: float
:raises NotImplementedError Not implemented in this base class
"""
raise NotImplementedError("Not supported by all models")
Expand Down Expand Up @@ -647,7 +649,7 @@ def startAutoCurrentStep(self, channel, startMilliamps,
:param stepMilliamps: Amount to increase current by in mA
:type stepMilliamps: int
:param stepTime: Time to wait before each increase, in Seconds
:type stepTime: int
:type stepTime: float
:raises NotImplementedError Not implemented in this base class
"""
raise NotImplementedError("Not supported by all models")
Expand Down Expand Up @@ -1034,7 +1036,7 @@ def startAutoVoltageStep(self, channel, startMillivolts,
:param stepMillivolts: Amount to increase voltage by in mV
:type stepMillivolts: int
:param stepTime: Time to wait before each increase, in Seconds
:type stepTime: int
:type stepTime: float
:raises TenmaException: If the channel or voltage is invalid
"""
self.checkChannel(channel)
Expand Down Expand Up @@ -1086,7 +1088,7 @@ def startAutoCurrentStep(self, channel, startMilliamps,
:param stepMilliamps: Amount to increase current by in mA
:type stepMilliamps: int
:param stepTime: Time to wait before each increase, in Seconds
:type stepTime: int
:type stepTime: float
:raises TenmaException: If the channel or current is invalid
"""
self.checkChannel(channel)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dclib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from tenma.tenmaDcLib import findSubclassesRecursively

class Base(object):
MATCH_STR = ['']
pass

class Test1(Base):
MATCH_STR = ['Test 1']

class Test2(Base):
MATCH_STR = ['Test 2', 'Test 2.5']

class Test3(Test2):
MATCH_STR = ['Test 3']

def test_findSubclassesRecursively():
expected_classes = [['Test 1'], ['Test 2', 'Test 2.5'], ['Test 3']]
actual_classes = []
for cls in findSubclassesRecursively(Base):
actual_classes.append(cls.MATCH_STR)
actual_classes.sort()
assert actual_classes == expected_classes
6 changes: 3 additions & 3 deletions tests/test_tenma.py → tests/test_psu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def tenma_psu():
psu = instantiate_tenma_class_from_device_response(port, debug=True)
else:
if sys.platform.startswith('win'):
ports = [f'COM{i}' for i in range(1, 256)]
ports = ['COM%s' % i for i in range(1, 256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
Expand Down Expand Up @@ -85,8 +85,8 @@ def test_psu_memory(tenma_psu):
tenma_psu.saveConfFlow(slot, 1)
# recall memory and assert voltages
tenma_psu.recallConf(slot)
assert tenma_psu.readVoltage(1) == slot * tenma_psu.MAX_MV / 5 / 1000
assert tenma_psu.readCurrent(1) == slot * tenma_psu.MAX_MA / 5 / 1000
assert tenma_psu.readVoltage(1) == slot * tenma_psu.MAX_MV / 5.0 / 1000.0
assert tenma_psu.readCurrent(1) == slot * tenma_psu.MAX_MA / 5.0 / 1000.0
# test invalid channels
with pytest.raises(TenmaException):
tenma_psu.saveConfFlow(1, -1)
Expand Down

0 comments on commit 2211727

Please sign in to comment.