Dynamic classes
Hello all. I was wondering if creating classes could be dynamic. I want to know if I can make a class Person, then read in a list of names (say people's names) so then I can have a class instance created for each name in the list? Why do I want to do this? I was just thinking if I had a name on the list, Dave, I could then be able to read the name in the list, and assign Maria.birthday = <> and all the other attributes I would want to use a class for, except this is dynamic. I don't know how to iterate thru the list to assign the different attributes yet, but this seemed reasonable to want to do, and thought I could learn from this. Thanks! Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic classes
Duh! how dumb am I? A dictionary solves all those problems, with each
entry named, and the value of each name could be a class instace. plus all
the class instances can be iterated by a loop.
Thanks Piet & Alex for your guidance!
-Dave
"Piet van Oostrum" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>>>>>> "Dave Rose" <[EMAIL PROTECTED]> (DR) wrote:
>
>>DR> Hello all.
>>DR> I was wondering if creating classes could be dynamic. I want to know
>>DR> if I can make a class Person, then read in a list of names (say
>>DR> people's names) so then I can have a class instance created for each
>>DR> name in the list?
>
> If you have the class Person, you are not creating it dynamically. And of
> course you can create instances dynamically as you describe.
>
>>DR> Why do I want to do this? I was just thinking if I had a name on
>>the
>>DR> list, Dave, I could then be able to read the name in the list, and
>>DR> assign Maria.birthday = <> and all the other attributes I would want
>>DR> to use a class for, except this is dynamic. I don't know how to
>>DR> iterate thru the list to assign the different attributes yet, but this
>>DR> seemed reasonable to want to do, and thought I could learn from this.
>
> If I understand you correctly, you want to create a variable with name
> 'Maria' when you read the name "Maria". Creating variables dynamically is
> possible in Python but is almost always the wrong thing to do. Instead it
> is usually better to use a dictionary.
>
> class Person:
> def __init__(self, name):
> self.name = name
>
> persons = {}
>
> now you have a loop that reads persons' names, say in name.
>
> myperson = persons[name] = Person(name)
>
> Now I suppose you want to read additional attributes, while the list of
> possible attributes is in principle open.
>
> So suppose you have read the attribute name in attr and the value in val.
> The you can dynamically create an instance attribute with:
>
> setattr(myperson, attr, val)
> --
> Piet van Oostrum <[EMAIL PROTECTED]>
> URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
> Private email: [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list
Re: I forgot to wind up my example .... ; (
So, I forgot the last part of my example that might gel in your mind why
Objects are useful in certain situations. Ok so you maybe followed my example
of the shopping cart. Let's just forget for a moment the use for shopping
carts is for websites. Let's just say you were going to write the lines
directly into Python, like maybe at the IDLE interpreter. Like maybe you're
testing the functionality of the routine for correctness, not actual
implementation.
You have a ShoppingCartClass(), and three users-> Dave, Tommy, Bryan.
ShoppngCartClass() has 3 methods:
.AddItem()
.RemoveItem()
.CheckOut()
These are really just 'def' routintes you write in the class to do some action
or calculation. Here, we want to either add an item to 'the cart', remove
one, or finalize the order.
In the interpreter, you could do this. Define 3 users of the
ShoppingCartClass.
Dave = ShoppingCartClass()
Tommy = ShoppingCartClass()
Bryan = ShoppingCartClass()
Ok. Now you could do different things to each:
Dave.AddItem(sku=5)
Tommy.AddItem(sku=77)
Tommy.AddItem(sku=12)
Tommy.RemoveItem(sku=12)
Dave.CheckOut(state=CT, ccard='visa', ccardnum='1234-5678-8765-431')
Tommy.CheckOut(stsate=RI, ccard='mastercard', ccardnum='431-123-4321-1234')
Bryan.CancelOrder()
so, if you were then to take account of what you had, you'd know:
Dave has item SKU=5
Tommy has item SKU=77
Bryan has his order cancelled.
This is still very hard-coded. You could abstract, or maybe variablize,
things more. Let's try:
You can mix classes with say dictionaries, to make their use in routines more
beneficial.
So, you could have:
user = âDaveâ
ShoppingCart={}
ShoppingCart[user] = ShoppingCartClass()
user = âTommyâ
ShoppingCart[user] = ShoppingCartClass()
user = âDaveâ
ShoppingCart[user].AddItem(sku=55)
user = âTommyâ
ShoppingCart[user].CheckOut( ... )
ShoppingCart[âDaveâ].CheckOut( ... )
Putting the classes in the dictionary allow you to use names from things like
fields, config files, text files, TextControls in my favorite program
wxPython, etc.
If you can wrap your mind around this, you're well on your way to using OOP I
believe. If not, , don't give up. I'm just a python/programming newbie and
maybe missed the boat completely with my posting.
-Dave
--
http://mail.python.org/mailman/listinfo/python-list
Byte-operations.
I hope someone can please help me. A few months ago, I found a VBS file,
MonitorEDID.vbs on the internet. It is great in that it will extract from the
registry of Windows the serial number of the attached monitor. (System
Administration / PC Inventory usage is obvious)
Standalone, it works fine. However, I'd like to integrate it with other system
interrogations via Tim Golden's WMI and Marc Hammonds Win32all to get RAM, HD
size, CPU, PC Serial Number, etc and to also take that info and toss into a
lite database, such as KirbyBase.
However, my Python is limited. I've gotten the first half of the program
translated (I believe, I haven't done exhaustive testing).
So, the data is stuck in a dictionary. A clipping is shown here:
{0: '\x00\xff\xff\xff\xff\xff\xff\x00$M\x93!\x01\ <-SNIP-> '}
Anyway, the functions from VBS I don't know how to translate to Python are:
#location(0)=mid(oRawEDID(i),0x36+1,18)
#location(1)=mid(oRawEDID(i),0x48+1,18)
#
##you can tell if the location contains a serial number if it starts
with 0x00 00 00 ff
#strSerFind=chr(0x00) & chr(0x00) & chr(0x00) & chr(0xff)
#
##or a model description if it starts with 0x00 00 00 fc
#strMdlFind=chr(0x00) & chr(0x00) & chr(0x00) & chr(0xfc)
##the week of manufacture is stored at EDID offset 0x10
#tmpmfgweek=ord(mid(oRawEDID(i),0x10+1,1))
##store it in month/year format
#if tmpEDIDMajorVer < 255-48 and tmpEDIDRev < 255-48 :
#tmpver=chr(48+tmpEDIDMajorVer) & "." & chr(48+tmpEDIDRev)
#else:
#tmpver="Not available"
#if (Byte1 and 16) > 0:
#Char1=Char1+4
If someone can help me understand what to do with these types of commands, I'd
be much appreciative. Once it's translated, I'd be happy to share this!
Thanks!
Dave
--
http://mail.python.org/mailman/listinfo/python-list
refactoring, unit testing, ant, etc.
Hello all I've been learning Python for the past few months, reading tutorials and postings here. I've bought the personal Komodo then downloaded Eclipse with Pydev. IDLE also is a staple of everyday use. ~10 years ago, I had CS as a minor in college. I learned some C and modula-2 to name the relevent languages. Back then I was compiling on a VAX. Much different than today. I was hoping someone could guide me a bit with current-day practices. I've been pythoning along, but I hear about things I don't know about (re-factoring, unit-testing, debugging, ant build tools) and I don't know how they're applicable to make my life easier. Tutorials didn't shed too much light -- they make it seem to be more work than I need now. To be clear, I've used debuggers in the past, but it seems when I set break- points, the Eclipse just skips merrily past them, or steps at every line, and for wxPython, that's annoying given all the support libraries. I also use wxPython and wxGlade, so those seem to ignore debug mode completely for some reason. Can someone please shed some light onto how I should really use my IDE to make my life more enjoyable? I feel I just don't know how to use the tools that are available, and that's frustrating. Thanks so much! Dave -- http://mail.python.org/mailman/listinfo/python-list
Python to open command script file
Hello all
I would like to know how to do the following. I'd like to have a generic
python program that the user will open a command-script file to do actions.
So, my python program will get a list of servers, enumerate them within a
checklistbox in wxpython. Then I want to open a command-file that will run
against each selected server that will look like this:
---SAMPLE
copy abc.xyz from c:\source to u:\target
copy all from c:\source to u:\target
unload netshld from server1
-END SAMPLE--
(where I have a class serverfuncs, where I can:
s = serverfuncs()
s.unload('', '')
Really, I can do all the actions, but I don't know how to trigger them from an
independent script file.
Thanks!
-Dave
--
http://mail.python.org/mailman/listinfo/python-list
