Sharing code between different projects?

Rob Day robert.day at merton.oxon.org
Mon Aug 13 18:02:59 EDT 2012


I'd just create a module - called shared_utils.py or similar - and import
that in both projects. It might be a bit messy if there's no 'unifying
theme' to the module - but surely it'd be a lot less messy than your
TempDirectory class, and anyone else who knows Python will understand
'import shared_utils' much more easily.

I realise you might not want to say, but if you could give some idea what
sort of projects these are, and what sorts of code you're trying to share,
it might make things a bit clearer.

I'm not really sure what your concerns about 'versioning and how to link
different pieces together' are - what d you think could go wrong here?

On 13 August 2012 17:53, andrea crotti <andrea.crotti.0 at gmail.com> wrote:

> I am in the situation where I am working on different projects that
> might potentially share a lot of code.
>
> I started to work on project A, then switched completely to project B
> and in the transiction I copied over a lot of code with the
> corresponding tests, and I started to modify it.
>
> Now it's time to work again on project A, but I don't want to copy
> things over again.
>
> I would like to design a simple and nice way to share between projects,
> where the things I want to share are simple but useful things as for
> example:
>
> class TempDirectory:
>     """Create a temporary directory and cd to it on enter, cd back to
>     the original position and remove it on exit
>     """
>     def __init__(self):
>         self.oldcwd = getcwd()
>         self.temp_dir = mkdtemp()
>
>     def __enter__(self):
>         logger.debug("create and move to temp directory %s" %
> self.temp_dir)
>         return self.temp_dir
>
>     def __exit__(self, type, value, traceback):
>         # I first have to move out
>         chdir(self.oldcwd)
>         logger.debug("removing the temporary directory and go back to
> the original position %s" % self.temp_dir)
>         rmtree(self.temp_dir)
>
>
> The problem is that there are functions/classes from many domains, so it
> would not make much sense to create a real project, and the only name I
> could give might be "utils or utilities"..
>
> In plus the moment the code is shared I must take care of versioning and
> how to link different pieces together (we use perforce by the way).
>
> If then someone else except me will want to use these functions then of
> course I'll have to be extra careful, designing really good API's and so
> on, so I'm wondering where I should set the trade-off between ability to
> share and burden to maintain..
>
> Anyone has suggestions/real world experiences about this?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Robert K. Day
robert.day at merton.oxon.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120813/3eb31797/attachment.html>


More information about the Python-list mailing list