[Tutor] How to create dictionaries loadable with import

Prasad, Ramit ramit.prasad at jpmorgan.com.dmarc.invalid
Wed Sep 25 00:23:06 CEST 2013


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.  


More information about the Tutor mailing list