I am experimenting memory_profiler in Python3 following
https://medium.com/zendesk-engineering/hunting-for-memory-leaks-in-python-applications-6824d0518774
thanks to @eyllanesc here PyQt5 designer gui and iterate/loop over QPushButton [duplicate]
I just created a mainwindow with 21 buttons a to z ; each time a press one of them I print the letter they represent.
While reading: Using lambda expression to connect slots in pyqt I came across:
" Beware! As soon as you connect your signal to a lambda slot with a reference to self, your widget will not be garbage-collected! That's because lambda creates a closure with yet another uncollectable reference to the widget.
Thus, self.someUIwidget.someSignal.connect(lambda p: self.someMethod(p)) is very evil :) "
Here my plot:
while pressing buttons.
Does my plot shows this behaviour ?? Or is it a straight line that doent looks straight ?
What is an alternative then? to my:
use for letter in "ABCDE": getattr(self, letter).clicked.connect(lambda checked, letter=letter: foo(letter))
main.py : hangman_pyqt5-muppy.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 5 19:21:27 2020
@author: Pietro
"""
import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QDesktopWidget
import hangman005
#from pympler import muppy, summary #########################################
#
#from pympler import tracker
#import resource
WORDLIST_FILENAME = "words.txt"
wordlist = hangman005.load_words(WORDLIST_FILENAME)
def main():
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
class MainMenu(QtWidgets.QMainWindow):
def __init__(self):
super(MainMenu, self).__init__()
uic.loadUi('main_window2.ui', self)
# self.ButtonQ.clicked.connect(self.QPushButtonQPressed)
self.centro = center(self)
self.centro
# self.show()
self.hangman()
def closeEvent(self, event): #Your desired functionality here
close = QtWidgets.QMessageBox.question(self,
"QUIT",
"Are you sure want to stop process?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if close == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def printo(self, i):
print('oooooooooooooooooooooo :', i)
# all_objects = muppy.get_objects()
# sum1 = summary.summarize(all_objects)# Prints out a summary of the large objects
# summary.print_(sum1) from pympler import tracker
#
#
# self.memory_tracker = tracker.SummaryTracker()
# self.memory_tracker.print_diff()
def hangman(self):
letters_guessed=[]
max_guesses = 6
secret_word = hangman005.choose_word(wordlist)
secret_word_lenght=len(secret_word)
secret_word=hangman005.splitt(secret_word)
vowels=('a','e','i','o','u')
secret_word_print=('_ '*secret_word_lenght )
self.word_to_guess.setText(secret_word_print )
letters=hangman005.splitt('ABCDEFGHIJKLMNOPQRSTYVWXXYZ')
print(letters )
for i in letters:
print('lllllllllllllll : ' ,i)
# button = "self.MainMenu."+i
# self.[i].clicked.connect(self.printo(i))
# button = getattr(self, i)
# button.clicked.connect((lambda : self.printo(i for i in letters) ) )
# button.clicked.connect(lambda j=self.printo(i) : j )
# button.clicked.connect(lambda : self.printo(i))
# button.clicked.connect(lambda: self.printo(i))
# button.clicked.connect(lambda j=self.printo(i) : j for i in letters )
# getattr(self, i).clicked.connect(lambda checked, i=i: self.printo(i))
# getattr(self, i).clicked.connect(lambda checked, j=i: self.printo(j))
getattr(self, i).clicked.connect(lambda pippo, j=i: self.printo(j))
# self.A.clicked.connect(self.printo)
# Add to leaky code within python_script_being_profiled.py
# Get references to certain types of objects such as dataframe
# dataframes = [ao for ao in all_objects if isinstance(ao, pd.DataFrame)]
#
# for d in dataframes:
# print (d.columns.values)
# print (len(d))
app = QtWidgets.QApplication(sys.argv)
# sshFile="coffee.qss"
# with open(sshFile,"r") as fh:
# app.setStyleSheet(fh.read())
window=MainMenu()
window.show()
app.exec_()
#all_objects = muppy.get_objects()
#sum1 = summary.summarize(all_objects)# Prints out a summary of the large objects
#summary.print_(sum1)# Get references to certain types of objects such as dataframe
if __name__ == '__main__':
main()
hangman005.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 29 19:36:32 2020
@author: Pietro
"""
import random
import string
#WORDLIST_FILENAME = "words.txt"
def load_words(WORDLIST_FILENAME):
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r')
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = line.split()
print(" ", len(wordlist), "words loaded.")
# print(line)
# for elem in line:
# print (elem)
# print(wordlist)
# for elem in wordlist:
# print ('
' , elem)
return wordlist
def choose_word(wordlist):
"""
wordlist (list): list of words (strings)
Returns a word from wordlist at random
"""
return random.choice(wordlist)
# end of helper code
# -----------------------------------
# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
#wordlist = load_words()
def splitt(word):
return [char for char in word]
def is_word_guessed(secret_word, letters_guessed):
'''
secret_word: string, the word the user is guessing; assumes all letters are
lowercase
letters_guessed: list (of letters), which letters have been guessed so far;
assumes that all letters are lowercase
returns: boolean, True if all the letters of secret_word are in letters_guessed;
False otherwise
'''
prova =all(item in letters_guessed for item in secret_word )
print(' prova : ' , prova )
return prova
#secret_word='popoli'
#letters_guessed=['p','o','p','o','l','i']
#letters_guessed=['p','i','l']
#print('
is_word_guessed : ', is_word_guessed(secret_word,letters_guessed))
#letters_guessed = []
def get_guessed_word(secret_word, letters_guessed):
'''
secret_word: string, the word the user is guessing
letters_guessed: list (of letters), which letters have been guessed so far
returns: string, comprised of letters, underscores (_), and spaces that represents
which letters in secret_word have been guessed so far.
'''
print('
secret_word_split' , secret_word)
print('letters_guessed', letters_guessed )
results=[]
for val in range(0,len(secret_word)):
if secret_word[val] in letters_guessed:
results.append(secret_word[val])
else:
results.append('_')
print('
results : ' , ' '.join(results ))
return results
def get_available_letters(letters_guessed):
'''secret_word_split
letters_guessed: list (of letters), which letters have been guessed so far
returns: string (of letters), comprised of letters that represents which letters have not
yet been guessed.
'''
entire_letters='abcdefghijklmnopqrstuvwxyz'
entire_letters_split=splitt(entire_letters)
entire_letters_split = [x for x in entire_letters_split if x not in letters_guessed]
return entire_letters_split
def hangman(secret_word):
'''
secret_word: string, the secret word to guess.
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secret_word contains and how many guesses s/he starts with.
* The user should start with 6 guesses
* Before each round, you should display to the user how many guesses
s/he has left and the letters that the user has not yet guessed.
* Ask the user to supply one guess per round. Remember to make
sure that the user puts in a letter!
* The user should receive feedback immediately after each guess
about whether their guess appears in the computer's word.
* After each guess, you should display to the user the
partially guessed word so far.
secret_word_split
Follows the other limitations detailed in the problem write-up.
'''
letters_guessed=[]
max_guesses = 6
secret_word_lenght=len(secret_word)
secret_word=splitt(secret_word)
vowels=('a','e','i','o','u')
print('
Welcome to HANGMAN ;-) ')
print('
secret_word_lenght : ' , secret_word_lenght )
print('
'+' _ '*secret_word_lenght )
print('
you have ' , max_guesses , ' guesses be carefull choosing')
while True:
guess= input('
make your first choice : ' )
if guess not in get_available_letters(letters_guessed):
print('You can only choose in' , ' '.join(get_available_letters(letters_guessed)))
continue
if guess in get_available_letters(letters_guessed):
letters_guessed.append(guess)
# print('
letters_guessed appended : ' , ' '.join(letters_guessed) )
# max_guesses -= 1
print(' a che punto sei : ' , ' '.join(get_guessed_word(secret_word, letters_guessed)))
# print('
you have ' , max_guesses , ' guesses be carefull choosing')
if guess in secret_word:
print('GOOD !!!!!!!!!!!!!')
print('
you still have ' , max_guesses , ' guesses be carefull choosing')
else:
print('ERRORE !!!!!!!!!!!!!!!!!!!!!!')
if guess in vowels:
max_guesses -= 2
else:
max_guesses -= 1
print('
now you