Newbie Questions

I'm a newbie with several questions:

I'm sure you will get several answers!

def to_lower(ciphertext): #converts ciphertext to lowercase
   lower_case = ""
   for character in ciphertext:
       if 'A' <= character <= 'Z':
           location = ord(character) - ord('A')
           new_ascii = location + ord('a')
           character = chr(new_ascii)
       lower_case = lower_case + character
   return lower_case

Hint #1: you can eliminate that entire function. ciphertext.lower()
does the same thing.

ciphertext=to_lower(ciphertext)

ciphertext = ciphertext.lower()

nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM=0,0,0,0,0,0,0,0,0,0,0,0,0
nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ=0,0,0,0,0,0,0,0,0,0,0,0,0

b=1
while b<=cipherlength:
   if ciphertext[b-1]=="a":
       nA=nA+1
   if ciphertext[b-1]=="b":
       nB=nB+1
   if ciphertext[b-1]=="c":
       nC=nC+1
   if ciphertext[b-1]=="d":
       nD=nD+1
   if ciphertext[b-1]=="e":
       nE=nE+1

   ...

Why do you start with b=1 if you really want to start at b=0?

Hint #2: use a dictionary. Replace the entire while loop with:

    letters = ''.join( [ chr(65+i) for i in range(26) ] )
    # or letters = 'abcdefghijklmnopqrstuvwxyz' if you prefer
    counts = {}
    for ch in letters:
        counts[ch] = 0
    for ch in ciphertext:
        counts[ch] += 1

NumLet=0

print
if nA!=0:
   NumLet=NumLet+1
   print "there are",nA,"occurrences of the letter a"
if nB!=0:
   NumLet=NumLet+1
   print "there are",nB,"occurrences of the letter b"
...

Hint #3:

    for ch in letters:
        if counts[ch]:
            NumLet += 1
            print "There are",counts[ch],"occurrances of the letter",ch

L=[nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM,nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ]
l='abcdefghijklmnopqrstuvwxyz'

Max=max(L)

cipherA=''
while Max>0:
   b=0
   while b<26:
       if L[b]==Max:
           cipherA=cipherA+l[b]
       b=b+1
   Max=Max-1

Hint #4: Turn the dict into a list and sort the list.

    L = [ (count,ch) for ch,count in counts ]
    L.sort()
    for count,ch in L:
        print count,ch

def decrypt(cipherA,common_letters,ciphertext):
   plaintext=ciphertext
   b=1
   while b<=NumLet:
       plaintext=plaintext.replace(cipherA[b-1],common_letters[b-1])
       b=b+1
   return plaintext

Hint #5: consider string.translate.

def merit(plaintext,cipherlength):
   merit=0
   b=1
   while b<=cipherlength:
       if plaintext[b-1]=='E':
           merit=merit+1
       if plaintext[b-1]=='T':
           merit=merit+1
       if plaintext[b-1]=='H':
           merit=merit+1
       #Most Common Trigrams
       if plaintext[b-1:b+2]=='THE':
           merit=merit+1
       if plaintext[b-1:b+2]=='ING':
           merit=merit+1
       if plaintext[b-1:b+2]=='AND':
           merit=merit+1
       if plaintext[b-1:b+2]=='HER':
           merit=merit+1

Hint #6: Any time you see a construct like this in Python, you need to
think about dicts and lists. Proper programming is all about data
structures, not brute force. You can replace that 600-line function
with this:

# Each tuple is word length, merit value, list of words:
commonWords = (
    ( 1, 1, ('E','T','R') ),
    ( 2, 1, ('TH','HE','IN','ER',... ) ),
    ( 3, 1, ('THE','ING','AND','HER','ERE', ... ) )
    ( 4, 2, ('THAT','WITH','THEY','HAVE','LIST' ... ) )
    ....
)

def merit( plaintext, cipherlength ):
    merit = 0
    for b in range( cipherlength ):
        for len, newmerit, wordList in commonWords:
            if plaintext[b:b+len] in wordList:
                merit += newmerit
    return merit

···

On Mon, 24 Apr 2006 20:12:53 -0400, "Daniel Ryan" <dvryan@gmail.com> wrote:

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim (and all),

Thanks so much for the help. My code is much more efficient now :-).

Have a good one,

Dan

···

On 4/25/06, Tim Roberts timr@probo.com wrote:

