Python Slots
2021年4月11日Register here: http://gg.gg/p04ia
SlotMachine import randomprint(’’Welcome to the Slot Machine SimulatorYou’ll start with $50. You’ll be asked if you want to play.Answer with yes/no. you can also use y/nNo case sensitivity in your answer.For example you can answer with YEs, yEs, Y, nO, N.To win you must get one of the following combinations:BARtBARtBARttpayst$250BELLtBELLtBELL/BARtpayst$20PLUMtPLUMtPLUM/BARtpayst$14ORANGEtORANGEtORANGE/BARtpayst$10CHERRYtCHERRYtCHERRYttpayst$7CHERRYtCHERRYt -ttpayst$5CHERRYt -t -ttpayst$2’’)#Constants:INIT_STAKE = 50ITEMS = [’CHERRY’, ’LEMON’, ’ORANGE’, ’PLUM’, ’BELL’, ’BAR’]firstWheel = NonesecondWheel = NonethirdWheel = Nonestake = INIT_STAKEdef play(): global stake, firstWheel, secondWheel, thirdWheel playQuestion = askPlayer() while(stake != 0 and playQuestion True): firstWheel = spinWheel() secondWheel = spinWheel() thirdWheel = spinWheel() printScore() playQuestion = askPlayer()def askPlayer(): ’’ Asks the player if he wants to play again. expecting from the user to answer with yes, y, no or n No case sensitivity in the answer. yes, YeS, y, y, nO . . . all works ’’ global stake while(True): answer = input(’You have $’ + str(stake) + ’. Would you like to play? ’) answer = answer.lower() if(answer ’yes’ or answer ’y’): return True elif(answer ’no’ or answer ’n’): print(’You ended the game with $’ + str(stake) + ’ in your hand.’) return False else: print(’wrong input!’)def spinWheel(): ’’ returns a random item from the wheel ’’ randomNumber = random.randint(0, 5) return ITEMS[randomNumber]def printScore(): ’’ prints the current score ’’ global stake, firstWheel, secondWheel, thirdWheel if((firstWheel ’CHERRY’) and (secondWheel != ’CHERRY’)): win = 2 elif((firstWheel ’CHERRY’) and (secondWheel ’CHERRY’) and (thirdWheel != ’CHERRY’)): win = 5 elif((firstWheel ’CHERRY’) and (secondWheel ’CHERRY’) and (thirdWheel ’CHERRY’)): win = 7 elif((firstWheel ’ORANGE’) and (secondWheel ’ORANGE’) and ((thirdWheel ’ORANGE’) or (thirdWheel ’BAR’))): win = 10 elif((firstWheel ’PLUM’) and (secondWheel ’PLUM’) and ((thirdWheel ’PLUM’) or (thirdWheel ’BAR’))): win = 14 elif((firstWheel ’BELL’) and (secondWheel ’BELL’) and ((thirdWheel ’BELL’) or (thirdWheel ’BAR’))): win = 20 elif((firstWheel ’BAR’) and (secondWheel ’BAR’) and (thirdWheel ’BAR’)): win = 250 else: win = -1 stake += win if(win > 0): print(firstWheel + ’t’ + secondWheel + ’t’ + thirdWheel + ’ -- You win $’ + str(win)) else: print(firstWheel + ’t’ + secondWheel + ’t’ + thirdWheel + ’ -- You lose’)play()
*Very simple and intuitive. For more information on Python decorators, you might want to checkout the article - Python Decorators Overview to familiarise yourself. Finally, let’s instantiate a Circle, hook up the signals to the slots, and move and resize it.
*Command have changed argument spec (Read Upgrading.rst for details). Fixed a bug when hashing the key it if was a python 3 byte string and it would cause it to route to wrong slot in the cluster (fossilet, Grokzen). Fixed a bug when reinitialize the nodemanager it would use the old nodescache instead of the new one that was just parsed (monklof). commented Dec 14, 2015
Instead of;if(answer ’yes’ or answer ’y’):
Do;if answer.lower() in [’yes’,y’] commented Jun 2, 2017
Hashes for dataslots-1.0.2-py2.py3-none-any.whl; Algorithm Hash digest; SHA256: 4fe302ab59c86e01a4fafe516776a198cd8a42dc696dcc9d525e2ec8ee0fe773: Copy.
I run it on python 2 ,it’s need to modify the 43 line (input -> raw_input)Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to commentPython老鸟都应该看过那篇非常有吸引力的 Saving 9 GB of RAM with Python’s __slots__ 文章,作者使用了__slots__让内存占用从25.5GB降到了16.2GB。在当时来说,这相当于用一个非常简单的方式就降低了30%的内存使用,着实惊人。作者并没有提到他的业务特点和代码,那我们就基于《fluent python》中的例子来验证下是不是有这么厉害:
我们分别跑一下这2个类:
2种方法初始内存略有差别,但是由于这个差别和总内存量相比太小而忽略不计,结论就是:Python Slots Performance使用slots可以让内存使用减少3.5倍!!# 通过 (200 - 4) / ((60 - 4) * 1.0) 计算得来
那么用slot就是非非常那个有必要吗?事实上500000个实例这种机会非常少见,用不用完全根据业务来决定,并不要以偏概全。因为(敲黑板了哈)使用__slots__也是有副作用的:
第三点有点难理解,我写个例子看看吧:
所以实例不超过万级别的类,__slots__是不太值得使用的。Pyside2 Signal SlotSlot Machine In Python
PS:《fluent python》比我狠,说的是小于百万级别实例不值得使用。
无耻的广告:《Python Web开发实战》上市了!Python Slots Machine
欢迎关注本人的微信公众号获取更多Python相关的内容(也可以直接搜索「Python之美」):
Register here: http://gg.gg/p04ia
https://diarynote-jp.indered.space
SlotMachine import randomprint(’’Welcome to the Slot Machine SimulatorYou’ll start with $50. You’ll be asked if you want to play.Answer with yes/no. you can also use y/nNo case sensitivity in your answer.For example you can answer with YEs, yEs, Y, nO, N.To win you must get one of the following combinations:BARtBARtBARttpayst$250BELLtBELLtBELL/BARtpayst$20PLUMtPLUMtPLUM/BARtpayst$14ORANGEtORANGEtORANGE/BARtpayst$10CHERRYtCHERRYtCHERRYttpayst$7CHERRYtCHERRYt -ttpayst$5CHERRYt -t -ttpayst$2’’)#Constants:INIT_STAKE = 50ITEMS = [’CHERRY’, ’LEMON’, ’ORANGE’, ’PLUM’, ’BELL’, ’BAR’]firstWheel = NonesecondWheel = NonethirdWheel = Nonestake = INIT_STAKEdef play(): global stake, firstWheel, secondWheel, thirdWheel playQuestion = askPlayer() while(stake != 0 and playQuestion True): firstWheel = spinWheel() secondWheel = spinWheel() thirdWheel = spinWheel() printScore() playQuestion = askPlayer()def askPlayer(): ’’ Asks the player if he wants to play again. expecting from the user to answer with yes, y, no or n No case sensitivity in the answer. yes, YeS, y, y, nO . . . all works ’’ global stake while(True): answer = input(’You have $’ + str(stake) + ’. Would you like to play? ’) answer = answer.lower() if(answer ’yes’ or answer ’y’): return True elif(answer ’no’ or answer ’n’): print(’You ended the game with $’ + str(stake) + ’ in your hand.’) return False else: print(’wrong input!’)def spinWheel(): ’’ returns a random item from the wheel ’’ randomNumber = random.randint(0, 5) return ITEMS[randomNumber]def printScore(): ’’ prints the current score ’’ global stake, firstWheel, secondWheel, thirdWheel if((firstWheel ’CHERRY’) and (secondWheel != ’CHERRY’)): win = 2 elif((firstWheel ’CHERRY’) and (secondWheel ’CHERRY’) and (thirdWheel != ’CHERRY’)): win = 5 elif((firstWheel ’CHERRY’) and (secondWheel ’CHERRY’) and (thirdWheel ’CHERRY’)): win = 7 elif((firstWheel ’ORANGE’) and (secondWheel ’ORANGE’) and ((thirdWheel ’ORANGE’) or (thirdWheel ’BAR’))): win = 10 elif((firstWheel ’PLUM’) and (secondWheel ’PLUM’) and ((thirdWheel ’PLUM’) or (thirdWheel ’BAR’))): win = 14 elif((firstWheel ’BELL’) and (secondWheel ’BELL’) and ((thirdWheel ’BELL’) or (thirdWheel ’BAR’))): win = 20 elif((firstWheel ’BAR’) and (secondWheel ’BAR’) and (thirdWheel ’BAR’)): win = 250 else: win = -1 stake += win if(win > 0): print(firstWheel + ’t’ + secondWheel + ’t’ + thirdWheel + ’ -- You win $’ + str(win)) else: print(firstWheel + ’t’ + secondWheel + ’t’ + thirdWheel + ’ -- You lose’)play()
*Very simple and intuitive. For more information on Python decorators, you might want to checkout the article - Python Decorators Overview to familiarise yourself. Finally, let’s instantiate a Circle, hook up the signals to the slots, and move and resize it.
*Command have changed argument spec (Read Upgrading.rst for details). Fixed a bug when hashing the key it if was a python 3 byte string and it would cause it to route to wrong slot in the cluster (fossilet, Grokzen). Fixed a bug when reinitialize the nodemanager it would use the old nodescache instead of the new one that was just parsed (monklof). commented Dec 14, 2015
Instead of;if(answer ’yes’ or answer ’y’):
Do;if answer.lower() in [’yes’,y’] commented Jun 2, 2017
Hashes for dataslots-1.0.2-py2.py3-none-any.whl; Algorithm Hash digest; SHA256: 4fe302ab59c86e01a4fafe516776a198cd8a42dc696dcc9d525e2ec8ee0fe773: Copy.
I run it on python 2 ,it’s need to modify the 43 line (input -> raw_input)Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to commentPython老鸟都应该看过那篇非常有吸引力的 Saving 9 GB of RAM with Python’s __slots__ 文章,作者使用了__slots__让内存占用从25.5GB降到了16.2GB。在当时来说,这相当于用一个非常简单的方式就降低了30%的内存使用,着实惊人。作者并没有提到他的业务特点和代码,那我们就基于《fluent python》中的例子来验证下是不是有这么厉害:
我们分别跑一下这2个类:
2种方法初始内存略有差别,但是由于这个差别和总内存量相比太小而忽略不计,结论就是:Python Slots Performance使用slots可以让内存使用减少3.5倍!!# 通过 (200 - 4) / ((60 - 4) * 1.0) 计算得来
那么用slot就是非非常那个有必要吗?事实上500000个实例这种机会非常少见,用不用完全根据业务来决定,并不要以偏概全。因为(敲黑板了哈)使用__slots__也是有副作用的:
第三点有点难理解,我写个例子看看吧:
所以实例不超过万级别的类,__slots__是不太值得使用的。Pyside2 Signal SlotSlot Machine In Python
PS:《fluent python》比我狠,说的是小于百万级别实例不值得使用。
无耻的广告:《Python Web开发实战》上市了!Python Slots Machine
欢迎关注本人的微信公众号获取更多Python相关的内容(也可以直接搜索「Python之美」):
Register here: http://gg.gg/p04ia
https://diarynote-jp.indered.space
コメント