using exec() to instantiate a new object.

2008-11-07 Thread RyanN
Hello,

I'm trying to teach myself OOP to do a data project involving
hierarchical data structures.

I've come up with an analogy for testing involving objects for
continents, countries, and states where each object contains some
attributes one of which is a list of objects. E.g. a country will
contain an attribute population and another countries which is a list
of country objects. Anyways, here is what I came up with at first:

class continent(object):
def __init__(self,continent_name):
self.name = continent_name
self.countries = []
def addCountry(self,country_name):
self.countries.append(country_name)
def listCountries(self):
for country in self.countries:
print country.name, "pop:",country.population,", states:"
country.listStates()

class country(object):
def __init__(self,name):
self.name = name
self.population = 0
self.states = []
def addState(self,state_name):
self.states.append(state_name)

def listStates(self):
for state in self.states:
print " ",state.name,"pop:",state.population
state.stateInfo()

class state(object):
def __init__(self,state_name):
self.name = state_name
self.color = 'unknown'
self.counties = []
self.population = 0
def addCounty(self,county):
self.counties.append(county)
def stateInfo(self):
print "   color:",self.color
print "   counties",self.counties[:]


NAm = continent('NAm')
usa= country('usa')
canada = country('canada')
mexico = country('mexico')
florida = state('florida')
maine = state('maine')
california = state('california')
quebec = state('quebec')

NAm.addCountry(usa)
NAm.addCountry(canada)
NAm.addCountry(mexico)
usa.addState(maine)
usa.addState(california)
usa.addState(florida)
canada.addState(quebec)
florida.addCounty('dade')
florida.addCounty('broward')
maine.addCounty('hancock')
california.addCounty('marin')

florida.population = 1000
california.population = 2000
maine.population = 500
quebec.population = 1000
florida.color = maine.color = california.color = 'blue'
NAm.listCountries()

--
so this works but is far more cumbersome than it should be.
I would like to create an object when I add it

so I wouldn't have to do:
usa= country('usa')
NAm.addCountry(usa)

I could just do
NAm.addCountry('usa')

which would first create a country object then add it to a countries
list

to do this I tried:

def addCountry(self,country_name):
# create an instance of country
exec(country_name + "= country('" + country_name + "')")
# Add this new instance of a country to a list
exec("self.countries.append(" + country_name + ")")

Which doesn't give an error, but doesn't seem to create an instance of
the country object.

Does this make sense? Can this be done?
For my real project, I won't know the names and quantities of objects.
They will be highly variable and based on data contained in the
"parent" object.

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: using exec() to instantiate a new object.

2008-11-10 Thread RyanN
Thank you both, I knew there had to be a good way of doing this.

-Ryan
--
http://mail.python.org/mailman/listinfo/python-list


Re: using exec() to instantiate a new object.

2008-11-10 Thread RyanN
On Nov 10, 7:47 am, RyanN wrote:
> Thank you both, I knew there had to be a good way of doing this.
>
> -Ryan

Just an update. I used dictionaries to hold objects and their names.
I'm beginning to understand better. Now to apply this to my actual
problem. Here's the code I ended up with:

class continent(object):
'''
A continent has a name and a dictionary of countries
'''
def __init__(self,continent_name):
self.name = continent_name
self.countries = {} #countries is a dictionary of country name
and object
def addCountry(self,country_name,population = 0):
self.countries[country_name] = country(country_name) #Create a
new instance of country() and add it to dictionary
self.countries[country_name].population = population #Set
country population
def addState(self,country_name,state_name,population = 0):
if country_name in self.countries:
 
self.countries[country_name].addState(state_name,population)
else: #This state must be in a new country
self.addCountry(country_name)
self.addState(country_name,state_name,population)
def listCountries(self):
for a_country in self.countries:
print a_country,
"pop:",self.countries[a_country].population,", states:"
self.countries[a_country].listStates()

class country(object):
'''
A country has a name, a population and a dictionary of states
'''
def __init__(self,name):
self.name = name
self.population = 0
self.states = {} #states is a dictionary of state name and
object
def addState(self,state_name,population = 0):
self.states[state_name] = state(state_name) #Create a new
instance of state() and add it to dictionary
self.states[state_name].population = population
self.population += population  #Add this state's population to
the country's

def listStates(self):
#print self.states[:]
for a_state in self.states:
self.states[a_state].stateInfo()

class state(object):
'''
A state has a name, color, and a population
'''
def __init__(self,state_name):
self.name = state_name
self.color = 'unknown'
self.population = 0
def stateInfo(self):
print "   ",self.name,"pop:",self.population,
"color:",self.color

