Wednesday, June 29, 2011

Re: Global generic files? Best practice(s)?

Hi Andre!

Thanks so much for the reply, I really appreciate the help! :)

On Wed, Jun 29, 2011 at 9:51 AM, Andre Terra <andreterra@gmail.com> wrote:
> If you're using it for multiple apps, but just one project and the apps are
> project-specific, then this code could very well lie in your project's
> utils.py or any other module.

Doh, sorry, I should have been more clear...

This code would be for one project. We do have multiple projects
installed... I had not considered sharing between projects! :)

I like your suggestion of using a utils.py file! I assume this would
exist at the same level as the apps? For example:

/my_project:

__init__.py
app1
app2
app3
manage.py
settings.py
templates/
urls.py
utils.py

This would be one example of code functionality that I would like to
share between my various apps:

https://gist.github.com/1027898

...

Just thinking out loud here... Bear with me for a moment.

Is it best to just copy code (like above) and duplicate it for each
app, or is it better to share one piece of code between apps?

Pros/cons of sharing code (like above) between apps:

PRO:

1. Following DRY principle.
2. Faster development times (common code in one place)?
3. Other?

CON:

1. When connected to many apps, the code becomes more risky if/when
re-factoring is needed.
2. Not self-contained to application.
3. Things become more splintered/abstracted/other?
4. Other?

> If you're writing code that's meant to be reused by third-parties, then the
> picture is rather different. I recently had a similar problem and I thought
> about writing a try/except block that tries to imports your global module
> with the necessary utilities and if it fails, it defines the functions
> itself. This way you allow for an easy global override of the needed
> utilities in case your needs vary.

Ooh, I like that idea! I think I have seen similar things in other
programming languages myself.

That's a great suggestion, thanks! :)

> I'm not sure this is the standard/best practice, but I believe I've seen
> similar bits of code in django core. A more experienced developer's point of
> view is most welcome here!

That's actually a great tip... I should probably browse through the
Django source code to find working examples. :)

Thanks so much for the informative and helpful reply Andre, I greatly
appreciate the help.

Have a great day!

Cheers,
Micky

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment