Skip to content

Instantly share code, notes, and snippets.

@Kelvinrr
Created July 23, 2017 22:02
Show Gist options
  • Select an option

  • Save Kelvinrr/3ac193b9f847237e2b23a69293aa340b to your computer and use it in GitHub Desktop.

Select an option

Save Kelvinrr/3ac193b9f847237e2b23a69293aa340b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"import os\n",
"import sys\n",
"sys.path.insert(0, os.path.abspath('/home/acpaquette/repos/autocnet'))\n",
"\n",
"from autocnet import cuda\n",
"from autocnet.examples import get_path\n",
"from autocnet.graph.network import CandidateGraph\n",
"from autocnet.graph.edge import Edge\n",
"from autocnet.vis.graph_view import *\n",
"# from autocnet.matcher.feature import FlannMatcher\n",
"\n",
"import itertools\n",
"import networkx as nx\n",
"import pandas as pd\n",
"from IPython.display import display\n",
"\n",
"import json\n",
"\n",
"from plio.io.io_autocnetgraph import load as load_graph\n",
"from plio.io.io_autocnetgraph import save as save_graph\n",
"\n",
"%pylab inline"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"from random import randint\n",
"\n",
"def decorated_class(cls, decorator, *args, **kwargs):\n",
" \"\"\"\n",
" \"\"\"\n",
" from copy import deepcopy\n",
" def decorate(cls):\n",
" for attr in cls.__dict__: # there's propably a better way to do this\n",
" if callable(getattr(cls, attr)) and getattr(cls, attr).__name__[0] != '_':\n",
" setattr(cls, attr, decorator(getattr(cls, attr)))\n",
" return cls\n",
" # return decorated copy (i.e. a subclass with decorations)\n",
" return decorate(type('cls_copy', cls.__bases__, dict(cls.__dict__))) "
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"def create_updater(sub):\n",
" \"\"\"\n",
" Create a decorator function using object\n",
" \"\"\"\n",
" def decorator(func):\n",
" def wrapper(*args, **kwargs):\n",
" sub.val = func(*args, **kwargs)\n",
" return sub.val\n",
" return wrapper\n",
" return decorator\n",
"\n",
"class SomeClass(object):\n",
" def __init__(self):\n",
" self.val=0\n",
" \n",
" def test(self):\n",
" self.val = randint(0,9999)\n",
" return self.val\n",
" \n",
"\n",
"class Sub(object):\n",
" def __init__(self):\n",
" PubClass = decorated_class(SomeClass, create_updater(self))\n",
" self.val = None\n",
" self.pub = PubClass()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6736\n",
"6736\n"
]
}
],
"source": [
"obj = Sub()\n",
"print(obj.pub.test())\n",
"print(obj.val)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"original\n",
"overwrite\n",
"original\n"
]
}
],
"source": [
"obj = TEST()\n",
"print(obj.test())\n",
"\n",
"CLASS = decorate_class(TEST, myDecorator)\n",
"obj = CLASS()\n",
"print(obj.test())\n",
"\n",
"obj = TEST()\n",
"print(obj.test())"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import copy\n",
"TEST is copy.copy(TEST)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"in call\n"
]
},
{
"data": {
"text/plain": [
"<__main__.MyKlass at 0x7efbdaedd978>"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MyKlass()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "AutoCNet",
"language": "python",
"name": "autocnet"
},
"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