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.<br>
<br>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.<br><br>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?<br><br><div class="gmail_quote">On 13 August 2012 17:53, andrea crotti <span dir="ltr"><<a href="mailto:andrea.crotti.0@gmail.com" target="_blank">andrea.crotti.0@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I am in the situation where I am working on different projects that<br>
might potentially share a lot of code.<br>
<br>
I started to work on project A, then switched completely to project B<br>
and in the transiction I copied over a lot of code with the<br>
corresponding tests, and I started to modify it.<br>
<br>
Now it's time to work again on project A, but I don't want to copy<br>
things over again.<br>
<br>
I would like to design a simple and nice way to share between projects,<br>
where the things I want to share are simple but useful things as for<br>
example:<br>
<br>
class TempDirectory:<br>
"""Create a temporary directory and cd to it on enter, cd back to<br>
the original position and remove it on exit<br>
"""<br>
def __init__(self):<br>
self.oldcwd = getcwd()<br>
self.temp_dir = mkdtemp()<br>
<br>
def __enter__(self):<br>
logger.debug("create and move to temp directory %s" % self.temp_dir)<br>
return self.temp_dir<br>
<br>
def __exit__(self, type, value, traceback):<br>
# I first have to move out<br>
chdir(self.oldcwd)<br>
logger.debug("removing the temporary directory and go back to<br>
the original position %s" % self.temp_dir)<br>
rmtree(self.temp_dir)<br>
<br>
<br>
The problem is that there are functions/classes from many domains, so it<br>
would not make much sense to create a real project, and the only name I<br>
could give might be "utils or utilities"..<br>
<br>
In plus the moment the code is shared I must take care of versioning and<br>
how to link different pieces together (we use perforce by the way).<br>
<br>
If then someone else except me will want to use these functions then of<br>
course I'll have to be extra careful, designing really good API's and so<br>
on, so I'm wondering where I should set the trade-off between ability to<br>
share and burden to maintain..<br>
<br>
Anyone has suggestions/real world experiences about this?<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Robert K. Day<div><a href="mailto:robert.day@merton.oxon.org" target="_blank">robert.day@merton.oxon.org</a></div><br>