Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > conditional print statement ?

Reply
Thread Tools

conditional print statement ?

 
 
Stef Mientki
Guest
Posts: n/a
 
      04-25-2007
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      04-25-2007
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      04-25-2007

"Stef Mientki" <S.Mientki-> wrote in message
news:2c923$462fb3e0$d443bb3a$. nl...
| 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





 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      04-26-2007
On 2007-04-25, Stef Mientki <S.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
 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      04-26-2007
Antoon Pardon wrote:
> On 2007-04-25, Stef Mientki <S.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
 
Reply With Quote
 
Dustan
Guest
Posts: n/a
 
      04-26-2007
On Apr 26, 1:58 am, Antoon Pardon <apar...@forel.vub.ac.be> wrote:
> On 2007-04-25, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl> 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



 
Reply With Quote
 
stef
Guest
Posts: n/a
 
      04-27-2007

> 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
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      04-27-2007
On Apr 26, 7:31 am, Dustan <DustanGro...@gmail.com> wrote:
> On Apr 26, 1:58 am, Antoon Pardon <apar...@forel.vub.ac.be> wrote:
>
>
>
>
>
> > On 2007-04-25, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl> 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

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      04-27-2007
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.
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      04-27-2007
On Apr 27, 9:45 am, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> Paul McGuire <p...@austin.rr.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
print 1 to n without any conditional statement, loop or jump. dev_cool C Programming 43 02-27-2007 11:10 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57