SHARE
TWEET

Untitled

a guest May 26th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3.  
  4. class Bucket(object):
  5.     """
  6.     一个桶,实验的组成部分,比如说 对照桶,实验桶
  7.     """
  8.  
  9.     def __init__(self, is_duizhao, dataset, bucket_num):
  10.         """
  11.         初始化一个实验桶
  12.         :param is_duizhao: 该桶是否是对照桶
  13.         :param dataset: 该桶的数据
  14.         :param bucket_num: 该桶的编号
  15.         """
  16.         self.is_duizhao = is_duizhao
  17.         self.dataset = dataset
  18.         self.bucket_num = bucket_num
  19.  
  20.     def describe(self):
  21.         """
  22.         这个桶可以做些事情
  23.         :return:
  24.         """
  25.         print("I am no. {0}, I am{1}duizhao bucket".format(self.bucket_num, (" " if self.is_duizhao else " not ")))
  26.  
  27.  
  28. class Experiment(object):
  29.     """
  30.     一次实验,包含多个实验桶
  31.     """
  32.  
  33.     def __init__(self, experiment_num, *buckets):
  34.         """
  35.         一个实验,包含实验编号,以及若干个实验桶
  36.         :param experiment_num: 该实验的实验编号
  37.         :param buckets: 该实验的实验桶 list
  38.         """
  39.         self.experiment_num = experiment_num
  40.         self.duizhao_bucket = None
  41.         self.experiment_buckets = list()
  42.         for bucket in buckets: # 遍历所有的bucket,将对照组取出
  43.             if bucket.is_duizhao:
  44.                 self.duizhao_bucket = bucket
  45.             else:
  46.                 self.experiment_buckets.append(bucket)
  47.  
  48.         if self.duizhao_bucket is None: # 如果没有找到对照组,抛出错误
  49.             raise ValueError("buckets contains no duizhao bucket")
  50.  
  51.         self.experiment_buckets_length = len(self.experiment_buckets)
  52.  
  53.         if self.experiment_buckets_length < 1: # 如果没有找到实验组,抛出错误
  54.             raise ValueError("buckets contains 1 or more experiment buckets")
  55.  
  56.     def describe(self):
  57.         print("I have 1 duizhao bucket and {0} experiment buckets".format(self.experiment_buckets_length))
  58.  
  59.     def do_something(self):
  60.         """
  61.         这里面可以循环的拿出来,将实验桶与对照桶比较
  62.         :return:
  63.         """
  64.         for bucket in self.experiment_buckets:
  65.             print("I am bucket no.{}, and my bucket number minus duizhao bucket number is {}"
  66.                   .format(bucket.bucket_num, bucket.bucket_num - self.duizhao_bucket.bucket_num))
  67.  
  68.  
  69. if __name__ == "__main__":
  70.     bucket1 = Bucket(True, None, 0)
  71.     bucket2 = Bucket(False, None, 1)
  72.     bucket3 = Bucket(False, None, 2)
  73.  
  74.     experiment = Experiment(0, bucket1, bucket2, bucket3)
  75.  
  76.     experiment.do_something()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top