From 13cc47d36b6a47549f42139051bb4eccf98e1890 Mon Sep 17 00:00:00 2001 From: sanurielf Date: Fri, 17 May 2013 22:07:01 -0500 Subject: [PATCH 1/2] algunas modificaciones para aceptar diccionarios. Falta optimizar --- ipy_table.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/ipy_table.py b/ipy_table.py index 259e6bb..a477c48 100755 --- a/ipy_table.py +++ b/ipy_table.py @@ -57,6 +57,14 @@ import copy +# Try to import numpy library +try: + import numpy +except ImportError: + pass + + + __version__ = 1.12 @@ -77,12 +85,31 @@ class IpyTable(object): # External methods #--------------------------------- - def __init__(self, array): + def __init__(self, input_data, order): + """Receive python dictionary as input. Sort the dictionary + as the 'order' list + The dictionary can have numpy arrays + """ + + if type(input_data) is dict and order: + header = order + values = [] + for key in order: + values.append(_convert_to_list(input_data[key])) + values = map(list, zip(*values)) + array = [header] + values + elif type(input_data) is list: + array = input_data + + + self.array = array self._num_rows = len(array) self._num_columns = len(array[0]) - + + + # Check that array is well formed for row in array: if len(row) != self._num_columns: @@ -402,11 +429,11 @@ def tabulate(data_list, columns, interactive=True): return get_interactive_return_value() -def make_table(array, interactive=True): +def make_table(array,order= None , interactive=True): """Create a table in interactive mode.""" global _TABLE global _INTERACTIVE - _TABLE = IpyTable(array) + _TABLE = IpyTable(array, order) _INTERACTIVE = interactive return get_interactive_return_value() From fc75e165a696ac013ac250a0bc9c34de0127cb18 Mon Sep 17 00:00:00 2001 From: sanurielf Date: Fri, 17 May 2013 22:23:31 -0500 Subject: [PATCH 2/2] =?UTF-8?q?ultimos=20cambios=20y=20modificado=20el=20n?= =?UTF-8?q?otebook=20de=20introducci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipy_table-Introduction.ipynb | 107 +++++++++++++++++++++++++++++++---- ipy_table.py | 22 ++++--- 2 files changed, 107 insertions(+), 22 deletions(-) diff --git a/ipy_table-Introduction.ipynb b/ipy_table-Introduction.ipynb index 880824e..d66bdc2 100644 --- a/ipy_table-Introduction.ipynb +++ b/ipy_table-Introduction.ipynb @@ -8,11 +8,10 @@ { "cells": [ { - "cell_type": "heading", - "level": 1, + "cell_type": "markdown", "metadata": {}, "source": [ - "Introduction" + "# Introduction" ] }, { @@ -27,11 +26,17 @@ ] }, { - "cell_type": "heading", - "level": 1, + "cell_type": "markdown", "metadata": {}, "source": [ - "Example" + "# Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Array as input" ] }, { @@ -69,7 +74,7 @@ "output_type": "pyout", "prompt_number": 1, "text": [ - "" + "" ] } ], @@ -100,7 +105,7 @@ "output_type": "pyout", "prompt_number": 2, "text": [ - "" + "" ] } ], @@ -129,7 +134,7 @@ "output_type": "pyout", "prompt_number": 3, "text": [ - "" + "" ] } ], @@ -158,12 +163,94 @@ "output_type": "pyout", "prompt_number": 4, "text": [ - "" + "" ] } ], "prompt_number": 4 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dict as input" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Also we can use a dictrionary to create a teble. We need a list to know the order to print the columns." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "\n", + "data = {\n", + " 'Planet': ['Mercury', 'Venus', 'Earth', 'Mars'],\n", + " 'Mass (kg)': np.array([3.3022E23, 4.896E24, 5.972E24, 6.4191E23]),\n", + " 'Diameter (km)': np.array([4879, 12104, 12735, 6772])\n", + " }\n", + "headers = ['Planet', 'Mass (kg)', 'Diameter (km)']\n", + "\n", + "make_table(data, headers)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "
PlanetMass (kg)Diameter (km)
Mercury330219999999999996854272.00004879
Venus4896000000000000201326592.000012104
Earth5972000000000000327155712.000012735
Mars641910000000000065536000.00006772
" + ], + "output_type": "pyout", + "prompt_number": 5, + "text": [ + "" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Also we can change the order of the columns:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "headers2 = headers[::-1]\n", + "make_table(data, headers2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "
Diameter (km)Mass (kg)Planet
4879330219999999999996854272.0000Mercury
121044896000000000000201326592.0000Venus
127355972000000000000327155712.0000Earth
6772641910000000000065536000.0000Mars
" + ], + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This can be useful when we have data stored in a dict." + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/ipy_table.py b/ipy_table.py index a477c48..8b40c27 100755 --- a/ipy_table.py +++ b/ipy_table.py @@ -56,12 +56,7 @@ """ import copy - -# Try to import numpy library -try: - import numpy -except ImportError: - pass +import sys @@ -86,25 +81,28 @@ class IpyTable(object): #--------------------------------- def __init__(self, input_data, order): - """Receive python dictionary as input. Sort the dictionary - as the 'order' list - The dictionary can have numpy arrays + """Receive Python dictionary as input. Sort the dictionary + as the 'order' list. The dictionary can have numpy arrays + Also can receive a list o lists """ + if type(input_data) is dict and order: - header = order + # Convert dict to a list o lists values = [] for key in order: values.append(_convert_to_list(input_data[key])) values = map(list, zip(*values)) - array = [header] + values + array = [order] + values elif type(input_data) is list: array = input_data + else: + sys.exit('Input data must be: \n -List of lists \n -Dictionary with a list of the order of keys to use.') + self.array = array - self._num_rows = len(array) self._num_columns = len(array[0])