Skip to content

Commit 4f54042

Browse files
ensonicddemidov
authored andcommitted
Use constants instead of repeating the strings. (#242)
Fixes #65.
1 parent ea66472 commit 4f54042

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

ev3dev/core.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,15 @@ def run_forever(self, **kwargs):
750750
"""
751751
for key in kwargs:
752752
setattr(self, key, kwargs[key])
753-
self.command = 'run-forever'
753+
self.command = self.COMMAND_RUN_FOREVER
754754

755755
def run_to_abs_pos(self, **kwargs):
756756
"""Run to an absolute position specified by `position_sp` and then
757757
stop using the action specified in `stop_action`.
758758
"""
759759
for key in kwargs:
760760
setattr(self, key, kwargs[key])
761-
self.command = 'run-to-abs-pos'
761+
self.command = self.COMMAND_RUN_TO_ABS_POS
762762

763763
def run_to_rel_pos(self, **kwargs):
764764
"""Run to a position relative to the current `position` value.
@@ -768,15 +768,15 @@ def run_to_rel_pos(self, **kwargs):
768768
"""
769769
for key in kwargs:
770770
setattr(self, key, kwargs[key])
771-
self.command = 'run-to-rel-pos'
771+
self.command = self.COMMAND_RUN_TO_REL_POS
772772

773773
def run_timed(self, **kwargs):
774774
"""Run the motor for the amount of time specified in `time_sp`
775775
and then stop the motor using the action specified by `stop_action`.
776776
"""
777777
for key in kwargs:
778778
setattr(self, key, kwargs[key])
779-
self.command = 'run-timed'
779+
self.command = self.COMMAND_RUN_TIMED
780780

781781
def run_direct(self, **kwargs):
782782
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
@@ -785,23 +785,23 @@ def run_direct(self, **kwargs):
785785
"""
786786
for key in kwargs:
787787
setattr(self, key, kwargs[key])
788-
self.command = 'run-direct'
788+
self.command = self.COMMAND_RUN_DIRECT
789789

790790
def stop(self, **kwargs):
791791
"""Stop any of the run commands before they are complete using the
792792
action specified by `stop_action`.
793793
"""
794794
for key in kwargs:
795795
setattr(self, key, kwargs[key])
796-
self.command = 'stop'
796+
self.command = self.COMMAND_STOP
797797

798798
def reset(self, **kwargs):
799799
"""Reset all of the motor parameter attributes to their default value.
800800
This will also have the effect of stopping the motor.
801801
"""
802802
for key in kwargs:
803803
setattr(self, key, kwargs[key])
804-
self.command = 'reset'
804+
self.command = self.COMMAND_RESET
805805

806806

807807
# ~autogen
@@ -1123,15 +1123,15 @@ def run_forever(self, **kwargs):
11231123
"""
11241124
for key in kwargs:
11251125
setattr(self, key, kwargs[key])
1126-
self.command = 'run-forever'
1126+
self.command = self.COMMAND_RUN_FOREVER
11271127

11281128
def run_timed(self, **kwargs):
11291129
"""Run the motor for the amount of time specified in `time_sp`
11301130
and then stop the motor using the action specified by `stop_action`.
11311131
"""
11321132
for key in kwargs:
11331133
setattr(self, key, kwargs[key])
1134-
self.command = 'run-timed'
1134+
self.command = self.COMMAND_RUN_TIMED
11351135

11361136
def run_direct(self, **kwargs):
11371137
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
@@ -1140,15 +1140,15 @@ def run_direct(self, **kwargs):
11401140
"""
11411141
for key in kwargs:
11421142
setattr(self, key, kwargs[key])
1143-
self.command = 'run-direct'
1143+
self.command = self.COMMAND_RUN_DIRECT
11441144

11451145
def stop(self, **kwargs):
11461146
"""Stop any of the run commands before they are complete using the
11471147
action specified by `stop_action`.
11481148
"""
11491149
for key in kwargs:
11501150
setattr(self, key, kwargs[key])
1151-
self.command = 'stop'
1151+
self.command = self.COMMAND_STOP
11521152

11531153

11541154
# ~autogen
@@ -1346,14 +1346,14 @@ def run(self, **kwargs):
13461346
"""
13471347
for key in kwargs:
13481348
setattr(self, key, kwargs[key])
1349-
self.command = 'run'
1349+
self.command = self.COMMAND_RUN
13501350

13511351
def float(self, **kwargs):
13521352
"""Remove power from the motor.
13531353
"""
13541354
for key in kwargs:
13551355
setattr(self, key, kwargs[key])
1356-
self.command = 'float'
1356+
self.command = self.COMMAND_FLOAT
13571357

13581358

13591359
# ~autogen

templates/motor_commands.liquid

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{% assign class_name = currentClass.friendlyName | camel_case | capitalize %}{%
2-
for propval in currentClass.propertyValues %}{%
3-
if propval.propertyName == "Command" %}{%
4-
for value in propval.values %}{%
2+
for prop in currentClass.propertyValues %}{%
3+
assign propName = prop.propertyName | upcase | underscore_spaces %}{%
4+
if prop.propertyName == "Command" %}{%
5+
for value in prop.values %}{%
56
assign cmd = value.name | replace:'-','_' %}
67
def {{ cmd }}(self, **kwargs):
78
"""{%
89
for line in value.description %}{{line}}
910
{% endfor %}"""
1011
for key in kwargs:
1112
setattr(self, key, kwargs[key])
12-
self.command = '{{value.name}}'
13+
self.command = self.{{ propName }}_{{ value.name | upcase | underscore_non_wc }}
1314
{%
1415
endfor %}{%
1516
endif %}{%

0 commit comments

Comments
 (0)