Importing functions that require parameters
Chris
cwitts at gmail.com
Mon Dec 10 06:10:13 EST 2007
On Dec 10, 12:41 pm, Matt_D <matt.debo... at gmail.com> wrote:
> Good afternoon.
>
> As a self-tutoring project I am writing a one-time-pad encrypt/decrypt
> script. I have completed the encryption portion and am working
> currently on the decryption algorithm. My goal is to have the encrypt
> and decrypt be individual modules vice two parts of the same.
>
> My problem, or perhaps more accurately, question, lies in importing a
> function from the otp_encrypt script. Here is the function I am
> attempting to call:
>
> def get_key(ptext):
> """Convert one-time-pad to uppercase, and strip spaces. On final
> line slice pad to match length of plain text. (OTP will not work if
> len(pad) != len(plaintext)"""
> ptext = upper_case(ptext)
> otp = # key removed just due to sheer length
> otp = string.upper(otp)
> new = ""
> for letter in otp:
> if letter in string.uppercase:
> new += letter
> return new[:len(ptext)]
>
> The parameter of get_key is sys.argv[1]. Now I understand why I'm
> getting the errors I'm getting (invalid syntax if I include () or
> ([parameter], or an IndexError if I don't include those), but my
> question is, is it feasible to import a function from a module when
> that function requires a parameter from elsewhere in the imported
> module? Or is it just better to just import * in all cases?
How is it requiring parameters from the module you are calling ? Do
you mean you just want to import the get_key() function by itself and
leave the rest of the module ?
# get_key.py
import re
def get_key(ptext):
otp = # Seriously large key.
return ''.join(re.findall('[A-Z]', otp.upper())[:len(ptext)]
That's what your code looks like it's doing...
# otp_encrypt.py
from get_key import get_key
"""Alternatively you can do
import get_key
getkey = get_key.get_key
"""
I not entirely sure if that is of help to you.
More information about the Python-list
mailing list