conditional print statement ?

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

    #1

    conditional print statement ?

    hello,


    As part of a procedure I've a number sequences like this:

    <Python>
    if Print_Info: print Datafile.readli ne()
    else: Datafile.readli ne()
    </Python>

    Is there a more compressed way to write such a statement,
    especially I dislike the redundancy "Datafile.readl ine()".

    thanks,
    Stef Mientki
  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: conditional print statement ?

    Stef Mientki schrieb:
    hello,
    >
    >
    As part of a procedure I've a number sequences like this:
    >
    <Python>
    if Print_Info: print Datafile.readli ne()
    else: Datafile.readli ne()
    </Python>
    >
    Is there a more compressed way to write such a statement,
    especially I dislike the redundancy "Datafile.readl ine()".
    d=Datafile.read line()
    if Print_info: print d

    It's still two lines, but only has a single call to .readline().

    HTH,
    Martin

    Comment

    • Terry Reedy

      #3
      Re: conditional print statement ?


      "Stef Mientki" <S.Mientki-nospam@mailbox. kun.nlwrote in message
      news:2c923$462f b3e0$d443bb3a$1 8429@news.speed linq.nl...
      | if Print_Info: print Datafile.readli ne()
      | else: Datafile.readli ne()

      Since both branches discard the data read, I presume Martin's fix is what
      you really want.

      | Is there a more compressed way to write such a statement,
      | especially I dislike the redundancy "Datafile.readl ine()".

      But for future reference, if you really do need to call a method in
      multiple places (or even just multiple times in a loop) you can condense
      like so:

      dread = Datafile.readli ne # followed by
      ....
      dread() # as needed

      Terry Jan Reedy





      Comment

      • Antoon Pardon

        #4
        Re: conditional print statement ?

        On 2007-04-25, Stef Mientki <S.Mientki-nospam@mailbox. kun.nlwrote:
        hello,
        >
        >
        As part of a procedure I've a number sequences like this:
        >
        ><Python>
        if Print_Info: print Datafile.readli ne()
        else: Datafile.readli ne()
        ></Python>
        >
        Is there a more compressed way to write such a statement,
        especially I dislike the redundancy "Datafile.readl ine()".
        >
        thanks,
        Stef Mientki
        You could consider the following

        def Print(arg):
        print arg

        def Noop(arg):
        pass

        (Print if Print_Info else Noop) (Datafile.readl ine())

        --
        Antoon Pardon

        Comment

        • stef

          #5
          Re: conditional print statement ?

          Antoon Pardon wrote:
          On 2007-04-25, Stef Mientki <S.Mientki-nospam@mailbox. kun.nlwrote:
          >
          >hello,
          >>
          >>
          >As part of a procedure I've a number sequences like this:
          >>
          ><Python>
          > if Print_Info: print Datafile.readli ne()
          > else: Datafile.readli ne()
          ></Python>
          >>
          >Is there a more compressed way to write such a statement,
          >especially I dislike the redundancy "Datafile.readl ine()".
          >>
          >thanks,
          >Stef Mientki
          >>
          >
          You could consider the following
          >
          def Print(arg):
          print arg
          >
          def Noop(arg):
          pass
          >
          (Print if Print_Info else Noop) (Datafile.readl ine())
          >
          >
          thank you all for your answers,
          I'll play a little with the suggested solutions.

          cheers,
          Stef Mientki

          Comment

          • Dustan

            #6
            Re: conditional print statement ?

            On Apr 26, 1:58 am, Antoon Pardon <apar...@forel. vub.ac.bewrote:
            On 2007-04-25, Stef Mientki <S.Mientki-nos...@mailbox. kun.nlwrote:
            >
            hello,
            >
            As part of a procedure I've a number sequences like this:
            >
            <Python>
            if Print_Info: print Datafile.readli ne()
            else: Datafile.readli ne()
            </Python>
            >
            Is there a more compressed way to write such a statement,
            especially I dislike the redundancy "Datafile.readl ine()".
            >
            thanks,
            Stef Mientki
            >
            You could consider the following
            >
            def Print(arg):
            print arg
            >
            def Noop(arg):
            pass
            >
            or (untested):

            if Print_Info:
            def printOrNot(arg) :
            print arg
            else:
            def printOrNot(arg) :
            pass

            printOrNot(Data file.readline() )
            (Print if Print_Info else Noop) (Datafile.readl ine())
            >
            --
            Antoon Pardon

            Comment

            • stef

              #7
              Re: conditional print statement ?

              or (untested):
              >
              if Print_Info:
              def printOrNot(arg) :
              print arg
              else:
              def printOrNot(arg) :
              pass
              >
              printOrNot(Data file.readline() )
              >
              >
              thanks for the creative solution, and indeed it does work ;-)

              cheers,
              Stef Mientki

              Comment

              • Paul McGuire

                #8
                Re: conditional print statement ?

                On Apr 26, 7:31 am, Dustan <DustanGro...@g mail.comwrote:
                On Apr 26, 1:58 am, Antoon Pardon <apar...@forel. vub.ac.bewrote:
                >
                >
                >
                >
                >
                On 2007-04-25, Stef Mientki <S.Mientki-nos...@mailbox. kun.nlwrote:
                >
                hello,
                >
                As part of a procedure I've a number sequences like this:
                >
                ><Python>
                if Print_Info: print Datafile.readli ne()
                else: Datafile.readli ne()
                ></Python>
                >
                Is there a more compressed way to write such a statement,
                especially I dislike the redundancy "Datafile.readl ine()".
                >
                thanks,
                Stef Mientki
                >
                You could consider the following
                >
                def Print(arg):
                print arg
                >
                def Noop(arg):
                pass
                >
                or (untested):
                >
                if Print_Info:
                def printOrNot(arg) :
                print arg
                else:
                def printOrNot(arg) :
                pass
                >
                printOrNot(Data file.readline() )
                >
                >
                >
                (Print if Print_Info else Noop) (Datafile.readl ine())
                >
                --
                Antoon Pardon- Hide quoted text -
                >
                - Show quoted text -- Hide quoted text -
                >
                - Show quoted text -
                The Enable/Disable decorators on the Python wiki (http://
                wiki.python.org/moin/PythonDecorator Library?highlig ht=%28decorator
                %29#head-8298dbf9ac7325d 9ef15e7130e6763 78bbbda572) help you do
                something very similar, without having to replicate the function being
                enabled/disabled.

                @(disabled,enab led)[Print_Info]
                def printOrNot(arg) :
                print arg

                -- Paul

                Comment

                • Duncan Booth

                  #9
                  Re: conditional print statement ?

                  Paul McGuire <ptmcg@austin.r r.comwrote:
                  The Enable/Disable decorators on the Python wiki (http://
                  wiki.python.org/moin/PythonDecorator Library?highlig ht=%28decorator
                  %29#head-8298dbf9ac7325d 9ef15e7130e6763 78bbbda572) help you do
                  something very similar, without having to replicate the function being
                  enabled/disabled.
                  >
                  @(disabled,enab led)[Print_Info]
                  def printOrNot(arg) :
                  print arg
                  >
                  Pardon me for asking, but isn't that a syntax error? Decorator syntax is:

                  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE

                  and you don't have a dotted_name.

                  Comment

                  • Paul McGuire

                    #10
                    Re: conditional print statement ?

                    On Apr 27, 9:45 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
                    Paul McGuire <p...@austin.rr .comwrote:
                    The Enable/Disable decorators on the Python wiki (http://
                    wiki.python.org/moin/PythonDecorator Library?highlig ht=%28decorator
                    %29#head-8298dbf9ac7325d 9ef15e7130e6763 78bbbda572) help you do
                    something very similar, without having to replicate the function being
                    enabled/disabled.
                    >
                    @(disabled,enab led)[Print_Info]
                    def printOrNot(arg) :
                    print arg
                    >
                    Pardon me for asking, but isn't that a syntax error? Decorator syntax is:
                    >
                    "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
                    >
                    and you don't have a dotted_name.
                    My bad. The wiki example assigns the appropriate decorator to another
                    name, and then uses that name, like this:

                    debugFlag = int(False)
                    state = (disabled,enabl ed)[debugFlag] # <-- proper way to do this

                    @state
                    def debugPrint(s):
                    print s

                    print "here comes some debug output"
                    debugPrint("xyz zy is the secret word")
                    print "that was it"


                    I think early in the decorator syntax discussions, there were some
                    proposals that decorators could be expressions, but I guess I forgot
                    which way that was decided. The example in this post does work (and
                    so does the one on the wiki) .

                    -- Paul

                    Comment

                    Working...