Skip to content

Instantly share code, notes, and snippets.

@nicktimko
Created November 7, 2017 22:18
Show Gist options
  • Save nicktimko/a74c04b4b2a040b7c657221a2f7ceaf5 to your computer and use it in GitHub Desktop.
Save nicktimko/a74c04b4b2a040b7c657221a2f7ceaf5 to your computer and use it in GitHub Desktop.
Python String Tutorial
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> A short esoteric string tutorial\n",
"\n",
"> ![](https://pbs.twimg.com/media/DNkOxYEWAAE8Zbx.jpg)\n",
"\n",
"> — https://twitter.com/dabeaz/status/925787482515533830\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"74\n",
"0B 00 00 00 00 00 00 00 F0 5B 43 0A 01 00 00 00 ........ð[C.....\n",
"01 00 00 00 00 00 00 00 F1 A4 50 36 2E 61 0B C4 ........ñ¤P6.a.Ä\n",
"A4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ¤...............\n",
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................\n",
"00 00 00 00 00 00 00 00 F1 00 ........ñ.\n",
"-----\n",
"77\n",
"0B 00 00 00 00 00 00 00 F0 5B 43 0A 01 00 00 00 ........ð[C.....\n",
"01 00 00 00 00 00 00 00 F1 A4 50 36 2E 61 0B C4 ........ñ¤P6.a.Ä\n",
"A4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ¤...............\n",
"02 00 00 00 00 00 00 00 90 8C 88 0B 01 00 00 00 ................\n",
"00 00 00 00 00 00 00 00 F1 00 00 00 00 ........ñ....\n"
]
}
],
"source": [
"import ctypes\n",
"import sys\n",
"\n",
"def memdumpobj(obj):\n",
" size = sys.getsizeof(obj)\n",
" ptr = ctypes.cast(id(a), ctypes.POINTER(ctypes.c_uint8))\n",
" return bytes([ptr[i] for i in range(size)])\n",
"\n",
"def chunks(l, n):\n",
" \"\"\"Yield successive n-sized chunks from l.\"\"\"\n",
" # https://stackoverflow.com/a/312464/194586\n",
" for i in range(0, len(l), n):\n",
" yield l[i:i + n]\n",
"\n",
"def nicedump(obj):\n",
" dump = memdumpobj(obj)\n",
" for line in chunks(dump, 16):\n",
" hex = ' '.join(format(c, '02X') for c in line)\n",
" raw = ''.join(chr(c) if c >= 32 and len(repr(chr(c))) == 3 else '.' for c in line)\n",
" print('{:<50} {}'.format(hex, raw))\n",
" \n",
"a = 'ñ'\n",
"print(sys.getsizeof(a))\n",
"nicedump(a)\n",
"\n",
"print('-----')\n",
"\n",
"try:\n",
" float(a)\n",
"except ValueError:\n",
" pass\n",
"print(sys.getsizeof(a))\n",
"nicedump(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Values change! I dunno...need to look at the actual definition of the string type to see what is what."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "OS Clients (3.6)",
"language": "python",
"name": "os36"
},
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment