473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

conditional print statement ?

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
Apr 25 '07 #1
9 2326
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
Apr 25 '07 #2

"Stef Mientki" <S.**************@mailbox.kun.nlwrote in message
news:2c***************************@news.speedlinq. 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

Apr 25 '07 #3
On 2007-04-25, Stef Mientki <S.**************@mailbox.kun.nlwrote:
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
Apr 26 '07 #4
Antoon Pardon wrote:
On 2007-04-25, Stef Mientki <S.**************@mailbox.kun.nlwrote:
>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
Apr 26 '07 #5
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.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

Apr 26 '07 #6
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
Apr 27 '07 #7
On Apr 26, 7:31 am, Dustan <DustanGro...@gmail.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.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

Apr 27 '07 #8
Paul McGuire <pt***@austin.rr.comwrote:
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.
Apr 27 '07 #9
On Apr 27, 9:45 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Paul McGuire <p...@austin.rr.comwrote:
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

Apr 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Max | last post by:
Hi, I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a...
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
7
by: Mark Hobley | last post by:
I have some information that states that the if conditional can be be inverted from the traditional syntax if (EXPRESSION) BLOCK to an alternative syntax: if BLOCK (EXPRESSION); I have a...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.