List problems in C code ported to Python

Paul McGuire ptmcg at austin.rr._bogus_.com
Sun Jan 16 19:42:39 EST 2005


"Lucas Raab" <pythongnome at hotmail.com> wrote in message
news:vrzGd.817$Rs.803 at newsread3.news.atl.earthlink.net...
> I'm done porting the C code, but now when running the script I
> continually run into problems with lists. I tried appending and
> extending the lists, but with no avail. Any help is much appreciated
> Please see both the Python and C code at
> http://home.earthlink.net/~lvraab. The two files are ENIGMA.C and
engima.py
>
> TIA

I didn't actually run your script, but you have some fundamental things to
fix first.  Here are some corrections that will get you closer:

- Single-character strings are still strings as far as Python is concerned.
Unlike C's distinction of single quotes for single characters (which allow
you to do integer arithmetic) and double quotes for string literals (which
don't support integer arithmetic), Python uses either quoting style for
strings.  So "A" == 'a' is true in Python, not true in C.  To do single-char
arithmetic, you'll need the ord() and asc() functions, so that instead of
    c-'A'
you'll need
    ord(c)-ord('A')
(and another little tip - since the ord('A') is likely to be invariant, and
used *heavily* in a function such as an Enigma simulator, you're best off
evaluating it once and stuffing it into a global, with an unimaginitive name
like ord_A = ord('A')

-Line 42: You are testing c == string.alpha_letters, when I think you
*really* want to test c in string.alpha_letters.

- encipher_file - the C version of this actually reads the file and calls
encipher() with each character in it.  Your Python version just calls
encipher() with the complete file contents, which is certain to fail.
(another tip - avoid variable names like 'file', 'string', 'list', 'dict',
etc. as these collide with global typenames - also, your variable naming is
pretty poor, using "file" to represent the filename, and "filename" to
represent the file contents - err???)

- usage() - print("blahblah \n") - the trailing \n is unnecessary unless you
want to double-space your text

Although you say you are "done porting the C code", you really have quite a
bit left to do yet.  You should really try to port this code a step at a
time - open a file, read its contents, iterate through the contents, call a
method, etc.  "Big-bang" porting like this is terribly inefficient!

-- Paul





More information about the Python-list mailing list