Can you optimise this python code? def gettags() -> pd.DataFrame: with engine.connect()…
- User
Can you optimise this python code?
def get_tags() -> pd.DataFrame: with engine.connect() as connection: all_commits = pd.read_sql(sql="select * from "+schema_name+"." + table_name_prefix + "commit", con=engine)
print(all_commits.columns) all_commits = all_commits[["unique_id", "commit_id", "branch", "delivered_at"]].drop_duplicates() all_commits = all_commits[all_commits["branch"].apply(lambda b: (b == "develop") or re.match(r'release/r\d{2}$', b) is not None)] all_commits["branch"] = all_commits["branch"].apply( lambda b: b if b == "develop" else b[len("release/"):] ) result = subprocess.run(["git", "tag"], capture_output=True, text=True, check=True) print(result) tags = result.stdout.strip().split('\n') print("Number of tags: ", len(tags)) data = [] for tag in tags: if re.match(r'\d{6}$', tag) is None: continue # get the commit SHA the tag points to (peeled) try: peeled = subprocess.run(["git", "rev-parse", f"{tag}^{{commit}}"], capture_output=True, text=True, check=True) commit = peeled.stdout.strip() data.append({'commit_id': commit, 'tag': tag}) except subprocess.CalledProcessError: print(f"Failed to peel tag (probably it is not existing or corrupted): {tag}") continue print(data) tag_mapping = pd.DataFrame(data) tag_mapping = tag_mapping.merge(all_commits, on="commit_id") tag_mapping.drop(columns=["commit_id"], axis=1, inplace=True) all_commits.drop(columns=["commit_id"], axis=1, inplace=True) print("All tags:") print(tag_mapping) tag_mapping.sort_values(by='delivered_at', inplace=True, ascending=False) all_commits["tag"] = "no tag found" for idx, row in tag_mapping.iterrows(): print("Calculating tags for:") print(row) all_commits.loc[(all_commits["branch"] == row["branch"]) & (all_commits["delivered_at"] <= row["delivered_at"]) , "tag"] = row["tag"] return all_commits.drop(columns=["branch"])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.