On Mon, 24 Apr 2006 20:12:53 -0400, “Daniel Ryan” dvryan@gmail.com wrote:

I’m a newbie with several questions:

I’m sure you will get several answers!

def to_lower(ciphertext): #converts ciphertext to lowercase
lower_case = “”
for character in ciphertext:
if ‘A’ <= character <= ‘Z’:
location = ord(character) - ord(‘A’)

       new_ascii = location + ord('a')
       character = chr(new_ascii)
   lower_case = lower_case + character

return lower_case

Hint #1: you can eliminate that entire function.
ciphertext.lower()
does the same thing.

ciphertext=to_lower(ciphertext)

ciphertext = ciphertext.lower()

nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM=0,0,0,0,0,0,0,0,0,0,0,0,0
nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ=0,0,0,0,0,0,0,0,0,0,0,0,0

b=1
while b<=cipherlength:
if ciphertext[b-1]==“a”:
nA=nA+1
if ciphertext[b-1]==“b”:
nB=nB+1
if ciphertext[b-1]==“c”:

   nC=nC+1

if ciphertext[b-1]==“d”:
nD=nD+1
if ciphertext[b-1]==“e”:
nE=nE+1

Why do you start with b=1 if you really want to start at b=0?

Hint #2: use a dictionary. Replace the entire while loop with:

letters = ''.join( [ chr(65+i) for i in range(26) ] )
# or letters = 'abcdefghijklmnopqrstuvwxyz' if you prefer
counts = {}

for ch in letters:
    counts[ch] = 0
for ch in ciphertext:
    counts[ch] += 1

NumLet=0

print
if nA!=0:
NumLet=NumLet+1
print “there are”,nA,“occurrences of the letter a”

if nB!=0:
NumLet=NumLet+1
print “there are”,nB,“occurrences of the letter b”

Hint #3:

for ch in letters:
    if counts[ch]:

        NumLet += 1
        print

“There are”,counts[ch],“occurrances of the letter”,ch

L=[nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM,nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ]
l=‘abcdefghijklmnopqrstuvwxyz’

Max=max(L)

cipherA=‘’
while Max>0:
b=0
while b<26:
if L[b]==Max:
cipherA=cipherA+l[b]
b=b+1
Max=Max-1

Hint #4: Turn the dict into a list and sort the list.

L = [ (count,ch) for ch,count in counts ]
L.sort()
for count,ch in L:
    print count,ch

def decrypt(cipherA,common_letters,ciphertext):

plaintext=ciphertext
b=1
while b<=NumLet:
plaintext=plaintext.replace(cipherA[b-1],common_letters[b-1])
b=b+1
return plaintext

Hint #5: consider string.translate.

def merit(plaintext,cipherlength):
merit=0
b=1
while b<=cipherlength:
if plaintext[b-1]==‘E’:
merit=merit+1

   if plaintext[b-1]=='T':
       merit=merit+1
   if plaintext[b-1]=='H':
       merit=merit+1
   #Most Common Trigrams
   if plaintext[b-1:b+2]=='THE':
       merit=merit+1
   if plaintext[b-1:b+2]=='ING':
       merit=merit+1
   if plaintext[b-1:b+2]=='AND':
       merit=merit+1
   if plaintext[b-1:b+2]=='HER':
       merit=merit+1

Hint #6: Any time you see a construct like this in Python, you need to
think about dicts and lists. Proper programming is all about data
structures, not brute force. You can replace that 600-line function

with this:

Each tuple is word length, merit value, list of words:

commonWords = (
( 1, 1, (‘E’,‘T’,‘R’) ),
( 2, 1, (‘TH’,‘HE’,‘IN’,‘ER’,… ) ),
( 3, 1, (‘THE’,‘ING’,‘AND’,‘HER’,‘ERE’, … ) )

( 4, 2, ('THAT','WITH','THEY','HAVE','LIST' ... ) )
....

)

def merit( plaintext, cipherlength ):
merit = 0
for b in range( cipherlength ):
for len, newmerit, wordList in commonWords:

        if plaintext[b:b+len] in wordList:
            merit += newmerit
return merit


Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Daniel Ryan
(410) 271-9673 (cell)

All,

I’m having issues when using py2exe to make my code a stand-alone
executable: when I run my setup file (setupGUI.py) with py2exe,
I get the following error, as found in my log:

