471,338 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Importing functions that require parameters

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?
Dec 10 '07 #1
5 1152
On Dec 10, 12:41 pm, Matt_D <matt.debo...@gmail.comwrote:
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.
Dec 10 '07 #2
On Dec 10, 9:41 pm, Matt_D <matt.debo...@gmail.comwrote:
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()
?
Dec 10 '07 #3
On Dec 10, 2:46 pm, John Machin <sjmac...@lexicon.netwrote:
"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 consolein <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.
Dec 10 '07 #4
Matt_D wrote:
>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 consolein <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
Dec 10 '07 #5
On Dec 10, 4:49 pm, Peter Otten <__pete...@web.dewrote:
>
Peter
Thanks, Peter. You answered my question precisely. I'm successfully
encrypting and decrypting now. Thank you again.

R,

Matt
Dec 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
reply views Thread by Mark English | last post: by
7 posts views Thread by Darren | last post: by
7 posts views Thread by Bob Stearns | last post: by
reply views Thread by Mike Collins | last post: by
2 posts views Thread by Mike Collins | last post: by
318 posts views Thread by jacob navia | last post: by
3 posts views Thread by Matthew Wilson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.