Presence Check using Blockly

[XML] [Smart Pascal] [JavaScript] [Python] [output]

This is a demonstration of a presence check of a string. It ensures that data is entered when required. We used the Blockly Playground to generate JavaScript and Python code from the same blocks.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="pmkbt-mj=7e!Y0$E/pBC" x="38" y="13">
    <mutation>
      <arg name="strPresenceCheck"></arg>
    </mutation>
    <field name="NAME">PresenceCheck</field>
    <comment pinned="false" h="80" w="160">Returns true if length of input is greater than 0</comment>
    <statement name="STACK">
      <block type="controls_if" id="%aS!(,+01);fwfxEp%}x">
        <mutation else="1"></mutation>
        <value name="IF0">
          <block type="logic_compare" id="C/=#o9,K9qIJ~RSj.?6`">
            <field name="OP">GT</field>
            <value name="A">
              <block type="text_length" id="$@V@hY^-`=P=MPcWeh[n">
                <value name="VALUE">
                  <shadow type="text" id="w1LAil=Tqx1jaXrDXfIn">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="!a|G8MOiu}9L{0WYm^4t">
                    <field name="VAR">strPresenceCheck</field>
                  </block>
                </value>
              </block>
            </value>
            <value name="B">
              <block type="math_number" id="yoIpEPIKMg4=hl7z,W]T">
                <field name="NUM">0</field>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO0">
          <block type="variables_set" id="fO;%UCB]O$)TC#ZCj29k">
            <field name="VAR">boolPresent</field>
            <value name="VALUE">
              <block type="logic_boolean" id="0.a)mSEdVGh3U1s[(#{W">
                <field name="BOOL">TRUE</field>
              </block>
            </value>
          </block>
        </statement>
        <statement name="ELSE">
          <block type="variables_set" id="$!j?LX_x9E8m.,?@2xrv">
            <field name="VAR">boolPresent</field>
            <value name="VALUE">
              <block type="logic_boolean" id="b9lDU{PB/h@CrZNzaWx;">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
          </block>
        </statement>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="nJf(?DzdK+jQF;?1QV!^">
        <field name="VAR">boolPresent</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="Z?#%2TDt$;YyWlywUQ#=" x="38" y="213">
    <field name="VAR">boolPresent</field>
    <value name="VALUE">
      <block type="logic_boolean" id="eB4NI;6~0C9$P8)7W`Np">
        <field name="BOOL">FALSE</field>
      </block>
    </value>
    <next>
      <block type="controls_whileUntil" id="id#sK{a1ix0scu}r}Qes">
        <field name="MODE">WHILE</field>
        <value name="BOOL">
          <block type="logic_compare" id=".rT5cRDYmcAF@|u,QiN=">
            <field name="OP">EQ</field>
            <value name="A">
              <block type="variables_get" id="6jn$1=6/N4AVNxb=,K-M">
                <field name="VAR">boolPresent</field>
              </block>
            </value>
            <value name="B">
              <block type="logic_boolean" id="]~$lRGF-6e!KCm]ND?`S">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO">
          <block type="variables_set" id="-1t-JB*tD7i9SQ/B1,2?">
            <field name="VAR">strRequired</field>
            <value name="VALUE">
              <block type="text_prompt_ext" id="`Op-8})?r7Fpoj(L5DKY">
                <mutation type="TEXT"></mutation>
                <field name="TYPE">TEXT</field>
                <value name="TEXT">
                  <shadow type="text" id="x*DDi%c@?LS|!3P/%~0A">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="text" id="j$MpcQ`5zAvDIw:^%;Rq">
                    <field name="TEXT">Please enter your title.</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="si[4HaLnx-jY$CpJW)H_">
                <field name="VAR">boolPresent</field>
                <value name="VALUE">
                  <block type="procedures_callreturn" id="@+12OCEZx^dpw#eX`Rq7">
                    <mutation name="PresenceCheck">
                      <arg name="strPresenceCheck"></arg>
                    </mutation>
                    <value name="ARG0">
                      <block type="variables_get" id="CQ;bg:|=Jwx[^/Lek4s^">
                        <field name="VAR">strRequired</field>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </next>
          </block>
        </statement>
        <next>
          <block type="text_append" id="@nKR+Y%5$]H!owtHHFk{">
            <field name="VAR">strRequired</field>
            <value name="TEXT">
              <shadow type="text" id="%:hj6h2FA,?BHXc`e^DN">
                <field name="TEXT"> accepted</field>
              </shadow>
            </value>
            <next>
              <block type="text_print" id="wON+C4qBL?#wRFQ!:3Zo">
                <value name="TEXT">
                  <shadow type="text" id="4%wM?NC9}I}r$u]W6A.h">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="%8{,jJD63V5R;.mlU/iU">
                    <field name="VAR">strRequired</field>
                  </block>
                </value>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var boolPresent: Boolean;
var strPresenceCheck: String;
var strRequired: String;


{
  Returns true if length of input is greater than 0
}
function PresenceCheck(strPresenceCheck: String): Boolean;
begin
  if ((length(strPresenceCheck) > 0)) then
  begin
    boolPresent := true;
  end
  else
  begin
    boolPresent := false;
  end;
  Result := boolPresent;
end;



boolPresent := false;
while ((boolPresent = false)) do
begin
  strRequired := prompt('Please enter your title.');
  boolPresent := PresenceCheck(strRequired);
end;
strRequired += ' accepted';
Console.writeln(strRequired);
    

Generated JavaScript

var boolPresent, strPresenceCheck, strRequired;

/**
 * Returns true if length of input is greater than 0
 */
function PresenceCheck(strPresenceCheck) {
  if (strPresenceCheck.length > 0) {
    boolPresent = true;
  } else {
    boolPresent = false;
  }
  return boolPresent;
}


boolPresent = false;
while (boolPresent == false) {
  strRequired = window.prompt('Please enter your title.');
  boolPresent = PresenceCheck(strRequired);
}
strRequired = String(strRequired) + String(' accepted');
window.alert(strRequired);

Generated Python Code

boolPresent = None
strPresenceCheck = None
strRequired = None

"""Returns true if length of input is greater than 0
"""
def PresenceCheck(strPresenceCheck):
  global boolPresent, strRequired
  if len(strPresenceCheck) > 0:
    boolPresent = True
  else:
    boolPresent = False
  return boolPresent

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


boolPresent = False
while boolPresent == False:
  strRequired = text_prompt('Please enter your title.')
  boolPresent = PresenceCheck(strRequired)
strRequired = str(strRequired) + str(' accepted')
print(strRequired)

Copy of Output

After pressing return immediately the prompt reappeared and the input Dr was entered:

Dr accepted
Programming - a skill for life!

Introduction to validation (length check, range check, presence check, type check and character check)