它说:AttributeError:'函数'对象没有属性'time_taken'

时间:2018-08-10 10:29:33

标签: python

class DiscoverVC: UIViewController {

     @IBOutlet weak var segmentedControl: UISegmentedControl!

     override func viewDidLoad() {
         super.viewDidLoad()
         segmentedControl.addUnderlineForSelectedSegment()
     }

     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         applyTheme()
     }
}

extension DiscoverVC {

    fileprivate func applyTheme() {
         segmentedControl.tintColor = Theme.current.tint
    }
}



extension UISegmentedControl{

func removeBorder() {
        let backgroundImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: self.bounds.size)
        self.setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default)
        self.setBackgroundImage(backgroundImage, for: .selected, barMetrics: .default)
        self.setBackgroundImage(backgroundImage, for: .highlighted, barMetrics: .default)

        let deviderImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: CGSize(width: 1.0, height: self.bounds.size.height))
        self.setDividerImage(deviderImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
        self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: Theme.current.segmentedControl_unselectedColor, NSAttributedStringKey.font: UIFont(name: Fonts.OpenSans_Regular, size: 13)!], for: .normal)
        self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: Theme.current.segmentedControl_tintColor, NSAttributedStringKey.font: UIFont(name: Fonts.OpenSans_Bold, size: 13)!], for: .selected)
    } 

    func addUnderlineForSelectedSegment() {
        removeBorder()
        let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)
        let underlineHeight: CGFloat = 2.0
        let underlineXPosition = CGFloat(selectedSegmentIndex * Int(underlineWidth))
        let underLineYPosition = self.bounds.size.height - 1.0
        let underlineFrame = CGRect(x: underlineXPosition, y: underLineYPosition, width: underlineWidth, height: underlineHeight)
        let underline = UIView(frame: underlineFrame)
        underline.backgroundColor = Theme.current.segmentedControl_tintColor
        underline.tag = 1
        self.addSubview(underline)
    }

    func changeUnderlinePosition() {
        guard let underline = self.viewWithTag(1) else {return}
        let underlineFinalXPosition = (self.bounds.width / CGFloat(self.numberOfSegments)) * CGFloat(selectedSegmentIndex)
        UIView.animate(withDuration: 0.1, animations: {
            underline.frame.origin.x = underlineFinalXPosition
        })
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将time_taken分配给包装函数,然后首先调用它才能访问time_taken变量

import time


def profile(function):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        ret_value = function(*args, **kwargs)
        end_time = time.time()
        wrapper.time_taken = end_time-start_time
        return ret_value

    return wrapper

"""
@profile
def calsqr(a,b):
    return a**b
"""
@profile
def expensive_operation():
   import time
   time.sleep(3)
   return 1


assert expensive_operation() == 1
print(expensive_operation.time_taken)