Skip to content

refactor: Use ._with_* for internal API methods#2305

Merged
MarcoGorelli merged 10 commits intomainfrom
less-from-more-with
Mar 27, 2025
Merged

refactor: Use ._with_* for internal API methods#2305
MarcoGorelli merged 10 commits intomainfrom
less-from-more-with

Conversation

@dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Mar 27, 2025

Closes #2248

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

  • First pass of renaming
  • Pruning any unused trailing commas

@dangotbanned dangotbanned marked this pull request as ready for review March 27, 2025 22:29
@dangotbanned dangotbanned requested review from FBruzzesi and MarcoGorelli and removed request for FBruzzesi March 27, 2025 22:30
@dangotbanned
Copy link
Member Author

@MarcoGorelli @FBruzzesi

Thought I'd try and rip the bandage off quickly to resolve this - hope that's okay 🙂

Comment on lines 17 to 58
def keep(self: Self) -> DaskExpr:
return self._from_alias_output_names(alias_output_names=None)
return self._with_alias_output_names(alias_output_names=None)

def map(self: Self, function: Callable[[str], str]) -> DaskExpr:
return self._from_alias_output_names(
return self._with_alias_output_names(
alias_output_names=lambda output_names: [
function(str(name)) for name in output_names
],
)

def prefix(self: Self, prefix: str) -> DaskExpr:
return self._from_alias_output_names(
return self._with_alias_output_names(
alias_output_names=lambda output_names: [
f"{prefix}{output_name}" for output_name in output_names
],
)

def suffix(self: Self, suffix: str) -> DaskExpr:
return self._from_alias_output_names(
return self._with_alias_output_names(
alias_output_names=lambda output_names: [
f"{output_name}{suffix}" for output_name in output_names
]
)

def to_lowercase(self: Self) -> DaskExpr:
return self._from_alias_output_names(
return self._with_alias_output_names(
alias_output_names=lambda output_names: [
str(name).lower() for name in output_names
],
)

def to_uppercase(self: Self) -> DaskExpr:
return self._from_alias_output_names(
return self._with_alias_output_names(
alias_output_names=lambda output_names: [
str(name).upper() for name in output_names
]
)

def _from_alias_output_names(
def _with_alias_output_names(
self: Self,
alias_output_names: Callable[[Sequence[str]], Sequence[str]] | None,
) -> DaskExpr:
Copy link
Member Author

@dangotbanned dangotbanned Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Pretty sure all the LazyExprNameNamespaces can share something like EagerExprNameNamespace

EagerExprNameNamespace

class EagerExprNameNamespace(
EagerExprNamespace[EagerExprT], NameNamespace[EagerExprT], Generic[EagerExprT]
):
def keep(self) -> EagerExprT:
return self._from_callable(lambda name: name, alias=False)
def map(self, function: AliasName) -> EagerExprT:
return self._from_callable(function)
def prefix(self, prefix: str) -> EagerExprT:
return self._from_callable(lambda name: f"{prefix}{name}")
def suffix(self, suffix: str) -> EagerExprT:
return self._from_callable(lambda name: f"{name}{suffix}")
def to_lowercase(self) -> EagerExprT:
return self._from_callable(str.lower)
def to_uppercase(self) -> EagerExprT:
return self._from_callable(str.upper)
@staticmethod
def _alias_output_names(func: AliasName, /) -> AliasNames:
def fn(output_names: Sequence[str], /) -> Sequence[str]:
return [func(name) for name in output_names]
return fn
def _from_callable(self, func: AliasName, /, *, alias: bool = True) -> EagerExprT:
expr = self.compliant
return type(expr)(
lambda df: [
series.alias(func(name))
for series, name in zip(expr(df), expr._evaluate_output_names(df))
],
depth=expr._depth,
function_name=expr._function_name,
evaluate_output_names=expr._evaluate_output_names,
alias_output_names=self._alias_output_names(func) if alias else None,
backend_version=expr._backend_version,
implementation=expr._implementation,
version=expr._version,
call_kwargs=expr._call_kwargs,
)

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @dangotbanned

@MarcoGorelli MarcoGorelli merged commit 5f1f19f into main Mar 27, 2025
29 checks passed
@MarcoGorelli MarcoGorelli deleted the less-from-more-with branch March 27, 2025 23:19
dangotbanned added a commit that referenced this pull request Mar 28, 2025
MarcoGorelli pushed a commit that referenced this pull request Mar 28, 2025
* refactor: Generalize `EagerExprNameNamespace`

Follow-up to #2305 (comment)

* refactor: Simplify lazy `*ExprNameNamespace`

Already a nice reduction, but I think I can go further 😏

* refactor: begone `expr_name.py`
@dangotbanned dangotbanned mentioned this pull request Mar 29, 2025
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consistently prefix (_?)with_ for "this, but with x changed"

2 participants