{"id":1110,"date":"2016-11-02T09:16:14","date_gmt":"2016-11-02T09:16:14","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=1110"},"modified":"2016-11-02T09:16:14","modified_gmt":"2016-11-02T09:16:14","slug":"creating-functions-dynamically","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2016\/11\/creating-functions-dynamically\/","title":{"rendered":"Creating functions dynamically"},"content":{"rendered":"<p>Sometimes you want to create functions on the fly, at runtime. This came up when using some of the SciPy optimisation functions that require constraints on the variables to be defined as functions.<\/p>\n<p><!--more--><\/p>\n<p>As per <a href=\"http:\/\/stackoverflow.com\/questions\/10303248\/true-dynamic-and-anonymous-functions-possible-in-python\" target=\"_blank\">this StackOverflow post<\/a> there seem to be several ways to accomplish this.<\/p>\n<p>All methods revolve in one way or another around Python&#8217;s built-in <code>ast<\/code> and <code>types<\/code> modules. For more info, see for example here:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/ast.html\" target=\"_blank\">ast<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/types.html\" target=\"_blank\">types<\/a><\/li>\n<li><a href=\"https:\/\/greentreesnakes.readthedocs.io\/en\/latest\/index.html\" target=\"_blank\">Green Tree Snakes<\/a><\/li>\n<li><a href=\"http:\/\/eli.thegreenplace.net\/2009\/11\/28\/python-internals-working-with-python-asts\" target=\"_blank\">Python internals: Working with Python ASTs<\/a><\/li>\n<\/ul>\n<p>For our problem with the constraint functions, we ended up using the following.<\/p>\n<pre class=\"brush:py;gutter:false;toolbars:false\">\r\nfuncs = {}\r\n\r\nfor i in range(10):\r\n    code = \"\"\"def constraint_{0}(x): return x[{0:d}] > 1e-12\"\"\".format(i)\r\n    exec(code, {}, funcs)\r\n<\/pre>\n<pre class=\"brush:py;gutter:false;toolbars:false\">\r\nimport random\r\n\r\nr = [random.random() for _ in range(10)]\r\n\r\nfor name in funcs:\r\n    print funcs[name].__name__\r\n    print type(funcs[name])\r\n    print funcs[name](r)\r\n    print\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to create functions on the fly, at runtime. This came up when using some of the SciPy optimisation functions that require constraints on the variables to be defined as functions.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1110","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=1110"}],"version-history":[{"count":5,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1110\/revisions"}],"predecessor-version":[{"id":1227,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1110\/revisions\/1227"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=1110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=1110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=1110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}