setup.py: data files

Date: January 22nd 2016
Last updated: January 25th 2016

setup.py provides metadata for your package. It can include information such as project data, text files, and extensions. Note there are only three required fields in setup.py, including name, version, and packages. Here I look at adding data files to a package for distribution.

1) The first option is to use a MANIFEST.in located inline with setup.py. The sdist command looks for a MANIFEST.in to include in the final package.

2) The second option is to specify the files in setup.py that are required in the final package. In this example, I have added vowel_dataset.txt to the vowels package

3) The third option is to add text files or config files using the data_files command in setup.py.

Example:

└── Project
        |
        ├── LICENSE.txt
        ├── MANIFEST.in     # (1) inline with setup.py
        ├── README.txt
        ├── setup.py
        |
        ├── static
        |    └── data.txt   # (1) found by MANIFEST.in
        ├── rt
        |    ├── S1.txt     # (3) files included using data_files command
        |    └── S2.txt 
        └── vowels
             ├── __init__.py
             ├──data
             |    └── vowel_dataset.txt   # (2) package data
             ├── vowels.log
             └── vowels.py

MANIFEST.in

include Project/static/*
# Note that the following line is required in setup.py
# include_package_data = True

Setup.py:

from setuptools import setup

setup(
    # REQUIRED FIELDS
    name = "vowels",
    version = "0.0.3",
    packages = ['vowels'],

    # use README.txt for the long description
    long_description=open('README.txt').read(),
    install_requires = ['logging'],


    #-------------------------------------------------------------
    # Include static files:
    # 1) Include MANIFEST.in during packaging
    include_package_data = True,

    # 2) Tell setuptools to package specific files
    package_data = {
        # If vowels package contains *.txt files, include them:
        'vowels': ['data/*.txt']},

    # 3) add addtional files 
    # ******(these need to be under version control)
    # installation directory = "random_text"
    # file names to include are in subdirectory "rt"
    # include two files
    data_files = [('random_text', ['rt/S1.txt', 'rt/S2.txt'])],
    #--------------------------------------------------------------


    # Metadata for registering ith Pypi        
    author = "Ray",
    author_email = "[email protected]",
    description = "Tools for manipulating vowels in a sentence",
    keywords = ["english","vowels"],
    url = "",   # project home page        
)

README.txt

A set of tools for manipulating vowels in a sentence written \n
in english. 

Two methods are included; 
1) remove vowels in a sentence 
2) count the number of vowels in a sentence

The code: vowels.py

"""Tools for manipulating vowels in a string."""
import logging
import doctest

# setup logging 
logging.basicConfig(filename='example.log', \
        format='%(asctime)s %(message)s', \
        datefmt='%m/%d/%Y %I:%M:%S %p', \
        level=logging.DEBUG)
logger = logging.getLogger(__name__)


def count_vowels(sentence):
    """
    Returns the number of vowels in a sentence.
    >>> count_vowels('testing this module with your text')
    10
    """
    count = 0 # set counter
    vowels = list('aeiou') # returns ['a','e','i','o','u']
    for letter in sentence: # loop over letters in the string
        if letter in vowels: # match letter to vowels
            count += 1 
        # add to a log file if logging enabled
        logging.debug('captured vowel: %s' %letter)
    # return the number of vowels
    return count


def remove_vowels(sentence):
    """
    Removes all vowels in a sentence.
    >>> remove_vowels('testing out the removal of vowels')
    'tstng t th rmvl f vwls'
    """
    removed_vowels = "".join([x for x in sentence if x not in list('aeiou')])
    return removed_vowels


# executed if run in the terminal: python vowels.py
if __name__ == "__main__":
    import doctest, sys
    #doctest.testmod()    
    doctest.testmod(sys.modules[__name__])
    users_text = raw_input('enter text: ')
    print count_vowels(users_text)
    print remove_vowels(users_text)

results matching ""

    No results matching ""