Design question.

L

Lacrima

Hello!

I am newbie in python and I have really simple question, but I need
your advice to know how to do best.
I need to store a number of dictionaries in certain place. I've
decided to store them in a separate module.
Like this:
dicts.py
-----------------------
dict1 = {....}
dict2 = {....}
dict3 = {....}

Then, when I need any dictionary, I can access it:
import dicts
dicts.dict1

Is it a good practice? Or should I store them as class attributes or
anything else?

Thanks in advance.

With regards, Max
(sorry if my English isn't very proper)
 
D

Diez B. Roggisch

Lacrima said:
Hello!

I am newbie in python and I have really simple question, but I need
your advice to know how to do best.
I need to store a number of dictionaries in certain place. I've
decided to store them in a separate module.
Like this:
dicts.py
-----------------------
dict1 = {....}
dict2 = {....}
dict3 = {....}

Then, when I need any dictionary, I can access it:
import dicts
dicts.dict1

Is it a good practice? Or should I store them as class attributes or
anything else?

It's perfectly fine to use dictionaries as module-level objects for storing
e.g. configuration-data.

OTOH it's also fine to do so as class-attributes. Choosing one over the
other has a lot to do with the actual problem you are working on.


Diez
 
L

Lacrima

It's perfectly fine to use dictionaries as module-level objects for storing
e.g. configuration-data.

OTOH it's also fine to do so as class-attributes. Choosing one over the
other has a lot to do with the actual problem you are working on.

Diez

Hi!

Thank you very much for your so soon reply!

With regards,
Max
 
J

Jean-Michel Pichavant

Lacrima said:
Hello!

I am newbie in python and I have really simple question, but I need
your advice to know how to do best.
I need to store a number of dictionaries in certain place. I've
decided to store them in a separate module.
Like this:
dicts.py
-----------------------
dict1 = {....}
dict2 = {....}
dict3 = {....}

Then, when I need any dictionary, I can access it:
import dicts
dicts.dict1

Is it a good practice? Or should I store them as class attributes or
anything else?

Thanks in advance.

With regards, Max
(sorry if my English isn't very proper)
Defining dict as a module attribute ic correct, but try to answer the
following question:

Is dict1 an attribute/property/declension of the object/entity defined
by the module dicts ?
If yes, then your design is correct.

An correct example:
fruits.py
------------
apple = {}
banana = {}

An incorrect one:
fruit.py
-----------
apple={}
bicycle={}

Basically, the rule is very straightforward, set your dict as a module
attribute only if its one of its attribute (very nice paraphrase !)
Most of people (including me) tend to have a module, where they put
everything they have no idea about their location. This is a bad habit
and result from a uncontrolled/undocumented design. Usually documenting
objects in such modules is really painful.

Your proposal is fine from a python syntax point of view. I can't tell
of your design with names like (dicts.py and dict1,dict2) hoping you do
not intend to name them that way.

JM
 
L

Lacrima

Defining dict as a module attribute ic correct, but try to answer the
following question:

Is dict1 an attribute/property/declension of the object/entity defined
by the module dicts ?
If yes, then your design is correct.

An correct example:
fruits.py
------------
apple = {}
banana = {}

An incorrect one:
fruit.py
-----------
apple={}
bicycle={}

Basically, the rule is very straightforward, set your dict as a module
attribute only if its one of its attribute (very nice paraphrase !)
Most of people (including me) tend to have a  module, where they put
everything they have no idea about their location. This is a bad habit
and result from a uncontrolled/undocumented design. Usually documenting
objects in such modules is really painful.

Your proposal is fine from a python syntax point of view. I can't tell
of your design with names like (dicts.py and dict1,dict2) hoping you do
not intend to name them that way.

JM

Hi, Jean-Michel!

Thanks for your answer.
I am not going to have names like dicts.py and dict1,dict2. That was
just example. I wanted to know if it is correct to have dictionaries
on a module level. Now I know that this is correct. I want to collect
in one module a number of dictionaries, every of which describes a
separate web service. And my function in another module will import
required dictionary, depending on what web service should be used.
Thanks again for the help.

With regards,
Max.
(sorry if my English isn't very proper)
 
D

Dave Angel

Lacrima said:
Hi, Jean-Michel!

Thanks for your answer.
I am not going to have names like dicts.py and dict1,dict2. That was
just example. I wanted to know if it is correct to have dictionaries
on a module level. Now I know that this is correct. I want to collect
in one module a number of dictionaries, every of which describes a
separate web service. And my function in another module will import
required dictionary, depending on what web service should be used.
Thanks again for the help.

With regards,
Max.
(sorry if my English isn't very proper)
It makes a lot of sense to store several dictionaries in a module, if
they describe parallel things, or alternate ways of accessing something
(like web services).

However, you might want to consider how they'll be accessed. If another
module is going to "import required dictionary," then you have some
means of selection. Perhaps that selection should be in the same
module, or at least the data structure to select it should be in the
same module.

Let's say you have four choices right now. And you add a fifth later.
Do you want to have to edit both the "function in the other module" as
well as this module?

This is similar to a feature that some programs have, "plug-ins."
There a customer can add an additional runtime just by dropping a new
..py file in the appropriate directory, and the program will find it and
add it to its "list of features."

So I'm just suggesting you figure out how the program might evolve, and
set up the data so changes may be localized, if practical. This turns
out usually to be parallel to the notion of naming things and grouping
them by similarity of functionality.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top