parsing engineering symbols

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh Jeevanandam

    #1

    parsing engineering symbols

    Hi,
    I want to convert a string to float value. The string contains
    engineering symbols.
    For example,

    s = '12k'

    I want some function which would return 12000
    function(s)
    => 12000.0
    I searched the web, but could not find any function.

    regards,
    Suresh
  • Larry Bates

    #2
    Re: parsing engineering symbols

    Something like:

    def cvt(input):
    if input.lower().e ndswith('k'): return float(input[:-1])*1000

    Comment

    • gene tani

      #3
      Re: parsing engineering symbols


      Suresh Jeevanandam wrote:[color=blue]
      > Hi,
      > I want to convert a string to float value. The string contains
      > engineering symbols.
      > For example,
      >
      > s = '12k'
      >
      > I want some function which would return 12000
      > function(s)
      > => 12000.0
      > I searched the web, but could not find any function.
      >
      > regards,
      > Suresh[/color]

      port this


      Comment

      • Bengt Richter

        #4
        Re: parsing engineering symbols

        On Tue, 20 Dec 2005 19:07:02 +0530, Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
        [color=blue]
        >Hi,
        > I want to convert a string to float value. The string contains
        >engineering symbols.
        > For example,
        >
        > s = '12k'
        >
        > I want some function which would return 12000
        > function(s)
        > => 12000.0
        > I searched the web, but could not find any function.
        >[/color]
        It should be easy enough, if you can define precisely what
        "contains engineering symbols" means ;-)

        Regards,
        Bengt Richter

        Comment

        • Erik Max Francis

          #5
          Re: parsing engineering symbols

          Suresh Jeevanandam wrote:
          [color=blue]
          > I want to convert a string to float value. The string contains
          > engineering symbols.
          > For example,
          >
          > s = '12k'
          >
          > I want some function which would return 12000
          > function(s)
          > => 12000.0
          > I searched the web, but could not find any function.[/color]

          There's some extensive code in the SI class in BOTEC which does this:



          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          Chance favors the trained mind.
          -- Louis Pasteur

          Comment

          • Fredrik Lundh

            #6
            Re: parsing engineering symbols

            Suresh Jeevanandam wrote:
            [color=blue]
            > I want to convert a string to float value. The string contains
            > engineering symbols.
            >
            > For example,
            >
            > s = '12k'
            >
            > I want some function which would return 12000
            > function(s)
            > => 12000.0
            > I searched the web, but could not find any function.[/color]

            how about:

            SI_prefixes = {
            'Y':24, 'Z':21, 'E':18, 'P':15, 'T':12, 'G':9, 'M':6, 'k':3,
            'h':2, 'd':-1, 'c':-2, 'm':-3, u'\xb5':-6, 'u':-6, 'n':-9, 'p':-12,
            'f':-15, 'a':-18, 'z':-21, 'y':-24
            }

            def myfloat(str):
            try:
            exp = SI_prefixes[str[-1]]
            return float(str[:-1]) * 10**exp
            except KeyError:
            return float(str)

            ?

            </F>



            Comment

            • Suresh Jeevanandam

              #7
              Re: parsing engineering symbols


              Exactly what I wanted.

              It would be nice if the standard float function takes care of these.

              regards,
              Suresh
              [color=blue]
              > how about:
              >
              > SI_prefixes = {
              > 'Y':24, 'Z':21, 'E':18, 'P':15, 'T':12, 'G':9, 'M':6, 'k':3,
              > 'h':2, 'd':-1, 'c':-2, 'm':-3, u'\xb5':-6, 'u':-6, 'n':-9, 'p':-12,
              > 'f':-15, 'a':-18, 'z':-21, 'y':-24
              > }
              >
              > def myfloat(str):
              > try:
              > exp = SI_prefixes[str[-1]]
              > return float(str[:-1]) * 10**exp
              > except KeyError:
              > return float(str)
              >
              > ?
              >
              > </F>
              >
              >
              >[/color]

              Comment

              • Bengt Richter

                #8
                Re: parsing engineering symbols

                On Tue, 20 Dec 2005 23:28:12 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
                [color=blue]
                >Suresh Jeevanandam wrote:
                >[color=green]
                >> I want to convert a string to float value. The string contains
                >> engineering symbols.
                >>
                >> For example,
                >>
                >> s = '12k'
                >>
                >> I want some function which would return 12000
                >> function(s)
                >> => 12000.0
                >> I searched the web, but could not find any function.[/color]
                >
                >how about:
                >
                >SI_prefixes = {
                > 'Y':24, 'Z':21, 'E':18, 'P':15, 'T':12, 'G':9, 'M':6, 'k':3,
                > 'h':2, 'd':-1, 'c':-2, 'm':-3, u'\xb5':-6, 'u':-6, 'n':-9, 'p':-12,
                > 'f':-15, 'a':-18, 'z':-21, 'y':-24
                >}
                >
                >def myfloat(str):
                > try:
                > exp = SI_prefixes[str[-1]]
                > return float(str[:-1]) * 10**exp
                > except KeyError:
                > return float(str)
                >
                >?[/color]

                Nit: why not move 10** out of myfloat into SI_prefixes
                (i.e., just retrieve precalculated factors)?

                Nit_2: No twinge of conscience shadowing str? ;-)

                Regards,
                Bengt Richter

                Comment

                • Dave Hansen

                  #9
                  Re: parsing engineering symbols

                  On Wed, 21 Dec 2005 19:10:21 +0530 in comp.lang.pytho n, Suresh
                  Jeevanandam <jm.suresh@gmai l.com> wrote:

                  [re: SI prefixes][color=blue]
                  >
                  >Exactly what I wanted.
                  >
                  >It would be nice if the standard float function takes care of these.[/color]

                  No, it wouldn't.

                  Regards,
                  -=Dave

                  --
                  Change is inevitable, progress is not.

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: parsing engineering symbols

                    Bengt Richter wrote:
                    [color=blue]
                    > Nit: why not move 10** out of myfloat into SI_prefixes
                    > (i.e., just retrieve precalculated factors)?[/color]

                    the table I cut and pasted from only listed the exponents. if you want
                    to prefix 1e to all the constants, be my guest.
                    [color=blue]
                    > Nit_2: No twinge of conscience shadowing str? ;-)[/color]

                    not the slightest.

                    </F>



                    Comment

                    • Jacob Rael

                      #11
                      Re: parsing engineering symbols

                      Just a newbie, trolling.

                      I like this solution. Simple, easy to understand.

                      Did you put the SI_prefixes outside the def so you could wrap the
                      whole thing in a module and reuse it in other blocks?

                      jr

                      Comment

                      • Peter Hansen

                        #12
                        Re: parsing engineering symbols

                        Jacob Rael wrote:[color=blue]
                        > Just a newbie, trolling.
                        >
                        > I like this solution. Simple, easy to understand.
                        >
                        > Did you put the SI_prefixes outside the def so you could wrap the
                        > whole thing in a module and reuse it in other blocks?[/color]

                        Do "import dis" and then try "dis.dis(myfloa t)" on Fredrik's version and
                        on your suggested variation... (Hint, it's likely more an instinct for
                        decent performance instead of an instinct for decent portability, though
                        it would serve both purposes.)

                        -Peter

                        Comment

                        • Jacob Rael

                          #13
                          Re: parsing engineering symbols

                          Wow!! 261 v. 75 lines!!

                          jr

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: parsing engineering symbols

                            Peter Hansen wrote:
                            [color=blue][color=green]
                            > > Did you put the SI_prefixes outside the def so you could wrap the
                            > > whole thing in a module and reuse it in other blocks?[/color]
                            >
                            > Do "import dis" and then try "dis.dis(myfloa t)" on Fredrik's version and
                            > on your suggested variation... (Hint, it's likely more an instinct for
                            > decent performance instead of an instinct for decent portability[/color]

                            I frankly don't see what "portabilit y" has to do with this. Why would the
                            location of a reusable static data structure matter ? Python code work
                            the same way in scripts and modules...

                            </F>



                            Comment

                            • Peter Hansen

                              #15
                              Re: parsing engineering symbols

                              Fredrik Lundh wrote:[color=blue]
                              > Peter Hansen wrote:[/color]
                              [the OP wrote][color=blue][color=green][color=darkred]
                              >>>Did you put the SI_prefixes outside the def so you could wrap the
                              >>>whole thing in a module and reuse it in other blocks?[/color]
                              >>
                              >>Do "import dis" and then try "dis.dis(myfloa t)" on Fredrik's version and
                              >>on your suggested variation... (Hint, it's likely more an instinct for
                              >>decent performance instead of an instinct for decent portability[/color]
                              >
                              > I frankly don't see what "portabilit y" has to do with this. Why would the
                              > location of a reusable static data structure matter ? Python code work
                              > the same way in scripts and modules...[/color]

                              A local variable (i.e. "inside the def") wouldn't be quite so accessible
                              as a global one...

                              At least, that's what I inferred the OP meant with his question above.

                              -Peter

                              Comment

                              Working...