Sharing common code between multiple scripts?

V

Victor Hooi

Hi,

NB - I'm the original poster here - https://groups.google.com/d/topic/comp.lang.python/WUuRLEXJP4E/discussion - however, that post seems to have diverted, and I suspect my original question was poorly worded.

I have several Python scripts that use similar functions.

Currently, these functions are duplicated in each script.

These functions wrap things like connecting to databases, reading in config files, writing to CSV etc.

I'd like to pull them out, and move them to a common module for all the scripts to import.

Originally, I thought I'd create a package, and have it all work:

my_package
__init__.py
common/
my_functions.py
script1/
__init__.py
config.yaml
script1.py
script2/
__init__.py
config.yaml
script2.py

However, there apparently isn't an easy way to have script1.py and script2.py import from common/my_functions.py.

So my new question is - what is the idiomatic way to structure this in Python, and easily share common functions between the scripts?

Ideally, I'd like to avoid having everything in a single directory - i.e. script1.py should be in it's own directory, as it has it's own config and other auxiliary files. However, if this is a bad idea, let me know.

Also, say I have a class in script1.py, and I want it pull in a common method as well. For example, I want multiples classes to have the following method:

def gzip_csv_file(self):
self.gzip_filename = '%s.gz' % self.output_csv
with open(self.output_csv, 'rb') as uncompressed:
with gzip.open(self.gzip_filename, 'wb') as compressed:
compressed.writelines(uncompressed)

self.logger.debug('Compressed to %s GZIP file.' % humansize(os.path.getsize(self.gzip_filename)))

How could I share this? Mixins? Or is there something better?

Cheers,
Victor
 

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
473,708
Messages
2,569,348
Members
44,663
Latest member
XiomaraBla

Latest Threads

Top