Skip to content

Instantly share code, notes, and snippets.

View kitelife's full-sized avatar
💪
I may be slow to respond.

xiayf kitelife

💪
I may be slow to respond.
View GitHub Profile
# 周期数据统计函数
def stat_cycle_data(cycles):
cycle_time_buckets = {}
for cycle in cycles:
if cycle.start_date:
start_date = (
int(cycle.start_date.strftime('%Y')), int(cycle.start_date.strftime('%m')))
else:
continue
if cycle.end_date:
@kitelife
kitelife / di-example.php
Last active August 29, 2015 13:57
依赖注入
<?php
/*
使用依赖注入重写我们的日志工厂
*/
class Log {
protected $engine = false;
public function add($message)
{
if ($this->engine) {
<?php
class Log_Factory {
public function getLog($type = 'file', array $options)
{
$type = strtolower($type);
$class = "Log_" . ucfirst($type);
require_once str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
$log = new $class($options);
<?php
/*
当你使用单例模式第一次调用对象时,它就会被实例化(称为延迟加载),之后每一次调用都将返回同一个对象。
*/
class Database extends PDO {
private static $_instance = null;
private function __construct()
{
parent::__construct(APP_DB_DSN, APP_DB_USER, APP_DB_PASSWORD);
@kitelife
kitelife / observer-example.php
Last active August 29, 2015 13:57
观察者模式
<?php
/*
观察者模式的核心在于允许你的应用程序注册一个回调,当某个特定的事件发生时便会触发它。
*/
class Event {
static protected $callbacks = array();
static public function registerCallback($eventName, $callback)
{
if (!is_callable($callback)) {
<?php
try {
$db_conn = new PDO('mysql:host=localhost;dbname=recipes', 'php-user', 'secret');
} catch (PDOException $e) {
echo "Could not connect to database";
exit;
}
try {
// start the transaction
@kitelife
kitelife / PDO_usage.php
Last active August 29, 2015 13:57
PDO Usage sample
<?php
try {
$db_conn = new PDO('mysql:host=localhost;dbname=recipes', 'php-user', 'secret');
} catch (PDOException $e) {
echo "Could not connect to database";
exit;
}
$sql = 'SELECT name, description, chef
FROM receipes
@kitelife
kitelife / about-interface.go
Created March 9, 2014 13:40
about interface in go
package main
import (
"fmt"
)
type Stringer interface {
String() string
}
type Printer interface {
@kitelife
kitelife / like-override.go
Created March 9, 2014 13:06
Like override
package main
import (
"fmt"
)
type User struct {
id int
name string
}
type FileObj struct {
name string
size int
attr struct {
perm int
owner int
}
}
func main() {