conditional print statement ?

Discussion in 'Python' started by Stef Mientki, Apr 25, 2007.

  1. Stef Mientki

    Stef Mientki Guest

    hello,


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

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

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

    thanks,
    Stef Mientki
    Stef Mientki, Apr 25, 2007
    #1
    1. Advertising

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


    d=Datafile.readline()
    if Print_info: print d

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

    HTH,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Apr 25, 2007
    #2
    1. Advertising

  3. Stef Mientki

    Terry Reedy Guest

    "Stef Mientki" <> wrote in message
    news:2c923$462fb3e0$d443bb3a$...
    | if Print_Info: print Datafile.readline()
    | else: Datafile.readline()

    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.readline()".

    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.readline # followed by
    ....
    dread() # as needed

    Terry Jan Reedy
    Terry Reedy, Apr 25, 2007
    #3
  4. On 2007-04-25, Stef Mientki <> wrote:
    > hello,
    >
    >
    > As part of a procedure I've a number sequences like this:
    >
    ><Python>
    > if Print_Info: print Datafile.readline()
    > else: Datafile.readline()
    ></Python>
    >
    > Is there a more compressed way to write such a statement,
    > especially I dislike the redundancy "Datafile.readline()".
    >
    > thanks,
    > Stef Mientki


    You could consider the following

    def Print(arg):
    print arg

    def Noop(arg):
    pass

    (Print if Print_Info else Noop) (Datafile.readline())

    --
    Antoon Pardon
    Antoon Pardon, Apr 26, 2007
    #4
  5. Stef Mientki

    stef Guest

    Antoon Pardon wrote:
    > On 2007-04-25, Stef Mientki <> wrote:
    >
    >> hello,
    >>
    >>
    >> As part of a procedure I've a number sequences like this:
    >>
    >> <Python>
    >> if Print_Info: print Datafile.readline()
    >> else: Datafile.readline()
    >> </Python>
    >>
    >> Is there a more compressed way to write such a statement,
    >> especially I dislike the redundancy "Datafile.readline()".
    >>
    >> thanks,
    >> Stef Mientki
    >>

    >
    > You could consider the following
    >
    > def Print(arg):
    > print arg
    >
    > def Noop(arg):
    > pass
    >
    > (Print if Print_Info else Noop) (Datafile.readline())
    >
    >

    thank you all for your answers,
    I'll play a little with the suggested solutions.

    cheers,
    Stef Mientki
    stef, Apr 26, 2007
    #5
  6. Stef Mientki

    Dustan Guest

    On Apr 26, 1:58 am, Antoon Pardon <> wrote:
    > On 2007-04-25, Stef Mientki <> wrote:
    >
    > > hello,

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

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

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

    >
    > > 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(Datafile.readline())

    > (Print if Print_Info else Noop) (Datafile.readline())
    >
    > --
    > Antoon Pardon
    Dustan, Apr 26, 2007
    #6
  7. Stef Mientki

    stef Guest


    > or (untested):
    >
    > if Print_Info:
    > def printOrNot(arg):
    > print arg
    > else:
    > def printOrNot(arg):
    > pass
    >
    > printOrNot(Datafile.readline())
    >
    >

    thanks for the creative solution, and indeed it does work ;-)

    cheers,
    Stef Mientki
    stef, Apr 27, 2007
    #7
  8. Stef Mientki

    Paul McGuire Guest

    On Apr 26, 7:31 am, Dustan <> wrote:
    > On Apr 26, 1:58 am, Antoon Pardon <> wrote:
    >
    >
    >
    >
    >
    > > On 2007-04-25, Stef Mientki <> wrote:

    >
    > > > hello,

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

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

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

    >
    > > > 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(Datafile.readline())
    >
    >
    >
    > > (Print if Print_Info else Noop) (Datafile.readline())

    >
    > > --
    > > 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/PythonDecoratorLibrary?highlight=%28decorator
    %29#head-8298dbf9ac7325d9ef15e7130e676378bbbda572) help you do
    something very similar, without having to replicate the function being
    enabled/disabled.

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

    -- Paul
    Paul McGuire, Apr 27, 2007
    #8
  9. Stef Mientki

    Duncan Booth Guest

    Paul McGuire <> wrote:

    > The Enable/Disable decorators on the Python wiki (http://
    > wiki.python.org/moin/PythonDecoratorLibrary?highlight=%28decorator
    > %29#head-8298dbf9ac7325d9ef15e7130e676378bbbda572) help you do
    > something very similar, without having to replicate the function being
    > enabled/disabled.
    >
    > @(disabled,enabled)[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.
    Duncan Booth, Apr 27, 2007
    #9
  10. Stef Mientki

    Paul McGuire Guest

    On Apr 27, 9:45 am, Duncan Booth <> wrote:
    > Paul McGuire <> wrote:
    > > The Enable/Disable decorators on the Python wiki (http://
    > > wiki.python.org/moin/PythonDecoratorLibrary?highlight=%28decorator
    > > %29#head-8298dbf9ac7325d9ef15e7130e676378bbbda572) help you do
    > > something very similar, without having to replicate the function being
    > > enabled/disabled.

    >
    > > @(disabled,enabled)[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,enabled)[debugFlag] # <-- proper way to do this

    @state
    def debugPrint(s):
    print s

    print "here comes some debug output"
    debugPrint("xyzzy 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
    Paul McGuire, Apr 27, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page