#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    London UK
    Posts
    4
    Rep Power
    0

    How to dynamically assign variables


    [Python Newbie]

    I want to iterate through a file to read lines of pairs of values and dynamically create and set variables.

    eg

    line = Variable1 Value1

    to create Variable1 with a value of Value1

    I don't know what the pairs will contain before reading the file.

    ConfigParser looks like it will do the job, but I can't figure out how (I don't want the variable to be in a Dict). Using open() and stepping through the lines allows me to grab the 2 items, but I can't work out how to assign a variable based on the 2 items.

    Any help appreciated
  2. #2
  3. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Can you show me an example of what you want, cause i really didnt understand.. sometimes i have this problem understanding users
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    London UK
    Posts
    4
    Rep Power
    0


    try this

    File (a portion of it)
    Code:
    Server_Port 8888
    Max_Connect 100
    Error_count 20
    Code:
    def load_from_file():
        f = open("c:\\myprog\\progdata.txt","r")
        r = f.readlines()
        f.close
        for line in r:
            var, val = line.split()[0], line.split()[1]
    so for the first line read I have a variable called var containing Server_port and a variable called val containing 8888. From these I need to create a variable called Server_Port with the value 8888 for
    Code:
    def StartServer():
        server = HTTPServer((' ', Server_Port), RequestHandler)
    In this case its an INI file, but not always, I don't want to have to list *all* the possible variable names within the prog itself so a dynamic method is preferable.

    TIA
  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    so you are saing that you are going to have data like so
    Code:
    Server_Port 8888
    Max_Connect 100
    Error_count 20
    
    Server_Port 8000
    Max_Connect 100
    Error_count 20
    just want to make sure i fully understand, as im not a python guru yet
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    London UK
    Posts
    4
    Rep Power
    0
    The file will look like that yes (but without duplicates )
  12. #7
  13. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by tdw
    The file will look like that yes (but without duplicates )
    Ok give me a few min while i comeup with something for you
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    London UK
    Posts
    4
    Rep Power
    0
    Originally Posted by Strike
    What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.
    I'm beginning to think so too

    Thanks All
  16. #9
  17. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    ok i got this little sample here.. but cant get rid of them \n see what you can do.. but it does give you the port numbers

    Code:
    for x in file('data.db'):
             lists  = x
             Server =  lists[lists.find('Server_Port'):20]
             print Server[Server.find(' '):]
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    This was discussed in a thread here a few weeks ago.

    You can assign them to the dictionary returned by the builtin function globals(), and they will become global variables.

    Code:
    def load_from_file():
        f = open("c:\\myprog\\progdata.txt","r")
        r = f.readlines()
        f.close
        for line in r:
            var, val = line.split()[0], line.split()[1]
            globals()[var] = val
    However you cannot create local variables this way, since the locals() function creates a copy of the local namespace and any changes to it will have no effect. To create a local variable you have to use
    Code:
    exec "%s = %s" % (var, val)
    instead.

    You can also create an attribute on an object with

    Code:
    setattr(obj, var, val)
    On the other hand, it may be simpler to use a dictionary, as others have said.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo