Assertion in list comprehension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beginner

    Assertion in list comprehension

    Hi,

    Does anyone know how to put an assertion in list comprehension? I have
    the following list comprehension, but I want to use an assertion to
    check the contents of rec_stdl. I ended up using another loop which
    essentially duplicates the functions of list comprehension. It just
    look like a waste of coding and computer time to me.

    I just wish I could put the assertions into list comprehensions.

    x=[(rec_stdl[0].st/10000.0,
    rec_stdl[0].cl,
    rec_stdl[0].bb,
    rec_stdl[0].bo,
    rec_stdl[1].bb,
    rec_stdl[1].bo,
    rec_stdl[0].ex
    )
    for rec_stdl in rec_by_ex if len(rec_stdl)== 2
    ]

    #duplicated loop
    if __debug__:
    for rec_stdl in rec_by_ex:
    l=len(rec_stdl)
    assert(l<=2 and l>0)
    if l==2:
    assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
    assert(rec_stdl[0].ex==rec_stdl[1].ex)
    assert(rec_stdl[0].st==rec_stdl[1].st)
    assert(rec_stdl[0].cp==rec_stdl[1].cp)

    Thanks,
    Geoffrey

  • danmcleran@yahoo.com

    #2
    Re: Assertion in list comprehension

    On Aug 1, 9:37 am, beginner <zyzhu2...@gmai l.comwrote:
    Hi,
    >
    Does anyone know how to put an assertion in list comprehension? I have
    the following list comprehension, but I want to use an assertion to
    check the contents of rec_stdl. I ended up using another loop which
    essentially duplicates the functions of list comprehension. It just
    look like a waste of coding and computer time to me.
    >
    I just wish I could put the assertions into list comprehensions.
    >
    x=[(rec_stdl[0].st/10000.0,
    rec_stdl[0].cl,
    rec_stdl[0].bb,
    rec_stdl[0].bo,
    rec_stdl[1].bb,
    rec_stdl[1].bo,
    rec_stdl[0].ex
    )
    for rec_stdl in rec_by_ex if len(rec_stdl)== 2
    ]
    >
    #duplicated loop
    if __debug__:
    for rec_stdl in rec_by_ex:
    l=len(rec_stdl)
    assert(l<=2 and l>0)
    if l==2:
    assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
    assert(rec_stdl[0].ex==rec_stdl[1].ex)
    assert(rec_stdl[0].st==rec_stdl[1].st)
    assert(rec_stdl[0].cp==rec_stdl[1].cp)
    >
    Thanks,
    Geoffrey
    Can't you just call a function from within your list comprehension and
    do whatever you want for each item? Something like this (not tested):

    def checker(item):
    assert(len(item ) <= 2 and len(item) 0)
    if len(item) == 2:
    assert(item[0].c == "C" and item[1].c == "P"

    return len(item) == 2

    x = [whatever for item in all_items if checker(item = item)]

    Comment

    • beginner

      #3
      Re: Assertion in list comprehension

      On Aug 1, 11:09 am, "danmcle...@yah oo.com" <danmcle...@yah oo.com>
      wrote:
      On Aug 1, 9:37 am, beginner <zyzhu2...@gmai l.comwrote:
      >
      >
      >
      >
      >
      Hi,
      >
      Does anyone know how to put an assertion in list comprehension? I have
      the following list comprehension, but I want to use an assertion to
      check the contents of rec_stdl. I ended up using another loop which
      essentially duplicates the functions of list comprehension. It just
      look like a waste of coding and computer time to me.
      >
      I just wish I could put the assertions into list comprehensions.
      >
      x=[(rec_stdl[0].st/10000.0,
      rec_stdl[0].cl,
      rec_stdl[0].bb,
      rec_stdl[0].bo,
      rec_stdl[1].bb,
      rec_stdl[1].bo,
      rec_stdl[0].ex
      )
      for rec_stdl in rec_by_ex if len(rec_stdl)== 2
      ]
      >
      #duplicated loop
      if __debug__:
      for rec_stdl in rec_by_ex:
      l=len(rec_stdl)
      assert(l<=2 and l>0)
      if l==2:
      assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
      assert(rec_stdl[0].ex==rec_stdl[1].ex)
      assert(rec_stdl[0].st==rec_stdl[1].st)
      assert(rec_stdl[0].cp==rec_stdl[1].cp)
      >
      Thanks,
      Geoffrey
      >
      Can't you just call a function from within your list comprehension and
      do whatever you want for each item? Something like this (not tested):
      >
      def checker(item):
      assert(len(item ) <= 2 and len(item) 0)
      if len(item) == 2:
      assert(item[0].c == "C" and item[1].c == "P"
      >
      return len(item) == 2
      >
      x = [whatever for item in all_items if checker(item = item)]- Hide quoted text -
      >
      - Show quoted text -
      Good idea! Thank you!

      Comment

      • Chris Mellon

        #4
        Re: Assertion in list comprehension

        On 8/1/07, beginner <zyzhu2000@gmai l.comwrote:
        Hi,
        >
        Does anyone know how to put an assertion in list comprehension? I have
        the following list comprehension, but I want to use an assertion to
        check the contents of rec_stdl. I ended up using another loop which
        essentially duplicates the functions of list comprehension. It just
        look like a waste of coding and computer time to me.
        >
        I just wish I could put the assertions into list comprehensions.
        >
        x=[(rec_stdl[0].st/10000.0,
        rec_stdl[0].cl,
        rec_stdl[0].bb,
        rec_stdl[0].bo,
        rec_stdl[1].bb,
        rec_stdl[1].bo,
        rec_stdl[0].ex
        )
        for rec_stdl in rec_by_ex if len(rec_stdl)== 2
        ]
        >
        #duplicated loop
        if __debug__:
        for rec_stdl in rec_by_ex:
        l=len(rec_stdl)
        assert(l<=2 and l>0)
        if l==2:
        assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
        assert(rec_stdl[0].ex==rec_stdl[1].ex)
        assert(rec_stdl[0].st==rec_stdl[1].st)
        assert(rec_stdl[0].cp==rec_stdl[1].cp)
        First: All your asserts are wrong. Assert is a statement, not a
        function. These specific ones will behave as expected, but it's easy
        to accidentally write ones that always pass this way.

        Secondly: This is a waste of code, because if __debug__ is not defined
        asserts will be skipped by the compiler. You could use the same loop
        block for both branches.

        Thirdly: This sort of testing is precisely what unit tests and/or
        doctests are for.

        Comment

        • Stargaming

          #5
          Re: Assertion in list comprehension

          On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
          On 8/1/07, beginner <zyzhu2000@gmai l.comwrote:
          >Hi,
          >>
          >Does anyone know how to put an assertion in list comprehension? I have
          >the following list comprehension, but I want to use an assertion to
          >check the contents of rec_stdl. I ended up using another loop which
          >essentially duplicates the functions of list comprehension. It just
          >look like a waste of coding and computer time to me.
          >>
          >I just wish I could put the assertions into list comprehensions.
          >>
          > x=[(rec_stdl[0].st/10000.0,
          > rec_stdl[0].cl,
          > rec_stdl[0].bb,
          > rec_stdl[0].bo,
          > rec_stdl[1].bb,
          > rec_stdl[1].bo,
          > rec_stdl[0].ex
          > )
          > for rec_stdl in rec_by_ex if len(rec_stdl)== 2
          > ]
          >>
          > #duplicated loop
          > if __debug__:
          > for rec_stdl in rec_by_ex:
          > l=len(rec_stdl)
          > assert(l<=2 and l>0)
          > if l==2:
          > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
          > assert(rec_stdl[0].ex==rec_stdl[1].ex)
          > assert(rec_stdl[0].st==rec_stdl[1].st)
          > assert(rec_stdl[0].cp==rec_stdl[1].cp)
          >
          First: All your asserts are wrong. Assert is a statement, not a
          function. These specific ones will behave as expected, but it's easy to
          accidentally write ones that always pass this way.
          Could you come up with an example? I can only think of accidentally
          injecting a comma, what would create a (true, in a boolean context) tuple.

          And, well, if you're only using () for readabilty, this might sometimes
          look messy when calling assert with the extended syntax::

          assert(False), "error text"

          Where one could expect the construction of a tuple.
          Secondly: This is a waste of code, because if __debug__ is not defined
          asserts will be skipped by the compiler. You could use the same loop
          block for both branches.
          Well, the `assert` isn't there for no reason, but if you're serious about
          it, `raise` could be better.
          Thirdly: This sort of testing is precisely what unit tests and/or
          doctests are for.
          Huh? What beginner is doing there seems more like input validation than
          testing. Unit or doctests are meant for testing (and in case of doctests,
          showing) whether a function works as expected.

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Assertion in list comprehension

            On Wed, 01 Aug 2007 16:55:53 +0000, Stargaming wrote:
            >Thirdly: This sort of testing is precisely what unit tests and/or
            >doctests are for.
            >
            Huh? What beginner is doing there seems more like input validation than
            testing. Unit or doctests are meant for testing (and in case of doctests,
            showing) whether a function works as expected.
            If it is input validation I wouldn't expect it protected by a ``if
            __debug__:``. That looks more like debugging/testing.

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • beginner

              #7
              Re: Assertion in list comprehension

              On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
              On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:
              >
              >
              >
              >
              >
              Hi,
              >
              Does anyone know how to put an assertion in list comprehension? I have
              the following list comprehension, but I want to use an assertion to
              check the contents of rec_stdl. I ended up using another loop which
              essentially duplicates the functions of list comprehension. It just
              look like a waste of coding and computer time to me.
              >
              I just wish I could put the assertions into list comprehensions.
              >
              x=[(rec_stdl[0].st/10000.0,
              rec_stdl[0].cl,
              rec_stdl[0].bb,
              rec_stdl[0].bo,
              rec_stdl[1].bb,
              rec_stdl[1].bo,
              rec_stdl[0].ex
              )
              for rec_stdl in rec_by_ex if len(rec_stdl)== 2
              ]
              >
              #duplicated loop
              if __debug__:
              for rec_stdl in rec_by_ex:
              l=len(rec_stdl)
              assert(l<=2 and l>0)
              if l==2:
              assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
              assert(rec_stdl[0].ex==rec_stdl[1].ex)
              assert(rec_stdl[0].st==rec_stdl[1].st)
              assert(rec_stdl[0].cp==rec_stdl[1].cp)
              >
              First: All your asserts are wrong. Assert is a statement, not a
              function. These specific ones will behave as expected, but it's easy
              to accidentally write ones that always pass this way.

              Do you mean I should not use the parentheses?

              Secondly: This is a waste of code, because if __debug__ is not defined
              asserts will be skipped by the compiler. You could use the same loop
              block for both branches.
              I know. My original question was how. Dan suggested to write a checker
              function.

              Thirdly: This sort of testing is precisely what unit tests and/or
              doctests are for.
              Agreed.

              Comment

              • Chris Mellon

                #8
                Re: Assertion in list comprehension

                On 8/1/07, beginner <zyzhu2000@gmai l.comwrote:
                On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
                On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:




                Hi,
                Does anyone know how to put an assertion in list comprehension? I have
                the following list comprehension, but I want to use an assertion to
                check the contents of rec_stdl. I ended up using another loop which
                essentially duplicates the functions of list comprehension. It just
                look like a waste of coding and computer time to me.
                I just wish I could put the assertions into list comprehensions.
                x=[(rec_stdl[0].st/10000.0,
                rec_stdl[0].cl,
                rec_stdl[0].bb,
                rec_stdl[0].bo,
                rec_stdl[1].bb,
                rec_stdl[1].bo,
                rec_stdl[0].ex
                )
                for rec_stdl in rec_by_ex if len(rec_stdl)== 2
                ]
                #duplicated loop
                if __debug__:
                for rec_stdl in rec_by_ex:
                l=len(rec_stdl)
                assert(l<=2 and l>0)
                if l==2:
                assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
                assert(rec_stdl[0].ex==rec_stdl[1].ex)
                assert(rec_stdl[0].st==rec_stdl[1].st)
                assert(rec_stdl[0].cp==rec_stdl[1].cp)
                First: All your asserts are wrong. Assert is a statement, not a
                function. These specific ones will behave as expected, but it's easy
                to accidentally write ones that always pass this way.
                >
                >
                Do you mean I should not use the parentheses?
                >
                Yes.
                >
                Secondly: This is a waste of code, because if __debug__ is not defined
                asserts will be skipped by the compiler. You could use the same loop
                block for both branches.
                >
                I know. My original question was how. Dan suggested to write a checker
                function.
                >
                Use the second block in all cases. In any situation where "if
                __debug__" is False, the asserts are a noop and in fact won't even be
                present in the bytecode.
                >
                Thirdly: This sort of testing is precisely what unit tests and/or
                doctests are for.
                >
                Agreed.
                >
                --

                >

                Comment

                • Chris Mellon

                  #9
                  Re: Assertion in list comprehension

                  On 01 Aug 2007 16:55:53 GMT, Stargaming <stargaming@gma il.comwrote:
                  On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
                  >
                  On 8/1/07, beginner <zyzhu2000@gmai l.comwrote:
                  Hi,
                  >
                  Does anyone know how to put an assertion in list comprehension? I have
                  the following list comprehension, but I want to use an assertion to
                  check the contents of rec_stdl. I ended up using another loop which
                  essentially duplicates the functions of list comprehension. It just
                  look like a waste of coding and computer time to me.
                  >
                  I just wish I could put the assertions into list comprehensions.
                  >
                  x=[(rec_stdl[0].st/10000.0,
                  rec_stdl[0].cl,
                  rec_stdl[0].bb,
                  rec_stdl[0].bo,
                  rec_stdl[1].bb,
                  rec_stdl[1].bo,
                  rec_stdl[0].ex
                  )
                  for rec_stdl in rec_by_ex if len(rec_stdl)== 2
                  ]
                  >
                  #duplicated loop
                  if __debug__:
                  for rec_stdl in rec_by_ex:
                  l=len(rec_stdl)
                  assert(l<=2 and l>0)
                  if l==2:
                  assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
                  assert(rec_stdl[0].ex==rec_stdl[1].ex)
                  assert(rec_stdl[0].st==rec_stdl[1].st)
                  assert(rec_stdl[0].cp==rec_stdl[1].cp)
                  First: All your asserts are wrong. Assert is a statement, not a
                  function. These specific ones will behave as expected, but it's easy to
                  accidentally write ones that always pass this way.
                  >
                  Could you come up with an example? I can only think of accidentally
                  injecting a comma, what would create a (true, in a boolean context) tuple.
                  >
                  And, well, if you're only using () for readabilty, this might sometimes
                  look messy when calling assert with the extended syntax::
                  >
                  assert(False), "error text"
                  >
                  It's very easy to write this as assert(False, "error text") if you're
                  in the habit of thinking that assert is a function.
                  Where one could expect the construction of a tuple.
                  >
                  Secondly: This is a waste of code, because if __debug__ is not defined
                  asserts will be skipped by the compiler. You could use the same loop
                  block for both branches.
                  >
                  Well, the `assert` isn't there for no reason, but if you're serious about
                  it, `raise` could be better.
                  >
                  Thirdly: This sort of testing is precisely what unit tests and/or
                  doctests are for.
                  >
                  Huh? What beginner is doing there seems more like input validation than
                  testing. Unit or doctests are meant for testing (and in case of doctests,
                  showing) whether a function works as expected.
                  Not in a big __debug__ block it isn't.

                  Comment

                  • beginner

                    #10
                    Re: Assertion in list comprehension

                    On Aug 1, 12:35 pm, "Chris Mellon" <arka...@gmail. comwrote:
                    On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:
                    >
                    >
                    >
                    >
                    >
                    On Aug 1, 11:28 am, "Chris Mellon" <arka...@gmail. comwrote:
                    On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:
                    >
                    Hi,
                    >
                    Does anyone know how to put an assertion in list comprehension? I have
                    the following list comprehension, but I want to use an assertion to
                    check the contents of rec_stdl. I ended up using another loop which
                    essentially duplicates the functions of list comprehension. It just
                    look like a waste of coding and computer time to me.
                    >
                    I just wish I could put the assertions into list comprehensions.
                    >
                    x=[(rec_stdl[0].st/10000.0,
                    rec_stdl[0].cl,
                    rec_stdl[0].bb,
                    rec_stdl[0].bo,
                    rec_stdl[1].bb,
                    rec_stdl[1].bo,
                    rec_stdl[0].ex
                    )
                    for rec_stdl in rec_by_ex if len(rec_stdl)== 2
                    ]
                    >
                    #duplicated loop
                    if __debug__:
                    for rec_stdl in rec_by_ex:
                    l=len(rec_stdl)
                    assert(l<=2 and l>0)
                    if l==2:
                    assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
                    assert(rec_stdl[0].ex==rec_stdl[1].ex)
                    assert(rec_stdl[0].st==rec_stdl[1].st)
                    assert(rec_stdl[0].cp==rec_stdl[1].cp)
                    >
                    First: All your asserts are wrong. Assert is a statement, not a
                    function. These specific ones will behave as expected, but it's easy
                    to accidentally write ones that always pass this way.
                    >
                    Do you mean I should not use the parentheses?
                    >
                    Yes.
                    >
                    >
                    >
                    Secondly: This is a waste of code, because if __debug__ is not defined
                    asserts will be skipped by the compiler. You could use the same loop
                    block for both branches.
                    >
                    I know. My original question was how. Dan suggested to write a checker
                    function.
                    >
                    Use the second block in all cases. In any situation where "if
                    __debug__" is False, the asserts are a noop and in fact won't even be
                    present in the bytecode.
                    >
                    >
                    >
                    >
                    >
                    Thirdly: This sort of testing is precisely what unit tests and/or
                    doctests are for.
                    >
                    Agreed.
                    >>
                    - Show quoted text -- Hide quoted text -
                    >
                    - Show quoted text -
                    I see. In fact I want to whole block surrounded by __debug__ to be
                    optimized away in non-debug runs. If the logic of my program is
                    correct, the asserts are guaranteed to be true, no matter what the
                    input is. It is not input checking.

                    Comment

                    • beginner

                      #11
                      Re: Assertion in list comprehension

                      On Aug 1, 12:38 pm, "Chris Mellon" <arka...@gmail. comwrote:
                      On 01 Aug 2007 16:55:53 GMT, Stargaming <stargam...@gma il.comwrote:
                      >
                      >
                      >
                      >
                      >
                      On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
                      >
                      On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:
                      >Hi,
                      >
                      >Does anyone know how to put an assertion in list comprehension? I have
                      >the following list comprehension, but I want to use an assertion to
                      >check the contents of rec_stdl. I ended up using another loop which
                      >essentially duplicates the functions of list comprehension. It just
                      >look like a waste of coding and computer time to me.
                      >
                      >I just wish I could put the assertions into list comprehensions.
                      >
                      > x=[(rec_stdl[0].st/10000.0,
                      > rec_stdl[0].cl,
                      > rec_stdl[0].bb,
                      > rec_stdl[0].bo,
                      > rec_stdl[1].bb,
                      > rec_stdl[1].bo,
                      > rec_stdl[0].ex
                      > )
                      > for rec_stdl in rec_by_ex if len(rec_stdl)== 2
                      > ]
                      >
                      > #duplicated loop
                      > if __debug__:
                      > for rec_stdl in rec_by_ex:
                      > l=len(rec_stdl)
                      > assert(l<=2 and l>0)
                      > if l==2:
                      > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
                      > assert(rec_stdl[0].ex==rec_stdl[1].ex)
                      > assert(rec_stdl[0].st==rec_stdl[1].st)
                      > assert(rec_stdl[0].cp==rec_stdl[1].cp)
                      >
                      First: All your asserts are wrong. Assert is a statement, not a
                      function. These specific ones will behave as expected, but it's easy to
                      accidentally write ones that always pass this way.
                      >
                      Could you come up with an example? I can only think of accidentally
                      injecting a comma, what would create a (true, in a boolean context) tuple.
                      >
                      And, well, if you're only using () for readabilty, this might sometimes
                      look messy when calling assert with the extended syntax::
                      >
                      assert(False), "error text"
                      >
                      It's very easy to write this as assert(False, "error text") if you're
                      in the habit of thinking that assert is a function.
                      >
                      Where one could expect the construction of a tuple.
                      >
                      Secondly: This is a waste of code, because if __debug__ is not defined
                      asserts will be skipped by the compiler. You could use the same loop
                      block for both branches.
                      >
                      Well, the `assert` isn't there for no reason, but if you're serious about
                      it, `raise` could be better.
                      >
                      Thirdly: This sort of testing is precisely what unit tests and/or
                      doctests are for.
                      >
                      Huh? What beginner is doing there seems more like input validation than
                      testing. Unit or doctests are meant for testing (and in case of doctests,
                      showing) whether a function works as expected.
                      >
                      Not in a big __debug__ block it isn't.

                      No I was trying to test the logic of my code not validating the
                      contents of the data. Thanks.
                      >
                      >>
                      - Show quoted text -- Hide quoted text -
                      >
                      - Show quoted text -

                      Comment

                      • Chris Mellon

                        #12
                        Re: Assertion in list comprehension

                        On 8/1/07, beginner <zyzhu2000@gmai l.comwrote:
                        On Aug 1, 12:35 pm, "Chris Mellon" <arka...@gmail. comwrote:
                        On 8/1/07, beginner <zyzhu2...@gmai l.comwrote:
                        <much snippage>
                        I see. In fact I want to whole block surrounded by __debug__ to be
                        optimized away in non-debug runs. If the logic of my program is
                        correct, the asserts are guaranteed to be true, no matter what the
                        input is. It is not input checking.
                        >
                        I inferred as much. That's why placing it in a unit or doctest is even better.

                        Comment

                        Working...