Traceback (most recent call last):
File “GUI.py”, line 555, in ?
File “PythonCard\model.pyc”, line 337, in init
File “PythonCard\resource.pyc”, line 45, in init
File “PythonCard\util.pyc”, line 37, in readAndEvalFile
IOError: [Errno 2] No such file or directory: ‘GUI.rsrc.py

I have my GUI.rsrc file in the same directory as my GUI.py file . . .

Any ideas what I’m doing wrong? I’ve attached my code, if that helps.

Thanks so much,

Dan

Thanks,

Dan

GUI.py (18.6 KB)

GUI.rsrc.py (1.25 KB)

setupGUI.py (1.02 KB)

···

On 4/25/06, Daniel Ryan dvryan@gmail.com wrote:

Tim (and all),

Thanks so much for the help. My code is much more efficient now :-).

Have a good one,

Dan

On 4/25/06, Tim Roberts < > timr@probo.com> wrote:

On Mon, 24 Apr 2006 20:12:53 -0400, “Daniel Ryan” dvryan@gmail.com wrote:

I’m a newbie with several questions:

I’m sure you will get several answers!

def to_lower(ciphertext): #converts ciphertext to lowercase
lower_case = “”
for character in ciphertext:
if ‘A’ <= character <= ‘Z’:
location = ord(character) - ord(‘A’)

       new_ascii = location + ord('a')
       character = chr(new_ascii)
   lower_case = lower_case + character

return lower_case

Hint #1: you can eliminate that entire function.
ciphertext.lower()
does the same thing.

ciphertext=to_lower(ciphertext)

ciphertext = ciphertext.lower()

nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM=0,0,0,0,0,0,0,0,0,0,0,0,0

nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ=0,0,0,0,0,0,0,0,0,0,0,0,0

b=1
while b<=cipherlength:
if ciphertext[b-1]==“a”:
nA=nA+1
if ciphertext[b-1]==“b”:
nB=nB+1
if ciphertext[b-1]==“c”:

   nC=nC+1

if ciphertext[b-1]==“d”:
nD=nD+1
if ciphertext[b-1]==“e”:
nE=nE+1

Why do you start with b=1 if you really want to start at b=0?

Hint #2: use a dictionary. Replace the entire while loop with:

letters = ''.join( [ chr(65+i) for i in range(26) ] )
# or letters = 'abcdefghijklmnopqrstuvwxyz' if you prefer
counts = {}


for ch in letters:
    counts[ch] = 0
for ch in ciphertext:
    counts[ch] += 1

NumLet=0

print
if nA!=0:
NumLet=NumLet+1
print “there are”,nA,“occurrences of the letter a”

if nB!=0:
NumLet=NumLet+1
print “there are”,nB,“occurrences of the letter b”

Hint #3:

for ch in letters:
    if counts[ch]:


        NumLet += 1
        print

“There are”,counts[ch],“occurrances of the letter”,ch

L=[nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM,nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ]
l=‘abcdefghijklmnopqrstuvwxyz’

Max=max(L)

cipherA=‘’
while Max>0:
b=0
while b<26:
if L[b]==Max:
cipherA=cipherA+l[b]
b=b+1
Max=Max-1

Hint #4: Turn the dict into a list and sort the list.

L = [ (count,ch) for ch,count in counts ]
L.sort()
for count,ch in L:
    print count,ch

def decrypt(cipherA,common_letters,ciphertext):

plaintext=ciphertext
b=1
while b<=NumLet:
plaintext=plaintext.replace(cipherA[b-1],common_letters[b-1])
b=b+1
return plaintext

Hint #5: consider string.translate.

def merit(plaintext,cipherlength):
merit=0
b=1
while b<=cipherlength:
if plaintext[b-1]==‘E’:
merit=merit+1

   if plaintext[b-1]=='T':
       merit=merit+1
   if plaintext[b-1]=='H':
       merit=merit+1
   #Most Common Trigrams
   if plaintext[b-1:b+2]=='THE':
       merit=merit+1
   if plaintext[b-1:b+2]=='ING':
       merit=merit+1
   if plaintext[b-1:b+2]=='AND':
       merit=merit+1
   if plaintext[b-1:b+2]=='HER':
       merit=merit+1

Hint #6: Any time you see a construct like this in Python, you need to
think about dicts and lists. Proper programming is all about data
structures, not brute force. You can replace that 600-line function

with this:

Each tuple is word length, merit value, list of words:

