isuhendro
asked on
Include file / module / package in PYTHON
dear experts,
i have a working python script within one file, but would like to split some part of the code for easy maintenance
Below is partial of the script called it monitor.py, and I would like to remove class Activity defined within the script as something like "include" file, so that can be reused by other script.
Could you advice me?
Thanks!!
i have a working python script within one file, but would like to split some part of the code for easy maintenance
Below is partial of the script called it monitor.py, and I would like to remove class Activity defined within the script as something like "include" file, so that can be reused by other script.
Could you advice me?
Thanks!!
import re, os, time, thread
class Activity(object):
def __init__(self, id=None, status=None, device_pin=None, start=None, duration=None, mobile=None, email=None):
self.id = id
self.status = status
self.device_pin = device_pin
self.start = start
self.duration = duration
self.mobile = mobile
self.email = email
def print_info(self):
print self.id, ":" + self.device_pin + "," + self.start + "," +self.duration + "," +self.mobile + "," +self.email
def process(activity):
activity.print_info()
print 'This is a thread for activity id %s' % (activity.id)
print "Starting"
opt_sleeping_time = 5
while True:
print "Fetching new jobs"
os.popen("copy jobs.xml jobs")
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much!!
Hi isuhendro,
I think the answer is in the comment above from my learned colleague pepr. You posted your question after he posted his comment, but perhaps you hadn't seen it at that point. Since you have now accepted it as the answer to your question, I trust it enabled you to solve your problem. If not, post another question here and I'm sure someone will help you.
--efn
I think the answer is in the comment above from my learned colleague pepr. You posted your question after he posted his comment, but perhaps you hadn't seen it at that point. Since you have now accepted it as the answer to your question, I trust it enabled you to solve your problem. If not, post another question here and I'm sure someone will help you.
--efn
The Python style guide says to use
import x
import y
import z
instead of
import x, y, z
Now, the command
from activity import Activity
However, it is a good idea not to use the "activity" identifier in your script if you use the module with the same name.
import x
import y
import z
instead of
import x, y, z
Now, the command
from activity import Activity
However, it is a good idea not to use the "activity" identifier in your script if you use the module with the same name.
ASKER
1) add import activity
2) print sys.path (line 85 and 86) to confirm
But the application generate error below.
any ideas, which one is wrong?
Thanks.
sys.path =
c:\Documents and Settings\soehendr\My Documents\python\CA
C:\WINDOWS\system32\python
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packa
Fetching new jobs
Traceback (most recent call last):
File "monitor.py", line 194, in <module>
_main(sys.argv)
File "monitor.py", line 162, in _main
activity = Activity()
NameError: global name 'Activity' is not defined
Open in new window