By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

use var to form name of object

P: n/a
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])

Jul 5 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
gel

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

Jul 5 '06 #2

P: n/a
gel wrote:
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 !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 5 '06 #3

P: n/a
gel
Bruno Desthuilliers wrote:
gel wrote:
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 !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
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

Jul 6 '06 #4

P: n/a
gel

gel wrote:
Bruno Desthuilliers wrote:
gel wrote:
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 !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"

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?

Jul 6 '06 #5

P: n/a
gel wrote:

<snip>
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.
>>k = 0
class foo:
global k
def wow(self, n):
self.k += n

>>foo.k
Traceback (most recent call last):
File "<pyshell#31>", line 1, in -toplevel-
foo.k
AttributeError: class foo has no attribute 'k'
>>f = foo()
f.k
Traceback (most recent call last):
File "<pyshell#32>", line 1, in -toplevel-
f.k
AttributeError: foo instance has no attribute 'k'
>>f.wow(23)
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

Jul 6 '06 #6

P: n/a
gel

Simon Forman wrote:
gel wrote:

<snip>
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.
>k = 0
class foo:
global k
def wow(self, n):
self.k += n

>foo.k

Traceback (most recent call last):
File "<pyshell#31>", line 1, in -toplevel-
foo.k
AttributeError: class foo has no attribute 'k'
>f = foo()
f.k

Traceback (most recent call last):
File "<pyshell#32>", line 1, in -toplevel-
f.k
AttributeError: foo instance has no attribute 'k'
>f.wow(23)

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)

Jul 6 '06 #7

P: n/a
In <11*********************@a14g2000cwb.googlegroups. com>, gel wrote:
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
Jul 6 '06 #8

P: n/a
gel

Marc 'BlackJack' Rintsch wrote:
In <11*********************@a14g2000cwb.googlegroups. com>, gel wrote:
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
Ah yes, thanks for that Marc

Jul 17 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.