Skip to content

Commit

Permalink
perf(core.Waiter): move _clean_up_table definition outside the main f…
Browse files Browse the repository at this point in the history
…or loop in df_list_by_lunch_time
  • Loading branch information
Michele-Alberti committed Jan 2, 2025
1 parent 1e18ef2 commit bc6330f
Showing 1 changed file with 47 additions and 51 deletions.
98 changes: 47 additions & 51 deletions dlunch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,14 +1040,60 @@ def df_list_by_lunch_time(
)
).all()
]
# Read dataframe (including notes)
# Read orders dataframe (including notes)
df = pd.read_sql_query(
self.config.db.orders_query.format(
schema=self.config.db.get("schema", models.SCHEMA)
),
engine,
)

# The following function prepare the dataframe before saving it into
# the dictionary that will be returned
def _clean_up_table(
config: DictConfig,
df_in: pd.DataFrame,
df_complete: pd.DataFrame,
):
df = df_in.copy()
# Group notes per menu item by concat users notes
# Use value counts to keep track of how many time a note is repeated
df_notes = (
df_complete[
(df_complete.lunch_time == time)
& (df_complete.note != "")
& (df_complete.user.isin(df.columns))
]
.drop(columns=["user", "lunch_time"])
.value_counts()
.reset_index(level="note")
)
df_notes.note = (
df_notes["count"]
.astype(str)
.str.cat(df_notes.note, sep=config.panel.gui.note_sep.count)
)
df_notes = df_notes.drop(columns="count")
df_notes = (
df_notes.groupby("item")["note"]
.apply(config.panel.gui.note_sep.element.join)
.to_frame()
)
# Add columns of totals
df[config.panel.gui.total_column_name] = df.sum(axis=1)
# Drop unused rows if requested
if config.panel.drop_unused_menu_items:
df = df[df[config.panel.gui.total_column_name] > 0]
# Add notes
df = df.join(df_notes)
df = df.rename(columns={"note": config.panel.gui.note_column_name})
# Change NaNs to '-'
df = df.fillna("-")
# Avoid mixed types (float and notes str)
df = df.astype(object)

return df

# Build a dict of dataframes, one for each lunch time
df_dict = {}
for time in df.lunch_time.sort_values().unique():
Expand All @@ -1072,56 +1118,6 @@ def df_list_by_lunch_time(
:, [c for c in df_users.columns if c in takeaway_list]
]

# The following function prepare the dataframe before saving it into
# the dictionary that will be returned
def _clean_up_table(
config: DictConfig,
df_in: pd.DataFrame,
df_complete: pd.DataFrame,
):
df = df_in.copy()
# Group notes per menu item by concat users notes
# Use value counts to keep track of how many time a note is repeated
df_notes = (
df_complete[
(df_complete.lunch_time == time)
& (df_complete.note != "")
& (df_complete.user.isin(df.columns))
]
.drop(columns=["user", "lunch_time"])
.value_counts()
.reset_index(level="note")
)
df_notes.note = (
df_notes["count"]
.astype(str)
.str.cat(
df_notes.note, sep=config.panel.gui.note_sep.count
)
)
df_notes = df_notes.drop(columns="count")
df_notes = (
df_notes.groupby("item")["note"]
.apply(config.panel.gui.note_sep.element.join)
.to_frame()
)
# Add columns of totals
df[config.panel.gui.total_column_name] = df.sum(axis=1)
# Drop unused rows if requested
if config.panel.drop_unused_menu_items:
df = df[df[config.panel.gui.total_column_name] > 0]
# Add notes
df = df.join(df_notes)
df = df.rename(
columns={"note": config.panel.gui.note_column_name}
)
# Change NaNs to '-'
df = df.fillna("-")
# Avoid mixed types (float and notes str)
df = df.astype(object)

return df

# Clean and add resulting dataframes to dict
# RESTAURANT LUNCH
if not df_users_restaurant.empty:
Expand Down

0 comments on commit bc6330f

Please sign in to comment.