Thanks for the suggestions and sorry about the errors when I tried to anonymize my code. It's turns out when I ran the code through setup, I had an __init__.py file incorrectly defined which generated the error. The error from the traceback was at the line where I was importing the module. I have it working now.
Bob -----Original Message----- From: Prasad, Ramit [mailto:ramit.pra...@jpmorgan.com] Sent: Tuesday, September 24, 2013 6:23 PM To: Treder, Robert (Research); tutor@python.org Subject: RE: How to create dictionaries loadable with import Treder, Robert wrote: > Hi Python tutors, > > I'm fairly new to Python. I'm working with Python v2.7.4 and the nltk > package on a couple of text > mining projects. I create several dictionaries that are pretty static. Will > probably only be updated > every or month or every couple of months. I want to turn those dictionaries > into loadable data sets > prior to running a module which uses them. If I define several dictionaries, > dict1, dict2 and dict3, > in a single module named myDict, I'd like to do > > from myDict import * > > I've tried defining the dictionaries in a the myDict module as follows: > > Dict1 = {} > with open('file1, 'rb') as infile: Without the closing quote for file1, this is a SyntaxError. > reader = csv.reader(infile, delimiter = ',') > for row in reader: > try: > Dict1[ row[1] ].append(row[0]) > except: > Dict1[ row[1] ] = [ row[0], ] > > Dict2 = {} > with open('file2, 'rb') as infile: Without the closing quote for file2, this is a SyntaxError. > reader = csv.reader(infile, delimiter = ',') > for row in reader: > try: > Dict2[ row[1] ].append(row[0]) > except: > Dict2[ row[1] ] = [ row[0], ] > > These are simple dictionary structures with no additional structure, i.e., > not embedded in classes or > functions. > The try/except sequence is because some of the keys may be duplicated in the > files and I want to > append the values rather than overwrite. Hmm, I think you are incorrect. The try/except logic is used when there is not row[1] in the dictionary (i.e. every first entry of a key). I guess that handles multiple keys, but it would be clearer if you used the setdefault method on the dictionary. Dict1.setdefault( row[1], [] ).append( row[0] ) > Now when I build the module with setup tools > > python setup.py install -prefix=C:\PY_MODULES Unless you have a non-standard environment (at least not one I am familiar with), you do not need setup tools as long as the script is somewhere on the PYTHONPATH. Also, the dictionaries are not created except on first import of the module (at run-time not compile-time). If they are static or mostly so, you may want to just hard code them in the myDict module (lowercase for module names is the community standard) for easier modification. In addition, that means you can get rid of the source files file1 and file2. You can use `print repr(Dict1)` to copy paste the dictionaries into the code. > > it builds without error but I can't find the dictionaries when I load the > module > from myDict import * > AttributeError: 'module' object has no attribute 'Dict1' > > How can I make the dictionaries loadable using import? The import and the error do not seem to match. Can you please copy/paste (do not paraphrase or try to retype) the FULL error? > > Thanks, > Bob ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -------------------------------------------------------------------------------- NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers. If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor