Skip to content

Instantly share code, notes, and snippets.

@simecek
Created November 4, 2018 22:13
Show Gist options
  • Save simecek/7b49fcddacf89a20f20dc9a537d5b74a to your computer and use it in GitHub Desktop.
Save simecek/7b49fcddacf89a20f20dc9a537d5b74a to your computer and use it in GitHub Desktop.
ReprlibDemo.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ReprlibDemo.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/simecek/7b49fcddacf89a20f20dc9a537d5b74a/reprlibdemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "LeInYWshyAoI",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Too long output from __repr__:"
]
},
{
"metadata": {
"id": "H9e_MH6ot-42",
"colab_type": "code",
"outputId": "1e34a1e4-7250-4e88-ee3a-fb004e2e2f83",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"cell_type": "code",
"source": [
"class TheProblem:\n",
"\n",
" def __init__(self, items):\n",
" self._items = list(items)\n",
" \n",
" def __repr__(self):\n",
" items = self._items\n",
" return \"{}\".format(items) \n",
"\n",
"print(TheProblem(range(100)))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "cpdynroKyGdg",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Solution with reprlib:"
]
},
{
"metadata": {
"id": "92LmMYzrutWN",
"colab_type": "code",
"outputId": "1803d090-d91d-4156-f5c4-2197409da4a3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"import reprlib\n",
"\n",
"class TheSolution:\n",
"\n",
" def __init__(self, items):\n",
" self._items = list(items)\n",
" \n",
" def __repr__(self):\n",
" items = reprlib.repr(self._items)\n",
" return \"{}\".format(items)\n",
"\n",
"print(TheSolution(range(100)))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, ...]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "l4XbFWShyPDR",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment