Basic Primer _python-built-in functions. Best practices for eval built-in functions in operations development?

Source: Internet
Author: User

Brief introduction:

Description: Evaluates a valid expression for a parameter string in the specified namespace, and returns an object

Help on built-in function eval in module __builtin__:eval (...)    Eval (source[, globals[, locals]), value Evaluate the source in the context of globals and locals.    The source is a string representing a Python expression or a code object as returned by compile ().    The globals must be a dictionary and locals can is any mapping, defaulting to the current globals and locals. If only globals are given, locals defaults to it.


Tip: eval is dangerous because it resolves statement expressions in the current namespace by default, but it supports setting namespaces to prevent the current namespace from being contaminated, effectively preventing injection


Best Practices:

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "#  description:  compatible absolute import from __future__  import absolute_import#  Description:  Import public module import timeimport operator#  Instructions:  Importing other modules From .alarm import alarm_templatefrom .alarm.api import weixin_notifydef  avg (alarmtmplist, redis_key, trigg_key, trigg_val, errors):     scope = {}    realdata_lst = []     (Service_Name,  converts_val, during_time, _, operator_val,     compare_time,  warnning_val, critical_val)  = trigg_val    convertsfunc =  Eval (converts_val, scope)     warnning_val = convertsfunc (warnning_val)     criticaL_val = convertsfunc (Critical_val)     datacate, host, plugin =  redis_key.split (':: ')     operatorfunc = getattr (operator, operator_ Val)     for cur_item in alarmtmplist:         cur_item = convertsfunc (cur_item[' data ' [' target '])          realdata_lst.append (Cur_item)     avg_realdata = sum (REALDATA_LST)/ Len (realdata_lst)     warnning_res = operatorfunc (avg_realdata, warnning_val )     critical_res = operatorfunc (Avg_realdata, critical_val)      msgtime = time.strftime ('%h:%m:%s ',  time.localtime ())      formats =  ' PLUGIN (%s)  duringtime (%s)  comparetimes (%s)  avg (%s)  operation (%s)  target (%s) '      if critical_res:        message = formats %   (Plugin, during_time, compare_time, avg_realdata, operator_val, critical_val)         res_msg = alarm_template %  (host,  ' Critical ',  errors, msgtime, message)         weixin_ Notify (RES_MSG)         return    if warnning_ res:        message = formats %  (plugin,  During_time, compare_time, avg_realdata, operator_val, warnning_val)          res_msg = alarm_template %  (host,  ' warnning ',  Errors,  msgtime, message)         weixin_notify (res_msg )          return 

Note: This file is an early warning System threshold processing interface file, passed over the parameters Converts_val may be str/int/float and other types of names, are built-in function name, in order not to pollute the current thread running environment with the same name built-in functions, define an empty scope, search in the A built-in function such as pure str/int/float is called in the __builtins__ of scope , and if undefined, the thread may not find a built-in function such as str/int/float the next time it runs.

This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1860640

Basic Primer _python-built-in functions. Best practices for eval built-in functions in operations development?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.