0

I get variables like these:

ocd[2].likelihood.range[1][0]=[-5219, -5191, 11.7];
ocd[2].likelihood.range[1][1]=[-5180, -5057, 56.5];

from an other program. I re-formate to:

range10=[-5219, -5191, 11.7];
range11=[-5180, -5057, 56.5];

for to use them in Python, but this is a lot of work, so my question is if it somehow is possible to use these variables with there original name in Python?

3
  • 3
    What exactly does "get variables from another program" mean? Why can't you use the original names? Commented Jul 1, 2012 at 17:30
  • do you get ocd list or just text? Commented Jul 1, 2012 at 17:31
  • ocd[2].likelihood.range[1][1]=[-5180, -5057, 56.5]; is one line in a long text file. I like to use [-5180, -5057, 56.5], and I hope to do so with "ocd[2].likelihood.range[1][1]" as a variable name in Python. If I can manage this I don't have to rename the variables before I use them i Python. As the number of variables is high it is a lot of work to rename them. Commented Jul 1, 2012 at 18:45

2 Answers 2

2

not sure what you are asking,, but i'll give it a try - in the "other" program's code do the following:

  1. add this code before the first use of range* variables:

    class Likelihood():
        def __init__(self, x, y):
            self.range = [{} for i in range(x)] # without default values
            # if you need default values, use this code or numpy.zeros(shape=(x,y))
            #self.range = [[0 for j in range(y)] for i in range(x)]
    
    class MyDataType():
        def __init__(self, dimension_1, dimension_2):
            self.likelihood = Likelihood(dimension_1, dimension_2)
    
    ocd = [0, 1, MyDataType(100, 100)]
    
    # copy-paste your code here:
    ocd[2].likelihood.range[1][0]=[-5219, -5191, 11.7];
    ocd[2].likelihood.range[1][1]=[-5180, -5057, 56.5];
    
    print(ocd[2].likelihood.range[1][0])
    
  2. replace all range10 to ocd[2].likelihood.range[1][0], e.g. in Notepad++ Regex replace:

    Find what:    range(\d)(\d)
    Replace with: ocd[2].likelihood.range[\1][\2]
    
    i.e. from code: print(range10)
    to code:        print(ocd[2].likelihood.range[1][0])
    
Sign up to request clarification or add additional context in comments.

5 Comments

ClassNames should be CamelCase according to PEP 8.
oh,, i forgot about lists being mutable and passed by handle, i fixed the code :)
Thanks a lot! Both versions worked, but only for ocd[2], the next data is ocd[3], etc (for instance: ocd[1121].likelihood.range[1][1]=[-5180, -5057, 56.5];) and for that i get "index out of range"
@PerPersson well, you can use ocd = [MyDataType(10, 10) for i in range(9999)] if you have enought memory to contain all data - but if you can't (or don't need to) stuff all data into memory, i would suggest parsing the output of previous program as text input - do you need to have access to all ocd, just 1 ocd or just 1 range at a time?
I used 999 and it worked fine! I do not think I come over 999. I am pleased with this solution. Otherwise I actually only need the variables one and one, I use Python to make figures of these data produced by an other program. At the moment I only want it to work. I promise that I will try to understand how it works later on. :-) Thanks for the help!
0

You can do it in a pythonic way:

def range_function(row, column):
    return ocd[2].likelihood.range[row][column]

range_function(1, 0) == [-5219, -5191, 11.7]

1 Comment

I get the variables as long text-file list as output from an other program, I want to use the variables in Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.