Skip to content

Commit

Permalink
tools: Update scripts for generating code
Browse files Browse the repository at this point in the history
  • Loading branch information
mckelvin committed Feb 5, 2017
1 parent 4bab2a0 commit 23b3dfc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -2,8 +2,10 @@ docs/html
docs/.tadoc.org.html

*.pyc
build
build/
dist/
*.so
.*
*~
*.egg-info/

24 changes: 12 additions & 12 deletions DEVELOPMENT
Expand Up @@ -12,34 +12,34 @@ Here's the full list of make commands (see the Makefile file):
make build # builds and places libs in the project directory; required for testing
make clean # cleans the local build files
make install # installs talib system-wide
make generate: # generates a fresh func.pyx file. Requires talib and TA-Lib to both be installed
make generate: # generates a fresh _func.pxi, _stream.pxi file. Requires talib and TA-Lib to both be installed
make perf # run performance profiling
make test # run tests

The source code is comprised of one python package, located in the talib
directory, which itself has three Cython modules: func, abstract, and
common.
directory, which itself has one Cython module (_ta_lib) which consists of
four parts: _common, _func, _abstract and _stream.

talib/common.pyx
An internal-use module for functionality shared between func and abstract.
talib/_common.pxi
An internal-use file for functionality shared between func and abstract.

talib/func.pyx
This file is generated automatically by tools/generate.py and any changes made
talib/_func.pxi
This file is generated automatically by tools/generate_func.py and any changes made
to it directly will get overwritten!

talib/abstract.pyx
talib/_abstract.pxi
This file contains the code for interfacing with the TA-Lib abstract interface
and wrapping it into a pythonic Function class.

talib/libta_lib.pxd
talib/_ta_lib.pyx
This "Cython header file" defines the C-level functions, variables and types we
need to use in the above pyx files.

talib/stream.pyx
talib/_stream.pxi
This file contains code for interfacing a "streaming" interface to TA-Lib.

tools/generate.py
A script that generates and prints func.pyx to stdout. Gets information
tools/generate_func.py,generate_stream.py
Scripts that generate and print _func.pxi or _stream.pxi to stdout. Gets information
about all functions from the C headers of the installed TA-Lib.

If you are interested in developing new indicator functions or whatnot on
Expand Down
11 changes: 9 additions & 2 deletions Makefile
@@ -1,11 +1,18 @@
.PHONY: build

build:
python setup.py build_ext --inplace

install:
python setup.py install

generate:
python tools/generate_func.py > talib/func.pyx
talib/_func.pxi: tools/generate_func.py
python tools/generate_func.py > talib/_func.pxi

talib/_stream.pxi: tools/generate_stream.py
python tools/generate_stream.py > talib/_stream.pxi

generate: talib/_func.pxi talib/_stream.pxi

clean:
rm -rf build talib/func*.so talib/abstract*.so talib/common*.so talib/stream*.so talib/*.pyc
Expand Down
2 changes: 0 additions & 2 deletions talib/_abstract.pxi
Expand Up @@ -14,8 +14,6 @@ cimport numpy as np
cimport _ta_lib as lib
# NOTE: _ta_check_success, MA_Type is defined in _common.pxi

lib.TA_Initialize()


__INPUT_ARRAYS_DEFAULTS = {'open': None,
'high': None,
Expand Down
2 changes: 1 addition & 1 deletion talib/abstract.py
Expand Up @@ -22,4 +22,4 @@ def Function(function_name, *args, **kwargs):
globals()[func_name] = Function(func_name)


__all__ = ["Function"] + __TA_FUNCTION_NAMES__
__all__ = ["Function", "_get_defaults_and_docs"] + __TA_FUNCTION_NAMES__
9 changes: 4 additions & 5 deletions tools/generate_func.py
Expand Up @@ -51,7 +51,7 @@
from numpy import nan
from cython import boundscheck, wraparound
from .common cimport _ta_check_success
# _ta_check_success: defined in _common.pxi
cdef double NaN = nan
Expand All @@ -63,10 +63,9 @@
np.import_array() # Initialize the NumPy C API
cimport libta_lib as lib
from libta_lib cimport TA_RetCode
cimport _ta_lib as lib
from _ta_lib cimport TA_RetCode
lib.TA_Initialize()
""")

# cleanup variable names to make them more pythonic
Expand Down Expand Up @@ -338,4 +337,4 @@ def cleanup(name):
print('')
print('')

print('__all__ = [%s]' % ','.join(['\"%s\"' % name for name in names]))
print('__TA_FUNCTION_NAMES__ = [%s]' % ','.join(['\"%s\"' % name for name in names]))
24 changes: 5 additions & 19 deletions tools/generate_stream.py
Expand Up @@ -48,25 +48,12 @@
# print headers
print("""\
cimport numpy as np
from numpy import nan
from cython import boundscheck, wraparound
cimport _ta_lib as lib
from _ta_lib cimport TA_RetCode
# NOTE: _ta_check_success, NaN are defined in common.pxi
# NumPy C API is initialize in _func.pxi
from .common cimport _ta_check_success
cdef double NaN = nan
cdef extern from "numpy/arrayobject.h":
int PyArray_TYPE(np.ndarray)
object PyArray_EMPTY(int, np.npy_intp*, int, int)
int PyArray_FLAGS(np.ndarray)
object PyArray_GETCONTIGUOUS(np.ndarray)
np.import_array() # Initialize the NumPy C API
cimport libta_lib as lib
from libta_lib cimport TA_RetCode
lib.TA_Initialize()
""")

# cleanup variable names to make them more pythonic
Expand Down Expand Up @@ -96,7 +83,7 @@ def cleanup(name):

print('@wraparound(False) # turn off relative indexing from end of lists')
print('@boundscheck(False) # turn off bounds-checking for entire function')
print('def %s(' % shortname, end=' ')
print('def stream_%s(' % shortname, end=' ')
docs = [' %s(' % shortname]
i = 0
for arg in args:
Expand Down Expand Up @@ -302,4 +289,3 @@ def cleanup(name):
print('')
print('')

print('__all__ = [%s]' % ','.join(['\"%s\"' % name for name in names]))

0 comments on commit 23b3dfc

Please sign in to comment.