Author Topic: Python math  (Read 1675 times)

0 Members and 1 Guest are viewing this topic.

PegLegTV

  • $upporter
  • Hero Member
  • *****
  • Posts: 500
  • Karma: 43
    • View Profile
Python math
« on: July 04, 2015, 01:28:02 PM »
when trying to use PY.ExecString, to do some math (result=6+09) if I use 08 or 09 it fails and shows this error

Quote
Error: System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.First[TSource](IEnumerable'1 source)
   at VCPlugin.iPy.execString(String strCode)
   at vcPlugin.Plugin.doAction(String[]parsedActions, String[]
parsedParams)
Plugin: C:\users\USERNAME\Documents\VC2\Plugins\PY\PY.dll

"01" through "07" work perfect,

here's some examples:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.5.1-->
<command id="635" name="Py Math" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=6+09</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=6+08</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=6+07</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

I tested it on 2 different PC and get the same result






jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python math
« Reply #1 on: July 05, 2015, 09:20:19 AM »
The correct solution is not to use a leading 0. That is because python interprets numbers that start with a zero as base 8 and base 8 can only use digits from 0 to 7 just as base 10 will only accept digits from 0 to 9

http://stackoverflow.com/questions/13431324/how-does-python-interpret-numbers-with-leading-zeroes

If you are having trouble finding a way to eliminate the leading 0 you can use the int function to convert a string to a base 10 integer.

So you could write

Code: [Select]
Print 6 + int("09")
« Last Edit: July 05, 2015, 09:21:21 AM by nime5ter »

PegLegTV

  • $upporter
  • Hero Member
  • *****
  • Posts: 500
  • Karma: 43
    • View Profile
Re: Python math
« Reply #2 on: July 05, 2015, 10:57:33 AM »
Thanks for the reply, I'm using it in the "Reminders with a memory" group I figured out away to drop the zero using regex using a pattern like this
Code: [Select]
([123456789])
I think that's right, I'm away from my pc for the next couple days, when I get back I'll upload a new version of Reminders with a memory

And thanks for the explanation

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7715
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Python math
« Reply #3 on: July 05, 2015, 11:31:55 AM »
I would need to see it in context, but it might be better to use the method I suggested for a couple of reasons.

1st reason : it's simpler and probably faster to execute since you are already making a python call anyway.  No extra actions required.

2nd reason: your regex will probably only work if you are expecting numbers ranging from 1 to 9.  So 0 or any number greater than or equal to 10 will not work.

If you really wanted to use regex for some reason it would probably need to be something like this to remove leading zeros but  still allow 0 or numbers >= 10

Code: [Select]
0*(\d+)
0* will remove as many zeroes as possible while still leaving at least 1 digit at the end.
« Last Edit: July 05, 2015, 11:39:40 AM by jitterjames »

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2012
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Python math
« Reply #4 on: July 05, 2015, 11:41:21 AM »
Relatedly, I wanted to ask whether this is mainly an issue when you're using the custom date time variables and doing different things depending on the minute or hour value returned?

If so, I'm wondering whether you could solve the problem by using different date/time variables?

e.g. {Dtcustom.HH} returns values from 00 to 23, but  {DtCustom.%H} should return 0 to 23, without the leading 0. Similarly, {Dtcustom.mm} returns values from 00 to 59, but {dtcustom.%m} should give you the minutes in a format of 0 to 59, so minutes below 10 will be 1 through 9.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

PegLegTV

  • $upporter
  • Hero Member
  • *****
  • Posts: 500
  • Karma: 43
    • View Profile
Re: Python math
« Reply #5 on: July 05, 2015, 11:47:18 AM »
I'll give yours a try when I get back to my PC because right now I'm having to use a logic block to perform the Regex replace, the top two logic blocks are used to remove the zero (one is for hours the other is for minutes)

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.5.1-->
<command id="332" name="What reminders do I have (Step 2)" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>01 02 03 04 05 06 07 08 09&amp;&amp;{DtCustom.HH}</ifParams>
    <then>
      <action>
        <cmdType>Results.RegEx</cmdType>
        <params>
          <param>([123456789])</param>
          <param />
          <param>{DtCustom.HH}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Current Hour</param>
          <param>{Match.1.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Current Hour</param>
          <param>{DtCustom.HH}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>01 02 03 04 05 06 07 08 09&amp;&amp;{DtCustom.mm}</ifParams>
    <then>
      <action>
        <cmdType>Results.RegEx</cmdType>
        <params>
          <param>([123456789])</param>
          <param />
          <param>{DtCustom.mm}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Current Min</param>
          <param>{Match.1.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Current Min</param>
          <param>{DtCustom.mm}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>File.Read</cmdType>
    <params>
      <param>{Path.VC}\Reminders Log2.txt</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegEx</cmdType>
    <params>
      <param>&lt;timer&gt;&lt;Set&gt;(.*?):(.*?)...&lt;/Set&gt;\W&lt;Reminder&gt;(.*?)&lt;/Reminder&gt;&lt;/timer&gt;</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>hour</param>
      <param>{Match.{1}.1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>minute</param>
      <param>{Match.{1}.2}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>reason</param>
      <param>{Match.{1}.3}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>01 02 03 04 05 06 07 08 09&amp;&amp;{Var.hour}</ifParams>
    <then>
      <action>
        <cmdType>Results.RegEx</cmdType>
        <params>
          <param>([123456789])</param>
          <param />
          <param>{Var.hour}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>hour</param>
          <param>{Match.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else />
  </if>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>01 02 03 04 05 06 07 08 09&amp;&amp;{Var.minute}</ifParams>
    <then>
      <action>
        <cmdType>Results.RegEx</cmdType>
        <params>
          <param>([123456789])</param>
          <param />
          <param>{Var.minute}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>minute1</param>
          <param>{Match.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>minute1</param>
          <param>{Var.minute}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result={Var.minute1}-{Var.Current Min}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;-</ifParams>
    <then>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>result={LastResult}+60</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>minute1</param>
          <param>{LastResult}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>hour1</param>
          <param>-1</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>minute1</param>
          <param>{LastResult}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>hour1</param>
          <param>0</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result={Var.hour}+{Var.hour1}-{Var.Current Hour}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>{LastResult}&amp;&amp;-</ifParams>
    <then>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>result={LastResult}+24</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>hour1</param>
          <param>{LastResult}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>hour1</param>
          <param>{LastResult}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>VC.TriggerEvent</cmdType>
    <params>
      <param>XBMC Notification</param>
      <param>C:\\Users\\sallyj\\Desktop\\VC2\\plugins\\TcpMic\\icon.PNG</param>
      <param>Reminder</param>
      <param>{1} - {Var.reason} - {Var.hour1}:{Var.minute1} Remaining</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.SpeakSync</cmdType>
    <params>
      <param>Reminder number {1}, {Var.reason} - has {Var.hour1} Hours &amp; {Var.minute1} Minutes remaining </param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <event>What reminders do I have</event>
</command>

I used remote desktop to grab theses, I haven't fully tested this command but the RegEx replace works, but if your method could cut out the logic blocks then that sounds great  ;D


Nim5ter, you genius,  :hugs that looks to be exactly what I need, I think using that I should be able to cut out 2 logic blocks in both of my commands,

Thank you Jitterjames and Nime5ter, you guys are awesome  ::banana