'Transferring Classes/Tables from a module to a main function:
recently I have been trying to recreate lua's Table module in python:
class Table():
def __init__(self):
try:
self.Size = enumerate(self)
except:
self.Size = 0
def size(query):
index = 0
for v in query.__dict__.items():
index+=1
return index
def create(query, items=None, t=None):
if items == None and t == None:
class temp():
def __init__(self):
self.Size = Table.size(self)
globals()[query] = temp
elif items != None and t == None:
class test():
def add_child(self, child, val):
setattr(self, child, val)
Size = 6
globals()[query] = test
_ = items.split(',')
Table.create('tempe')
for _z in _:
_f = _z.split('=')
setattr(tempe,_f[0],_f[1])
msg = ('v',str(Table.size(test)+1))
setattr(test,''.join(msg),tempe)
elif t == 'child' and query is not None and type(query) == type:
_ = items.split(',')
Table.create('tempe')
for _z in _:
_f = _z.split('=')
setattr(tempe, _f[0], _f[1])
msg = ('v',str(Table.size(query)+1))
setattr(query,''.join(msg),tempe)
else:
pass
def insert(query, items):
Table.create(query, items, 'child')
def remove(query, item):
temp = ('v', str(item))
item = ''.join(temp)
for k in query.__dict__.items():
for m in k:
if m == item:
del m
def pairs(query,func):
index = 0
for _ in query.__dict__.items():
index+=1
if index > query.Size:
for out in _:
if type(out) == type:
i = index-query.Size
v = out
func(i,v)
Now on its own, that works fine. But now I'm trying to put it into a seperate file so I can do:
from Resources import Table, #other stuff
Table.create('test', 'var1 = 10, var2=Hello World!, var3 = 69')
def pr(i,v):
print(f'{i} = [var1 = {v.var1}, var2 = {v.var2}, var3 = {v.var3}]')
pairs(test,pr)
If I ran that not as a module, I would get:
#1 = [var1 = 10, var2 = Hello World!, var3 = 69]
However when its in a separate file, 'test' does not become a variable.
Any advice/tips are apprecianted.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
