Splitting program up into multiple files to improve readability
up vote
0
down vote
favorite
I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)
edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)
To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".
However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
Here is a snippet of the code:
from Data_Funcs import *
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
Here is Data_Funcs:
def receiveData(FROM=""):
print("In receive data")
if FROM:
print("From:", FROM)
# sendPickledData(dataReady)
dataString = conn.recv(4096)
try:
dataString = pickle.loads(dataString)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)
time.sleep(0.2)
print("Data received:")
print(dataString)
return dataString
def sendPickledData(data):
print("In sendPickled,nSending:",data)
data = pickle.dumps(data)
conn.sendall(data)
time.sleep(0.2)
However when I run the program, I get the error:
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
data = receiveData("handler")
File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
dataString = conn.recv(4096)
NameError: name 'conn' is not defined
Despite the variable conn being defined in the "if name == 'main':" part of the main program
python-3.x import module
add a comment |
up vote
0
down vote
favorite
I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)
edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)
To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".
However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
Here is a snippet of the code:
from Data_Funcs import *
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
Here is Data_Funcs:
def receiveData(FROM=""):
print("In receive data")
if FROM:
print("From:", FROM)
# sendPickledData(dataReady)
dataString = conn.recv(4096)
try:
dataString = pickle.loads(dataString)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)
time.sleep(0.2)
print("Data received:")
print(dataString)
return dataString
def sendPickledData(data):
print("In sendPickled,nSending:",data)
data = pickle.dumps(data)
conn.sendall(data)
time.sleep(0.2)
However when I run the program, I get the error:
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
data = receiveData("handler")
File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
dataString = conn.recv(4096)
NameError: name 'conn' is not defined
Despite the variable conn being defined in the "if name == 'main':" part of the main program
python-3.x import module
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)
edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)
To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".
However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
Here is a snippet of the code:
from Data_Funcs import *
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
Here is Data_Funcs:
def receiveData(FROM=""):
print("In receive data")
if FROM:
print("From:", FROM)
# sendPickledData(dataReady)
dataString = conn.recv(4096)
try:
dataString = pickle.loads(dataString)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)
time.sleep(0.2)
print("Data received:")
print(dataString)
return dataString
def sendPickledData(data):
print("In sendPickled,nSending:",data)
data = pickle.dumps(data)
conn.sendall(data)
time.sleep(0.2)
However when I run the program, I get the error:
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
data = receiveData("handler")
File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
dataString = conn.recv(4096)
NameError: name 'conn' is not defined
Despite the variable conn being defined in the "if name == 'main':" part of the main program
python-3.x import module
I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)
edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)
To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".
However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
Here is a snippet of the code:
from Data_Funcs import *
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
Here is Data_Funcs:
def receiveData(FROM=""):
print("In receive data")
if FROM:
print("From:", FROM)
# sendPickledData(dataReady)
dataString = conn.recv(4096)
try:
dataString = pickle.loads(dataString)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)
time.sleep(0.2)
print("Data received:")
print(dataString)
return dataString
def sendPickledData(data):
print("In sendPickled,nSending:",data)
data = pickle.dumps(data)
conn.sendall(data)
time.sleep(0.2)
However when I run the program, I get the error:
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
data = receiveData("handler")
File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
dataString = conn.recv(4096)
NameError: name 'conn' is not defined
Despite the variable conn being defined in the "if name == 'main':" part of the main program
python-3.x import module
python-3.x import module
asked Nov 11 at 0:29
James Green
407
407
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244754%2fsplitting-program-up-into-multiple-files-to-improve-readability%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown