473,405 Members | 2,187 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,405 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 1224
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):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.