use var to form name of object

G

gel

Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables. Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])
 
G

gel

gel said:
Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables. Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])

I sorted it out
 
B

Bruno Desthuilliers

gel said:
gel wrote:

Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables. Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])


I sorted it out
Then do a favour to other persons facing the same problem : share your
solution. As a side effect, you'll also have your code checked by lot of
confirmed Python programmer !-)
 
G

gel

Bruno said:
gel said:
gel wrote:

Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables. Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])


I sorted it out
Then do a favour to other persons facing the same problem : share your
solution. As a side effect, you'll also have your code checked by lot of
confirmed Python programmer !-)

OK, I would love to, the reason I did not was for fear of wasting
peoples time with basic stuff... I ended up doing it a bit differently

what I am doing is using pyro to make a package that can be run on PCs
to control how many PC on a network are running a piece of software,the
function

def a_d(self,software,mac):
global d_software
d_software[software][0].append(mac)
return d_software

is in the module on the pyro server. When this object is used on the
client it adds the clients mac address to the dictionary list. When
another client is runs the software being watched it checks how many
are already running by counting the number of mac addresses already in
there. If the number of PCs already running the software is below the
number allowed, which is set in the pyro module it is allowed to
continue to run it. If the limit has been reached the client software
kills the software, Below is the client and the pyro module...

Client

import wmi
import time
import Pyro.core
c = wmi.WMI()
o=Pyro.core.getAttrProxyForURI('PYRONAME://:Default.test')
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
# print interface.Description, interface.MACAddress
for ip_address in interface.IPAddress:
mac = interface.MACAddress
mac = 1
current_time = time.time()
sw = "notepad.exe"

#o.a_d(sw, mac, current_time, "Maybe More Extra Stuff too")

while 1:
time.sleep(1.0)
ps_li=[]
for process in c.Win32_Process ():
# print process.ProcessId, process.Name
ps_li += [process.Name]

if sw in ps_li:
if len(o.r_d()[sw][0]) != 0:
if mac in o.r_d()[sw][0]:
print "Already in there"
else:
#ps_id = ps_li.index(sw)
if len(o.r_d()[sw][0]) < o.r_l()[sw]:
o.a_d(sw, mac)
print "There are some"
print o.r_d()

else:
print "There are none"
for process in c.Win32_Process ():
if process.Name == "notepad.exe":
print process.ProcessId, process.Name
result = process.Terminate ()
print process.Name +" has been killed"
else:
o.a_d(sw, mac)
print "First in"
else:
print sw + " not running"

pyro module

class testclass:
import wmi
import time
global d_software
global l_notepad
global d_licence_numbers
d_licence_numbers = {"notepad.exe":1, "Adobe":1}
l_notepad =[]
d_software = {"notepad.exe":[[],[]], "Adobe":[[],[]]}


def r_len_d(self):
return len(d_licence_numbers)

def a_d(self,software,mac):
global d_software
d_software[software][0].append(mac)
return d_software

def r_d(self):
return d_software

def r_l(self):
return d_licence_numbers
 
G

gel

gel said:
Bruno said:
gel said:
gel wrote:


Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables. Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])


I sorted it out
Then do a favour to other persons facing the same problem : share your
solution. As a side effect, you'll also have your code checked by lot of
confirmed Python programmer !-)

OK, I would love to, the reason I did not was for fear of wasting
peoples time with basic stuff... I ended up doing it a bit differently

what I am doing is using pyro to make a package that can be run on PCs
to control how many PC on a network are running a piece of software,the
function

def a_d(self,software,mac):
global d_software
d_software[software][0].append(mac)
return d_software

is in the module on the pyro server. When this object is used on the
client it adds the clients mac address to the dictionary list. When
another client is runs the software being watched it checks how many
are already running by counting the number of mac addresses already in
there. If the number of PCs already running the software is below the
number allowed, which is set in the pyro module it is allowed to
continue to run it. If the limit has been reached the client software
kills the software, Below is the client and the pyro module...

Client

import wmi
import time
import Pyro.core
c = wmi.WMI()
o=Pyro.core.getAttrProxyForURI('PYRONAME://:Default.test')
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
# print interface.Description, interface.MACAddress
for ip_address in interface.IPAddress:
mac = interface.MACAddress
mac = 1
current_time = time.time()
sw = "notepad.exe"

#o.a_d(sw, mac, current_time, "Maybe More Extra Stuff too")

while 1:
time.sleep(1.0)
ps_li=[]
for process in c.Win32_Process ():
# print process.ProcessId, process.Name
ps_li += [process.Name]

