Skip to content

Commit 2464387

Browse files
committed
Cleanup helper commands
There is no need to create _properties dict or _helper function. setarg(self, key, kwargs[key]) will access the correct property and deal with property type (string, int, etc) automatically.
1 parent 2008091 commit 2464387

File tree

2 files changed

+104
-125
lines changed

2 files changed

+104
-125
lines changed

ev3dev/ev3dev.py

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -557,59 +557,64 @@ def __set_time_sp(self, value):
557557
#~autogen
558558
#~autogen python_generic-helper-function classes.motor>currentClass
559559

560-
_properties = {
561-
'command' : __set_command
562-
, 'duty_cycle_sp' : __set_duty_cycle_sp
563-
, 'encoder_polarity' : __set_encoder_polarity
564-
, 'polarity' : __set_polarity
565-
, 'position' : __set_position
566-
, 'position_p' : __set_position_p
567-
, 'position_i' : __set_position_i
568-
, 'position_d' : __set_position_d
569-
, 'position_sp' : __set_position_sp
570-
, 'speed_sp' : __set_speed_sp
571-
, 'ramp_up_sp' : __set_ramp_up_sp
572-
, 'ramp_down_sp' : __set_ramp_down_sp
573-
, 'speed_regulation_enabled' : __set_speed_regulation_enabled
574-
, 'speed_regulation_p' : __set_speed_regulation_p
575-
, 'speed_regulation_i' : __set_speed_regulation_i
576-
, 'speed_regulation_d' : __set_speed_regulation_d
577-
, 'stop_command' : __set_stop_command
578-
, 'time_sp' : __set_time_sp }
579-
580-
def _helper( self, **kwargs ):
581-
for p,v in kwargs.iteritems():
582-
if p in self._properties:
583-
self._properties[p]( self, v )
584-
585560
def run_forever( self, **kwargs ):
586-
self._helper( **kwargs )
587-
self._set_string_attribute( 'command', 'command', 'run-forever' )
588-
561+
"""Run the motor until another command is sent.
562+
"""
563+
for key in kwargs:
564+
setattr(self, key, kwargs[key])
565+
self.command = 'run-forever'
566+
589567
def run_to_abs_pos( self, **kwargs ):
590-
self._helper( **kwargs )
591-
self._set_string_attribute( 'command', 'command', 'run-to-abs-pos' )
592-
568+
"""Run to an absolute position specified by `position_sp` and then
569+
stop using the command specified in `stop_command`.
570+
"""
571+
for key in kwargs:
572+
setattr(self, key, kwargs[key])
573+
self.command = 'run-to-abs-pos'
574+
593575
def run_to_rel_pos( self, **kwargs ):
594-
self._helper( **kwargs )
595-
self._set_string_attribute( 'command', 'command', 'run-to-rel-pos' )
596-
576+
"""Run to a position relative to the current `position` value.
577+
The new position will be current `position` + `position_sp`.
578+
When the new position is reached, the motor will stop using
579+
the command specified by `stop_command`.
580+
"""
581+
for key in kwargs:
582+
setattr(self, key, kwargs[key])
583+
self.command = 'run-to-rel-pos'
584+
597585
def run_timed( self, **kwargs ):
598-
self._helper( **kwargs )
599-
self._set_string_attribute( 'command', 'command', 'run-timed' )
600-
586+
"""Run the motor for the amount of time specified in `time_sp`
587+
and then stop the motor using the command specified by `stop_command`.
588+
"""
589+
for key in kwargs:
590+
setattr(self, key, kwargs[key])
591+
self.command = 'run-timed'
592+
601593
def run_direct( self, **kwargs ):
602-
self._helper( **kwargs )
603-
self._set_string_attribute( 'command', 'command', 'run-direct' )
604-
594+
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
595+
Unlike other run commands, changing `duty_cycle_sp` while running *will*
596+
take effect immediately.
597+
"""
598+
for key in kwargs:
599+
setattr(self, key, kwargs[key])
600+
self.command = 'run-direct'
601+
605602
def stop( self, **kwargs ):
606-
self._helper( **kwargs )
607-
self._set_string_attribute( 'command', 'command', 'stop' )
608-
603+
"""Stop any of the run commands before they are complete using the
604+
command specified by `stop_command`.
605+
"""
606+
for key in kwargs:
607+
setattr(self, key, kwargs[key])
608+
self.command = 'stop'
609+
609610
def reset( self, **kwargs ):
610-
self._helper( **kwargs )
611-
self._set_string_attribute( 'command', 'command', 'reset' )
612-
611+
"""Reset all of the motor parameter attributes to their default value.
612+
This will also have the effect of stopping the motor.
613+
"""
614+
for key in kwargs:
615+
setattr(self, key, kwargs[key])
616+
self.command = 'reset'
617+
613618

614619
#~autogen
615620
#~autogen python_generic-class classes.dcMotor>currentClass
@@ -807,36 +812,38 @@ def __set_time_sp(self, value):
807812

808813
#~autogen python_generic-helper-function classes.dcMotor>currentClass
809814

810-
_properties = {
811-
'command' : __set_command
812-
, 'duty_cycle_sp' : __set_duty_cycle_sp
813-
, 'polarity' : __set_polarity
814-
, 'ramp_down_sp' : __set_ramp_down_sp
815-
, 'ramp_up_sp' : __set_ramp_up_sp
816-
, 'stop_command' : __set_stop_command
817-
, 'time_sp' : __set_time_sp }
818-
819-
def _helper( self, **kwargs ):
820-
for p,v in kwargs.iteritems():
821-
if p in self._properties:
822-
self._properties[p]( self, v )
823-
824815
def run_forever( self, **kwargs ):
825-
self._helper( **kwargs )
826-
self._set_string_attribute( 'command', 'command', 'run-forever' )
827-
816+
"""Run the motor until another command is sent.
817+
"""
818+
for key in kwargs:
819+
setattr(self, key, kwargs[key])
820+
self.command = 'run-forever'
821+
828822
def run_timed( self, **kwargs ):
829-
self._helper( **kwargs )
830-
self._set_string_attribute( 'command', 'command', 'run-timed' )
831-
823+
"""Run the motor for the amount of time specified in `time_sp`
824+
and then stop the motor using the command specified by `stop_command`.
825+
"""
826+
for key in kwargs:
827+
setattr(self, key, kwargs[key])
828+
self.command = 'run-timed'
829+
832830
def run_direct( self, **kwargs ):
833-
self._helper( **kwargs )
834-
self._set_string_attribute( 'command', 'command', 'run-direct' )
835-
831+
"""Run the motor at the duty cycle specified by `duty_cycle_sp`.
832+
Unlike other run commands, changing `duty_cycle_sp` while running *will*
833+
take effect immediately.
834+
"""
835+
for key in kwargs:
836+
setattr(self, key, kwargs[key])
837+
self.command = 'run-direct'
838+
836839
def stop( self, **kwargs ):
837-
self._helper( **kwargs )
838-
self._set_string_attribute( 'command', 'command', 'stop' )
839-
840+
"""Stop any of the run commands before they are complete using the
841+
command specified by `stop_command`.
842+
"""
843+
for key in kwargs:
844+
setattr(self, key, kwargs[key])
845+
self.command = 'stop'
846+
840847

841848
#~autogen
842849

@@ -1014,28 +1021,20 @@ def __get_state(self):
10141021

10151022
#~autogen python_generic-helper-function classes.servoMotor>currentClass
10161023

1017-
_properties = {
1018-
'command' : __set_command
1019-
, 'max_pulse_sp' : __set_max_pulse_sp
1020-
, 'mid_pulse_sp' : __set_mid_pulse_sp
1021-
, 'min_pulse_sp' : __set_min_pulse_sp
1022-
, 'polarity' : __set_polarity
1023-
, 'position_sp' : __set_position_sp
1024-
, 'rate_sp' : __set_rate_sp }
1025-
1026-
def _helper( self, **kwargs ):
1027-
for p,v in kwargs.iteritems():
1028-
if p in self._properties:
1029-
self._properties[p]( self, v )
1030-
10311024
def run( self, **kwargs ):
1032-
self._helper( **kwargs )
1033-
self._set_string_attribute( 'command', 'command', 'run' )
1034-
1025+
"""Drive servo to the position set in the `position_sp` attribute.
1026+
"""
1027+
for key in kwargs:
1028+
setattr(self, key, kwargs[key])
1029+
self.command = 'run'
1030+
10351031
def float( self, **kwargs ):
1036-
self._helper( **kwargs )
1037-
self._set_string_attribute( 'command', 'command', 'float' )
1038-
1032+
"""Remove power from the motor.
1033+
"""
1034+
for key in kwargs:
1035+
setattr(self, key, kwargs[key])
1036+
self.command = 'float'
1037+
10391038

10401039
#~autogen
10411040

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
{% assign class_name = currentClass.friendlyName | camel_case | capitalize %}{%
2-
assign properties = " _properties = {" %}{%
3-
for propval in currentClass.systemProperties %}{%
4-
if propval.writeAccess %}{%
5-
capture properties %}{{ properties }}
6-
, '{{ propval.name | replace:' ','_' | downcase }}' : __set_{{ propval.name | replace:' ','_' | downcase }}{% endcapture %}{%
7-
endif %}{%
8-
endfor %}
9-
{{ properties | replace_first:',',' ' }} }
10-
11-
def _helper( self, **kwargs ):
12-
for p,v in kwargs.iteritems():
13-
if p in self._properties:
14-
self._properties[p]( self, v )
15-
{% for propval in currentClass.propertyValues %}{%
16-
if propval.propertyName == "Command" %}{%
17-
for value in propval.values %}{%
18-
assign cmd = value.name | replace:'-','_' %}
2+
for propval in currentClass.propertyValues %}{%
3+
if propval.propertyName == "Command" %}{%
4+
for value in propval.values %}{%
5+
assign cmd = value.name | replace:'-','_' %}
196
def {{ cmd }}( self, **kwargs ):
20-
self._helper( **kwargs )
21-
self._set_string_attribute( 'command', 'command', '{{ value.name }}' )
22-
{% endfor %}{%
23-
endif %}{%
24-
endfor %}{%
25-
comment %}{%
26-
for func in currentClass.helperFunctions %}{%
27-
assign func_name = func.name | downcase | underscore_spaces %}{%
28-
assign param_list = "" %}{%
29-
for param in func.parameters %}{%
30-
capture param_list %}{{ param_list }}, {{ param.name }}{% endcapture %}{%
31-
endfor %}
32-
def {{ func_name }}( {{ param_list | remove_first:', ' }} ):
33-
pass {%
34-
endfor %}{%
7+
"""{%
8+
for line in value.description %}{{line}}
9+
{% endfor %}"""
10+
for key in kwargs:
11+
setattr(self, key, kwargs[key])
12+
self.command = '{{value.name}}'
13+
{%
14+
endfor %}{%
15+
endif %}{%
3516
endfor %}
36-
{% endcomment %}

0 commit comments

Comments
 (0)