I have a bit of python code that's set to run on a schedule. (I think my problem is the same as if it were within a loop.)
Let's say that in its most basic form, the code snippet looks something like this:
A = 1
B = 2
renameMe = A + B
Let's say the scheduler runs the same snippet of code every 5 minutes. The values of variables A & B are different each time the code is run, but the operation renameMe = A + B is always the same.
The values for A & B are grabbed out of a dataframe that's updated every 5 minutes, so I don't know what they are in advance, but if I need to do something with them beforehand instead of assigning them to A & B right away, I can.
I recently found out that for other things to work, I need to be able to rename the variable renameMe every time that snippet of code runs. In other words, I want the variable's name to be renameMe1 the first time the code snippet runs, then renameMe2 when it runs 5 minutes later, and so on.
It doesn't really matter in which way the variable's name changes (ints, strs, whatever) as long as I'm able to find out what the new variable name is, and use it elsewhere.
Do NOT use a variable variable name, you will have problems, use a container:
a list:
# first time
container = []
# each loop/run
container.append(A+B)
## last value
container[-1]
a dictionary:
# first time
container = {}
# each loop/run
container['new_id'] = A+B
# access arbitrary value
container['my_previous_id']
If you need persistence, use a flat file or a database.
I think it is suitable to use a class so that setattr can be used:
class newVal:
def __init__(self):
self.n = 1
def addVal(self, a, b):
setattr(self, f”val{self.n}”, a+b)
self.n += 1
Values = newVal()
Values.addVal(a, b)
Values.val1 would now be assigned
I aggree with Mozway when saying variables names are likely to cause problems, but this is also something you could strictly manage.
globals() stores all variables names and values in the form of a collection of 2-tuples, like this one :
dict_items([('__name__', '__main__'), ..., ('thisName', 'renaMe1'), ('renaMe18', 10)])
So you should register your new variable name but not forget to delete the previous one in order to avoid overloading.
If you follow a natural law of equal births and deaths, you will avoid overpopulation.
I propose you this bunch of code (with comments inside) :
basename = 'renaMe'
def varUpdate():
# Get previous variable name
thisName = [i for i, j in globals().items() if i[:len(basename)] == basename][0]
# Define the new variable name
newName = basename + '%d'%sum([int(thisName[len(basename):]), 1])
# Register the new variable name
globals()[newName] = globals()[thisName]
# Delete previous variable name from global
del globals()[thisName]
def process(i):
# Isolate from process content for readibility
varUpdate()
# PROCESS BELOW
# ....
newVar = [i for i, j in globals().items() if i[:len(basename)] == basename][0]
print(newVar, " : ", globals()[newVar])
# With this for` loop we simulate 4 entries in process
for i in range(4):
### we enter in the process
process(i)
Test in the shell
First restart your shell and let's suppose we have at the beginning renaMe12 = 12 :
>>> renaMe12 = 12
>>> Proposed Script ...
Result
Variable increments it's proper name at each iteration.
renaMe13 : 12
renaMe14 : 12
renaMe15 : 12
renaMe16 : 12
If you check in the shell now, you could see at the end of iteration, renaMe12 to renaMe15 no longer exist.
Only the variable renaMe16 exists with value 12.
>>> renaMe16
12
>>>> renaMe15
Retraçage (dernier appel le plus récent) :
Shell Python, prompt 4, line 1
builtins.NameError: name 'renaMe15' is not defined
Conclusion
This discussion is just for the sake of experimentation, but if I were you I would do my possible to avoid such code complexification unless it's necessary.
I agree Mozway when thinking you should avoid pain headaches...
Related
I'm using a dictionary to refer to a global variable in Python inside a function. I want to use the function to update the global variable. (In reality my global variables are more complicated than this.)
global_variable_A=5
global_variable_B=1
dictionary={'A':global_variable_A,'B':global_variable_B}
def f(x,glob):
global global_variable_A
dictionary[glob]+=x
f(2,'A')
print(global_variable_A)
This returns 5 rather than 7. I understand why, is there anyway to let Python know that I mean the variable dictionary[glob] to refer to the global rather than the local variable, while still referring to the variable through a dictionary?
Thanks for your time, and apologies if I've missed something completely obvious.
When you assign a value to a name name = 5, you're creating a reference to 5 that you can use the identifier name to access. Normally, if you then have some code with a narrower scope, you can either use that reference
def f():
print(name)
or create a local reference using the same identifier, potentially to an unrelated value
def g():
name = 100
print(name)
The global keyword allows you to instead manipulate the identifier as if you weren;t in the more narrow scope, allowing you to reassign the global name to a different reference:
def h():
global name
name = 100
h()
print(name) # 100
However, when you use a reference to create another reference, there isn't any relation ship between those two references. So
name = 5
l = [name]
leaves us with two references to the value 5: one from the identifier name, and one from the first position of l. Crucially, those two references are not related; we can change one without changing the other.
name = 6
print(l) # [5]
One way to accomplish what you want is to use a boxed type. That means that you create an object that points to another object. All of your references can then point to the first object (the box) and you can freely change what it points to (the thing "inside" the box), by only updating a single reference:
class Box:
def __init__(self, value):
self.value = value
box = Box(5)
l = [box]
box.value = 10
print(l[0].value) # 10
If you think you need to do this, there's a design flaw in your code. I really wouldn't recommend doing this.
global_a = 5
def add_to_global(key, value):
globals()[key] += value
add_to_global("global_a", 2)
print(global_a)
Output:
7
so I know this has been asked in several forms before, but I cannot relate to any of those, either I have something different or I just don't understand them.
The problem is I have script A and script B, and in script A I calculate and have all the variables I want to use in script B.
Script A has various functions, let's say for now I just want to pass a simple number from a variable in script A to script B , let's call the variable value .
I used from script_A import value .
Now, I have value initialized in script_A with 0 right at the top to say so, but script_A processes value, and gets a result clearly different from 0, but when I debug, I am getting in script_B value == 0, and not value == calculated_value_that_should_be_there.
I did not know what to do so I tough about scope,so I put it in the return of a function, I tried making variable value a Global variable. Nothing seems to work in the way that I am not passing the calculated 'value' but I am passing to script_B that 0 initialization.
P.S last thing I tried and what I saw from this topic is to import script_A as it was said with no namespaces. This has worked. When I write script_A.value it is calculated_value_that_should_be_there. But, I do not know why anything else that I described did not work.
script_A
from definitions import *
variable_1 = 0
variable_2 = 0
variable_3 = 0
variable_4 = 0
total = 0
respected = 0
time_diff = {}
seconds_all_writes = "write"
class Detect():
def __init__(self, data_manager, component_name, bus_name_list=None):
def __Function_A(self):
"""
global time_diff
global seconds_all_writes
process
script_B:
from script_A import respected
from script_A import total
import script_A
print aln_mon_detector.total
print aln_mon_detector.respected
I also want a dictionary
table_content.append(script_A.time_diff[file[script_A.seconds_all_writes])
I get
KeyError: 'writes'
this sounds a bit confusing without an example, but, in principle, what you're trying to do should work. Have a look at a minimum example below.
ModuleA - defining the variable
# create the variable
someVariable = 1.
# apply modifications to the variable when called
def someFunc(var):
return var + 2
# ask for changes
someVariable = someFunc(someVariable)
ModuleB - using the variable
import moduleA
# retrieve variable
var = moduleA.someVariable
print(var) # returns 3
This probably has to do with immutability. Depends on what value is. If value is a list (that is, a mutable object) and you append to it, the change should be visible. However, if you write
from module import x
x = 5
you are not changing the actual value, so other references to x will still show the original object.
If you have script A like this:
# imports
value = 0
... # some calculations
Re-organize script A as:
# imports
def main():
value = 0
... # some calculations
return value
Now you can import script A in script B and run calculations inside script B:
import script_A
value = script_A.main()
That is how you should organize code pieces in Python.
I am trying to understand what does this class
class undo:
def __init__(self, ss):
self.ss = ss
In my head it should simply put the value of ss uniquely in the variables i decide to use,but when I'm using it it rewrites all the variables, as if it was shared.
sum_expenses[100][0] = 100
sum_expenses[99][2] = 99
s = 1
a = [0 for i in range(100)]
a[s] = undo(sum_expenses)
output(a[1].ss)
sum_expenses[100][0] = 0
b = undo(sum_expenses)
print " "
print b.ss
print " "
sum_expenses[99][2] = 1
a[2] = undo(sum_expenses)
print a[2].ss
I do not understand why it overwrites all the variables with the current values of sum_expense, when I try to put it individually so that I can use the past values of sum_expenses.
Thank you and have a good day!
It happens because you're giving __init__ a reference to the list. If you change the list somewhere else, the reference in .ss still points to the same list, so the changes are visible there, too.
You could copy the list, since it's 2D try deepcopy.
Everytime you call this function it overrides reference to thr whole array ss, changing it to the new one you just passed to the constructor.
You can also create this array within the class and pass indexes and value to it like so:
undo.add(index1,index2,value)
Or you can make another variable ss_old and have make the function return that variable before you set it to a new value.
if i've a program running on a server, which one will use more memory:
a = operation1()
b = operation2()
c = doOperation(a, b)
or directy:
a = doOperation(operation1(), operation2())
Edit:
1: i'm using CPython.
2: i'm asking this question because sometimes, i love readability in my code, so instead of writing looong sequences of operation, u just split them into variables.
Edit2:
here is the full code:
class Reset(BaseHandler):
#tornado.web.asynchronous
#tornado.gen.engine
def get(self, uri):
uri = self.request.uri
try:
debut = time.time()
tim = uri[7:]
print tim
cod = yield tornado.gen.Task(db.users.find_one, ({"reset.timr":tim})) # this is temporary variable
code = cod[0]["reset"][-1]["code"] # this one too
dat = simpleencode.decode(tim, code)
now = datetime.datetime.now() # this one too
temps = datetime.datetime.strptime(dat[:19], "%Y-%m-%d %H:%M:%S") # this one too
valid = now - temps # what if i put them all here
if valid.days < 2:
print time.time() - debut # here time.time() has not been set to another variable, used directly
self.render("reset.html")
else:
self.write("hohohohoo")
self.finish()
except (ValueError, TypeError, UnboundLocalError):
self.write("pirate")
self.finish()
as you can see there are variables that are only temporarly useful.
Provided doOperation() does not clear it's own references to the arguments passed in, or create more references to the arguments, until doOperation() completes, the two approaches are exactly the same.
The latter will use less memory once doOperation() completes, because by then the local variables of the function are cleaned up. In the first option, because a and b still hold references, the ref count does not drop to 0.
CPython uses reference counting to clean up any objects that are no longer used; once the reference count drops to 0 objects are automatically cleaned up.
If memory and readability are a concern, you can delete references explicitly:
a = operation1()
b = operation2()
c = doOperation(a, b)
del a, b
but remember that local variables inside a function are cleaned up automatically, so the following would also result in the a and b references being removed:
def foo():
a = operation1()
b = operation2()
c = doOperation(a, b)
Memory occupied by values will only be reclaimed when the values are no longer referenced. Just looking at the examples you gave, it's impossible to tell when those values are no longer referenced, because we don't know what doOperation does.
One thing to keep in mind: assignment never copies values, so merely assigning a value to a name won't increase the memory use.
Also, unless you have an actual memory problem, don't worry about it. :)
I want to use a while loop to initialize class objects with a simple incremented naming convention. The goal is to be able to scale the number of class objects at will and have the program generate the names automatically. (ex. h1...h100...h1000...) Each h1,h2,h3... being its own instance.
Here is my first attempt... have been unable to find a good example.
class Korker(object):
def __init__(self,ident,roo):
self.ident = ident
self.roo = roo
b = 1
hwinit = 'h'
hwstart = 0
while b <= 10:
showit = 'h' + str(b)
print(showit) #showit seems to generate just fine as demonstrated by print
str(showit) == Korker("test",2) #this is the line that fails
b += 1
The errors I get range from a string error to a cannot use function type error.... Any help would be greatly appreciated.
If you want to generate a number of objects, why not simply put them in an array / hash where they can be looked up later on:
objects = {}
for b in range(1,11):
objects['h'+str(b)] = Korker("test", 2)
# then access like this:
objects['h3']
Of course there are ways to make the names available locally, but that's not a very good idea unless you know why you need it (via globals() and locals()).
Variables are names that point to objects that hold data. You are attempting to stick data into the variable names. That's the wrong way around.
instead of h1 to h1000, just call the variable h, and make it a list. Then you get h[0] to h[999].
Slightly different solution to viraptor's: use a list.
h = []
for i in range(10):
h.append(Korker("test",2))
In fact, you can even do it on one line with a list comprehension:
h = [Korker("test", 2) for i in range(10)]
Then you can get at them with h[0], h[1] etc.