From charlesreid1

(Created page with "First step is to create the top-level directory that all the code related to this thing will live. I'll call it <code>myproject</code> Now in myproject we create <code>setup.py...")
 
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
First step is to create the top-level directory that all the code related to this thing will live.
=Basics=


I'll call it <code>myproject</code>
==Basic Python Package Skeleton==


Now in myproject we create <code>setup.py</code>:
A skeleton Python package takes the following structure:
 
<pre>
myproject/
 
    setup.py
 
    myproject/
        __init__.py
        code.py
        submodule/
            somecode.py
            morecode.py
 
    tests/
        test_code.py
        test_somecode.py
        test_morecode.py
</pre>
 
==Setup.py==
 
Here's what the package setup file looks like:


<pre>
<pre>
Line 26: Line 49:
setup(**config)
setup(**config)
</pre>
</pre>
==Test_code.py==
It is important to test your code. Here's a skeleton nose test suite:
<pre>
from nose.tools import *
import NAME
def setup():
    print "setup"
def teardown():
    print "tear down"
def test_basic():
    print "i run good"
</pre>
=Creating Objects in Setup=
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
# 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
</source>
Then in your call to setup, you can add this option:
<source lang="python">
setup( name = ...,
version = ...,
...,
cmdclass = { 'clean': CleanCommand })
</source>
=References=
[[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 })

References