if-elsif Anti-Bandwurm Kur (Beispiel: UConv)

Begonnen von RichardCZ, 13 April 2020, 00:55:19

Vorheriges Thema - Nächstes Thema

RichardCZ

hier am Beispiel von UConv

https://gl.petatech.eu/root/HomeBot/-/commit/1fba0042c8c14dd4dfa49115eefc1e50a84dcf79#61592d9a606399c3ff575e5d4106ab869efce529

Exhibit A: Tabular Ternaries, PBP 121-123
Exhibit B: Data-Driven Design




Vielleicht finden auch andere Inspiration. Ist jetzt nicht 100% kompatibel, weil ich die Rückgabeliste von bft2condition leicht umsortiert habe um das hier machen zu können: https://gl.petatech.eu/root/HomeBot/-/commit/e54a7f723176a5ad15e9ca8783a15c2e60e20df2
Aber das kann man ja leicht anpassen. (IN der Tabelle müssten die RGB Werte in der Mitte sein)
Witty House Infrastructure Processor (WHIP) is a modern and
comprehensive full-stack smart home framework for the 21st century.

mahowi

Ich habe jetzt (zumindest bei mir in Github) in 59_WUup.pm mal ein bißchen rumgespielt:
@@ -240,40 +234,19 @@
             $o //= 0;
             $value = ReadingsVal( $d, $r, 0 ) + $o;
         }
-        if ( $key =~ m{\w+f \z}xms ) {
-            $value = UConv::c2f( $value, $rnd );
-        }
-        if ( $key =~ m{\w+mph [^\n]*}xms ) {
+        $value =
+            $key =~ m{\w+f \z}xms ? UConv::c2f( $value, $rnd )
+            : ( $key =~ m{\w+mph [^\n]*}xms )
+            && ( $unit_windspeed eq 'm/s' )
+            ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
+            : $key =~ m{\w+mph [^\n]*}xms ? UConv::kph2mph( $value, $rnd )
+            : $key eq 'baromin' ? UConv::hpa2inhg( $value, $rnd )
+            : $key =~ m{rainin \z}xms ? UConv::mm2in( $value, $rnd )
+            : ( $key eq 'solarradiation' )
+            && ( $unit_solarradiation eq 'lux' )
+            ? UConv::lux2wpsm( $value, $rnd )
+            : $value;

-            if ( $unit_windspeed eq 'm/s' ) {
-                Log3( $name, 5, qq{WUup ($name) - windspeed unit is m/s} );
-                $value =
-                    UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ),
-                    $rnd );
-            }
-            else {
-                Log3( $name, 5, qq{WUup ($name) - windspeed unit is km/h} );
-                $value = UConv::kph2mph( $value, $rnd );
-            }
-        }
-        if ( $key eq 'baromin' ) {
-            $value = UConv::hpa2inhg( $value, $rnd );
-        }
-        if ( $key =~ m{rainin \z}xms ) {
-            $value = UConv::mm2in( $value, $rnd );
-        }
-        if ( $key eq 'solarradiation' ) {
-
-            if ( $unit_solarradiation eq 'lux' ) {
-                Log3( $name, 5,
-                    qq{WUup ($name) - solarradiation unit is lux} );
-                $value = UConv::lux2wpsm( $value, $rnd );
-            }
-            else {
-                Log3( $name, 5,
-                    qq{WUup ($name) - solarradiation unit is W/m²} );
-            }
-        }
         $data .= "&$key=$value";
     }


Ganz so übersichtlich finde ich allerdings
        $value =
            $key =~ m{\w+f \z}xms ? UConv::c2f( $value, $rnd )
            : ( $key =~ m{\w+mph [^\n]*}xms )
            && ( $unit_windspeed eq 'm/s' )
            ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
            : $key =~ m{\w+mph [^\n]*}xms ? UConv::kph2mph( $value, $rnd )
            : $key eq 'baromin' ? UConv::hpa2inhg( $value, $rnd )
            : $key =~ m{rainin \z}xms ? UConv::mm2in( $value, $rnd )
            : ( $key eq 'solarradiation' )
            && ( $unit_solarradiation eq 'lux' )
            ? UConv::lux2wpsm( $value, $rnd )
            : $value;

auch nicht. Bekommt man das noch etwas lesbarer hin?
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

RichardCZ

Zitat von: mahowi am 14 April 2020, 16:24:45
Ganz so übersichtlich finde ich allerdings
        $value =
            $key =~ m{\w+f \z}xms ? UConv::c2f( $value, $rnd )
            : ( $key =~ m{\w+mph [^\n]*}xms )
            && ( $unit_windspeed eq 'm/s' )
            ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
            : $key =~ m{\w+mph [^\n]*}xms ? UConv::kph2mph( $value, $rnd )
            : $key eq 'baromin' ? UConv::hpa2inhg( $value, $rnd )
            : $key =~ m{rainin \z}xms ? UConv::mm2in( $value, $rnd )
            : ( $key eq 'solarradiation' )
            && ( $unit_solarradiation eq 'lux' )
            ? UConv::lux2wpsm( $value, $rnd )
            : $value;

auch nicht. Bekommt man das noch etwas lesbarer hin?

Das ist nicht nur unübersichtlich.  ;)
Natürlich kann man! Wenn man es so macht wie im Beispiel dargestellt:

