Skip to content

Commit

Permalink
the dictionary (or Mapping) used by getrwatch/setrwatch is used direc…
Browse files Browse the repository at this point in the history
…tly without copying & can be mutated directly
  • Loading branch information
dutc committed May 13, 2017
1 parent 9095894 commit e3ac65c
Showing 1 changed file with 68 additions and 40 deletions.
108 changes: 68 additions & 40 deletions Living_in_a_noisy_world_with_James_Powell_rwatch_module.ipynb
Expand Up @@ -67,7 +67,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -92,15 +94,17 @@
"Then, the core part will be to install and import [James Powell (`dutc`)](https://github.com/dutc/) [`rwatch`](https://github.com/dutc/rwatch) module.\n",
"If you don't have it installed :\n",
"\n",
"1. Be sure to have at least Python 3.5,\n",
"1. Be sure to have CPython 3.5. `rwatch` patches the CPython eval loop, so it's fixed to specific versions of Python & the author lazy about keeping it updated.\n",
"2. Then `pip install dutc-rwatch`. It should work, but it fails for me.\n",
"3. (alternative) You can just `cd /tmp/ && git clone https://github.com/dutc/rwatch && cd rwatch/src/ && make` and copy the `rwatch.so` dynamic library wherever you need..."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -159,7 +163,9 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -262,6 +268,7 @@
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [
Expand Down Expand Up @@ -303,16 +310,15 @@
"outputs": [],
"source": [
"def delrwatch(idobj):\n",
" all_rwatches = getrwatch()\n",
" if idobj in all_rwatches:\n",
" del all_rwatches[idobj]\n",
" setrwatch(all_rwatches)"
" getrwatch().pop(idobj, None)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -344,7 +350,9 @@
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -393,7 +401,9 @@
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
Expand All @@ -417,7 +427,9 @@
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -497,7 +509,9 @@
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -563,7 +577,9 @@
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -593,7 +609,9 @@
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -679,11 +697,7 @@
" self.idobj = id(obj)\n",
" \n",
" def __enter__(self):\n",
" rws = getrwatch()\n",
" rws.update({\n",
" self.idobj: debug_view\n",
" })\n",
" setrwatch(rws)\n",
" getrwatch()[self.idobj] = debug_view\n",
"\n",
" def __exit__(self, exc_type, exc_val, exc_tb):\n",
" delrwatch(self.idobj)"
Expand All @@ -699,7 +713,9 @@
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -767,7 +783,9 @@
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -801,7 +819,9 @@
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -890,8 +910,7 @@
" info = getframeinfo(frame)\n",
" if '<stdin>' in info.filename or '<ipython-' in info.filename:\n",
" return obj + np.random.normal()\n",
" else:\n",
" return obj"
" return obj"
]
},
{
Expand All @@ -904,7 +923,9 @@
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand All @@ -923,9 +944,7 @@
"setrwatch({})\n",
"x = 1234\n",
"print(x)\n",
"setrwatch({\n",
" id(x): add_white_noise_to_numbers\n",
"})\n",
"getrwatch()[id(x)] = add_white_noise_to_numbers\n",
"print(x) # huhoww, that's noisy!\n",
"print(10 * x + x + x**2) # and noise propagate!\n",
"setrwatch({})\n",
Expand Down Expand Up @@ -959,14 +978,15 @@
" info = getframeinfo(frame)\n",
" if '<stdin>' in info.filename or '<ipython-' in info.filename:\n",
" return obj + np.random.normal() + np.random.normal() * 1j\n",
" else:\n",
" return obj"
" return obj"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -1038,7 +1058,9 @@
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -1091,8 +1113,7 @@
" info = getframeinfo(frame)\n",
" if '<stdin>' in info.filename or '<ipython-' in info.filename:\n",
" return noise(obj)\n",
" else:\n",
" return obj\n",
" return obj\n",
"\n",
" self.rwatch = add_white_noise_to_complex\n",
"\n",
Expand All @@ -1106,7 +1127,9 @@
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand All @@ -1128,7 +1151,9 @@
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand All @@ -1150,7 +1175,9 @@
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -1184,8 +1211,9 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
Expand All @@ -1199,7 +1227,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.5.2"
},
"notify_time": "5",
"toc": {
Expand Down

0 comments on commit e3ac65c

Please sign in to comment.