未加星标

Worst/Trickiest code I have ever seen

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

It’s easy to write bad code, but it takes a real genius to produce truly terrible code. And the guys who wrote the python program hyperopt were clearly very clever.

Have a look at this function: (don’t worry about what it is doing) from tpe.py

# These produce conditional estimators for various prior distributions @adaptive_parzen_sampler('uniform') def ap_uniform_sampler(obs, prior_weight, low, high, size=(), rng=None): prior_mu = 0.5 * (high + low) prior_sigma = 1.0 * (high - low) weights, mus, sigmas = scope.adaptive_parzen_normal(obs, prior_weight, prior_mu, prior_sigma) return scope.GMM1(weights, mus, sigmas, low=low, high=high, q=None, size=size, rng=rng)

The details don’t matter here, but clearly it’s calling some function “adaptive_parzen_normal” which returns three values, then it passes that to another function called “GMM1” and returns the result.

Pretty straight forward? With me so far? Great.

Now here is some code that calls this function:

fn = adaptive_parzen_samplers[node.name] named_args = [[kw, memo[arg]] for (kw, arg) in node.named_args] a_args = [obs_above, prior_weight] + aa a_post = fn(*a_args, **dict(named_args))

Okay this is getting quite messy, but with a bit of thinking we can understand it. It’s just calling the ‘ap_uniform_sampler’ function, whatever that does, but letting us pass in parameters in some funky way.

So a_post is basically whatever “GMM1” returns (which is a list of numbers, fwiw)

Okay, let’s continue!

fn_lpdf = getattr(scope, a_post.name + '_lpdf') a_kwargs = dict([(n, a) for n, a in a_post.named_args if n not in ('rng', 'size')]) above_llik = fn_lpdf(*([b_post] + a_post.pos_args), **a_kwargs)

and that’s it. There’s no more code using a_post.

This took me a whole day to figure out what on earth is going on. But I’ll give you, the reader, a hint. This is not running any algorithm it’s constructing an Abstract Syntax Tree and manipulating it.

If you want, try and see if you can figure out what it’s doing.

Answer:

In fact the return value of GMM1 is not used at all . GMM1 isn’t even run.

So let’s look ap_uniform_sampler again:

# These produce conditional estimators for various prior distributions @adaptive_parzen_sampler('uniform') def ap_uniform_sampler(obs, prior_weight, low, high, size=(), rng=None): prior_mu = 0.5 * (high + low) prior_sigma = 1.0 * (high - low) weights, mus, sigmas = scope.adaptive_parzen_normal(obs, prior_weight, prior_mu, prior_sigma) return scope.GMM1(weights, mus, sigmas, low=low, high=high, q=None, size=size, rng=rng)

So this is return a tree structure that looks like:

Call “GMM1” with parameters: Get first tuple value from result of Call “adaptive_parzen_normal” Get second tuple value from result of Call “adaptive_parzen_normal” Get third tuple value from result of Call “adaptive_parzen_normal” Set named parameter “low” to “low” Set named parameter “high” to “high” Set named parameter “q” to “q” Set named parameter “size” to “size” Set named parameter “rng” to “rng”

adaptive_parzen_normal is run here, but it also returns a tree structure. Note that ‘low’, ‘high’ etc variables are also call graphs themselves.

Now here is the tricky part:

The code doesn’t run this graph, but grabs this literal string “GMM1” and adds “_lpdf” to it, then runs that function, using a different set of samples and the parameters that were supposed to be passed to GMM1, minus the ‘size’ and ‘rng’ parameters. So effectively it’s doing:

# These produce conditional estimators for various prior distributions @adaptive_parzen_sampler('uniform') def ap_uniform_sampler(samples, obs, prior_weight, low, high, size=(), rng=None): prior_mu = 0.5 * (high + low) prior_sigma = 1.0 * (high - low) weights, mus, sigmas = scope.adaptive_parzen_normal(obs, prior_weight, prior_mu, prior_sigma) return scope.GMM1_lpdf(samples, weights, mus, sigmas, low=low, high=high, q=None)

Got that? Good!

Maintainers are usually nervous about changing code that works and is in production for many users, so I’ve made a pull request on github to at least document this:

https://github.com/hyperopt/hyperopt/pull/290

Thoughts? Am I just stupid? Was this code obvious to you? How would you improve this?

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: GM
分页:12
转载请注明
本文标题:Worst/Trickiest code I have ever seen
本站链接:http://www.codesec.net/view/533373.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(53)