$value = $key =~ m{\w+f \z}xms                                             ? UConv::c2f( $value, $rnd )
       : ( $key =~ m{\w+mph [^\n]*}xms ) && ( $unit_windspeed eq 'm/s' )   ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
       : $key =~ m{\w+mph [^\n]*}xms                                       ? UConv::kph2mph( $value, $rnd )
       : $key eq 'baromin'                                                 ? UConv::hpa2inhg( $value, $rnd )
       : $key =~ m{rainin \z}xms                                           ? UConv::mm2in( $value, $rnd )
       : ( $key eq 'solarradiation' ) && ( $unit_solarradiation eq 'lux' ) ? UConv::lux2wpsm( $value, $rnd )
       : $value;


Überhaupt erst so kapiere ich was der code machen soll. Und erst jetzt sieht man auch, dass einige (nicht all) Äste unabhängig voneinander sind.
Prinzipiell grätschen Dir da diese '&&' Conditions rein. Da ist die Frage ob man deren Wahrheitswert nicht vorher in einer Variablen ablegt:

$mph_metric    = $key =~ m{\w+mph [^\n]*}xms && $unit_windspeed eq 'm/s';
$lux_radiation = $key eq 'solarradiation'    && $unit_solarradiation eq 'lux';

$value = $key =~ m{\w+f \z}xms        ? UConv::c2f( $value, $rnd )
       : $mph_pmetric                 ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
       : $key =~ m{\w+mph [^\n]*}xms  ? UConv::kph2mph( $value, $rnd )
       : $key eq 'baromin'            ? UConv::hpa2inhg( $value, $rnd )
       : $key =~ m{rainin \z}xms      ? UConv::mm2in( $value, $rnd )
       : $lux_radiation               ? UConv::lux2wpsm( $value, $rnd )
       : $value;
Witty House Infrastructure Processor (WHIP) is a modern and
comprehensive full-stack smart home framework for the 21st century.

mahowi

    while ( my ( $key, $value ) = each(%$a) ) {
        next if substr( $key, 0, 2 ) ne 'wu';
        $key = substr( $key, 2, length($key) - 2 );
        ( $d, $r, $o ) = split( ":", $value );
       
        if ( defined($r) ) {
            $o //= 0;
            $value = ReadingsVal( $d, $r, 0 ) + $o;
        }

        my $mph_metric =
            $key =~ m{\w+mph [^\n]*}xms && $unit_windspeed eq 'm/s';
        my $lux_radiation =
            $key eq 'solarradiation'    && $unit_solarradiation eq 'lux';

        $value = $key =~ m{\w+f \z}xms       ? UConv::c2f( $value, $rnd )
               : $key =~ m{\w+mph [^\n]*}xms ? UConv::kph2mph( $value, $rnd )
               : $key eq 'baromin'           ? UConv::hpa2inhg( $value, $rnd )
               : $key =~ m{rainin \z}xms     ? UConv::mm2in( $value, $rnd )
               : $mph_metric                 ? UConv::kph2mph( ( UConv::mps2kph( $value, $rnd ) ), $rnd )
               : $lux_radiation              ? UConv::lux2wpsm( $value, $rnd )
               : $value;

        $data .= "&$key=$value";
    }


So sieht das schon besser aus.  :)  Den if..elsif Bandwurm bin ich los und perlcritic -3 ist auch zufrieden.  :D
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

mahowi

Kann ich eigentlich perltidy irgendwie beibringen, mir die Formatierung hier nicht wieder zu zerschießen?

Ich habe die Einstellungen aus PBC übernommen:

-l=78 # Max line width is 78 cols
-i=4 # Indent level is 4 cols
-ci=4 # Continuation indent is 4 cols
-st # Output to STDOUT
-se # Errors to STDERR
-vt=2 # Maximal vertical tightness
-cti=0 # No extra indentation for closing brackets
-pt=1 # Medium parenthesis tightness
-bt=1 # Medium brace tightness
-sbt=1 # Medium square bracket tightness
-bbt=1 # Medium block brace tightness
-nsfs # No space before semicolons
-nolq # Don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ !~ < > | & **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
        # Break before all operators
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

CoolTux

Zitat von: mahowi am 15 April 2020, 19:23:48
Kann ich eigentlich perltidy irgendwie beibringen, mir die Formatierung hier nicht wieder zu zerschießen?

Ich habe die Einstellungen aus PBC übernommen:

-l=78 # Max line width is 78 cols
-i=4 # Indent level is 4 cols
-ci=4 # Continuation indent is 4 cols
-st # Output to STDOUT
-se # Errors to STDERR
-vt=2 # Maximal vertical tightness
-cti=0 # No extra indentation for closing brackets
-pt=1 # Medium parenthesis tightness
-bt=1 # Medium brace tightness
-sbt=1 # Medium square bracket tightness
-bbt=1 # Medium block brace tightness
-nsfs # No space before semicolons
-nolq # Don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ !~ < > | & **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
        # Break before all operators


Ich habe aktuell das selbe Problem.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

RichardCZ

Um ehrlich zu sein, benutze ich perltidy (mit default-Werten) bei eigenem Code nur für die Eingangsnormalisierung. Nachdem ich den Code unter meine Fittiche genommen habe, soll den perltidy nicht mehr anfassen.
Witty House Infrastructure Processor (WHIP) is a modern and
comprehensive full-stack smart home framework for the 21st century.