[Tutor] How to create dictionaries loadable with import

Joel Goldstick joel.goldstick at gmail.com
Tue Sep 24 23:08:45 CEST 2013


On Tue, Sep 24, 2013 at 4:33 PM, Treder, Robert <
Robert.Treder at morganstanley.com> 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:****
>
>     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:****
>
>     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. ****
>
> ** **
>
> Now when I build the module with setup tools****
>
> ** **
>
> python setup.py install –prefix=C:\PY_MODULES****
>
> ** **
>
> 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'
>

Rather than snip this error, can you show the complete traceback, which
will show what line it occurs in your code.  Does the error occur in the
MyDict module or in code that imports it?

A couple of things.  It is considered sketchy to do from xxx import *
Better is to do import MyDict

then you can access your dictionaries as MyDict.Dict1  and so on.

> ****
>
> ** **
>
> How can I make the dictionaries loadable using import? ****
>
> ** **
>
> Thanks, ****
>
> Bob****
>
> ** **
>
> ** **
>
>
> ------------------------------
>
> 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130924/12d07b78/attachment-0001.html>


More information about the Tutor mailing list