{"title":"funcptr - python","subtitle":"An engineer's technical notebook","link":[{"@attributes":{"rel":"self","type":"application\/atom+xml","href":"https:\/\/funcptr.net\/language\/python\/atom.xml"}},{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/"}}],"generator":"Zola","updated":"2021-02-15T00:00:00+00:00","id":"https:\/\/funcptr.net\/language\/python\/atom.xml","entry":[{"title":"Python Packaging and Distribution","published":"2018-05-21T14:00:00+00:00","updated":"2018-05-21T14:00:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2018\/python-packaging\/"}},"id":"https:\/\/funcptr.net\/2018\/python-packaging\/","content":"<p>There's been many a discussion online related to a variety of tools related to\nPython packaging and distribution. There is <a rel=\"external\" href=\"https:\/\/pip.pypa.io\">pip<\/a>, <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\">pipenv<\/a> and\n<a rel=\"external\" href=\"https:\/\/poetry.eustace.io\">poetry<\/a> that have been the tools under discussion.<\/p>\n<p>As an open source maintainer as part of the <a rel=\"external\" href=\"https:\/\/pylonsproject.org\">Pylons Project<\/a>, while I would\nlove to be writing code I end up spending a lot of time dealing with user\nquestions around packaging\/distributing their source code using the software\nI've helped build, and as we move forward myself and other maintainers were\nwondering if we were actually helping users move forward in the best way\npossible using best of breed tools.<sup class=\"footnote-reference\" id=\"fr-201805tooldiscussionpyramid-1\"><a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fn-201805tooldiscussionpyramid\">[1]<\/a><\/sup><\/p>\n<p>As the Python community has moved from <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20180527214739\/http:\/\/setuptools.readthedocs.io\/en\/latest\/easy_install.html#easy-install\">easy_install<\/a> to pip, we too have\nkept the documentation up to date. We went from <code>python setup.py develop<\/code> to\n<code>pip install -e .<\/code> to create editable installs of local projects, and try to\nlet people know the pitfalls of using both easy_install and pip in the same\nproject (mostly with an answer that falls in line with: remove your virtual\nenvironment and start over, just use pip).<\/p>\n<p>As part of <a rel=\"external\" href=\"https:\/\/trypyramid.com\">Pyramid<\/a> we have developed and maintain various different\n<a rel=\"external\" href=\"https:\/\/cookiecutter.readthedocs.io\/en\/latest\/\">cookiecutter<\/a> templates, and our goal is to attempt to provide templates\nthat are both useful, but also follow best practices that are being adopted\nwithin the community at large so that newcomers can use their existing\nskills\/knowledge and those that are starting with us walk away with a knowledge\nand experience that applies not just to development of Pyramid applications,\nbut also applies to the broader community as a whole.<\/p>\n<h2 id=\"pip\">pip<\/h2>\n<p>Pip is a great tool that has simplified installation of packages, it supports\nusing binary distributions named <a rel=\"external\" href=\"https:\/\/github.com\/pypa\/wheel\">wheels<\/a> and has a way to easily install\nsoftware from the <a rel=\"external\" href=\"https:\/\/pypi.org\">Python Packaging Index<\/a>. It has a rather naive\ndependency resolution process, but for the most part it works and works well.\nIt replaced easy_install as the tool to use for installing packages.<\/p>\n<p>While you can use a <code>requirements.txt<\/code> file with pip to install a \"blessed\"\nlist of software there is no good way to \"lock\" the dependencies of\ndependencies without manually adding it to the list of requirements. This ends\nup making it very difficult to manage, and it is very difficult to know that\nwhat has been tested is what the user is actually going to get because packages\nmay be updated at anytime, and re-creating the same exact environment is\ndifficult and fraught with errors.<\/p>\n<p>This is where <a rel=\"external\" href=\"https:\/\/github.com\/pypa\/pipfile\">Pipfile<\/a> is supposed to help. This is a project to add a\nnew, more descriptive requirements file, as well as allowing for a lockfile\nthat would lock not just your primary packages you have listed, but also all\ndependencies of dependencies all the way down the tree. This helps with\nreproducibility and allows for the same installation on two different systems\nto have the exact same software\/dependencies installed.<\/p>\n<h2 id=\"pipenv\">pipenv<\/h2>\n<p>While pip is a great tool, and with the Pipfile changes it would allow for\nlocking of dependencies, there is one more puzzle piece missing. When\ninstalling packages while you can install them into the global namespace, the\nrecommended way is to install all packages for a particular tool\/project into a\nvirtual environment.<\/p>\n<p>Normally you'd invoke <a rel=\"external\" href=\"https:\/\/virtualenv.pypa.io\/en\/stable\/\">virtualenv<\/a>, to create this environment and then\nyou'd make sure to install all packages within it, thereby isolating it from\nthe rest of the system.<\/p>\n<p>pipenv automates this for you, as well as using <code>Pipfile<\/code> it also supports\nlocking using <code>Pipfile.lock<\/code> and provides a bunch of tooling around\nadding\/removing dependencies from a local project.<\/p>\n<p>pipenv allows you to easily create an environment and manage dependencies, but\nit makes no effort to solve the problem of distributing and building a package\nthat may be installed by third parties.<\/p>\n<h2 id=\"poetry\">poetry<\/h2>\n<p>Poetry is a similar project to pipenv, with a major difference being that it\nwas built to help with distributing\/developing applications and building a\ndistributable package that may then be installed using pip.<\/p>\n<p>Instead of using a <code>Pipfile<\/code> it uses a recently standardised <code>pyproject.toml<\/code>\nfile instead. Like pipenv it also supports locking, and it provides tooling\naround adding\/removing dependencies as well as managing what versions are\nrequired.<\/p>\n<p>Ultimately those dependencies are going to end up as metadata in a\ndistributable package.<\/p>\n<p>Poetry makes it easier to manage a software development project, whether that\nis for an application using various libraries for internal use, or for\nlibraries that are going to be distributed to other developers.<\/p>\n<h2 id=\"the-divide\">The divide<\/h2>\n<p>This is where the divide really starts, while you can use pipenv with a\nstandard <a rel=\"external\" href=\"https:\/\/setuptools.readthedocs.io\/en\/latest\/\">setuptools<\/a> project, any dependencies you add to the Pipfile\nusing pipenv's tooling will not be listed as a dependencies for your project\nwhen you distribute it, this either means you need to duplicate the list in\nboth <code>setup.py<\/code> as well as the <code>Pipfile<\/code>, or you have to add your current\nproject as an editable install within your <code>Pipfile<\/code> which means your <code>Pipfile<\/code>\nis now not as easily distributable.<\/p>\n<p>There are work-arounds that people have used, such as having <code>setup.py<\/code> read a\n<code>requirements.txt<\/code>, so that you could have all your requirements listed in a\ntext file, and not in <code>setup.py<\/code>, but asking to do the same with a <code>Pipfile<\/code> in\npipenv was met with a <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20201020045235\/https:\/\/github.com\/pypa\/pipenv\/issues\/209#issuecomment-332553113\">\"Do not do this.\"<\/a>.<\/p>\n<p>poetry explicitly allows you to add dependencies in one place, and those\ndependency listings are then automatically inserted into the package metadata\nthat is created when you build your distributable package.<\/p>\n<h2 id=\"the-two-use-cases\">The two use cases<\/h2>\n<p>There are two competing use cases, one is the deployment of software packages\nand being able to run them, but not as a developer, the other is a developer of\nsoftware packages that needs to define dependencies for the project to run.<\/p>\n<p>pipenv solves the deployment case. If I was a user I could very simply grab a\nknown good <code>Pipfile.lock<\/code> and use pipenv to install a known good set of\nsoftware, this is great when I am deploying a project. It is the use case that\nmany in the <a rel=\"external\" href=\"https:\/\/www.pypa.io\/en\/latest\/\">Python Packaging Authority<\/a> also seem to be optimizing for.<\/p>\n<p>The other use case is for developers that are building new software, either by\nusing a list of existing packages and deploying privately, or people developing\nsoftware for other developers to be published on the Python Packaging Index.<\/p>\n<p>This latter group of people is under represented due to it likely being much\nsmaller, and existing tools like <code>setuptools<\/code> and <code>setup.py<\/code> already providing\na \"good enough\" experience. Innovation in this area is something that readily\nneeds to be improved upon to make it easier to create new libraries\/packages\nthat follow best practices. The amount of copy and pastes people have done for\nadding a <code>setup.py<\/code> to their projects or to make something work is long. It's\nall a little bit of black magic, and there is a great many things that have\nbeen carried over because of cargo cult programming.<\/p>\n<h2 id=\"explicit-mentions-by-the-python-packaging-authority\">Explicit mentions by the Python Packaging Authority<\/h2>\n<p>Reading the packaging guide on <a rel=\"external\" href=\"https:\/\/packaging.python.org\/tutorials\/managing-dependencies\/\">managing dependencies<\/a>, pipenv is the\nrecommended tool:<\/p>\n<blockquote>\n<p>This tutorial walks you through the use of Pipenv to manage dependencies for\nan application. It will show you how to install and use the necessary tools\nand make strong recommendations on best practices.<\/p>\n<\/blockquote>\n<p>this language, along with what <code>packaging.python.org<\/code> implies as a URL makes it\ndifficult as a project maintainer to recommend alternate tools, becuase even if\nthose tools are superior for the use case we are recommending them for it is\nalways going to lead to questions from users, such as:<\/p>\n<blockquote>\n<p>Why are you not using pipenv, the official tool recommended by Python.org?<\/p>\n<\/blockquote>\n<p>We get similar questions about <code>easy_install<\/code> vs <code>pip<\/code> all of the time, as\nwell as why people should switch, and we can point to various bits of\ndocumentation that explains why pip is a better choice.<\/p>\n<p>If we were to recommend an alternate the appeal to authority that <code>python.org<\/code>\nimplies is going to make it much more difficult, and the question will become\n\"why is the Pylons Project not using recommended tooling?\"<\/p>\n<p>poetry is listed as a footnote on that page, alongside <a rel=\"external\" href=\"https:\/\/github.com\/jazzband\/pip-tools\">pip-tools<\/a> and\n<a rel=\"external\" href=\"https:\/\/github.com\/ofek\/hatch\">hatch<\/a>, and is mentioned only for doing library development, with no\nmention of other requirements that may make it a much better tool for\ndeveloping locally.<\/p>\n<h2 id=\"deployment-is-not-development\">Deployment is not development<\/h2>\n<p>If I am using pipenv with a non-installable project (no setup.py) I end up\nhaving to figure out how to get the code, and the <code>Pipfile<\/code>\/<code>Pipfile.lock<\/code> to\nmy environment I am deploying into. pipenv's install provides a way to make\nsure to only install if the <code>Pipfile.lock<\/code> is up to date or otherwise will fail\nto continue. If you are using a local project though, and it uses <code>setup.py<\/code>\nthe only way that the <code>Pipfile.lock<\/code> will contain any sub-dependencies of your\n<code>setup.py<\/code> project is if you install it as <code>editable<\/code>. Otherwise\nsub-dependencies are not locked.<sup class=\"footnote-reference\" id=\"fr-201805subdepends-1\"><a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fn-201805subdepends\">[2]<\/a><\/sup><\/p>\n<p>If I am using poetry I get an pip installable project, but it doesn't contain\nany hard pins or lock files. I'd have to distribute <code>pyproject.lock<\/code> as well as\nmy wheel. This gets me a little closer, but still no lock file that includes my\nnewly produced wheel, and has all of its dependencies locked.<\/p>\n<p>The Python Packaging Authority based on Twitter conversations with its members\nand the documentation on <code>packaging.python.org<\/code> suggest using pipenv for\ndevelopment. pipenv is particular ill-suited for development if the goal is to\ncreate a package to be deployed to production. With two locations to define\ndependencies it leaves people scratching their heads as to which is canonical,\nand if a dependency is added to <code>Pipfile<\/code> but not <code>setup.py<\/code> it may leave a\ndeveloper thinking their package is ready for distribution when in reality it\nis missing a dependency that is required to run\/use said distribution.<\/p>\n<p>At this point using both projects seems like a win-win. Use poetry to\nbuild\/develop a package, then use pipenv in the integration phase to create a\n<code>Pipfile.lock<\/code> that is used to deploy in production. This way you get the best\nof both worlds. A great tool that can help you register entry points and\nanother that can help you with deploying a known good set of dependencies.<\/p>\n<p>Interestingly, even the pipenv docs seem to agree that it is a deployment tool:<\/p>\n<blockquote>\n<p>Specify your target Python version in your Pipfile\u2019s [requires] section.\nIdeally, you should only have one target Python version, as this is a\ndeployment tool.<\/p>\n<p>-- <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\/basics\/#general-recommendations-version-control\">Pipenv - General Recommendations &amp; Version Control<\/a><\/p>\n<\/blockquote>\n<p>Use pipenv if you have a script that requires a couple of dependencies and\ndoesn't need all of the extra overhead of packaging metadata\/packaging. Use\npoetry if you want to build a distributable project that can easily be deployed\nby others, and use both if you develop a project and need a known good\nenvironment to deploy.<\/p>\n<h2 id=\"in-summary\">In summary<\/h2>\n<p>There will likely never be a time that one single tool is considered good\nenough, and competition between tools is a way to keep advancing forward.\nPackaging in the Python community for a long time has been difficult. Wheels\nhas made things a little better. pip has made management of installing new\npackages easier and improved upon easy_install. Here's to the next evolution.<\/p>\n<hr \/>\n<p>Now, can we talk about standardising on <code>pyproject.toml<\/code> since that is already\nwhere \"project\" metadata needs to go, might as well re-use the name instead of\nhaving two different names\/files. Oh, and <a rel=\"external\" href=\"https:\/\/www.python.org\/dev\/peps\/pep-0517\/\">PEP 517<\/a> can't come soon enough\nso that alternate tools like <a rel=\"external\" href=\"https:\/\/flit.readthedocs.io\/en\/latest\/\">flit<\/a> can be used instead of\nsetuptools\/<code>setup.py<\/code>.<\/p>\n<section class=\"footnotes\">\n<ol class=\"footnotes-list\">\n<li id=\"fn-201805tooldiscussionpyramid\">\n<p>We created an issue named <a rel=\"external\" href=\"https:\/\/github.com\/Pylons\/pyramid\/issues\/3270\">Support poetry,\nflit, pipenv, or ...?<\/a> that attempts to go over the pros and cons of the\nvarious tools and how we currently support our users in our documentation on\nbuilding projects using pyramid, including how to create a project that is\ndistributable.  Pyramid heavily uses <a rel=\"external\" href=\"http:\/\/setuptools.readthedocs.io\/en\/latest\/pkg_resources.html\"><code>pkg_resources<\/code><\/a> and entry\npoints.  The way to register the entry points is to have an installable\npackage. The framework is flexible enough that there is no requirement for\nentry points, but at that point you are in territory where the default\ntooling provided by the project will not work, and some of the convenience\ntools\/functionality that Pyramid provides it's users\/developers is not\navailable. <a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fr-201805tooldiscussionpyramid-1\">\u21a9<\/a><\/p>\n<\/li>\n<li id=\"fn-201805subdepends\">\n<p>See documentation for <a rel=\"external\" href=\"https:\/\/docs.pipenv.org\/basics\/#editable-dependencies-e-g-e\">Editable Dependencies (e.g.  <code>-e   .<\/code>)<\/a> which as of this writing states: <blockquote>Sub-dependencies are <strong>not<\/strong> added to the Pipfile.lock if you leave the -e option out.<\/blockquote> <a href=\"https:\/\/funcptr.net\/2018\/python-packaging\/#fr-201805subdepends-1\">\u21a9<\/a><\/p>\n<\/li>\n<\/ol>\n<\/section>\n"},{"title":"Build numbers in binaries using waf","published":"2014-06-10T17:32:03+00:00","updated":"2014-06-10T17:32:03+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/"}},"id":"https:\/\/funcptr.net\/2014\/build-numbers-in-binaries-using-waf\/","content":"<p>My build system of choice these days for any C++ project is <a rel=\"external\" href=\"https:\/\/waf.io\">waf<\/a>. One of\nthe things I always like havig is the build number included in the final\nbinary, so that with a simple <code>.\/binary --version<\/code> or even <code>.\/binary<\/code> the\nversion is printed that it was built from. This can make it much simpler to\ndebug any potential issues, especially if fixes may have already been made but\na bad binary was deployed.<\/p>\n<h2 id=\"setup-the-wscript\">Setup the wscript<\/h2>\n<p>Make sure that your wscript somewhere near the top contains the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">myapp<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">0.0.0<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span><\/code><\/pre>\n<p>Then in your <code>configure(cfg)<\/code> add the following:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> VERSION<\/span><\/span>\n<span class=\"giallo-l\"><span>cfg.env.<\/span><span style=\"color: #82AAFF;\">APPNAME<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> APPNAME<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>git_version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #B2CCD6;\"> try_git_version<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">if<\/span><span> git_version:<\/span><\/span>\n<span class=\"giallo-l\"><span>    cfg.env.<\/span><span style=\"color: #82AAFF;\">VERSION<\/span><span style=\"color: #C792EA;\"> +=<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">-<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #C792EA;\"> +<\/span><span> git_version<\/span><\/span><\/code><\/pre>\n<p>The <code>try_git_version()<\/code> function is fairly simple and looks like this:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> try_git_version<\/span><span style=\"color: #D9F5DD;\">()<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> os<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    import<\/span><span> sys<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span>    version<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #FF5874;\"> None<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    try<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>        version<\/span><span style=\"color: #C792EA;\"> =<\/span><span> os.<\/span><span style=\"color: #B2CCD6;\">popen<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">git describe --always --dirty --long<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">read<\/span><span>().<\/span><span style=\"color: #B2CCD6;\">strip<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    except<\/span><span style=\"color: #C5E478;\"> Exception<\/span><span style=\"color: #C792EA;font-style: italic;\"> as<\/span><span> e:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        print<\/span><span> e<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    return<\/span><span> version<\/span><\/span><\/code><\/pre>\n<p>It runs <code>git describe --always --dirty --long<\/code> which will return something\nalong these lines: <code>401b85f-dirty<\/code>. If you have any annoted tags, it will\nreturn the tag name as well.<\/p>\n<p>If <code>git<\/code> is not installed, or it is not a valid <code>git<\/code> directory, then it will\nsimply return <code>None<\/code>. At that point all we have to go on is the <code>VERSION<\/code>\nvariable set at the top of the wscript.<\/p>\n<p>Now that we have our configuration environment set up with the <code>VERSION<\/code> we\nwant to get that into a file that we can then include in our C++ source code.<\/p>\n<h2 id=\"create-a-build-version-h-in-file\">Create a <code>build_version.h.in<\/code> file<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">ifndef<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">define<\/span><span style=\"color: #7FDBCA;font-style: italic;\"> BUILD_VERSION_H_IN_941AD1F24D0A9D<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">char<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span>[]<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">@VERSION@<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>;<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">endif<\/span><span style=\"color: #637777;font-style: italic;\"> \/* BUILD_VERSION_H_IN_941AD1F24D0A9D *\/<\/span><\/span><\/code><\/pre><h2 id=\"add-the-following-to-build-ctx\">Add the following to <code>build(ctx)<\/code><\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #B2CCD6;\">ctx<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">features<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">subst<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        source<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h.in<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        target<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&#39;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        VERSION<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #82AAFF;\"> ctx.env<\/span><span>[<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">VERSION<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>]<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span>        )<\/span><\/span><\/code><\/pre>\n<p>This uses the substitution feature to transform <code>build_version.h.in<\/code> into\n<code>build_version.h<\/code>, while inserting the version into the file.<\/p>\n<h2 id=\"include-build-version-h-in-your-source-code\">Include <code>build_version.h<\/code> in your source code<\/h2>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">#<\/span><span style=\"color: #C792EA;font-style: italic;\">include<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">build_version.h<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><\/span><\/code><\/pre>\n<p>And add something along these lines to your <code>main()<\/code>:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"cpp\"><span class=\"giallo-l\"><span>std::<\/span><span style=\"color: #C5E478;\">cerr<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">Version: <\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span style=\"color: #C5E478;\"> VERSION<\/span><span style=\"color: #C792EA;\"> &lt;&lt;<\/span><span> std::<\/span><span style=\"color: #C5E478;\">endl<\/span><span>;<\/span><\/span><\/code><\/pre>\n<p>This will print out the <code>VERSION<\/code> that has been stored in <code>build_version.h<\/code>.<\/p>\n<h2 id=\"full-example\">Full example<\/h2>\n<p>Check out my <a rel=\"external\" href=\"https:\/\/github.com\/digitalresistor\/mdns-announce\">mdns-announce<\/a> project on Github for an example of how this is\nimplemented.<\/p>\n"},{"title":"Generating stylesheets for pygments","published":"2011-11-27T00:00:00+00:00","updated":"2021-02-15T00:00:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2011\/pygments-stylesheets\/"}},"id":"https:\/\/funcptr.net\/2011\/pygments-stylesheets\/","content":"<p>Update: this blog is no longer using the same technology for deployment as\nbefore, the below information is available for historical reasons.<\/p>\n<hr>\n<p>For this blog I am using <a rel=\"external\" href=\"http:\/\/freewisdom.org\/projects\/python-markdown\/\">Python markdown<\/a> with the <a rel=\"external\" href=\"http:\/\/pygments.org\/\">Pygments<\/a> plugin\nthat is available. Pygments does all of the code highlighting and outputs the\nrequired <code>span<\/code> tags and inputs them within the page.<\/p>\n<p>If you were to look at the <code>&lt;div&gt;&lt;pre&gt;&lt;\/pre&gt;&lt;\/div&gt;<\/code> block that is generated\nyou'd see something like this:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"plain\"><span class=\"giallo-l\"><span>&lt;div class=&quot;codehilite&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;def&lt;\/span&gt; &lt;span class=&quot;nf&quot;&gt;function&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;\/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;\/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;\/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;\/span&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span>&lt;span class=&quot;k&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;\/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;\/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;\/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;\/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;\/span&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span>    &lt;span class=&quot;k&quot;&gt;print&lt;\/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;\/span&gt;<\/span><\/span>\n<span class=\"giallo-l\"><span>&lt;\/pre&gt;&lt;\/div&gt;<\/span><\/span><\/code><\/pre>\n<p>which is generated from the following snippet of code:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> function<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">var=<\/span><span style=\"color: #FF5874;\">None<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> var<\/span><span style=\"color: #C792EA;\"> is not<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        print<\/span><span> var<\/span><\/span><\/code><\/pre>\n<p>As you see the top css <code>class<\/code> is <code>codehilite<\/code>, and then underneath that there\nare a bunch of other classes such as <code>k<\/code>, <code>n<\/code>, <code>o<\/code>, and so forth. Those are all\nclasses that can be styled. Now one could write such a stylesheet by hand or we\ncan simply use one of the stylesheets that Pygments ships with.<\/p>\n<p>The documentation for Pygments tells you how to get <a rel=\"external\" href=\"http:\/\/pygments.org\/docs\/styles\/#builtin-styles\">built-in styles<\/a>, which\nis as simple as:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;\">&gt;&gt;&gt;<\/span><span style=\"color: #C792EA;font-style: italic;\"> from<\/span><span> pygments.styles<\/span><span style=\"color: #C792EA;font-style: italic;\"> import<\/span><span> get_all_styles<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">&gt;&gt;&gt;<\/span><span> styles<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> list<\/span><span>(<\/span><span style=\"color: #B2CCD6;\">get_all_styles<\/span><span>())<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">&gt;&gt;&gt;<\/span><span> styles<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D9F5DD;\">[&#39;<\/span><span style=\"color: #ECC48D;\">monokai<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">manni<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">perldoc<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">borland<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">colorful<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &#39;<\/span><span style=\"color: #ECC48D;\">default<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>,<\/span><span style=\"color: #82AAFF;\"> ...<\/span><span style=\"color: #D9F5DD;\">]<\/span><span> <\/span><\/span><\/code><\/pre>\n<p>These are all the styles that are available to you the user, now how do we get\nthe style into an actual <code>.css<\/code> file that we can link to in our sites HTML\nfile? We use the <code>pygmentize<\/code> command line tool:<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"shellscript\"><span class=\"giallo-l\"><span style=\"color: #82AAFF;font-style: italic;\">pygmentize<\/span><span style=\"color: #82AAFF;\"> -f<\/span><span style=\"color: #ECC48D;\"> html<\/span><span style=\"color: #82AAFF;\"> -S<\/span><span style=\"color: #ECC48D;\"> trac<\/span><span style=\"color: #82AAFF;\"> -a<\/span><span style=\"color: #ECC48D;\"> .codehilite<\/span><span style=\"color: #7FDBCA;\"> &gt;<\/span><span style=\"color: #ECC48D;\"> pygments.css<\/span><\/span><\/code><\/pre>\n<p>Feel free to replace <code>trac<\/code> with any of the styles you have installed on your\nsystem. <code>-f<\/code> tells it what formatter to use, since we want it to output the css\nwe ask it to use the HTML formatter, <code>-S<\/code> tells <code>pygmentize<\/code> what style to\nuse, and <code>-a<\/code> prepends the <code>.codehilite<\/code> class before every rule (this way we\ndon't pollute the whole CSS namespace with all the aforementioned class's such\nas <code>k<\/code>, <code>o<\/code>, <code>ow<\/code> and others).<\/p>\n"},{"title":"Magic Properties on Google AppEngine","published":"2010-03-24T02:15:33+00:00","updated":"2010-03-24T02:15:33+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/magic-properties-on-google-appengine\/"}},"id":"https:\/\/funcptr.net\/2010\/magic-properties-on-google-appengine\/","content":"<p>Update to my previous <a href=\"\/2010\/auto-updating-properties-in-google-appengine-models\/\">Google AppEngine auto-updating properties post<\/a>.<\/p>\n<p>In my last post I was talking about:<\/p>\n<blockquote>\n<p>[...] however there is still an issue, for now the MagicProperty is only\nupdated the first time the property it affects is set<\/p>\n<\/blockquote>\n<p>I fixed that issue, it has a local cache that is added to the model, so if the\nvalue does change, the cache would no longer be valid and the value is\nrecreated, otherwise the already computed value is returned. There was however\nanother issue I had to deal with, and that was the fact that the cache was not\nbeing written to the datastore, this meant that upon getting the data back from\nthe datastore we recomputed it anyway. Now the programmer has to do some extra\nwork, they have to add a cache unindexed property to the model, and pass it\ninto the <code>MagicProperty<\/code> to be used as the caching variable. Within the\n<code>save()<\/code> they have to prime the cache by setting a temporary variable equal to\nthe to be computed variable just in case the cache variables have not been\nprimed, this has not yet been documented other than in my SVN history.<\/p>\n<p>We still don't allow setting of the magic property, however upon returning from\nthe datastore it calls <code>make_value_from_datastore<\/code>, which returns a\n<code>_MagicDatastore<\/code>, as long as we set with an instance of <code>_MagicDatastore<\/code> it will\nallow it to go through, so we can get the value from the datastore without\nhaving to recompute the value.<\/p>\n<p>Read the code for an example on how to use this.<\/p>\n<p>Code is added below, feel free to use it as you wish under the license that is\nattached.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  1<\/span><span style=\"color: #637777;font-style: italic;\">###<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  2<\/span><span style=\"color: #637777;font-style: italic;\"> # Copyright (c) 2010 Delta Regeer;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  3<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  4<\/span><span style=\"color: #637777;font-style: italic;\"> # Permission to use, copy, modify, and distribute this software for any<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  5<\/span><span style=\"color: #637777;font-style: italic;\"> # purpose with or without fee is hereby granted, provided that the above<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  6<\/span><span style=\"color: #637777;font-style: italic;\"> # copyright notice and this permission notice appear in all copies.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  7<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  8<\/span><span style=\"color: #637777;font-style: italic;\"> # THE SOFTWARE IS PROVIDED &quot;AS IS&quot; AND THE AUTHOR DISCLAIMS ALL WARRANTIES<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">  9<\/span><span style=\"color: #637777;font-style: italic;\"> # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 10<\/span><span style=\"color: #637777;font-style: italic;\"> # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 11<\/span><span style=\"color: #637777;font-style: italic;\"> # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 12<\/span><span style=\"color: #637777;font-style: italic;\"> # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 13<\/span><span style=\"color: #637777;font-style: italic;\"> # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 14<\/span><span style=\"color: #637777;font-style: italic;\"> # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 15<\/span><span style=\"color: #637777;font-style: italic;\"> #<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 16<\/span><span style=\"color: #637777;font-style: italic;\">###<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 17<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 18<\/span><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> logging<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 19<\/span><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> hashlib<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 20<\/span><span style=\"color: #C792EA;font-style: italic;\">from<\/span><span> google.appengine.ext<\/span><span style=\"color: #C792EA;font-style: italic;\"> import<\/span><span> db<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 21<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 22<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 23<\/span><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> MagicProperty<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> cache_prop=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> pass_instance=<\/span><span style=\"color: #FF5874;\">False<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 24<\/span><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> magic_func:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 25<\/span><span style=\"color: #637777;font-style: italic;\">        # No pants required.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 26<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cache_prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> pass_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 27<\/span><span style=\"color: #C792EA;font-style: italic;\">    else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 28<\/span><span style=\"color: #637777;font-style: italic;\">        # We are putting some pants on the function.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 29<\/span><span style=\"color: #C792EA;\">        def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> pants<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">magic_func<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 30<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cache_prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> pass_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 31<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> pants<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 32<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 33<\/span><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicDatastore<\/span><span>():<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 34<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">This is an internal class for _MagicProperty.<\/span><span style=\"color: #D9F5DD;\">&quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 35<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 36<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> val<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 37<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.value<\/span><span style=\"color: #C792EA;\"> =<\/span><span> val<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 38<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 39<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> retval<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 40<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.value<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 41<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 42<\/span><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Property<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 43<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty which will modify output based on a function that it is given.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 44<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 45<\/span><span style=\"color: #ECC48D;\">    This has several ways in which it may be called:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 46<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 47<\/span><span style=\"color: #ECC48D;\">    In this example we create the model, no caching is done, so any data that is returned from the datastore<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 48<\/span><span style=\"color: #ECC48D;\">    will be erased the first time that the MagicProperty is accessed and recomputed.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 49<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 50<\/span><span style=\"color: #ECC48D;\">        class MagicTest(db.Model):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 51<\/span><span style=\"color: #ECC48D;\">            title = db.StringProperty(required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 52<\/span><span style=\"color: #ECC48D;\">            chars = utils.MagicProperty(title, len, required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 53<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 54<\/span><span style=\"color: #ECC48D;\">        mytest = MagicTest(title=&quot;It was for the good of the school!&quot;)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 55<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 56<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 57<\/span><span style=\"color: #ECC48D;\">        It was for the good of the school!<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 58<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 59<\/span><span style=\"color: #ECC48D;\">        34<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 60<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 61<\/span><span style=\"color: #ECC48D;\">        mytest = MagicTest.all().get()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 62<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 63<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 64<\/span><span style=\"color: #ECC48D;\">        Hello<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 65<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytest.chars\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 66<\/span><span style=\"color: #ECC48D;\">        5\t# Do note, this is recalculated the first time it is called, as long as title does not change it won&#39;t be recomputed.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 67<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 68<\/span><span style=\"color: #ECC48D;\">    In this example we create the model, and we also create a caching property so that even when we get values back <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 69<\/span><span style=\"color: #ECC48D;\">    from the datastore we use the cached computed value rather than running the function again. Do note that this requires overriding put()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 70<\/span><span style=\"color: #ECC48D;\">    to prime the cache as there is currently no way to specify that certain properties should be &quot;saved&quot; before others.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 71<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 72<\/span><span style=\"color: #ECC48D;\">        class MagicTesting(db.Model):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 73<\/span><span style=\"color: #ECC48D;\">            title = db.StringProperty(required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 74<\/span><span style=\"color: #ECC48D;\">            cache = db.UnindexedProperty()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 75<\/span><span style=\"color: #ECC48D;\">            chars = utils.MagicProperty(title, len, cache_prop=cache, required=True)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 76<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 77<\/span><span style=\"color: #ECC48D;\">            def put(self, *args, **kw):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 78<\/span><span style=\"color: #ECC48D;\">                prime_cache = self.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 79<\/span><span style=\"color: #ECC48D;\">                super(MagicTesting).put(*args, **kw)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 80<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 81<\/span><span style=\"color: #ECC48D;\">        mytesting = MagicTesting(title=&quot;Get the pocket knife out of my boot.&quot;)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 82<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 83<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 84<\/span><span style=\"color: #ECC48D;\">        Get the pocket knife out of my boot.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 85<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.chars<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 86<\/span><span style=\"color: #ECC48D;\">        36<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 87<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 88<\/span><span style=\"color: #ECC48D;\">        mytesting = MagicTest.all().get()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 89<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 90<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.title<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 91<\/span><span style=\"color: #ECC48D;\">        How are you?<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 92<\/span><span style=\"color: #C792EA;font-style: italic;\">        &gt;&gt;&gt; <\/span><span style=\"color: #ECC48D;\">print mytesting.chars\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 93<\/span><span style=\"color: #ECC48D;\">        12\t# This is not recomputed so long as the title has not changed, however it uses more datastore space to store a hash.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 94<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 95<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 96<\/span><span style=\"color: #ECC48D;\">    Inspired by: <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 97<\/span><span style=\"color: #ECC48D;\">    http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 98<\/span><span style=\"color: #ECC48D;\">    http:\/\/code.google.com\/appengine\/articles\/extending_models.html<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\"> 99<\/span><span style=\"color: #ECC48D;\">    http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">100<\/span><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">101<\/span><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">102<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> cache_prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> pass_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">103<\/span><span style=\"color: #D9F5DD;\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">104<\/span><span style=\"color: #ECC48D;\">        Extra parameters you can give this initializer.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">105<\/span><span style=\"color: #ECC48D;\">        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">106<\/span><span style=\"color: #ECC48D;\">            prop\t\t= Property to be acted upon<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">107<\/span><span style=\"color: #ECC48D;\">            magic_func\t= The function to be called when the property is accessed<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">108<\/span><span style=\"color: #ECC48D;\">            cache_prop\t= The property that can hold our cache, I suggest it is an db.UnindexedProperty() since it just stores sha1 hashes<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">109<\/span><span style=\"color: #D9F5DD;\">        &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">110<\/span><span style=\"color: #C5E478;\">        super<\/span><span>(<\/span><span style=\"color: #82AAFF;\">_MagicProperty<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>).<\/span><span style=\"color: #C5E478;\">__init__<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">*<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">111<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_func<\/span><span style=\"color: #C792EA;\"> =<\/span><span> magic_func<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">112<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_prop<\/span><span style=\"color: #C792EA;\"> =<\/span><span> prop<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">113<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> =<\/span><span> cache_prop<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">114<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_pass<\/span><span style=\"color: #C792EA;\"> =<\/span><span> pass_instance<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">115<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">116<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> get_cache_val<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">117<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> is not<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">118<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">119<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">orig<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">120<\/span><span>            <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">121<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> set_cache_val<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> val<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">122<\/span><span>        val<\/span><span style=\"color: #C792EA;\"> =<\/span><span> hashlib.<\/span><span style=\"color: #B2CCD6;\">sha1<\/span><span>(<\/span><span style=\"color: #82AAFF;\">val<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">hexdigest<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">123<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">124<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_cache<\/span><span style=\"color: #C792EA;\"> is not<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">125<\/span><span style=\"color: #8EACE3;\">            self<\/span><span>.magic_cache.<\/span><span style=\"color: #C5E478;\">__set__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> val<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">126<\/span><span style=\"color: #C5E478;\">        setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">orig<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #82AAFF;\"> val<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">127<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">128<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> attr_name<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">129<\/span><span style=\"color: #637777;font-style: italic;\">        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">130<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>._attr_name:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">131<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">132<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">133<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">_<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.name<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">134<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">135<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __get__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">136<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> model_instance<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">137<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">138<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">139<\/span><span>        cur<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_prop.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">140<\/span><span>        cur<\/span><span style=\"color: #C792EA;\"> =<\/span><span> cur.<\/span><span style=\"color: #B2CCD6;\">encode<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">utf-8<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span>)\t\t<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">141<\/span><span>        last<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">get_cache_val<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">142<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> last<\/span><span style=\"color: #C792EA;\"> ==<\/span><span> hashlib.<\/span><span style=\"color: #B2CCD6;\">sha1<\/span><span>(<\/span><span style=\"color: #82AAFF;\">cur<\/span><span>).<\/span><span style=\"color: #B2CCD6;\">hexdigest<\/span><span>():<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">143<\/span><span>            logging.<\/span><span style=\"color: #B2CCD6;\">info<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Cache hit: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> %<\/span><span style=\"color: #82AAFF;\"> (cur)<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">144<\/span><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">145<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">146<\/span><span>        logging.<\/span><span style=\"color: #B2CCD6;\">info<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Cache miss: <\/span><span style=\"color: #82AAFF;\">%s<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> %<\/span><span style=\"color: #82AAFF;\"> (cur)<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">147<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">148<\/span><span>        magic_done<\/span><span style=\"color: #C792EA;\"> = u<\/span><span style=\"color: #D9F5DD;\">&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">149<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.magic_pass:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">150<\/span><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">151<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">152<\/span><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #82AAFF;\">cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">153<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">154<\/span><span style=\"color: #637777;font-style: italic;\">        # Set the attribute in the model<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">155<\/span><span style=\"color: #C5E478;\">        setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_done<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">156<\/span><span style=\"color: #8EACE3;\">        self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">set_cache_val<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> cur<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">157<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">158<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> magic_done<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">159<\/span><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">160<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">161<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __set__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> value<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">162<\/span><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #C5E478;\"> isinstance<\/span><span>(<\/span><span style=\"color: #82AAFF;\">value<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> _MagicDatastore<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">163<\/span><span style=\"color: #C5E478;\">            setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> value.<\/span><span style=\"color: #B2CCD6;\">retval<\/span><span>())<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">164<\/span><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">165<\/span><span style=\"color: #C792EA;font-style: italic;\">            raise<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">DerivedPropertyError<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty is magic. Magic may not be modified.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">166<\/span><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">167<\/span><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> make_value_from_datastore<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> value<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span aria-hidden=\"true\" class=\"giallo-ln\" style=\"color: #4B6479;\">168<\/span><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicDatastore<\/span><span>(<\/span><span style=\"color: #82AAFF;\">value<\/span><span>)<\/span><\/span><\/code><\/pre>"},{"title":"Auto-updating Properties in Google AppEngine Models","published":"2010-02-21T04:03:42+00:00","updated":"2010-03-24T00:00:00+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/auto-updating-properties-in-google-appengine-models\/"}},"id":"https:\/\/funcptr.net\/2010\/auto-updating-properties-in-google-appengine-models\/","content":"<p>EDIT: I have an updated version of this code available in a newer post, please\ngo to <a href=\"\/2010\/magic-properties-on-google-appengine\/\">Magic Properties on Google AppEngine<\/a> for the updated information.\nFor the generic information that has not changed, and the idea behind the code\nplease continue below.<\/p>\n<hr \/>\n<p>In Google AppEngine a model may contain many different properties, from this\nmodel you can generate a form using djangoforms. This is all good and well\nuntil you have a required property on a model that needs to have part of it be\ncalculated based upon something the user types in. For example, a blog entry\ngenerally has a \"slug\" which is generated from the title, now in Google\nAppEngine's models, if a property is required it has to be set at object\ncreation time.<\/p>\n<p>Djangoforms has a <code>form.save(committed=False)<\/code> that allows one to get back an\nunsaved <code>Model<\/code>, however since slug is not in the form (since it is auto\ngenerated) it cannot be set at model creation time. Even adding a hidden field\nto the form with the name of the property is not going to satisfy the model\nsince a value of <code>None<\/code> is considered to be empty, which means it does not meet\nthe <code>required=True<\/code> status.<\/p>\n<p>After trying a multitude of different options I ran across an <a rel=\"external\" href=\"http:\/\/code.google.com\/appengine\/articles\/extending_models.html\">article about\nextending models<\/a> that specifically talked about adding custom properties\nthat were saved into the DB, and could be searched on, this got me digging and\nI found <a rel=\"external\" href=\"http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html\">a blog post on the Google AppEngine blogspot<\/a> about the exact same\nthing, using yet again a different example. The one that got me on the right\ntrack was the following post by <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20090517114229\/http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute\/\">Rodrigo Moraes on his custom model\nproperties<\/a>.<\/p>\n<p>I came up with the following fairly quickly after also taking a look at\n<code>googleappengine.ext.db<\/code> within the Google AppEngine framework, however there is\nstill an issue, for now the <code>MagicProperty<\/code> is only updated the first time the\nproperty it affects is set. I am not yet sure how to work around that, I will\nhave to do some more digging, maybe there is a way to get notified if another\nproperty gets modified or updated.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">import<\/span><span> logging<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">from<\/span><span> google.appengine.ext<\/span><span style=\"color: #C792EA;font-style: italic;\"> import<\/span><span> db<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> MagicProperty<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func=<\/span><span style=\"color: #FF5874;\">None<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    if<\/span><span> magic_func:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # No pants required.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">    else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # We are putting some pants on the function.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">        def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> pants<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">magic_func<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #B2CCD6;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #82AAFF;\">prop<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_func<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> *<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> pants<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> _MagicProperty<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Property<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty which will modify output based on a function that it is given.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    Inspired by: <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/appengine-cookbook.appspot.com\/recipe\/custom-model-properties-are-cute<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/code.google.com\/appengine\/articles\/extending_models.html<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    http:\/\/googleappengine.blogspot.com\/2009\/07\/writing-custom-property-classes.html<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #ECC48D;\">    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D9F5DD;\">    &quot;&quot;&quot;<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __init__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> prop<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> magic_func<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> **kw<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">        super<\/span><span>(<\/span><span style=\"color: #82AAFF;\">_MagicProperty<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>).<\/span><span style=\"color: #C5E478;\">__init__<\/span><span>(<\/span><span style=\"color: #7FDBCA;\">*<\/span><span style=\"color: #82AAFF;\">args<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #7FDBCA;\"> **<\/span><span style=\"color: #82AAFF;\">kw<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_func<\/span><span style=\"color: #C792EA;\"> =<\/span><span> magic_func<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #8EACE3;\">        self<\/span><span>.magic_prop<\/span><span style=\"color: #C792EA;\"> =<\/span><span> prop<\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #82AAFF;font-style: italic;\"> attr_name<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # In google.appengine.ex.db there is an explicit warning not to use this method, so we test for it first.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>._attr_name:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        else<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">_<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #C792EA;\"> +<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.name<\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __get__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> model_instance<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> class_instance<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> model_instance<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">            return<\/span><span style=\"color: #8EACE3;\"> self<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        #<\/span><span style=\"color: #C792EA;\"> TODO<\/span><span style=\"color: #637777;font-style: italic;\">: If the property that this one is functioning on is changed, we need a way to know that so that we recalculate<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">        # Original __get__ in google.appengine.ext.db has getattr to retrieve the value from the model instance<\/span><\/span>\n<span class=\"giallo-l\"><span>        magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #C5E478;\"> getattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #FF5874;\"> None<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        if<\/span><span> magic_done<\/span><span style=\"color: #C792EA;\"> is<\/span><span style=\"color: #FF5874;\"> None<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>            magic_done<\/span><span style=\"color: #C792EA;\"> =<\/span><span style=\"color: #8EACE3;\"> self<\/span><span>.<\/span><span style=\"color: #B2CCD6;\">magic_func<\/span><span>(<\/span><span style=\"color: #8EACE3;\">self<\/span><span style=\"color: #82AAFF;\">.magic_prop.<\/span><span style=\"color: #C5E478;\">__get__<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> class_instance<\/span><span>))<\/span><\/span>\n<span class=\"giallo-l\"><span>            <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\">            # Set the attribute in the model<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C5E478;\">            setattr<\/span><span>(<\/span><span style=\"color: #82AAFF;\">model_instance<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #8EACE3;\"> self<\/span><span style=\"color: #82AAFF;\">.<\/span><span style=\"color: #B2CCD6;\">_attr_name<\/span><span>()<\/span><span style=\"color: #D9F5DD;\">,<\/span><span style=\"color: #82AAFF;\"> magic_done<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        return<\/span><span> magic_done<\/span><\/span>\n<span class=\"giallo-l\"><span>        <\/span><\/span>\n<span class=\"giallo-l\"><span>    <\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    def<\/span><span style=\"color: #C5E478;\"> __set__<\/span><span style=\"color: #D9F5DD;\">(<\/span><span style=\"color: #7FDBCA;\">self<\/span><span>,<\/span><span style=\"color: #7FDBCA;\"> *args<\/span><span style=\"color: #D9F5DD;\">)<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;font-style: italic;\">        raise<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">DerivedPropertyError<\/span><span>(<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">MagicProperty is magic. Magic may not be modified.<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span><\/code><\/pre>\n<p>The approach that the <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20100208035715\/http:\/\/code.google.com\/appengine\/docs\/python\/datastore\/typesandpropertyclasses.html#DateTimeProperty\">DateTimeProperty<\/a> takes is one that I can't use\nbecause my class is dependant upon an external property, not whether or not a\nvalue was set when the object was instantiated.<\/p>\n<p>Google AppEngine is posing some rather interesting challenges. Although, for\nslugs it may not be entirely bad that it can only be set once, as that means\ntitle changes on the article won't cause the slug to change possibly breaking\nold links. This definitely requires more research and more thinking.<\/p>\n"},{"title":"Property class on AppEngine does not support a tuple for choices","published":"2010-02-15T04:56:59+00:00","updated":"2010-02-15T04:56:59+00:00","author":{"name":"Delta Regeer"},"link":{"@attributes":{"rel":"alternate","type":"text\/html","href":"https:\/\/funcptr.net\/2010\/property-class-on-google-appengine-not-supporting-a-tuple-of-choices\/"}},"id":"https:\/\/funcptr.net\/2010\/property-class-on-google-appengine-not-supporting-a-tuple-of-choices\/","content":"<p><a rel=\"external\" href=\"http:\/\/code.google.com\/appengine\/\">Google AppEngine<\/a> does not allow one to set the choices field of a <a rel=\"external\" href=\"https:\/\/web.archive.org\/web\/20100212221437\/http:\/\/code.google.com\/appengine\/docs\/python\/datastore\/propertyclass.html#Property\">model\nproperty<\/a><\/a> to a tuple; this workaround will have have a form give the\nuser the choices with the user friendly items but have the backend store the\nnon-user friendly version.<\/p>\n<pre class=\"giallo\" style=\"color: #D6DEEB; background-color: #011627;\" ><code data-lang=\"python\"><span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># Set up our non-user friendly and user friendly choices<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #82AAFF;\">REDIRECT_CHOICES<\/span><span style=\"color: #C792EA;\"> =<\/span><span> (<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">301<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">301 - Permanent Redirect<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">302<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">302 - Temporary Redirect<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">403<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">403 - Access Denied<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">404<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">404 - File Not Found<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>),<\/span><\/span>\n<span class=\"giallo-l\"><span>    (<\/span><span style=\"color: #F78C6C;\">500<\/span><span>,<\/span><span style=\"color: #D9F5DD;\"> &quot;<\/span><span style=\"color: #ECC48D;\">500 - Internal Server Error<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span>)<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># Sample model that has the required Property with the choices set to the<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># non-user friendly values<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> Shorturl<\/span><span>(<\/span><span style=\"color: #C5E478;\">db<\/span><span>.<\/span><span style=\"color: #C5E478;\">Model<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span>    uripath<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">StringProperty<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Path<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><span style=\"color: #D7DBE0;\"> required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><span>    httpcode<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">IntegerProperty<\/span><span>(<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">HTTP code<\/span><span style=\"color: #D9F5DD;\">&quot;,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        default<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #F78C6C;\">301<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        choices<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">[<\/span><span style=\"color: #82AAFF;\">x<\/span><span>[<\/span><span style=\"color: #F78C6C;\">0<\/span><span>]<\/span><span style=\"color: #C792EA;font-style: italic;\"> for<\/span><span style=\"color: #82AAFF;\"> x<\/span><span style=\"color: #C792EA;font-style: italic;\"> in<\/span><span style=\"color: #82AAFF;\"> REDIRECT_CHOICES<\/span><span style=\"color: #D9F5DD;\">]<\/span><\/span>\n<span class=\"giallo-l\"><span>    )<\/span><\/span>\n<span class=\"giallo-l\"><span>    location<\/span><span style=\"color: #C792EA;\"> =<\/span><span> db.<\/span><span style=\"color: #B2CCD6;\">StringProperty<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">verbose_name<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span style=\"color: #ECC48D;\">Location<\/span><span style=\"color: #D9F5DD;\">&quot;<\/span><span>)<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># This is our djangoform that has the choices set as required so that the user<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #637777;font-style: italic;\"># gets the user friendly version and not just simply the numbers.<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">class<\/span><span style=\"color: #FFCB8B;\"> ShorturlForm<\/span><span>(<\/span><span style=\"color: #C5E478;\">djangoforms<\/span><span>.<\/span><span style=\"color: #C5E478;\">ModelForm<\/span><span>):<\/span><\/span>\n<span class=\"giallo-l\"><span>    httpcode<\/span><span style=\"color: #C792EA;\"> =<\/span><span> forms.<\/span><span style=\"color: #B2CCD6;\">IntegerField<\/span><span>(<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        widget<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;\">forms.<\/span><span style=\"color: #B2CCD6;\">Select<\/span><span>(<\/span><span style=\"color: #D7DBE0;\">choices<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #82AAFF;\">REDIRECT_CHOICES<\/span><span>)<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        required<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #FF5874;\">True<\/span><span style=\"color: #D9F5DD;\">,<\/span><\/span>\n<span class=\"giallo-l\"><span style=\"color: #D7DBE0;\">        label<\/span><span style=\"color: #C792EA;\">=<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><span style=\"color: #ECC48D;\">HTTP code<\/span><span style=\"color: #D9F5DD;\">&#39;<\/span><\/span>\n<span class=\"giallo-l\"><span>    )<\/span><\/span>\n<span class=\"giallo-l\"><\/span>\n<span class=\"giallo-l\"><span style=\"color: #C792EA;\">    class<\/span><span style=\"color: #FFCB8B;\"> Meta<\/span><span>:<\/span><\/span>\n<span class=\"giallo-l\"><span>        model<\/span><span style=\"color: #C792EA;\"> =<\/span><span> Shorturl<\/span><\/span><\/code><\/pre>\n<p>Given the above tuple it will set the choices in the model for Google AppEngine\nand pass it to the field on the Django form allowing user friendly output. This\nwill allow the user to pick the right redirect choice without having to know\nthat various different redirect codes or have to look them up.<\/p>\n"}]}