Source code for ahkab.printing

# -*- coding: iso-8859-1 -*-
# printing.py
# Printing module
# Copyright 2006 Giuseppe Venturini

# This file is part of the ahkab simulator.
#
# Ahkab is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# Ahkab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License v2
# along with ahkab.  If not, see <http://www.gnu.org/licenses/>.

"""
This is the printing module of the simulator.
Using its functions, the output will be somewhat uniform.

The functions defined in this module can be divided in the following groups:

- :ref:`info-functions`: functions to print information, errors and warnings to
  the user during command-line execution.
- :ref:`netlist_syntax_printing`: functions to print conformingly to the netlist
  syntax, often to show information to the user for debugging purposes.
- :ref:`convenience_functions`: functions to abstract low level issues such as
  Unicode handling of text and number printing formats,
- :ref:`tabular_functions`: functions to format and display data into tables,
  which we provide straight from the ``tabulate`` module.
- :ref:`printing_analysis_results` in a consistent fashion.


.. _info-functions:

Informative functions
=====================

.. autosummary::
    print_general_error
    print_info_line
    print_parse_error
    print_warning

.. _netlist_syntax_printing:

Printing netlist lines
======================

.. autosummary::
    print_analysis

.. _printing_analysis_results:

Printing analysis results
=========================

.. autosummary::
    print_fourier
    print_spicefft
    print_symbolic_equations
    print_symbolic_results
    print_symbolic_transfer_functions

.. _convenience_functions:

Convenience functions
---------------------

.. autosummary::
    open_utf8
    printoptions

.. _tabular_functions:

Tabular formatting of data
--------------------------

.. autosummary::
    table

All functions in alphabetical order
===================================

"""

from __future__ import (unicode_literals, absolute_import,
                        division, print_function)

import contextlib
import sys
import os

import tabulate as _tabulate
import numpy as np

from . import options
from . import py3compat

if py3compat.PY2:
    import codecs
    if not py3compat.IPYTHON:
        UTF8Writer = codecs.getwriter('utf8')
        sys.stdout = UTF8Writer(sys.stdout)

[docs]def open_utf8(filename): """Get a file handle wrapped in a UTF-8 writer The file is opened in ``w`` mode. **Parameters:** filename : string The file name, just like you would pass to Python's built-in ``open()`` method. **Returns:** fp : codecs.UTF8Writer object The wrapped file pointer. """ if py3compat.PY2: fp = open(filename, 'w') UTF8Writer = codecs.getwriter('utf8') fp = UTF8Writer(fp) else: fp = open(filename, 'w', encoding='UTF-8') return fp
# else: suppressed.
[docs]def table(data, *args, **argsd): """Format a fixed width table for pretty printing No data processing is done here, instead we call `tabulate <https://pypi.python.org/pypi/tabulate/>`_'s ``tabulate.tabulate()``, passing all arguments unmodified. **Parameters:** data : list-of-lists or a dictionary of iterables or a 2D NumPy array (or more). The tabular data. The remaining arguments, not documented here, are: headers : sequence, optional An explicit list of column headers. tablefmt : str, optional Table formatting specification. floatfmt : str, optional Floats formatting specification. numalign : str, optional Alignment flag for numbers. stralign : str, optional Alignment specification for strings, eg. "right". missingval : str, optional Element for the missing values. """ return _tabulate.tabulate(data, *args, **argsd)
@contextlib.contextmanager
[docs]def printoptions(*args, **kwargs): """A context manager for ``numpy.set_printoptions``""" original = np.get_printoptions() np.set_printoptions(*args, **kwargs) yield np.set_printoptions(**original)
locale = os.getenv('LANG') if not locale: print_warning('Locale appears not set! please export LANG="en_US.UTF-8" or' ' equivalent, ') print_warning('or ahkab\'s unicode support is broken.')