Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List problems in C code ported to Python

Reply
Thread Tools

List problems in C code ported to Python

 
 
Lucas Raab
Guest
Posts: n/a
 
      01-16-2005
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
 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      01-16-2005
Lucas Raab wrote:

> Please see both the Python and C code at
> http://home.earthlink.net/~lvraab. The two files are ENIGMA.C and engima.py


If you post a small testcase here you are much more likely to get helped.
--
Michael Hoffman
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      01-16-2005
On 2005-01-16, Lucas Raab <(E-Mail Removed)> wrote:
> 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


http://www.catb.org/~esr/faqs/smart-questions.html

--
Grant Edwards grante Yow! Did an Italian CRANE
at OPERATOR just experience
visi.com uninhibited sensations in
a MALIBU HOT TUB?
 
Reply With Quote
 
Lucas Raab
Guest
Posts: n/a
 
      01-16-2005
Grant Edwards wrote:
> On 2005-01-16, Lucas Raab <(E-Mail Removed)> wrote:
>
>>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

>
>
> http://www.catb.org/~esr/faqs/smart-questions.html
>


I didn't expect to get bitched out just because I didn't follow "protocol."
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      01-17-2005
"Lucas Raab" <(E-Mail Removed)> wrote in message
news:vrzGd.817$(E-Mail Removed) .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


 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-17-2005
"Paul McGuire" <(E-Mail Removed)._bogus_.com> wrote:
> "A" == 'a' is true in Python, not true in C.


It could be true in C, if the string is stored in very low memory
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      01-17-2005
Paul McGuire wrote:

> So "A" == 'a' is true in Python, not true in C.


>>> "A" == 'a'

False

I think you meant:

>>> "A" == "A"

True
--
Michael Hoffman
 
Reply With Quote
 
Irmen de Jong
Guest
Posts: n/a
 
      01-17-2005
Paul McGuire wrote:
> So "A" == 'a' is true in Python, not true in C.


It's not true in Python either.
You probably meant to say: "a" == 'a'
(lowercase a)

--Irmen
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      01-17-2005
Lucas Raab wrote:
> Grant Edwards wrote:
>> http://www.catb.org/~esr/faqs/smart-questions.html

>
> I didn't expect to get bitched out just because I didn't follow "protocol."


I didn't see anyone bitch you out. And you were lucky that one
person was kind enough to go through your web site and make some
suggestions. If you had written a better question I guarantee you would
have had more people answering your question sooner.

Oh yeah, and:

http://www.catb.org/~esr/faqs/smart-...tml#not_losing
--
Michael Hoffman
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      01-17-2005
Michael Hoffman wrote:
> Paul McGuire wrote:
>> So "A" == 'a' is true in Python, not true in C.

> I think you meant:
>
> >>> "A" == "A"

> True


Er, "A" == 'A'
--
Michael Hoffman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any news on when some libraries will be ported to Python 3.0? Just Another Victim of the Ambient Morality Python 3 01-07-2009 07:22 PM
Anybody has ported talib to Python via SWIG Aldo Ceccarelli Python 2 01-28-2008 01:23 PM
Tool for Cross-Referencing Ported Code brandon.e.taylor@gmail.com C Programming 7 08-27-2007 08:26 AM
[ANN] Bishop 0.3.0 - bayesian classifier for Ruby ported from Python Matt Mower Ruby 8 04-21-2005 09:17 AM
anybody ported Jrunner to NIOS ron proveniers VHDL 0 08-18-2004 02:53 PM



Advertisments