commonWords = (
( 1, 1, (‘E’,‘T’,‘R’) ),
( 2, 1, (‘TH’,‘HE’,‘IN’,‘ER’,… ) ),
( 3, 1, (‘THE’,‘ING’,‘AND’,‘HER’,‘ERE’, … ) )

( 4, 2, ('THAT','WITH','THEY','HAVE','LIST' ... ) )
....

)

def merit( plaintext, cipherlength ):
merit = 0
for b in range( cipherlength ):
for len, newmerit, wordList in commonWords:

        if plaintext[b:b+len] in wordList:
            merit += newmerit
return merit


Tim Roberts,
timr@probo.com
Providenza & Boekelheide, Inc.


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Daniel Ryan
(410) 271-9673 (cell)


Daniel Ryan
(410) 271-9673 (cell)

Daniel Ryan wrote:

All,

I’m having issues when using py2exe to make my code a stand-alone
executable: when I run my setup file (setupGUI.py) with py2exe,

I get the following error, as found in my log:

Traceback (most recent call last):

File “GUI.py”, line 555, in ?

File “PythonCard\model.pyc”, line 337, in init

File “PythonCard\resource.pyc”, line 45, in init

File “PythonCard\util.pyc”, line 37, in readAndEvalFile

IOError: [Errno 2] No such file or directory: ‘GUI.rsrc.py

I have my GUI.rsrc file in the same directory as my GUI.py file . . .

Any ideas what I’m doing wrong? I’ve attached my code, if that helps.

[setupGUI.py]


from distutils.core import setup
import py2exe
setup(
# The first three parameters are not required, if at least a
# 'version' is given, then a versioninfo resource is built from
# them and added to the executables.
version = "1.0",
description = "Decryption Program",
#name = "py2exe samples",
# targets to build
windows = ["GUI.py"],
#console = ["GUI.rsrc"],
)

I’ve never used py2exe (until 2 minutes ago) so there may be other
issues here, but …

on , I see
For a PythonCard program, you’re going to need at least the rsrc.py
files, and they almost certainly won’t be found automatically, because
they are pulled in by the PythonCard infrastructure at start-up, rather
than being "import"ed.
Give that a try - though it might just get you to the next missing
file. Alternatively, use the standaloneBuilder that comes with PythonCard
(that’s how I’ve done all my distribution), or look through it’s
documentation for more hints on additional files you may need to add
for py2exe (doc is at PythonCard/docs/html/standalone.html within your
PythonCard installation)
btw - you may want to ask any follow-up questions on the Pythoncard
user list, since I think they are likely to be PythonCard specific,
rather than general wxPython questions.

···

www.py2exe.org

Specifying additional files

Some applications need additional files at runtime, like
configuration
files, fonts, or bitmaps.

py2exe can copy these files into subdirectories
of dist if
they
are specified in the setup script with the data_files
option. data_files
should contain a sequence of (target-dir, files) tuples, where files is a
sequence of files to be copied.

Here’s an example:


# setup.py
from distutils.core import setup
import glob
import py2exe
setup(console=["myscript.py"],
data_files=[("bitmaps",
["bm/large.gif", "bm/small.gif"]),
("fonts",
glob.glob("fonts\\*.fnt"))],
)

This would create a subdirectory dist\bitmaps , containing the two
.gif
files, and a subdirectory dist\fonts , containing all the
.fnt files.

-- Alex Tweedly

http://www.tweedly.net

Alex,

I tried what you suggested and made progress . . . but it’s still not
there. Here’s the new error (my new code is attached):

Traceback (most recent call last):
File “GUI.py”, line 555, in ?
File “PythonCard\model.pyc”, line 337, in init
File “PythonCard\resource.pyc”, line 48, in getResource
File “PythonCard\resource.pyc”, line 86, in init
File “PythonCard\resource.pyc”, line 91, in init
File “PythonCard\resource.pyc”, line 91, in init
File “PythonCard\resource.pyc”, line 96, in init
File “PythonCard\resource.pyc”, line 139, in enforceSpec
File “PythonCard\resource.pyc”, line 30, in loadComponentModule
ImportError: cannot import module 'staticline

I tried the other way you suggested- with the McMillan Installer, but I
didn’t get it to run successfully there, either. I’m not a
CompSci guy (if that wasn’t already painfully obvious), so I’m pretty
lost . . .

  • Dan

