List problems in C code ported to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucas Raab

    #1

    List problems in C code ported to Python

    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
  • Michael Hoffman

    #2
    Re: List problems in C code ported to Python

    Lucas Raab wrote:
    [color=blue]
    > Please see both the Python and C code at
    > http://home.earthlink.net/~lvraab. The two files are ENIGMA.C and engima.py[/color]

    If you post a small testcase here you are much more likely to get helped.
    --
    Michael Hoffman

    Comment

    • Grant Edwards

      #3
      Re: List problems in C code ported to Python

      On 2005-01-16, Lucas Raab <pythongnome@ho tmail.com> wrote:[color=blue]
      > 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[/color]



      --
      Grant Edwards grante Yow! Did an Italian CRANE
      at OPERATOR just experience
      visi.com uninhibited sensations in
      a MALIBU HOT TUB?

      Comment

      • Lucas Raab

        #4
        Re: List problems in C code ported to Python

        Grant Edwards wrote:[color=blue]
        > On 2005-01-16, Lucas Raab <pythongnome@ho tmail.com> wrote:
        >[color=green]
        >>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[/color]
        >
        >
        > http://www.catb.org/~esr/faqs/smart-questions.html
        >[/color]

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

        Comment

        • Paul McGuire

          #5
          Re: List problems in C code ported to Python

          "Lucas Raab" <pythongnome@ho tmail.com> wrote in message
          news:vrzGd.817$ Rs.803@newsread 3.news.atl.eart hlink.net...[color=blue]
          > 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[/color]
          engima.py[color=blue]
          >
          > TIA[/color]

          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_le tters, when I think you
          *really* want to test c in string.alpha_le tters.

          - 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


          Comment

          • Roy Smith

            #6
            Re: List problems in C code ported to Python

            "Paul McGuire" <ptmcg@austin.r r._bogus_.com> wrote:[color=blue]
            > "A" == 'a' is true in Python, not true in C.[/color]

            It could be true in C, if the string is stored in very low memory :-)

            Comment

            • Michael Hoffman

              #7
              Re: List problems in C code ported to Python

              Paul McGuire wrote:
              [color=blue]
              > So "A" == 'a' is true in Python, not true in C.[/color]
              [color=blue][color=green][color=darkred]
              >>> "A" == 'a'[/color][/color][/color]
              False

              I think you meant:
              [color=blue][color=green][color=darkred]
              >>> "A" == "A"[/color][/color][/color]
              True
              --
              Michael Hoffman

              Comment

              • Irmen de Jong

                #8
                Re: List problems in C code ported to Python

                Paul McGuire wrote:[color=blue]
                > So "A" == 'a' is true in Python, not true in C.[/color]

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

                --Irmen

                Comment

                • Michael Hoffman

                  #9
                  Re: List problems in C code ported to Python

                  Lucas Raab wrote:[color=blue]
                  > Grant Edwards wrote:[color=green]
                  >> http://www.catb.org/~esr/faqs/smart-questions.html[/color]
                  >
                  > I didn't expect to get bitched out just because I didn't follow "protocol."[/color]

                  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:


                  --
                  Michael Hoffman

                  Comment

                  • Michael Hoffman

                    #10
                    Re: List problems in C code ported to Python

                    Michael Hoffman wrote:[color=blue]
                    > Paul McGuire wrote:[color=green]
                    >> So "A" == 'a' is true in Python, not true in C.[/color]
                    > I think you meant:
                    >[color=green][color=darkred]
                    > >>> "A" == "A"[/color][/color]
                    > True[/color]

                    Er, "A" == 'A'
                    --
                    Michael Hoffman

                    Comment

                    • Grant Edwards

                      #11
                      Re: List problems in C code ported to Python

                      On 2005-01-16, Lucas Raab <pythongnome@ho tmail.com> wrote:
                      [color=blue][color=green][color=darkred]
                      >>>Please see both the Python and C code at
                      >>>http://home.earthlink.net/~lvraab. The two files are ENIGMA.C
                      >>>and engima.py[/color]
                      >>
                      >> http://www.catb.org/~esr/faqs/smart-questions.html[/color]
                      >
                      > I didn't expect to get bitched out just because I didn't
                      > follow "protocol."[/color]

                      You didn't get "bitched out". You did get some very sound
                      advice. You want help solving a problem, and there are ways you
                      can greatly increase the chances that you'll get help with your
                      problem. After being told the best ways to get help, you
                      whined about it rather than following it.

                      Nobody owes you anything.

                      Remember that.

                      [You're darned lucky somebody did take the time to go to your
                      web site and proof your code for you after your posting said in
                      effect "I'm too lazy to compose and post a precise question, so
                      go look at my program and fix it for me."]

                      Now, go back and read the smart questions reference.

                      --
                      Grant Edwards grante Yow! Hello? Enema
                      at Bondage? I'm calling
                      visi.com because I want to be happy,
                      I guess...

                      Comment

                      • Paul McGuire

                        #12
                        Re: List problems in C code ported to Python

                        "Michael Hoffman" <cam.ac.uk@mh39 1.invalid> wrote in message
                        news:csf51g$cum $1@gemini.csx.c am.ac.uk...[color=blue]
                        > Michael Hoffman wrote:[color=green]
                        > > Paul McGuire wrote:[color=darkred]
                        > >> So "A" == 'a' is true in Python, not true in C.[/color]
                        > > I think you meant:
                        > >[color=darkred]
                        > > >>> "A" == "A"[/color]
                        > > True[/color]
                        >
                        > Er, "A" == 'A'
                        > --
                        > Michael Hoffman[/color]

                        Yeah, that's the one I meant... :)

                        -- Paul


                        Comment

                        • Lucas Raab

                          #13
                          Re: List problems in C code ported to Python

                          Grant Edwards wrote:[color=blue]
                          > On 2005-01-16, Lucas Raab <pythongnome@ho tmail.com> wrote:
                          >
                          >[color=green][color=darkred]
                          >>>>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[/color]
                          >>
                          >>I didn't expect to get bitched out just because I didn't
                          >>follow "protocol."[/color]
                          >
                          >
                          > You didn't get "bitched out". You did get some very sound
                          > advice. You want help solving a problem, and there are ways you
                          > can greatly increase the chances that you'll get help with your
                          > problem. After being told the best ways to get help, you
                          > whined about it rather than following it.
                          >
                          > Nobody owes you anything.
                          >
                          > Remember that.
                          >
                          > [You're darned lucky somebody did take the time to go to your
                          > web site and proof your code for you after your posting said in
                          > effect "I'm too lazy to compose and post a precise question, so
                          > go look at my program and fix it for me."]
                          >
                          > Now, go back and read the smart questions reference.
                          >[/color]

                          Sorry about that. I had a bad day. First there was the migraine and then
                          the fight with my significant other, so yesterday was not a good day. I
                          apologize for what I said.

                          Comment

                          • wittempj@hotmail.com

                            #14
                            Re: List problems in C code ported to Python

                            Lucas Raab wrote:[color=blue]
                            > 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[/color]
                            engima.py[color=blue]
                            >
                            > TIA[/color]

                            You need something like a matrix too for this, if we combine this with
                            the
                            already posted idea of caching of 'ord' results you could go this way:

                            class matrix(object):
                            """based on

                            """
                            def __init__(self, *args):
                            from types import IntType, StringType

                            self.__data = []

                            if len(args) == 2 and type(args[0]) == IntType and type(args[1]
                            == IntType):
                            #args[0] = #rows, args[1] = #columns
                            for r in range(args[0]):
                            self.__data.app end([])
                            for j in range(args[1]):
                            self.__data[r].append(0)
                            else:
                            for arg in args:
                            if type(arg) == StringType:
                            self.__data.app end(map(ord, list(arg)))

                            def __repr__(self):
                            ret = ''
                            for r in self.__data:
                            ret = '%s\n%s' % (ret, r)

                            return ret

                            def __getitem__(sel f, (row, col)):
                            return self.__data[row][col]

                            def __setitem__(sel f, (row, col), value):
                            self.__data[row][col] = value



                            #setup rotor data
                            A = ord('A')
                            ref_rotor = map(ord, "YRUHQSLDPXNGOK MIEBFZCWVJAT")
                            print ref_rotor

                            data = matrix(8, 26)
                            for i in range(26):
                            data[(4, i)] = (ref_rotor[i] - A + 26) % 26
                            print data

                            step_data = (16, 4, 21, 9, 25) #steps at: q, e, v, j, z
                            order = range(3)
                            rotor = matrix("EKMFLGD QVZNTOWYHXUSPAI BRCJ",
                            "AJDKSIRUXBLHWT MCQGZNPYFVOE", \
                            "BDFHJLCPRTXVZN YEIWGAKMUSQO",
                            "ESOVPZJAYQUIRH XLNFTGKDCMWB", \
                            "VZBRGITYUPSDNH LXAWMJQOFECK")
                            step = range(3)
                            for i in range(1, 4):
                            step[i - 1] = step_data[order[i-1]]
                            for j in range(26):
                            data[(i, j)] = (rotor[(order[i-1], j)] - A + 26) % 26
                            print data

                            Comment

                            • Grant Edwards

                              #15
                              Re: List problems in C code ported to Python

                              On 2005-01-17, Lucas Raab <pythongnome@ho tmail.com> wrote:
                              [color=blue]
                              > Sorry about that. I had a bad day. First there was the
                              > migraine and then the fight with my significant other, so
                              > yesterday was not a good day. I apologize for what I said.[/color]

                              No worries. As somebody else said, the best way to get help
                              solving problems is to post as small an example as possible
                              that exhibits the problem behavior. This may take a bit of
                              effort, since problems sometimes go away when you try to
                              reproduce them in a small example (less than 50 lines or so).
                              If you can post a small example that doesn't do what you want
                              it to, I gaurantee that somebody will explain why it doesn't do
                              what you want and how to fix it.

                              --
                              Grant Edwards grante Yow! LOU GRANT froze
                              at my ASSETS!!
                              visi.com

                              Comment

                              Working...