473,225 Members | 1,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 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 1219
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
7
by: Darren | last post by:
I have been attempting to create a reservation planning form in excel that imports Data from an Access database and inserts that information automaticly into the correct spreed sheet and the...
7
by: Bob Stearns | last post by:
Is there an option in php to do a 'require xxx.php' if, when a function call to xxx is encountered, it is not defined? It would look in all the standard places.
0
by: Mike Collins | last post by:
I am importing a XML file and have not been having the best of luck in doing this, but I do have the following solution below. I will not be importing more than 2000 records at a time, but will be...
2
by: Mike Collins | last post by:
I am importing a XML file and have not been having the best of luck in doing this, but I do have the following solution below. I will not be importing more than 2000 records at a time, but will be...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
3
by: Matthew Wilson | last post by:
I started with a module with a bunch of classes that represent database tables. A lot of these classes have methods that use other classes inside, sort of like this: class C(object):...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.