if sw in ps_li:
if len(o.r_d()[sw][0]) != 0:
if mac in o.r_d()[sw][0]:
print "Already in there"
else:
#ps_id = ps_li.index(sw)
if len(o.r_d()[sw][0]) < o.r_l()[sw]:
o.a_d(sw, mac)
print "There are some"
print o.r_d()

else:
print "There are none"
for process in c.Win32_Process ():
if process.Name == "notepad.exe":
print process.ProcessId, process.Name
result = process.Terminate ()
print process.Name +" has been killed"
else:
o.a_d(sw, mac)
print "First in"
else:
print sw + " not running"

pyro module

class testclass:
import wmi
import time
global d_software
global l_notepad
global d_licence_numbers
d_licence_numbers = {"notepad.exe":1, "Adobe":1}
l_notepad =[]
d_software = {"notepad.exe":[[],[]], "Adobe":[[],[]]}


def r_len_d(self):
return len(d_licence_numbers)

def a_d(self,software,mac):
global d_software
d_software[software][0].append(mac)
return d_software

def r_d(self):
return d_software

def r_l(self):
return d_licence_numbers

To make the client side a little less CPU intensive, I would like to
catch the events when a new process starts and when a process ends,
using wmi. That will mean that I dont have to search through all
running processes. Can anyone help with the best way to trap the
starting of a new process and the ending of an existing one?
 
S

Simon Forman

gel wrote:

class testclass:
import wmi
import time
global d_software
global l_notepad
global d_licence_numbers
d_licence_numbers = {"notepad.exe":1, "Adobe":1}
l_notepad =[]
d_software = {"notepad.exe":[[],[]], "Adobe":[[],[]]}


Wow! For a second there I thought you could make a "global" class
attribute in a way I'd never seen before...

But it's turns out you can't.
global k
def wow(self, n):
self.k += n


Traceback (most recent call last):
File "<pyshell#31>", line 1, in -toplevel-
foo.k
AttributeError: class foo has no attribute 'k'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in -toplevel-
f.k
AttributeError: foo instance has no attribute 'k'
Traceback (most recent call last):
File "<pyshell#33>", line 1, in -toplevel-
f.wow(23)
File "<pyshell#29>", line 4, in wow
self.k += n
AttributeError: foo instance has no attribute 'k'

BTW, that's a weird place to put import statements.

Peace,
~Simon
 
G

gel

Simon said:
gel wrote:

class testclass:
import wmi
import time
global d_software
global l_notepad
global d_licence_numbers
d_licence_numbers = {"notepad.exe":1, "Adobe":1}
l_notepad =[]
d_software = {"notepad.exe":[[],[]], "Adobe":[[],[]]}


Wow! For a second there I thought you could make a "global" class
attribute in a way I'd never seen before...

But it's turns out you can't.
global k
def wow(self, n):
self.k += n


Traceback (most recent call last):
File "<pyshell#31>", line 1, in -toplevel-
foo.k
AttributeError: class foo has no attribute 'k'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in -toplevel-
f.k
AttributeError: foo instance has no attribute 'k'
Traceback (most recent call last):
File "<pyshell#33>", line 1, in -toplevel-
f.wow(23)
File "<pyshell#29>", line 4, in wow
self.k += n
AttributeError: foo instance has no attribute 'k'

BTW, that's a weird place to put import statements.

Peace,
~Simon

Yeah I am still getting my head around things... not exactly sure what
you where saying about the globals, but this works


global k
k = 5
class foo:

def wow(self, n):
global k
k += n
return k


f=foo()
f.wow(55)
 
M

Marc 'BlackJack' Rintsch

Yeah I am still getting my head around things... not exactly sure what
you where saying about the globals, but this works


global k
k = 5
class foo:

def wow(self, n):
global k
k += n
return k


f=foo()
f.wow(55)

The first ``global`` does nothing. ``global`` at module level makes no
sense. And the snippet could be easily written without assigning to
global names from within a function/method:

k = 5
class Foo:
def wow(self, n):
return k + n

f = Foo()
k = f.wow(55)

Ciao,
Marc 'BlackJack' Rintsch
 
G

gel

Marc said:
The first ``global`` does nothing. ``global`` at module level makes no
sense. And the snippet could be easily written without assigning to
global names from within a function/method:

k = 5
class Foo:
def wow(self, n):
return k + n

f = Foo()
k = f.wow(55)

Ciao,
Marc 'BlackJack' Rintsch

Ah yes, thanks for that Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top