Skip to content

Instantly share code, notes, and snippets.

@tjdevries
Created August 16, 2016 22:34
Show Gist options
  • Save tjdevries/5b9a0e72775be721b575d5ba7f203def to your computer and use it in GitHub Desktop.
Save tjdevries/5b9a0e72775be721b575d5ba7f203def to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"import shlex\n",
"import sys"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is what it looks like when you just dump json.\n"
]
},
{
"data": {
"text/plain": [
"'{\"a \\\\\"quote\\\\\" in string\": \"hello\", \"wow\": 1}'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_test = {'wow': 1, 'a \"quote\" in string': \"hello\"}\n",
"print('This is what it looks like when you just dump json.')\n",
"json.dumps(my_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is what it looks like when you escape the json correctly for the command line. Note that there are more escapes.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'\\'{\"a \\\\\"quote\\\\\" in string\": \"hello\", \"wow\": 1}\\''"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shlex.quote(json.dumps(my_test))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'{\"a \\\"quote\\\" in string\": \"hello\", \"wow\": 1}'\n"
]
}
],
"source": [
"print(shlex.quote(json.dumps(my_test)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is how you would probably end up parsing this\n",
"\n",
"You would invoke the command something like:\n",
"\n",
"```python3.4 -u test_file.py '{\"a \\\\\"quote\\\\\" in string\": \"hello\", \"wow\": 1}'```"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a \"quote\" in string': 'hello', 'wow': 1}\n"
]
}
],
"source": [
"# Pretend the sys_argv = sys.argv\n",
"# Since I can't change the arguments here\n",
"json_string_from_command_line = shlex.quote(json.dumps(my_test))\n",
"sys_argv = ['test_file.py', shlex.split(json_string_from_command_line)[0]]\n",
"\n",
"\n",
"loaded = json.loads(sys_argv[1])\n",
"print(json.loads(sys_argv[1]))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Look! You can now interact with a dictionary."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(loaded['wow'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.1"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment