Importing functions that require parameters

M

Matt_D

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?
 
C

Chris

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.
 
J

John Machin

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()
?
 
M

Matt_D

"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.

When I try:

from otp_encrypt import get_key

I get:

-----------------------------------------------
IndexError Trace

C:\WINDOWS\system32\<ipython console> in <modul

Q:\python\my pys\otp_encrypt.py in <module>()
62 cipher += letter
63 return cipher
64
---> 65 print final(sys.argv[1])
66

IndexError: list index out of range

In [13]: from otp_encrypt import get_key()

I know why I'm getting the error -- I'm importing a function from a
module in iPython with a sys.argv parameter. No big mystery there.
BTW, how is the uppercase function different from string.upper, and
why aren't you using string methods e.g. otp = otp.upper()
?

To be honest, I think I tried it once, but probably left off the ().
When I got an error I more than likely changed it to string.upper(otp)
and since it worked I didn't worry about it. This is like the second
full script I've actually finished so I'm trying to get all my
functionality in first before I start optimizing the script. While I'm
sure things like this are obvious to you, I've only been coding for a
week so any questions like, "Why did you do x when y is much better?"
can probably be answered with, "Stupid newb."

Thanks again.
 
P

Peter Otten

Matt_D said:
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.

When I try:

from otp_encrypt import get_key

I get:

-----------------------------------------------
IndexError Trace

C:\WINDOWS\system32\<ipython console> in <modul

Q:\python\my pys\otp_encrypt.py in <module>()
62 cipher += letter
63 return cipher
64
---> 65 print final(sys.argv[1])
66

IndexError: list index out of range

In [13]: from otp_encrypt import get_key()

I know why I'm getting the error -- I'm importing a function from a
module in iPython with a sys.argv parameter. No big mystery there.

No you don't know -- you are trying to use a module that is meant to work
as a stand-alone script as a library. As a python module is executed when
it is imported, so is the print statement in line 65. To prohibit execution
of the script-only parts use an if-suite, e. g.:

def get_key(...):
# ...

if __name__ == "__main__":
print final(sys.argv[1])


Now the print statement will be executed if you invoke your script from
the command line

$ python otp_encrypt.py

but not by

import otp_encrypt

where the value of __name__ is "otp_encrypt".

Peter
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top