0

I need help understanding a concept in Python.

I want to print the specific value of a variable running inside a python loop at the point in time that the import/print code is actually run.

By 'current' value, I mean the (single) value of the variable at the point in time the import and print code is actually run.

The actual code/reason I need to do this is a little abstract and would likely complicate things if I use the exact code, so I have provided a simplified example below for the sake of simplicity and clarity.

I have two files:

file1.py

import datetime
while True:
    x = datetime.datetime.now()
    print x

This when run prints and scrolls the current date/time as shown below:

2017-03-08 11:34:22.363000
2017-03-08 11:34:22.363000
2017-03-08 11:34:22.452000
2017-03-08 11:34:22.452000
2017-03-08 11:34:22.782000
2017-03-08 11:34:22.782000
2017-03-08 11:34:22.970000
....

I would like to be able to run another python file/process that imports and prints the specific value of x at the specific time the import and print code is actually run.

As an example, if the import/print code from file2.py is executed at 2017-03-08 11:34:22.782000 just the following should be printed...

2017-03-08 11:34:22.782000
Process finished with exit code 1

The issue that I have is that when I run the below import and print statements, the date/time prints in a scrolling list which just seems to mirror what file1.py is doing and does not return the specific point in time value of x as I would expect...

file2.py

import file1
print file1.x

So in short, my question is, how can I import and print a specific point in time value of a variable from a repeating process rather than importing and printing the full repeating process.

I hope this makes sense and forgive me if I am missing something here but I have been trying to find a solution to this without success so thought I would see if I can get any pointers here...

Any help is much appreciated. Thanks...

Mark Smith
  • 757
  • 2
  • 16
  • 27
  • 5
    Why not call `datetime.datetime.now()` directly from the other script? Or is this an XY problem? http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – DeepSpace Mar 08 '17 at 12:34
  • 2
    Possible duplicate of [Why is Python running my module when I import it, and how do I stop it?](http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) (and if not then I don't understand what you want) – Tadhg McDonald-Jensen Mar 08 '17 at 12:37
  • Thanks DeepSpace, I am just using date/time as an example here, so I think its an XY problem - I have a variable which is updated by an external process and I need to be able to use the 'point in time' value of the variable but python seems to update the imported output each time the source variables value is updated from the external source... – Mark Smith Mar 08 '17 at 12:38
  • @Tadhg McDonald-Jensen - this is not a duplicate of this question. – Mark Smith Mar 08 '17 at 12:40
  • 2
    @MarkSmith with your code example, it certainly is a duplicate. When you `import test1` in file2.py, the `while True`-loop of file1.py gets executed and you never reach your `print test1.x`-statement (which should throw an error). – Christian König Mar 08 '17 at 12:42
  • 1
    @MarkSmith it is a duplicate, read the duplicate. – Reut Sharabani Mar 08 '17 at 12:46
  • 1
    IMHO you have a confusion between file and process. If an *external process* updates a variable, importing its source does not magically establish a link with the external process but just runs a new copy of the original script. If test1 looks like your code, you do not want to import it. – Serge Ballesta Mar 08 '17 at 12:46
  • 1
    @MarkSmith if this isn't a duplicate you very much need to edit your question since as it currently stands it seems this is solely about the import mechanic, perticularly the statement "just seems to mirror what file1.py is doing" well yeah, file1 gets executed when it's imported, unless it should only run that when it's main... – Tadhg McDonald-Jensen Mar 08 '17 at 12:49
  • Thanks for your replies - Yes I am confused - and I now see why I am getting this problem - but I still don't understand how I can achieve what I need to in terms of 'grabbing' the point in time value of x as I thought just importing file1.x would grab the variable value only and not run the full code (which is not within a module or class...) – Mark Smith Mar 08 '17 at 12:52
  • @Tadhg McDonald-Jensen - Thanks, please forgive me I am a newbie so didnt understand how this was a duplicate - I will review the duplicate you point to and amend/close my question as needed - thanks for pointing me in the right direction... – Mark Smith Mar 08 '17 at 12:54
  • @Reut Sharabani - Thanks, I will review and amend/close my question as needed - I am a python newbie so please forgive my ignorance... – Mark Smith Mar 08 '17 at 12:56
  • @Christian König - thank you for your comment - this really helps and I now see that my question may well be a duplicate - I will do some investigations and either amend or close my question - thanks for your help! – Mark Smith Mar 08 '17 at 13:15
  • @Serge Ballesta - I am starting to see where i have gone wrong or lack understanding - the info you provided helped so thank you! – Mark Smith Mar 08 '17 at 13:16

1 Answers1

1

if you want the act of accessing file1.x to calculate the value of x as if it was a property you can sort of work-around to form a module object that contains properties:

import types, sys, datetime

class UpdateDetector(types.ModuleType):
    @property
    def x(self):
        x = datetime.datetime.now()
        return x

self = UpdateDetector(__name__)

sys.modules[__name__] = self

Note that if this is the sort of behaviour you are looking for it'd be significantly easier (and significantly less annoying to other people who are not expecting this) to not override the module and just use an object with properties instead of a module object. Or better, just use (getter) functions, like datetime.datetime.now() ;)

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • Thank you Tadhg McDonald-Jensen - I need to take some time to understand what you have written above and also what is written in the duplicate you all point to - the actual issue I have is with a variable which is updated via pika/RabbitMQ. I have a process which receives amqp messages and stores them in a variable, when I try to get a specific point in time value for the variable I receive a scrolling list which suggests I am duplicating the receiver code as you and others have pointed out... I will investigate this further and come back on this - thank you (all) very much for your help! – Mark Smith Mar 08 '17 at 13:06