我有一个函数可以编写各种格式的python列表。 <?php
/*
Class to send push notifications using Google Cloud Messaging for Android
Example usage
-----------------------
$an = new GCMPushMessage($apiKey);
$an->setDevices($devices);
$response = $an->send($message);
-----------------------
$apiKey Your GCM api key
$devices An array or string of registered device tokens
$message The mesasge you want to push out
@author Matt Grundy
Adapted from the code available at:
http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging
*/
class GCMPushMessage {
var $url = 'https://android.googleapis.com/gcm/send';
var $serverApiKey = "";
var $devices = array();
/*
Constructor
@param $apiKeyIn the server API key
*/
function GCMPushMessage($apiKeyIn){
$this->serverApiKey = $apiKeyIn;
}
/*
Set the devices to send to
@param $deviceIds array of device tokens to send to
*/
function setDevices($deviceIds){
if(is_array($deviceIds)){
$this->devices = $deviceIds;
} else {
$this->devices = array($deviceIds);
}
}
/*
Send the message to the device
@param $message The message to send
@param $data Array of data to accompany the message
*/
function send($message, $data = false){
if(!is_array($this->devices) || count($this->devices) == 0){
$this->error("No devices set");
}
if(strlen($this->serverApiKey) < 8){
$this->error("Server API Key not set");
}
$fields = array(
'registration_ids' => $this->devices,
'data' => array( "message" => $message ),
);
if(is_array($data)){
foreach ($data as $key => $value) {
$fields['data'][$key] = $value;
}
}
$headers = array(
'Authorization: key=' . $this->serverApiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $this->url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Avoids problem with https certificate
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
function error($msg){
echo "Android send notification failed with error:";
echo "\t" . $msg;
exit(1);
}
}
?>
是我想写的格式之一。我知道我使用内置的csv
Python模块,但我的函数目前以下列方式编写:
csv
我可以通过执行以下操作来检查csv.writer对象的类型:
def foo(self, save_to=None):
if save_to is None:
print self.my_list
如果writer = csv.writer(open('test.csv','w'))
writer
<_csv.writer at 0x7f278125aca8>
type(writer)
_csv.writer
是save_to
对象,如何查看我的函数?通常我会在函数函数中使用csv
,如下所示:
isinstance
但我得到def foo(self, save_to=None):
if save_to is None:
print self.my_list
if isinstance(save_to, _csv.writer):
save_to.writerow([self.my_list])
和NameError
AttrubuteError
答案 0 :(得分:1)
您不应在此处使用isinstance
检查(尤其不要使用_csv
等私有模块中的类:isinstance
调用一般违反OOP。更具体的Python,它的“duck-typing”意味着你应该尝试使用方法并捕获属性错误:如果它像鸭子一样嘎嘎叫,它就像鸭子一样 - 如果它像csv编写器那样实现writerow
,这是一个csv作家。
try:
save_to.writerow([self.my_list])
except AttributeError:
# not a csv writer, do something else
更具体地说,您获得详细错误的原因是因为未导入子模块。