#Now some examples of how to set and access this information
NAm = continent('NAm')   #First we add our continent
NAm.addCountry('canada',700) #Now add a a country to NAm
NAm.addState('usa','maine',400) #We can add a state even if we haven't
added the country yet
NAm.addState('usa','california',2000)
NAm.addState('canada','quebec',700) # I know it's actually a province
NAm.addState('mexico','QR',550)
usa = NAm.countries['usa']  # we can make things easier on ourselves
usa.population = 5000 #short for: NAm.countries['usa'].population =
5000
usa.addState('florida') #Another way to add a state, we can set
population seperately
NAm.countries['usa'].states['florida'].population = 2000
for any_state in usa.states: #Set an attribute for all state objects
usa.states[any_state].color = 'blue'
NAm.listCountries() # Generates a report
# three ways to get to print the same information
print NAm.countries['usa'].states['maine'].name,
NAm.countries['usa'].states['maine'].population
print usa.states['maine'].name, usa.states['maine'].population # We
already assigned usa to NAm.countries['usa']
maine = usa.states['maine']
print maine.name, maine.population
--
http://mail.python.org/mailman/listinfo/python-list


win32com and DispatchWithEvents

2008-11-13 Thread RyanN
Greetings,

I'm trying to get DispatchWithEvents() to work with HyperAccess
(terminal program) without much success. I've done a bunch of
searching and found some examples using IE:

This works but doesn't handle the "Event Driven Functions":
haObj = win32com.client.Dispatch(r"HAWin32")

And so does this Internet Explorer Example:
ieObj = win32com.client.DispatchWithEvents
("InternetExplorer.Application", ExplorerEvents)

But this does not:
haObj = win32com.client.DispatchWithEvents("HAWin32", HAEvents)
It fails with the error message:
"TypeError: This COM object can not automate the makepy process -
please run makepy manually for this object"

After some searching I tried:
mod = win32com.client.gencache.EnsureModule('{5178CCE1-AAEF-11CE-
AE75-00AA0030EBC8}', 0, 1, 0)
haObj = win32com.client.DispatchWithEvents(mod, HAEvents)
Which fails with the error message:
ttributeError: 'module' object has no attribute 'GetTypeInfo'

So I tried this approach with IE:
mod = win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-
A7EB-C05BAE0B8}', 0, 1, 0)
ieObj = win32com.client.DispatchWithEvents(mod, ExplorerEvents)
But this too breaks with the same GetTypeInfo error.


Any ideas or resources? I can provide the genpy output if that helps.
--
http://mail.python.org/mailman/listinfo/python-list


Re: win32com and DispatchWithEvents

2008-11-14 Thread RyanN
On Nov 13, 2:16 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote:
> On Nov 13, 10:27 am, RyanN <[EMAIL PROTECTED]> wrote:
>
>
>
> > Greetings,
>
> > I'm trying to get DispatchWithEvents() to work with HyperAccess
> > (terminal program) without much success. I've done a bunch of
> > searching and found some examples using IE:
>
> > This works but doesn't handle the "Event Driven Functions":
> > haObj = win32com.client.Dispatch(r"HAWin32")
>
> > And so does this Internet Explorer Example:
> > ieObj = win32com.client.DispatchWithEvents
> > ("InternetExplorer.Application", ExplorerEvents)
>
> > But this does not:
> > haObj = win32com.client.DispatchWithEvents("HAWin32", HAEvents)
> > It fails with the error message:
> > "TypeError: This COM object can not automate the makepy process -
> > please run makepy manually for this object"
>
> Did you try the makepy process? I haven't used it lately, but I seem
> to recall it was easy to use...
>
>
>
> > After some searching I tried:
> > mod = win32com.client.gencache.EnsureModule('{5178CCE1-AAEF-11CE-
> > AE75-00AA0030EBC8}', 0, 1, 0)
> > haObj = win32com.client.DispatchWithEvents(mod, HAEvents)
> > Which fails with the error message:
> > ttributeError: 'module' object has no attribute 'GetTypeInfo'
>
> > So I tried this approach with IE:
> > mod = win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-
> > A7EB-C05BAE0B8}', 0, 1, 0)
> > ieObj = win32com.client.DispatchWithEvents(mod, ExplorerEvents)
> > But this too breaks with the same GetTypeInfo error.
>
> > Any ideas or resources? I can provide the genpy output if that helps.
>
> There's the PyWin32 user's group, where the maintainers of PyWin32
> hang out and answer questions:
>
> http://mail.python.org/mailman/listinfo/python-win32
>
> Mike

Thanks. I had used makepy to get the class ID and yes it was easy to
use. I have asked on the python-win32 group as you suggested.

-Ryan
--
http://mail.python.org/mailman/listinfo/python-list