Changeset f619de7 in sasmodels for sasmodels/generate.py


Ignore:
Timestamp:
Apr 11, 2016 11:14:50 AM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
7ae2b7f
Parents:
9a943d0
Message:

more type hinting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    r6d6508e rf619de7  
    164164from .modelinfo import Parameter 
    165165from .custom import load_custom_kernel_module 
     166 
     167try: 
     168    from typing import Tuple, Sequence, Iterator 
     169    from .modelinfo import ModelInfo 
     170except ImportError: 
     171    pass 
    166172 
    167173TEMPLATE_ROOT = dirname(__file__) 
     
    220226 
    221227def format_units(units): 
     228    # type: (str) -> str 
    222229    """ 
    223230    Convert units into ReStructured Text format. 
     
    226233 
    227234def make_partable(pars): 
     235    # type: (List[Parameter]) -> str 
    228236    """ 
    229237    Generate the parameter table to include in the sphinx documentation. 
     
    256264 
    257265def _search(search_path, filename): 
     266    # type: (List[str], str) -> str 
    258267    """ 
    259268    Find *filename* in *search_path*. 
     
    269278 
    270279def model_sources(model_info): 
     280    # type: (ModelInfo) -> List[str] 
    271281    """ 
    272282    Return a list of the sources file paths for the module. 
     
    277287 
    278288def timestamp(model_info): 
     289    # type: (ModelInfo) -> int 
    279290    """ 
    280291    Return a timestamp for the model corresponding to the most recently 
     
    288299 
    289300def convert_type(source, dtype): 
     301    # type: (str, np.dtype) -> str 
    290302    """ 
    291303    Convert code from double precision to the desired type. 
     
    312324 
    313325def _convert_type(source, type_name, constant_flag): 
     326    # type: (str, str, str) -> str 
    314327    """ 
    315328    Replace 'double' with *type_name* in *source*, tagging floating point 
     
    330343 
    331344def kernel_name(model_info, is_2d): 
     345    # type: (ModelInfo, bool) -> str 
    332346    """ 
    333347    Name of the exported kernel symbol. 
     
    337351 
    338352def indent(s, depth): 
     353    # type: (str, int) -> str 
    339354    """ 
    340355    Indent a string of text with *depth* additional spaces on each line. 
     
    345360 
    346361 
    347 _template_cache = {} 
     362_template_cache = {}  # type: Dict[str, Tuple[int, str, str]] 
    348363def load_template(filename): 
     364    # type: (str) -> str 
    349365    path = joinpath(TEMPLATE_ROOT, filename) 
    350366    mtime = getmtime(path) 
     
    355371 
    356372def model_templates(): 
     373    # type: () -> List[str] 
    357374    # TODO: fails DRY; templates are listed in two places. 
    358375    # should instead have model_info contain a list of paths 
     
    371388 
    372389def _gen_fn(name, pars, body): 
     390    # type: (str, List[Parameter], str) -> str 
    373391    """ 
    374392    Generate a function given pars and body. 
     
    385403 
    386404def _call_pars(prefix, pars): 
     405    # type: (str, List[Parameter]) -> List[str] 
    387406    """ 
    388407    Return a list of *prefix.parameter* from parameter items. 
     
    393412                           flags=re.MULTILINE) 
    394413def _have_Iqxy(sources): 
     414    # type: (List[str]) -> bool 
    395415    """ 
    396416    Return true if any file defines Iqxy. 
     
    414434 
    415435def make_source(model_info): 
     436    # type: (ModelInfo) -> str 
    416437    """ 
    417438    Generate the OpenCL/ctypes kernel from the module info. 
    418439 
    419     Uses source files found in the given search path. 
     440    Uses source files found in the given search path.  Returns None if this 
     441    is a pure python model, with no C source components. 
    420442    """ 
    421443    if callable(model_info.Iq): 
    422         return None 
     444        raise ValueError("can't compile python model") 
    423445 
    424446    # TODO: need something other than volume to indicate dispersion parameters 
     
    447469    q, qx, qy = [Parameter(name=v) for v in ('q', 'qx', 'qy')] 
    448470    # Generate form_volume function, etc. from body only 
    449     if model_info.form_volume is not None: 
     471    if isinstance(model_info.form_volume, str): 
    450472        pars = partable.form_volume_parameters 
    451473        source.append(_gen_fn('form_volume', pars, model_info.form_volume)) 
    452     if model_info.Iq is not None: 
     474    if isinstance(model_info.Iq, str): 
    453475        pars = [q] + partable.iq_parameters 
    454476        source.append(_gen_fn('Iq', pars, model_info.Iq)) 
    455     if model_info.Iqxy is not None: 
     477    if isinstance(model_info.Iqxy, str): 
    456478        pars = [qx, qy] + partable.iqxy_parameters 
    457479        source.append(_gen_fn('Iqxy', pars, model_info.Iqxy)) 
     
    509531 
    510532def load_kernel_module(model_name): 
     533    # type: (str) -> module 
    511534    if model_name.endswith('.py'): 
    512535        kernel_module = load_custom_kernel_module(model_name) 
     
    522545                            %re.escape(string.punctuation)) 
    523546def _convert_section_titles_to_boldface(lines): 
     547    # type: (Sequence[str]) -> Iterator[str] 
    524548    """ 
    525549    Do the actual work of identifying and converting section headings. 
     
    543567 
    544568def convert_section_titles_to_boldface(s): 
     569    # type: (str) -> str 
    545570    """ 
    546571    Use explicit bold-face rather than section headings so that the table of 
     
    553578 
    554579def make_doc(model_info): 
     580    # type: (ModelInfo) -> str 
    555581    """ 
    556582    Return the documentation for the model. 
     
    562588                 name=model_info.name, 
    563589                 title=model_info.title, 
    564                  parameters=make_partable(model_info.parameters), 
     590                 parameters=make_partable(model_info.parameters.kernel_parameters), 
    565591                 returns=Sq_units if model_info.structure_factor else Iq_units, 
    566592                 docs=docs) 
     
    569595 
    570596def demo_time(): 
     597    # type: () -> None 
    571598    """ 
    572599    Show how long it takes to process a model. 
     
    582609 
    583610def main(): 
     611    # type: () -> None 
    584612    """ 
    585613    Program which prints the source produced by the model. 
Note: See TracChangeset for help on using the changeset viewer.