On 10/04/2010 22:57, Clinton Lee Taylor wrote:
  This post is a little off topic, but I believe with-in scope of
python and how pywin32 is used, if anybody is offended by this post,
just delete it ... ;-)

Looks pretty much on-topic to me. At any rate, I'll bite...

  I'm currently write a util that is meant to sync Contacts with a
database, which can be found at ...
http://code.google.com/p/pycontactsync/ ... Please don't laugh at the
code, I'm still a very junior  python programmer and still working
through some of the basic OO ideas.

I've pulled the current subversion trunk to look at. There appears to
be only one directory: tests with a set of non-package subdirectories
(ie without an __init__.py). This is just so you know what I'm referring
to.

One very initial comment is that you're repeating the adoconstants.py
in two different "packages" (not sure if they're meant to be packages
or not). This is something of a code smell, but I assume that this is
not your final structure so...

A second point is that the ado constants can (probably) be pulled
automatically when you dispatch the adodb connection:

<code>
import win32com.client
constants = win32com.client.constants

ado = win32com.client.gencache.EnsureDispatch ("ADODB.Connection")
#
# At this point, win32com.client.constants will contain a classalike
# with a bunch of ad_... constants
#
print constants.adLongVarChar
print constants.adMarshallAll

</code>

  Looking to map the attribs in the Contacts, into a nice to read and
easy to use array of sorts ... Currently I'm writing alot of code and
looking plenty of things up ... I was reading into
lists/tuples/dictionaries, but can't come up with code that works or
makes sense.  Was thinking of multi-dimensional array, but I'm really
lost.

It may be that my last answer solved part at least of your issues. However,
you've got several options here, depending on exactly how you want to
use things (and personal preference, of course). If all you want is a
nice mapping to-and-from constant and constant name just use a dictionary,
or two dictionaries:

double_dict = {}
double_dict['const1'] = 1
double_dict[1] = 'const1'

or

dict1 = {}
dict2 = {}
dict1['const1'] = 1
dict2[1] = 'const1'

Obviously these fall down pretty quick when you've got the same value for 
different
names -- which will obviously happen. So you have to invent your own namespaces
(say, a list of dicts or a dict of dicts) at the risk of reinventing Python :).

Your example above talks of tracking a description and maybe other things. Two
or three close choices here, depending on how you expect things to work:

Dict of tuples:

<code>
stuff = [
  ('adDouble', constants.adDouble, 'A double-width whatever in ad'),
  ('adPropRequired', constants.adPropRequired, 'A property is required'),
]
data_dictionary = dict ((s[0], s) for s in stuff)
</code>

This will give you a dictionary keyed by name with the value being the tuple.
This works adequately for fixed-length tuples but doesn't scale. You could
throw in the recently-added namedtuple for better effect, but it still outgrows
itself soonish.

Classes:

<code>
class Stuff (object):
  def __init__ (self, name, value, description):
    self.name = name
    self.value = value
    self.description = description

stuff = [
  ('adDouble', constants.adDouble, 'A double-width whatever in ad'),
  ('adPropRequired', constants.adPropRequired, 'A property is required'),
]
data_dictionary = dict ((s[0], Stuff (*s)) for s in stuff)

</code>

This is a very slightly more scalable version of the last one. The class I've
defined is very Noddy but it could be extended if that were required.

You could do the same kind of thing with dictionaries, which is a sort of
halfway house. (The class I've sketched above is little more than a dict
in disguise).

Hopefully that's food for thought. Do come back with clearer requirements of
questions

TJG
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to