Importing functions that require parameters

John Machin sjmachin at lexicon.net
Mon Dec 10 06:46:58 EST 2007


On Dec 10, 9: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?

"requires a parameter from elsewhere in the imported module" is a
concept I don't understand.

Here is what I think that you need to do in your main script:

import sys
import otp_encrypt
the_key = opt_encrypt.get_key(sys.argv[1])

If that isn't what you want, you'll need to explain the sentence that
starts "Now I understand", with examples of what you have tried.

BTW, how is the uppercase function different from string.upper, and
why aren't you using string methods e.g. otp = otp.upper()
?



More information about the Python-list mailing list