setupGUI.py (1.07 KB)

···

On 4/26/06, Alex Tweedly alex@tweedly.net wrote:

All,

I’m having issues when using py2exe to make my code a stand-alone
executable: when I run my setup file (setupGUI.py) with py2exe,

I get the following error, as found in my log:

Traceback (most recent call last):

File “GUI.py”, line 555, in ?

File “PythonCard\model.pyc”, line 337, in init

File “PythonCard\resource.pyc”, line 45, in init

File “PythonCard\util.pyc”, line 37, in readAndEvalFile

IOError: [Errno 2] No such file or directory: ‘GUI.rsrc.py

I have my GUI.rsrc file in the same directory as my GUI.py file . . .

Any ideas what I’m doing wrong? I’ve attached my code, if that helps.

Daniel Ryan wrote:

[setupGUI.py]

from distutils.core import setup
import py2exe

setup(
    # The first three parameters are not required, if at least a
    # 'version' is given, then a versioninfo resource is built from
    # them and added to the executables.

    version = "1.0",
    description = "Decryption Program",
    #name = "py2exe samples",

    # targets to build
    windows = ["GUI.py"],
    #console = ["
GUI.rsrc"],
    )


I’ve never used py2exe (until 2 minutes ago) so there may be other
issues here, but …

on www.py2exe.org, I see

Specifying additional files

Some applications need additional files at runtime, like
configuration
files, fonts, or bitmaps.

py2exe can copy these files into subdirectories
of dist if
they
are specified in the setup script with the data_files
option. data_files
should contain a sequence of (target-dir, files) tuples, where files is a
sequence of files to be copied.

Here’s an example:

# setup.py
from distutils.core import setup
import glob
import py2exe

setup(console=["myscript.py"],
      data_files=[("bitmaps",
                   ["bm/large.gif", "bm/small.gif"]),

                  ("fonts",
                   glob.glob("fonts\\*.fnt"))],
)

This would create a subdirectory dist\bitmaps , containing the two
.gif
files, and a subdirectory dist\fonts , containing all the
.fnt files.

For a PythonCard program, you’re going to need at least the rsrc.py
files, and they almost certainly won’t be found automatically, because
they are pulled in by the PythonCard infrastructure at start-up, rather
than being "import"ed.

Give that a try - though it might just get you to the next missing
file.

Alternatively, use the standaloneBuilder that comes with PythonCard
(that’s how I’ve done all my distribution), or look through it’s
documentation for more hints on additional files you may need to add
for py2exe (doc is at PythonCard/docs/html/standalone.html within your
PythonCard installation)

btw - you may want to ask any follow-up questions on the Pythoncard
user list, since I think they are likely to be PythonCard specific,
rather than general wxPython questions.

--
Alex Tweedly [http://www.tweedly.net](http://www.tweedly.net)

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.4.6/323 - Release Date: 24/04/2006


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Daniel Ryan

(410) 271-9673 (cell)

Daniel Ryan wrote:

Alex,

I tried what you suggested and made progress . . . but it's still not there. Here's the new error (my new code is attached):

Traceback (most recent call last):
  File "GUI.py", line 555, in ?
  File "PythonCard\model.pyc", line 337, in __init__
  File "PythonCard\resource.pyc", line 48, in getResource
  File "PythonCard\resource.pyc", line 86, in __init__
  File "PythonCard\resource.pyc", line 91, in __init__
  File "PythonCard\resource.pyc", line 96, in __init__
  File "PythonCard\resource.pyc", line 139, in enforceSpec
  File "PythonCard\resource.pyc", line 30, in loadComponentModule
ImportError: cannot import module 'staticline

I tried the other way you suggested- with the McMillan Installer, but I didn't get it to run successfully there, either. I'm not a CompSci guy (if that wasn't already painfully obvious), so I'm pretty lost . . .

staticline is one of the components in PythonCard
Try adding PythonCard/components/staticline.py to your list of extra files.

If you run into more missing imports, try searching (e.g. with findfiles) for them within the PythonCard directory.

If you want to, send me your project file from the standalone builder and I'll give it a try here ....alex@tweedly.net
which versions of Python, wxPython and PythonCard are you using and I'll see if I can find a matching set, or somewhere close ...

···

--
Alex Tweedly http://www.tweedly.net

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.4.6/323 - Release Date: 24/04/2006