Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Policy Mode

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Python strategy mode". In daily operation, I believe many people have doubts about how to use Python strategy mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Python strategy mode". Next, please follow the editor to study!

The first is the base class of the generator:

Class Generator: # initialization function # period: periodic def _ init__ (self, period): self.period= period # calculate the proportion of input values converted to the last cycle def rate (self, t): return (t-int (t / self.period) * self.period) / self.period # calculation output def output (self, t): pass

The generator base class Gererator defines the policy interface output, whose function is to calculate the corresponding output data according to the input time value. Because of the periodicity of the signal generator, in the actual processing, as long as the time value is converted to a period and then the result can be calculated, this conversion function is the rate method. Then there is the concrete strategy sine generator SinGerneator and the square wave generator SquareGenerator. The specific implementation is as follows:

Class SinGenerator (Generator): def _ init__ (self,period): Generator.__init__ (self,period) # Compute output def output (self, t): returnsin (2 * 3.1415926 * self.rate (t)) class SquareGenerator (Generator): def _ init__ (self,period): Generator.__init__ (self,period) # Compute output def output (self) T): if self.rate (t) < 0.5: return-1 else:

The two representation classes implement the output method to output sinusoidal data and square wave data respectively. In order to experience the function of the generator strategy, we designed the following Mini Program.

Def test_strategy (generator): result=''# output exactly one cycle for t in range (0,16): # call policy interface value= generator.output (t) # format output result= result +'{: + .2f}, '.format (round (value, 2)) print (result)

If _ _ name__ = ='_ main__': # uses sinusoidal strategy, period 20 test_strategy (SinGenerator (16)) # uses square wave strategy, period 20 test_strategy (SquareGenerator (16))

The code uses exactly the same logic, using two signal generators. The following is the result of its program execution:

+ 0.00meme "0.38meme" 0.71memoir "0.92memoir" 1.00meme "0.92meme" 0.1pyrrine "0.38pyrus" 0.00pyrum 0.38pence 0.71meme 0.92pyrus 1.00pyrus 0.1pyrus 0.1pyrus 1.00pyrus 1.00pyrus 1.00meme 1.00meme 1.00meme "1.00meme" 1.00meme "1.00meme" 1.00meme "1.00meme" + 1.00, 1.00, 1.00, 1.00, so far. The study on "how to use the Python strategy mode" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report