This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 周期数据统计函数 | |
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| 使用依赖注入重写我们的日志工厂 | |
| */ | |
| class Log { | |
| protected $engine = false; | |
| public function add($message) | |
| { | |
| if ($this->engine) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| 当你使用单例模式第一次调用对象时,它就会被实例化(称为延迟加载),之后每一次调用都将返回同一个对象。 | |
| */ | |
| class Database extends PDO { | |
| private static $_instance = null; | |
| private function __construct() | |
| { | |
| parent::__construct(APP_DB_DSN, APP_DB_USER, APP_DB_PASSWORD); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| 观察者模式的核心在于允许你的应用程序注册一个回调,当某个特定的事件发生时便会触发它。 | |
| */ | |
| class Event { | |
| static protected $callbacks = array(); | |
| static public function registerCallback($eventName, $callback) | |
| { | |
| if (!is_callable($callback)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type Stringer interface { | |
| String() string | |
| } | |
| type Printer interface { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type User struct { | |
| id int | |
| name string | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type FileObj struct { | |
| name string | |
| size int | |
| attr struct { | |
| perm int | |
| owner int | |
| } | |
| } | |
| func main() { |