From d239f28aa92bef337b7b68e0021ae7eb270db290 Mon Sep 17 00:00:00 2001 From: jejellyroll-fr <47847785+jejellyroll-fr@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:54:04 +0200 Subject: [PATCH] cleaning code fstring --- Mucked.py | 189 ++++++++++++++++++++++++++---------------------------- 1 file changed, 90 insertions(+), 99 deletions(-) diff --git a/Mucked.py b/Mucked.py index ef6db82..07c5377 100644 --- a/Mucked.py +++ b/Mucked.py @@ -44,37 +44,31 @@ # FreePokerTools modules import Card import Aux_Base +# Utility routine to get the number of valid cards in the card tuple - -# Utility routine to get the number of valid cards in the card tuple def valid_cards(ct): - n = 0 - for c in ct: - if c != 0: - n += 1 - return n + return sum(c != 0 for c in ct) class Stud_mucked(Aux_Base.Aux_Window): def __init__(self, hud, config, params): - self.hud = hud # hud object that this aux window supports - self.config = config # configuration object for this aux window to use - self.params = params # hash aux params from config + self.hud = hud # hud object that this aux window supports + self.config = config # configuration object for this aux window to use + self.params = params # hash aux params from config try: site_params = self.config.get_site_parameters(self.hud.site) self.hero = site_params['screen_name'] - except: + except Exception: self.hero = '' - self.mucked_list = Stud_list(self, params, config, self.hero) - self.mucked_cards = Stud_cards(self, params, config) + self.mucked_list = Stud_list(self, params, config, self.hero) + self.mucked_cards = Stud_cards(self, params, config) self.mucked_list.mucked_cards = self.mucked_cards def create(self): - self.container = QWidget() self.vbox = QVBoxLayout() self.container.setLayout(self.vbox) @@ -85,25 +79,25 @@ def create(self): self.container.show() def update_data(self, new_hand_id, db_connection): -# uncomment next line when action is available in the db -# self.mucked_cards.update_data(new_hand_id, db_connection) + # uncomment next line when action is available in the db + # self.mucked_cards.update_data(new_hand_id, db_connection) self.mucked_list.update_data(new_hand_id, db_connection) def update_gui(self, new_hand_id): self.mucked_cards.update_gui(new_hand_id) self.mucked_list.update_gui(new_hand_id) - + + class Stud_list(object): def __init__(self, parent, params, config, hero): - self.parent = parent - self.params = params - self.config = config - self.hero = hero + self.parent = parent + self.params = params + self.config = config + self.hero = hero def create(self, container): - self.container = container - + self.container = container self.treeview = QTableView() self.liststore = QStandardItemModel(0, 4, self.treeview) self.treeview.setModel(self.liststore) @@ -120,10 +114,10 @@ def update_data(self, new_hand_id, db_connection): winners = '' for player in list(self.winners.keys()): pot = pot + int(self.winners[player]) - if not winners == '': - winners = winners + ", " + if winners != '': + winners = f"{winners}, " winners = winners + player - pot_dec = "%.2f" % (old_div(float(pot),100)) + pot_dec = "%.2f" % (old_div(float(pot), 100)) hero_cards = self.get_hero_cards(self.parent.hero) self.info_row = ((new_hand_id, hero_cards, pot_dec, winners), ) @@ -132,26 +126,29 @@ def get_hero_cards(self, hero): """Formats the hero cards for inclusion in the table.""" if hero == '': return "xxxxxx" - else: - # find the hero's seat from the stat_dict - for stat in list(self.parent.hud.stat_dict.values()): - if stat['screen_name'] == hero: - return Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][0]) +\ - Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][1]) +\ - Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][2]) - return "xxxxxx" + return next( + ( + Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][0]) + + Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][1]) + + Card.valueSuitFromCard(self.parent.hud.cards[stat['seat']][2]) + for stat in list(self.parent.hud.stat_dict.values()) + if stat['screen_name'] == hero + ), + "xxxxxx", + ) def update_gui(self, new_hand_id): self.liststore.appendRow(list(map(QStandardItem, self.info_row[0]))) self.treeview.resizeColumnsToContents() self.treeview.horizontalHeader().setStretchLastSection(True) + class Stud_cards(object): def __init__(self, parent, params, config): - self.parent = parent - self.params = params - self.config = config + self.parent = parent + self.params = params + self.config = config self.card_images = self.parent.hud.parent.deck.get_all_card_images() self.grid_contents = {} @@ -161,32 +158,32 @@ def __init__(self, parent, params, config): self.cols = 7 def create(self, container): - self.container = container + self.container = container self.grid = QGridLayout() - for r in range(0, self.rows): - for c in range(0, self.cols): + for r in range(self.rows): + for c in range(self.cols): # Start by creating a box of nothing but card backs self.eb[(c, r)]= QLabel() self.eb[(c, r)].setPixmap(self.card_images[0]) # set up the contents for the cells - for r in range(0, self.rows): - self.grid_contents[( 0, r)] = QLabel("%d" % (r + 1)) - self.grid_contents[( 1, r)] = QLabel("player %d" % (r + 1)) - self.grid_contents[( 4, r)] = QLabel("-") - self.grid_contents[( 9, r)] = QLabel("-") - self.grid_contents[( 2, r)] = self.eb[( 0, r)] - self.grid_contents[( 3, r)] = self.eb[( 1, r)] - self.grid_contents[( 5, r)] = self.eb[( 2, r)] - self.grid_contents[( 6, r)] = self.eb[( 3, r)] - self.grid_contents[( 7, r)] = self.eb[( 4, r)] - self.grid_contents[( 8, r)] = self.eb[( 5, r)] - self.grid_contents[(10, r)] = self.eb[( 6, r)] + for r in range(self.rows): + self.grid_contents[(0, r)] = QLabel("%d" % (r + 1)) + self.grid_contents[(1, r)] = QLabel("player %d" % (r + 1)) + self.grid_contents[(4, r)] = QLabel("-") + self.grid_contents[(9, r)] = QLabel("-") + self.grid_contents[(2, r)] = self.eb[(0, r)] + self.grid_contents[(3, r)] = self.eb[(1, r)] + self.grid_contents[(5, r)] = self.eb[(2, r)] + self.grid_contents[(6, r)] = self.eb[(3, r)] + self.grid_contents[(7, r)] = self.eb[(4, r)] + self.grid_contents[(8, r)] = self.eb[(5, r)] + self.grid_contents[(10, r)] = self.eb[(6, r)] # add the cell contents to the table - for c in range(0, self.cols + 4): - for r in range(0, self.rows): + for c in range(self.cols + 4): + for r in range(self.rows): self.grid.addWidget(self.grid_contents[(c, r)], r, c) self.container.addLayout(self.grid) @@ -200,10 +197,10 @@ def update_data(self, new_hand_id, db_connection): for act in street: temp = temp + act[0] + " " + act[1] + "s " if act[2] > 0: - if act[2]%100 > 0: - temp = temp + "%4.2f\n" % (old_div(float(act[2]),100)) + if act[2] % 100 > 0: + temp = temp + "%4.2f\n" % (old_div(float(act[2]), 100)) else: - temp = temp + "%d\n" % (old_div(act[2],100)) + temp = temp + "%d\n" % (old_div(act[2], 100)) else: temp = temp + "\n" self.tips.append(temp) @@ -215,29 +212,27 @@ def update_gui(self, new_hand_id): self.grid_contents[(1, c - 1)].setText(self.get_screen_name(c)) for i in ((0, cards[0]), (1, cards[1]), (2, cards[2]), (3, cards[3]), (4, cards[4]), (5, cards[5]), (6, cards[6])): - if not i[1] == 0: + if i[1] != 0: # Pixmaps are stored in dict with rank+suit keys (_rank, _suit) = Card.valueSuitFromCard(i[1]) _rank = Card.card_map[_rank] self.eb[(i[0], c - 1)].setPixmap(self.card_images[_suit][_rank]) - # action in tool tips for 3rd street cards - for c in (0, 1, 2): - for r in range(0, self.rows): - #self.eb[(c, r)].set_tooltip_text(self.tips[0]) - pass - # action in tools tips for later streets round_to_col = (0, 3, 4, 5, 6) - #for round in range(1, len(self.tips)): + # for round in range(1, len(self.tips)): # for r in range(0, self.rows): # self.eb[(round_to_col[round], r)].set_tooltip_text(self.tips[round]) def get_screen_name(self, seat_no): """Gets and returns the screen name from stat_dict, given seat number.""" - for k in list(self.parent.hud.stat_dict.keys()): - if self.parent.hud.stat_dict[k]['seat'] == seat_no: - return self.parent.hud.stat_dict[k]['screen_name'] - return ("No Name") + return next( + ( + self.parent.hud.stat_dict[k]['screen_name'] + for k in list(self.parent.hud.stat_dict.keys()) + if self.parent.hud.stat_dict[k]['seat'] == seat_no + ), + "No Name", + ) def clear(self): for r in range(0, self.rows): @@ -246,6 +241,7 @@ def clear(self): # Start by creating a box of nothing but card backs self.eb[(c, r)].setPixmap(self.card_images[0]) + class Flop_Mucked(Aux_Base.Aux_Seats, QObject): """Aux_Window class for displaying mucked cards for flop games.""" @@ -258,11 +254,11 @@ def __init__(self, hud, config, params): self.uses_timer = True # this Aux_seats object uses a timer to control hiding def create_common(self, x, y): - "Create the window for the board cards and do the initial population." + """Create the window for the board cards and do the initial population.""" w = self.aw_class_window(self, "common") self.positions["common"] = self.create_scale_position(x, y) - w.move(self.positions["common"][0]+ self.hud.table.x, - self.positions["common"][1]+ self.hud.table.y) + w.move(self.positions["common"][0] + self.hud.table.x, + self.positions["common"][1] + self.hud.table.y) if 'opacity' in self.params: w.setWindowOpacity(float(self.params['opacity'])) return w @@ -280,12 +276,10 @@ def create_contents(self, container, i): # # Thus the individual hands (cards for seat) are tuples def update_contents(self, container, i): - if type(i) is int: - hist_seat = self.hud.layout.hh_seats[i] - else: - hist_seat = i - if hist_seat not in self.hud.cards: return - + hist_seat = self.hud.layout.hh_seats[i] if type(i) is int else i + if hist_seat not in self.hud.cards: + return + cards = self.hud.cards[hist_seat] # Here we want to know how many cards the given seat showed; # board is considered a seat, and has the id 'common' @@ -297,7 +291,7 @@ def update_contents(self, container, i): scratch = QPixmap(int(self.card_width) * n_cards, int(self.card_height)) painter = QPainter(scratch) - x = 0 # x coord where the next card starts in scratch + x = 0 # x coord where the next card starts in scratch for card in cards: # concatenate each card image to scratch # flop game never(?) has unknown cards. @@ -312,38 +306,32 @@ def update_contents(self, container, i): px = self.card_images[_suit][_rank] painter.drawPixmap(x, 0, px) x += px.width() - + painter.end() if container is not None: container.seen_cards.setPixmap(scratch) - container.resize(1,1) + container.resize(1, 1) container.move(self.positions[i][0] + self.hud.table.x, self.positions[i][1] + self.hud.table.y) # here is where I move back container.show() self.displayed = True - if i != "common": - id = self.get_id_from_seat(i) - if id is not None: - self.m_windows[i].setToolTip(self.hud.stat_dict[id]['screen_name']) + if i != "common" and self.get_id_from_seat(i) is not None: + self.m_windows[i].setToolTip(self.hud.stat_dict[self.get_id_from_seat(i)]['screen_name']) def save_layout(self, *args): """Save new common position back to the layout element in the config file.""" - new_locs = {} - for (i, pos) in list(self.positions.items()): - if i == 'common': - new_locs[i] = ((pos[0]), (pos[1])) - else: - # seat positions are owned by the aux controlling the stat block - # we share the locations from that aux, so don't write-back their - # locations here - pass - + new_locs = { + i: ((pos[0]), (pos[1])) + for i, pos in list(self.positions.items()) + if i == 'common' + } self.config.save_layout_set(self.hud.layout_set, self.hud.max, new_locs, width=None, height=None) def update_gui(self, new_hand_id): """Prepare and show the mucked cards.""" - if self.displayed: self.hide() + if self.displayed: + self.hide() # See how many players showed a hand. Skip if only 1 shows (= hero) n_sd = self.count_seats_with_cards(self.hud.cards) if n_sd < 2: @@ -364,12 +352,15 @@ def button_press_cb(self, widget, event, i, *args): """Handle button clicks in the event boxes.""" if event.button == 2: # middle button event, hold display (do not timeout) - if self.timer_on: self.timer_on = False - else: self.timer_on = False; self.hide() + if self.timer_on: + self.timer_on = False + else: + self.timer_on = False; self.hide() elif event.button == 1 and i == "common": # left button event (move) # firstly, cancel timer, otherwise block becomes locked if move event # is happening when timer eventually times-out - if self.timer_on: self.timer_on = False + if self.timer_on: + self.timer_on = False # only allow move on "common" element - seat block positions are # determined by aux_hud, not mucked card display window = widget.get_parent()