Python Package: Difference between revisions
From charlesreid1
| (2 intermediate revisions by the same user not shown) | |||
| Line 72: | Line 72: | ||
You can create objects in <code>setup.py</code> to assist with things like testing and cleanup. Do this by importing Command objects from the <code>distutils</code> package, then extending them: | You can create objects in <code>setup.py</code> to assist with things like testing and cleanup. Do this by importing Command objects from the <code>distutils</code> package, then extending them: | ||
< | <source lang="python"> | ||
from distutils.core import Command, setup | from distutils.core import Command, setup | ||
| Line 106: | Line 106: | ||
except: | except: | ||
pass | pass | ||
</ | </source> | ||
Then in your call to setup, you can add this option: | Then in your call to setup, you can add this option: | ||
< | <source lang="python"> | ||
setup( name = ..., | setup( name = ..., | ||
version = ..., | |||
..., | |||
cmdclass = { 'clean': CleanCommand }) | |||
</ | </source> | ||
=References= | =References= | ||
[[Category:Python]] | [[Category:Python]] | ||
Latest revision as of 04:32, 19 March 2015
Basics
Basic Python Package Skeleton
A skeleton Python package takes the following structure:
myproject/
setup.py
myproject/
__init__.py
code.py
submodule/
somecode.py
morecode.py
tests/
test_code.py
test_somecode.py
test_morecode.py
Setup.py
Here's what the package setup file looks like:
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
config = {
'description': 'My Project',
'author': 'My Name',
'url': 'URL to get it at.',
'download_url': 'Where to download it.',
'author_email': 'My email.',
'version': '0.1',
'install_requires': ['nose'],
'packages': ['NAME'],
'scripts': [],
'name': 'projectname'
}
setup(**config)
Test_code.py
It is important to test your code. Here's a skeleton nose test suite:
from nose.tools import *
import NAME
def setup():
print "setup"
def teardown():
print "tear down"
def test_basic():
print "i run good"
Creating Objects in Setup
You can create objects in setup.py to assist with things like testing and cleanup. Do this by importing Command objects from the distutils package, then extending them:
from distutils.core import Command, setup
# The code for the Test and Clean commands came from
# http://da44en.wordpress.com/2002/11/22/using-distutils/
class TestCommand(Command):
user_options = [ ]
class CleanCommand(Command):
user_options = [ ]
def initialize_options(self):
self._clean_me = [ ]
for files in os.listdir('.'):
if files.endswith('.png') or \
files.endswith('.dat' ) or \
files.endswith('.h5' ) or \
files.endswith('.txt' ) or \
files.endswith('.dot' ) or \
files.endswith('.pyc' ) or \
files.endswith('log' ) :
print "Removing file " + str(files)
self._clean_me.append(files)
def finalize_options(self):
pass
def run(self):
for clean_me in self._clean_me:
try:
os.unlink(clean_me)
except:
pass
Then in your call to setup, you can add this option:
setup( name = ...,
version = ...,
...,
cmdclass = { 'clean': CleanCommand })