{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# writing python 2 /3 compatible byte to string converters\n", "\n", "[python-future on strings and bytes](http://python-future.org/compatible_idioms.html#strings-and-bytes)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'ABCD�'\n" ] } ], "source": [ "from builtins import bytes, chr\n", "bytes_string = bytes(b'ABCD\\xf9')\n", "print(bytes_string)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from builtins import bytes, chr\n", "def convertBytesToString(bytes_string):\n", " \"\"\"\n", " Parameters\n", " ----------\n", " bytes_string: bytes\n", " \n", " Returns\n", " -------\n", " converted: str\n", " \n", " \"\"\"\n", " convert = \"\".join([chr(ints) for ints in bytes_string])\n", "\n", " return convert" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "is basestring: True\n", "ABCDù\n" ] } ], "source": [ "from past.builtins import basestring \n", "bytes_str = bytes(b'ABCD\\xf9')\n", "converted = convertBytesToString(bytes_str)\n", "print(type(converted))\n", "print \"is basestring:\", isinstance(converted, basestring)\n", "print(converted)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'ABCD\\xf9'\n" ] } ], "source": [ "from builtins import bytes, chr\n", "bytes_string = bytes(b'ABCD\\xf9')\n", "print(bytes_string)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from builtins import bytes, chr\n", "def convertBytesToString(bytes_string):\n", " \"\"\"\n", " Parameters\n", " ----------\n", " bytes_string: bytes\n", " \n", " Returns\n", " -------\n", " converted: str\n", " \n", " \"\"\"\n", " convert = \"\".join([chr(ints) for ints in bytes_string])\n", " return convert" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "is basestring: True\n", "ABCDù\n" ] } ], "source": [ "from past.builtins import basestring\n", "\n", "bytes_str = bytes(b'ABCD\\xf9')\n", "converted = convertBytesToString(bytes_str)\n", "print(type(converted))\n", "print(\"is basestring:\", isinstance(converted, basestring))\n", "print(converted)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }