Convert string to command..

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

    Convert string to command..

    I want to convert a string to command..
    For example i have a string:
    a="['1']"
    I want to do this list..
    How can i do ?

  • Adam Atlas

    #2
    Re: Convert string to command..

    On Oct 18, 10:23 am, Abandoned <best...@gmail. comwrote:
    I want to convert a string to command..
    For example i have a string:
    a="['1']"
    I want to do this list..
    How can i do ?
    Use the builtin function "eval".

    Comment

    • Diez B. Roggisch

      #3
      Re: Convert string to command..

      Abandoned wrote:
      I want to convert a string to command..
      For example i have a string:
      a="['1']"
      I want to do this list..
      How can i do ?
      The correct wording here would be expression. To evaluate expressions, there
      is the function eval:

      a = eval("['1']")

      But beware: if the expression contains some potentially harmful code, it
      will be executed. So it is generally considered bad style to use eval.

      An example that fails would be

      a = eval("10**2000* *2000")

      or such thing.

      Another alternative is to use parsers like simplejson to extract the
      information. This of course only works, if your expressions are valid json.

      Diez

      Comment

      • Abandoned

        #4
        Re: Convert string to command..

        Thanks you all answer..
        But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
        (100.000 elements)
        Is there any easy alternative ?

        Comment

        • Diez B. Roggisch

          #5
          Re: Convert string to command..

          Abandoned wrote:
          Thanks you all answer..
          But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
          (100.000 elements)
          Is there any easy alternative ?
          How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
          parsed. Which I find fast.

          Diez

          Comment

          • Abandoned

            #6
            Re: Convert string to command..

            On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
            Abandoned wrote:
            Thanks you all answer..
            But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
            (100.000 elements)
            Is there any easy alternative ?
            >
            How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
            parsed. Which I find fast.
            >
            Diez
            173.000 dict elements and it tooks 2.2 seconds this very big time for
            my project

            Comment

            • Hrvoje Niksic

              #7
              Re: Convert string to command..

              Abandoned <besturk@gmail. comwrites:
              173.000 dict elements and it tooks 2.2 seconds this very big time
              for my project
              If you're generating the string from Python, use cPickle instead.
              Much faster:
              >>import time
              >>d = dict((i, i+1) for i in xrange(170000))
              >>len(d)
              170000
              >>s=repr(d)
              >>t0 = time.time(); d2 = eval(s); t1 = time.time()
              >>t1-t0
              1.5457899570465 088
              >>import cPickle as pickle
              >>s = pickle.dumps(d, -1)
              >>len(s)
              1437693
              >>t0 = time.time(); d2 = pickle.loads(s) ; t1 = time.time()
              >>t1-t0
              0.0603079795837 40234
              >>len(d2)
              170000

              That is 25x speedup. Note that cPickle's format is binary. Using the
              textual format makes for more readable pickles, but reduces the
              speedup to "only" 9.5x on my machine.


              P.S.
              Before someone says that using pickle is unsafe, remember that he is
              considering *eval* as the alternative. :-)

              Comment

              • Diez B. Roggisch

                #8
                Re: Convert string to command..

                Abandoned wrote:
                On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                >Abandoned wrote:
                Thanks you all answer..
                But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
                (100.000 elements)
                Is there any easy alternative ?
                >>
                >How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
                >parsed. Which I find fast.
                >>
                >Diez
                >
                173.000 dict elements and it tooks 2.2 seconds this very big time for
                my project
                Where does the data come from?

                Diez

                Comment

                • Abandoned

                  #9
                  Re: Convert string to command..

                  On Oct 18, 6:26 pm, Hrvoje Niksic <hnik...@xemacs .orgwrote:
                  Abandoned <best...@gmail. comwrites:
                  173.000 dict elements and it tooks 2.2 seconds this very big time
                  for my project
                  >
                  If you're generating the string from Python, use cPickle instead.
                  Much faster:
                  >
                  >import time
                  >d = dict((i, i+1) for i in xrange(170000))
                  >len(d)
                  170000
                  >s=repr(d)
                  >t0 = time.time(); d2 = eval(s); t1 = time.time()
                  >t1-t0
                  1.5457899570465 088
                  >import cPickle as pickle
                  >s = pickle.dumps(d, -1)
                  >len(s)
                  1437693
                  >t0 = time.time(); d2 = pickle.loads(s) ; t1 = time.time()
                  >t1-t0
                  >
                  0.0603079795837 40234>>len(d2)
                  >
                  170000
                  >
                  That is 25x speedup. Note that cPickle's format is binary. Using the
                  textual format makes for more readable pickles, but reduces the
                  speedup to "only" 9.5x on my machine.
                  >
                  P.S.
                  Before someone says that using pickle is unsafe, remember that he is
                  considering *eval* as the alternative. :-)

                  import cPickle as pickle
                  a="{2:3,4:6,2:7 }"
                  s=pickle.dumps( a, -1)
                  g=pickle.loads( s);
                  print g
                  '{2:3,4:6,2:7}'

                  Thank you very much for your answer but result is a string ??

                  Comment

                  • Abandoned

                    #10
                    Re: Convert string to command..

                    On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    Abandoned wrote:
                    On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                    Abandoned wrote:
                    Thanks you all answer..
                    But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
                    (100.000 elements)
                    Is there any easy alternative ?
                    >
                    How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
                    parsed. Which I find fast.
                    >
                    Diez
                    >
                    173.000 dict elements and it tooks 2.2 seconds this very big time for
                    my project
                    >
                    Where does the data come from?
                    >
                    Diez
                    Data come from database..
                    I want to cache to speed up my system and i save the dictionary to
                    database for speed up but eval is very slow for do this.
                    Not: 2.2 second only eval operation.

                    Comment

                    • Marc 'BlackJack' Rintsch

                      #11
                      Re: Convert string to command..

                      On Thu, 18 Oct 2007 08:41:30 -0700, Abandoned wrote:
                      import cPickle as pickle
                      a="{2:3,4:6,2:7 }"
                      s=pickle.dumps( a, -1)
                      g=pickle.loads( s);
                      print g
                      '{2:3,4:6,2:7}'
                      >
                      Thank you very much for your answer but result is a string ??
                      In Python terms yes, strings in Python can contain any byte value. If you
                      want to put this into a database you need a BLOB column or encode it as
                      base64 or something similar more ASCII safe.

                      Ciao,
                      Marc 'BlackJack' Rintsch

                      Comment

                      • Abandoned

                        #12
                        Re: Convert string to command..

                        On Oct 18, 6:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
                        On Thu, 18 Oct 2007 08:41:30 -0700, Abandoned wrote:
                        import cPickle as pickle
                        a="{2:3,4:6,2:7 }"
                        s=pickle.dumps( a, -1)
                        g=pickle.loads( s);
                        print g
                        '{2:3,4:6,2:7}'
                        >
                        Thank you very much for your answer but result is a string ??
                        >
                        In Python terms yes, strings in Python can contain any byte value. If you
                        want to put this into a database you need a BLOB column or encode it as
                        base64 or something similar more ASCII safe.
                        >
                        Ciao,
                        Marc 'BlackJack' Rintsch
                        '{2:3,4:6,2:7}' already in database, i select this and convert to real
                        dictionary..

                        Comment

                        • Diez B. Roggisch

                          #13
                          Re: Convert string to command..

                          Abandoned wrote:
                          On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                          >Abandoned wrote:
                          On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                          >Abandoned wrote:
                          Thanks you all answer..
                          But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
                          (100.000 elements)
                          Is there any easy alternative ?
                          >>
                          >How big? How slow? For me, a 10000-element list takes 0.04 seconds to
                          >be parsed. Which I find fast.
                          >>
                          >Diez
                          >>
                          173.000 dict elements and it tooks 2.2 seconds this very big time for
                          my project
                          >>
                          >Where does the data come from?
                          >>
                          >Diez
                          >
                          Data come from database..
                          I want to cache to speed up my system and i save the dictionary to
                          database for speed up but eval is very slow for do this.
                          Not: 2.2 second only eval operation.
                          Does the dictionary change often?

                          And you should store a pickle to the database then. Besides, making a
                          database-query of that size (after all, we're talking a few megs here) will
                          take a while as well - so are you SURE the 2.2 seconds are a problem? Or is
                          it just that you think they are?

                          Diez

                          Comment

                          • Abandoned

                            #14
                            Re: Convert string to command..

                            On Oct 18, 6:57 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                            Abandoned wrote:
                            On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                            Abandoned wrote:
                            On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
                            Abandoned wrote:
                            Thanks you all answer..
                            But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
                            (100.000 elements)
                            Is there any easy alternative ?
                            >
                            How big? How slow? For me, a 10000-element list takes 0.04 seconds to
                            be parsed. Which I find fast.
                            >
                            Diez
                            >
                            173.000 dict elements and it tooks 2.2 seconds this very big time for
                            my project
                            >
                            Where does the data come from?
                            >
                            Diez
                            >
                            Data come from database..
                            I want to cache to speed up my system and i save the dictionary to
                            database for speed up but eval is very slow for do this.
                            Not: 2.2 second only eval operation.
                            >
                            Does the dictionary change often?
                            >
                            And you should store a pickle to the database then. Besides, making a
                            database-query of that size (after all, we're talking a few megs here) will
                            take a while as well - so are you SURE the 2.2 seconds are a problem? Or is
                            it just that you think they are?
                            >
                            Diez- Hide quoted text -
                            >
                            - Show quoted text -
                            I'm very confused :(
                            I try to explain main problem...
                            I have a table like this:
                            id-1 | id-2 | value
                            23 24 34
                            56 68 66
                            56 98 32455
                            55 62 655
                            56 28 123
                            ..... ( 3 millions elements)

                            I select where id=56 and 100.000 rows are selecting but this took 2
                            second. (very big for my project)
                            I try cache to speed up this select operation..
                            And create a cache table:
                            id-1 | all
                            56 {68:66, 98:32455, 62:655}

                            When i select where id 56 i select 1 row and its took 0.09 second but
                            i must convert text to dictionary..

                            Have you got any idea what can i do this conver operation ?
                            or
                            Have you got any idea what can i do cache for this table ?

                            Comment

                            • Hrvoje Niksic

                              #15
                              Re: Convert string to command..

                              Abandoned <besturk@gmail. comwrites:
                              import cPickle as pickle
                              a="{2:3,4:6,2:7 }"
                              s=pickle.dumps( a, -1)
                              g=pickle.loads( s);
                              print g
                              '{2:3,4:6,2:7}'
                              >
                              Thank you very much for your answer but result is a string ??
                              Because you gave it a string. If you give it a dict, you'll get a
                              dict:
                              >>import cPickle as pickle
                              >>a = {1:2, 3:4}
                              >>s = pickle.dumps(a, -1)
                              >>g = pickle.loads(s)
                              >>g
                              {1: 2, 3: 4}

                              If your existing database already has data in the "{...}" format, then
                              eval it only the first time. Then you'll get the dict which you can
                              cache thruogh the use of dumps/loads.

                              Comment

                              Working...