Return variables from modules ??

R

Rigga

Hi,

I am new to Python and am currentky just playing with some simple functions however
I can not work out how to return a variable back from a module, probably easier if you see the code..
what I want it to do is to repeat the while loop until it is no longer equal to 'repeat',
code is below, go easy on me!:
===========================================================================
import sys
import os
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK | os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
sys.exit()

==============================================================================

I have tried setting reply as a global variable but to no avail, I assume that I need to pass the variable
back from the chkpth module but I do not know how to, any help appreciated.

Regards

Rig
 
S

Skip Montanaro

Rigga> I am new to Python and am currentky just playing with some simple
Rigga> functions however I can not work out how to return a variable
Rigga> back from a module...

This is not possible. Modules are not callable objects. They define
namespaces which hold other objects, among which are functions, which are
callable.

Your module becomes something like

import sys import os

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK | os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

if __name__ == "__main__":
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!

Suppose the above code is in chkpth.py. You can execute

python chkpth.py

to run it standalone, or from other Python code write:

import chkpth

...

result = chkpth.chkpth(somepath)

Skip
 
D

Dave Kuhlman

Rigga said:
Hi,

I am new to Python and am currentky just playing with some simple
functions however I can not work out how to return a variable back
from a module, probably easier if you see the code.. what I want
it to do is to repeat the while loop until it is no longer equal
to 'repeat', code is below, go easy on me!:
===========================================================================
import sys import os
# Check that the folder is accessible and writeable
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
sys.exit()

==============================================================================

I have tried setting reply as a global variable but to no avail, I
assume that I need to pass the variable back from the chkpth
module but I do not know how to, any help appreciated.

A common way to do this is to put the code in your module into a
function (in that module) and at the end of that function, return
a value. Then do:

import mymodule
reply = mymodule.myfunction()

And, here is how you might modify your module:

===========================================================================
import sys
import os

# Check that the folder is accessible and writeable
def myfunction():
reply = 'repeat'
while reply == 'repeat' :
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth
result
print reply # always prints repeat no matter what!
return reply
==============================================================================

Dave
 
R

Rigga

I have tried the code you supplied however it still does not return the
reply as expected, I have even tried the following code but I just cant get
it to return a variable from a module:

My module: we will call it filechk.py
********************************************************************
import sys
import os

def myfunction():
reply = 'repeat'
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

return chkpth

print chkpth(FilePath) # used to show me chkpth result
print reply # always prints repeat no matter what!
return reply

********************************************************************

Main program: we will call it testme.py

********************************************************************
import filechk
result = filechk.myfunction()
print result

********************************************************************


results when ran:
---------------------------------------------------
root@mybox:> python testme.py
root@mybox:> Enter Path to files: /root/
root@mybox:> None


What I expected to happen in the example above for it to return OK (and if
I queried the reply variable for it to return 'stop)

What am I doing wrong?
 
M

Mark Roach

I have tried the code you supplied however it still does not return the
reply as expected, I have even tried the following code but I just cant get
it to return a variable from a module:

My module: we will call it filechk.py
********************************************************************
import sys
import os

def myfunction():
reply = 'repeat'
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):

Here you define chkpth, but you never call it... Why have you made this
into a separate function inside of myfunction? It seems you should remove
this def and unindent everything under it.

-Mark
 
D

Duncan Booth

What am I doing wrong?

You are assigning to the local variable 'reply' inside the function chkpth.
This is not the same as the variable 'reply' in 'myfunction'.

You seem to be keen on the idea of reusing variable names, as I see you
also have a local variable 'chkpth' inside the function 'chkpth'. Python is
a free programming language, you don't actually get charged for each new
variable you use, so be generous and use some different names.

You also seem to have a slight indentation problem so it looks like chkpth
(the function, not the variable) is never going to get called.

Try something like this:

import sys
import os

def myfunction():
# assignment to outer reply variable removed from here.
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

# Returning the reply and the chkpth variable
return reply, chkpth


# Pick up the results so we can use them
reply, result = chkpth(FilePath) # used to show me chkpth
result
print result
print reply # always prints repeat no matter what!
return reply
 
R

Rigga

Duncan said:
You are assigning to the local variable 'reply' inside the function
chkpth. This is not the same as the variable 'reply' in 'myfunction'.

You seem to be keen on the idea of reusing variable names, as I see you
also have a local variable 'chkpth' inside the function 'chkpth'. Python
is a free programming language, you don't actually get charged for each
new variable you use, so be generous and use some different names.

You also seem to have a slight indentation problem so it looks like chkpth
(the function, not the variable) is never going to get called.

Try something like this:

import sys
import os

def myfunction():
# assignment to outer reply variable removed from here.
FilePath = raw_input("Enter path to files: ")

def chkpth(FilePath):
if os.path.exists(FilePath):
# File location exists
AccFlag = os.access(FilePath,os.R_OK |
os.X_OK | os.W_OK)

if (AccFlag):
# Cool you have FULL access to
# the location
chkpth = "OK"
reply = 'stop'
else:
# You do not have access
chkpth = "DENIED"
reply = 'repeat'

else:
# No files found exiting...
chkpth = "NOT FOUND"
reply = 'repeat'

# Returning the reply and the chkpth variable
return reply, chkpth


# Pick up the results so we can use them
reply, result = chkpth(FilePath) # used to show me chkpth
result
print result
print reply # always prints repeat no matter what!
return reply
er yeah well spotted!! I did get a bit confused didnt I lol. THanks for
clearing it up for me it all maks sense now.

Cheers

RiGGa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top