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(ptex t)
otp = # key removed just due to sheer length
otp = string.upper(ot p)
new = ""
for letter in otp:
if letter in string.uppercas e:
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? 5 1238
On Dec 10, 12:41 pm, Matt_D <matt.debo...@g mail.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(ptex t)
otp = # key removed just due to sheer length
otp = string.upper(ot p)
new = ""
for letter in otp:
if letter in string.uppercas e:
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.find all('[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
"""Alternativel y you can do
import get_key
getkey = get_key.get_key
"""
I not entirely sure if that is of help to you.
On Dec 10, 9:41 pm, Matt_D <matt.debo...@g mail.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(ptex t)
otp = # key removed just due to sheer length
otp = string.upper(ot p)
new = ""
for letter in otp:
if letter in string.uppercas e:
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()
?
On Dec 10, 2:46 pm, John Machin <sjmac...@lexic on.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\syst em32\<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(ot p)
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.
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\syst em32\<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_encryp t".
Peter
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 important
changes, so in this thread I would like to focus on that issue alone.
I have edited the Proposed Syntax example below to take out the
changes unecessary to this discussion. I left in the change of
"instance variable" syntax (...
|
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:
Python 2.4 on Windows 2000
For example:
mypackage contains:
|
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 correct cells within
that worksheet. The Excel Book is made up of 12 worksheets named
Jan-Dec. Each worksheet has columns labeled as each day of that month.
Column 'A' is reserved for 19 rows named "room1 - room19". The data I
am importing from the...
|
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.
|
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 importing data to many
different tables. Can someone explain if there is a better way to do what I
am doing without having to call a stored procedure for each row (like I'm
doing below)? A good example would be great as my understanding of xml or...
| |
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 importing data to many
different tables. Can someone explain if there is a better way to do what I
am doing without having to call a stored procedure for each row (like I'm
doing below)? A good example would be great as my understanding of xml or...
|
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 started to try to find possible errors,
a harmless passtime they seem to enjoy.
One of their remarks was that I used "int" instead of
"size_t" for the input of my allocator function.
|
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):
@classmethod
def c1(cls, a):
return a
class D(object):
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |