2

Hi I am trying to implement conditional compilation in python similar to this in C,I have seen this thread and this thread.

But this is not working. I am relatively new to python,how can we fix this ?

Community
  • 1
  • 1
Quixotic
  • 2,424
  • 7
  • 36
  • 58

5 Answers5

2

Looks like you are trying to use this to submit solutions to an online judge. For gcc, the judge machine supplies a paramater -D ONLINE_JUDGE. This has the same effect as having the following in your code:

#define ONLINE_JUDGE

Python does not have a preprocessor. So there is no way of defining a macro (in the same sense as in C) either within your code or from the command line when you invoke the interpreter. So, I think it is unlikely that the online judge provides a similar option for Python. But it might provide something as a command line argument that you might be able to use via sys.argv[1:]. Check the command used to invoke Python (must be mentioned somewhere on their website) at the online judge.

MAK
  • 26,140
  • 11
  • 55
  • 86
  • I checked [here](http://www.codeforces.com/blog/entry/79), so i guess I have to make the arrangement myself. Thanks. – Quixotic Mar 14 '11 at 06:44
  • @Tretwick Marian: Ah CodeForces :). You can request for them add a parameter to their command line. They are quite receptive about feedback. – MAK Mar 14 '11 at 06:52
  • It's not just codeforces only,I guess the standard is pretty much same for all other judges like spoj and sgu, I am more active in spoj however:-) – Quixotic Mar 14 '11 at 06:59
2

A bit too heavy for my taste. How about this one:

import sys
try: fin = open('Problem.in')
except: fin = sys.stdin

Universal and really convenient per se. Two in one.

gorlum0
  • 1,425
  • 12
  • 12
1

You need to define the ONLINE_JUDGE variable - this is an "if", not really an "ifdef".

ONLINE_JUDGE = 0
if ONLINE_JUDGE:
    import math
E.Benoît
  • 796
  • 6
  • 9
  • No,the ONLINE_JUDGE is defined to be true in the online judges then this won't work I guess. – Quixotic Mar 14 '11 at 06:34
  • @Tretwick: That comment makes no sense. Names do not simply "appear"; either they are defined, or they are imported. – Ignacio Vazquez-Abrams Mar 14 '11 at 06:37
  • @Ignacio Vazquez-Abrams: I said that names is defined to be true in online judges read my comment carefully.The original site seems down check out this [cached](http://webcache.googleusercontent.com/search?q=cache:xxdOhpKUYYQJ:acm.tju.edu.cn/toj/faq.html+%23define+ONLINE_JUDGE&cd=5&hl=en&ct=clnk&gl=in&source=www.google.co.in) – Quixotic Mar 14 '11 at 06:43
  • That's a C define. Python is not C. – Ignacio Vazquez-Abrams Mar 14 '11 at 06:44
  • @Tretwick Marian: from your link: "then you may try to read some problems and submit your solution in C/C++/Pascal/Java." - I don't think it supports Python. – E.Benoît Mar 14 '11 at 06:46
  • @Ignacio Vazquez-Abrams:No I python is not C but it is [written in C](http://webcache.googleusercontent.com/search?q=cache:P1mpxQ4d0aoJ:en.wikipedia.org/wiki/CPython+cpython&cd=1&hl=en&ct=clnk&gl=in&source=www.google.co.in) – Quixotic Mar 14 '11 at 06:47
  • @Tretwick: CPython is written in C. Some other implementations are written in other languages. – Ignacio Vazquez-Abrams Mar 14 '11 at 06:49
  • @Ignacio Vazquez-Abrams:What those other languages are written in ? – Quixotic Mar 14 '11 at 07:56
  • @Tretwick: Jython is written in Java. IronPython is written in C#. PyPy is written in Python. – Ignacio Vazquez-Abrams Mar 14 '11 at 08:01
  • @Ignacio Vazquez-Abrams: if CPython is written in C then PyPy is written in RPython. There is a significant difference between RPython and Python as programming languages. – jfs Mar 14 '11 at 08:05
1

codeforces.com (linked in your comment) invokes Python scripts as python -O %s. You can detect it in your script via __debug__. Compare:

$ python -c 'print __debug__'
True

and

$ python -O -c 'print __debug__'
False

So you could write in your script:

ONLINE_JUDGE = not __debug__
# ...
if ONLINE_JUDGE:
   pass # here goes online judge specific stuff
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

Use pypreprocessor

Which can also be found at the PYPI (Python Package Index) so it can be loaded using pip.

Your specific example would look something like:

from pypreprocessor import pypreprocessor
pypreprocessor.parse()

#define onlinejudge

#ifdef onlinejudge
import math
#endif

To adding a define via the command line is also easily accomplished:

import sys
from pypreprocessor import pypreprocessor

#exclude

# defined if 'mode' is found in the command line arguments
if 'mode1' in sys.argv:
    pypreprocessor.defines.append('mode1')

# defined if 'mode2' is found in the command line arguments
if 'mode2' in sys.argv:
    pypreprocessor.defines.append('mode2')

#endexclude

pypreprocessor.parse()

#ifdef mode1
print('this script is running in mode1')
#ifdef mode2
print('this script is running in mode2')
#else
print('no modes specified')
#endif

Here's what the outputs should produce...

'python prog.py mode1':

this script is running in mode1

'python prog.py mode2':

this script is running in mode2

'python prog.py mode1 mode2':

this script is running in mode1 this script is running in mode2

'python prog.py':

no modes specified

SideNote: pypreprocessor supports both python2x and python3k.

Disclaimer: I'm the author of pypreprocessor

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94