how to dynamically assign variables

This is the place for queries that don't fit in any of the other categories.

how to dynamically assign variables

Postby asc123321 » Fri Apr 04, 2014 2:49 am

Hello,

I have a variable file that assigns items like so:

Code: Select all
unixMachine1=nj09cldl5076
unixMachine1nodeManager=nj09cldl5076
nodeManager1ListenAddress=nj09cldl5076.mhf.mhc
nodeManager1ListenPort=22099

unixMachine2=nj09cldl5079
unixMachine2nodeManager=nj09cldl5079
nodeManager2ListenAddress=nj09cldl5079.mhf.mhc
nodeManager2ListenPort=22099

The code that uses this script is a jython script:
Code: Select all
cd('/')
create(unixMachine1, 'UnixMachine')
cd('Machine/'+unixMachine1)
create(unixMachine1nodeManager, 'NodeManager')
cd('NodeManager/'+unixMachine1nodeManager)
set('ListenAddress', nodeManager1ListenAddress)
set('ListenPort', int(nodeManager1ListenPort))

print '\t unix machine: '+unixMachine2

cd('/')
create(unixMachine2, 'UnixMachine')
cd('Machine/'+unixMachine2)
create(unixMachine2nodeManager, 'NodeManager')
cd('NodeManager/'+unixMachine2nodeManager)
set('ListenAddress', nodeManager2ListenAddress)
set('ListenPort', int(nodeManager2ListenPort))

So the script is not dynamic. If I wanted to create a third "machine," I would need to add the following in my properties file:
Code: Select all
unixMachine3=nj09cldl5079
unixMachine3nodeManager=nj09cldl5079
nodeManager3ListenAddress=nj09cldl5079.mhf.mhc
nodeManager3ListenPort=22099

and then modify the script accordingly:
Code: Select all
cd('/')
create(unixMachine1, 'UnixMachine')
cd('Machine/'+unixMachine1)
create(unixMachine1nodeManager, 'NodeManager')
cd('NodeManager/'+unixMachine1nodeManager)
set('ListenAddress', nodeManager1ListenAddress)
set('ListenPort', int(nodeManager1ListenPort))

I would like to be able to specify a variable called numberOfMachines, then the script would create the number of machines I need, assigning the proper variable as designated in the properties file. For example, if my property file read:
Code: Select all
numberOfMachines=2

unixMachine1=nj09cldl5076
unixMachine1nodeManager=nj09cldl5076
nodeManager1ListenAddress=nj09cldl5076.mhf.mhc
nodeManager1ListenPort=22099

unixMachine2=nj09cldl5079
unixMachine2nodeManager=nj09cldl5079
nodeManager2ListenAddress=nj09cldl5079.mhf.mhc
nodeManager2ListenPort=22099

then the script would execute the code for range(1, int(numberOfMachines)) times:
Code: Select all
print 'Creating Unix Machines...'

print '\t unix machine: '+unixMachine##

cd('/')
create(unixMachine##, 'UnixMachine')
cd('Machine/'+unixMachine##)  <----- since this is iteration 1, the variable would be expanded into whatever unixMachine1 is in the properties file
create(unixMachine##nodeManager, 'NodeManager')  <----- same thing here, the variable would be expanded into whatever unixMachine1nodeManager is in the properties file.
cd('NodeManager/'+unixMachine##nodeManager)
set('ListenAddress', nodeManager##ListenAddress) <---- this would be nodeManager1ListenAddress, as per the props file
set('ListenPort', int(nodeManager##ListenPort))

print '\t unix machine: '+unixMachine##  <---- this would be the start of iteration 2

cd('/')
create(unixMachine##, 'UnixMachine') <---- variable should be expanded into whatever unixMachine2 is in the properties file
cd('Machine/'+unixMachine##) <---- same deal here, assign value of unixMachine2
create(unixMachine##nodeManager, 'NodeManager') <---- expanded to unixMachine2nodeManager from props file
cd('NodeManager/'+unixMachine##nodeManager) <---- same thing here
set('ListenAddress', nodeManager##ListenAddress) <---- expanded to nodeManager2ListenAddress
set('ListenPort', int(nodeManager##ListenPort)) <---- expanded to nodeManager2ListenPort

I'm working with weblogic server, which has a utility called the WLST (weblogic scripting tool), basically a jython interpreter. you run the code as follows:
Code: Select all
java weblogic.WLST <jython script> -loadProperties <properties file>

hope this isn't too confusing. would really appreciate any input.

thank you
Last edited by Mekire on Fri Apr 04, 2014 2:56 am, edited 1 time in total.
Reason: First post lock. Hopefully got the code tags right.
asc123321
 
Posts: 1
Joined: Fri Apr 04, 2014 2:36 am

Re: how to dynamically assign variables

Postby metulburr » Fri Apr 04, 2014 10:02 am

hope this isn't too confusing

It is. Make your question more simple and you will get more responses. Take out what you are reallly doing, and make an example script that reflects what you are doing, while making it a lot easier for forum users to help with.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: how to dynamically assign variables

Postby Mekire » Fri Apr 04, 2014 10:05 am

Basically I think it comes down to the fact that you are trying to make numbered variables. Instead you should create new objects and append them to a list.

But yeah, just adding the code tags to your post was a challenge...
-Mek
User avatar
Mekire
 
Posts: 1140
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Nano95 and 6 guests

cron