1

script such as python and high level language such as C looks different in this behavior but I want to know there is possible way or other way to realize that.

In my python script, in one point of view, I want to see the procedure of Monte-Carlo simulation to see what is happening and in the other point, I want to run the dynamics and see the final result at the end. At the moment it is possible by changing some parts as below. But if I am going to use two different main.py's it would be very cumbersome because whenever you change something in a file, you need to change the same part in the other file. The present method is as below. This is the structure of animation script which shows every frame during simulation

import python_animation_modules
import ...

def init():
  bla, bla
  return lines,

def simulation():
  globals
  bla, bla
  set_lines() - redefine data using set_data
  return lines,

animation.FuncAniaion(..frames=Nstep..)

plt.show()

I can change this to show the last figure.

def init():
  bla, bla
  return lines,

while (i<Nax_num):
#def simulation():
#  globals
  bla, bla
  set_lines()       # redefine data using set_data
  #return lines,

#animation.FuncAniation(...MAX_num.)
plt.show()

Is there possible way such as C using preprocessor or other delicate technic in python. In C, this is possible,

#define DYNAMICS

#ifndef DYNAMICS
while( i< MAX_num):
#else
def simulation():
#  globals
Joonho Park
  • 511
  • 5
  • 17
  • 1
    You can make control flow statements to do this, but if you're asking whether there's a preprocessor in python => nope :) – anthony sottile Mar 06 '17 at 02:21
  • [This answer](http://stackoverflow.com/a/2987538/1270789) points to a Python module [now on GitHub](https://github.com/trentm/preprocess) that allows you to say `# #ifndef DYNAMICS`, it seems. I've never tried it, so please don't ask me any more about it! – Ken Y-N Mar 06 '17 at 02:27
  • To Anthony: Thank you for your correct answer. In my thought it is just to write the same code twice, inside the function of animation and outside again to use while-sentence, isn't it? – Joonho Park Mar 06 '17 at 02:32
  • Oops, [that answer](http://stackoverflow.com/a/2987538/1270789) seems to describe another project - not quite sure how I got there, but [this one](https://github.com/trentm/preprocess) looks like the better of the two. – Ken Y-N Mar 06 '17 at 02:32
  • You can `try` and `except NameError` to catch the name errors that will pop up if you use a name that isn't defined (`AttributeError` for missing attributes) ... However I haven't really seen the need for this in Python code... – mgilson Mar 06 '17 at 02:36
  • 3
    You have an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem); you've already decided on your solution, and you're asking how to implement it. You'd be better off asking about your problem. Your problem is your code is poorly designed and your subroutines do too much and have too many side effects. Your solution is to use C macros. If you do that, you will have two problems. You should ask instead about the design of your code and give more detail about the problem you're trying to solve. – Schwern Mar 06 '17 at 02:43
  • It would be better if I load the script here but it is too large. In my animation, the figure shows one additional line in the next frame. To see the working of simulation, I wrote the animation code. Now I need to raise the number of lines, a linear rod to thousand or ten thousand. Because I confirmed the algorithm, just I want to see the results of long time simulation. Do I need to show more explanation? – Joonho Park Mar 06 '17 at 03:16

1 Answers1

1

You seem to be wanting to use #IFDEF to keep from repeating code... but why not do it explicitly using code? something like:

def simulate():
    bla, bla
    set_lines()

def simulation():
    globals
    simulate()
    return lines,

def dynamics():
    animation.FuncAniation(...MAX_num.)

def no_dynamics():
    while (i < MAX_num):
        simulate()

if DYNAMICS: dynamics()
else: no_dynamics()
plt.show()

The root idea is: factor out the common bits into its own routine, then call that from wrappers that contain case-specific code.

pjz
  • 41,842
  • 6
  • 48
  • 60
  • This is exactly what I want if it's possible. But all the Monte-Carlo simulation parts are at the moment located in simulation(). Then I need to decompose simulation() and simulate() where all the MC codes exist in simulate(). I want to continue this discussion after I do something more. – Joonho Park Mar 06 '17 at 03:56