Skip to content

Instantly share code, notes, and snippets.

@holdenweb
Created September 29, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holdenweb/33ad89b01e1f897d8777e22d6eb4c969 to your computer and use it in GitHub Desktop.
Save holdenweb/33ad89b01e1f897d8777e22d6eb4c969 to your computer and use it in GitHub Desktop.
Traitlets early experiments
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import sys; print(sys.version)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import traitlets\n",
"print(\"traitlets\", traitlets.__version__, traitlets.__file__)\n",
"from traitlets import HasTraits, Int, Float, Unicode, observe, Any, All\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class MyClass(HasTraits):\n",
" i = Int()\n",
" f = Float()\n",
" u = Unicode()\n",
"\n",
"mc1 = MyClass()\n",
"mc1.i = 42"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"try:\n",
" mc1.i = \"33\"\n",
"except Exception as e:\n",
" print(type(e))\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class MyClass(HasTraits):\n",
" i = Int()\n",
" f = Float()\n",
" u = Unicode()\n",
" def __init__(self, name):\n",
" self.name = name\n",
" @observe(All)\n",
" def _class_observed_changed(self, c):\n",
" print(\"class observed obj {}.{} was {!r} is now {!r}\".format(self.name, c.name, c.old, c.new), sep='\\n')\n",
" c[\"timestamp\"] = time.asctime(time.localtime())\n",
"\n",
"mc1 = MyClass('mc1')\n",
"mc1.i = 42\n",
"mc1.f = 3.14159\n",
"mc1.u = \"Python Programming\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def report_change(c):\n",
" print(\"R_C: {}.{} was {!r} is now {!r}\".format(c.owner.name, c.name, c.old, c.new))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc1.observe(report_change) # reports all trait changes on instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc1.i = 102 # Now reported by both class and instance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc1.f = 3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc1.u = \"the quick brown fox\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc2 = MyClass('mc2')\n",
"def _mc_changed(c):\n",
" print(\"{}.{} changed\".format(c.owner.name, c.name))\n",
"#mc2.observe(report_change)\n",
"mc2.observe(_mc_changed, [\"i\", \"f\"]) # # reports selected trait changes on instance\n",
"\n",
"print(\"-- i changes --\")\n",
"mc2.i = 101\n",
"print(\"-- f changes --\")\n",
"mc2.f = 1.234\n",
"print(\"-- u changes --\")\n",
"mc2.u = \"Steve Holden\" # still gets class notification and report change"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def _mc_changed(c):\n",
" print(\"instance changed\", c.name, \"to\", c.new)\n",
"mc1.observe(_mc_changed, [\"i\", \"f\"]) # reports changes only to mc2.i and mc2.f\n",
"\n",
"print(\"-- mc1.i changes --\")\n",
"mc1.i = 1001\n",
"print(\"-- mc1.f changes --\")\n",
"mc1.f = 1.543219\n",
"print(\"-- mc1.u changes --\")\n",
"mc1.u = \"Sydney Poitier!\" # still gets class notification and report change\n",
"print(\"-- mc2.i changes --\")\n",
"mc2.i = 999\n",
"print(\"-- mc2.f changes --\")\n",
"mc2.f = 3.14159\n",
"print(\"-- mc2.u changes --\")\n",
"mc2.u = \"Somebody\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So, here's a question.\n",
"If you repeat the execution of the cell above, there are no notifications.\n",
"Why this different output?\n",
"\n",
"Here's another one.\n",
"If you change the values being bound to the traits and then execute the cell a _third_ time,\n",
"why do you see the repeated notifications?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mc2 = MyClass()\n",
"mc2.observe(lambda c: (c.old, c.new))\n",
"with mc1.hold_trait_notifications():\n",
" mc1.i = 1\n",
" mc2.i = 101\n",
" print(\"Releasing notification hold\")"
]
},
{
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment