Skip to content

Commit

Permalink
0.4.4 (#49)
Browse files Browse the repository at this point in the history
* Adopt varname 0.8.0

* Add `base.make_names` and `base.name_unique`

* 0.4.4

* Install python-slugify in CI
  • Loading branch information
pwwang authored Aug 16, 2021
1 parent a5697fb commit 4052194
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 157 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
poetry install -v
pip install wcwidth
pip install scipy
pip install python-slugify
# reinstall pandas to specific version
pip install $PANDAS
env:
Expand Down
9 changes: 4 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ repos:
pass_filenames: false
always_run: true
language: system
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.4.4
- repo: local
hooks:
- id: pylint
name: Run pylint
files: ^datar/.+$
pass_filenames: false
entry: pylint datar
types: [python]
args: [datar]
- repo: local
hooks:
language: system
- id: poetry2setuppy
name: Convert pyproject.toml to setup.py
entry: dephell deps convert --from=poetry --to=setup.py
Expand Down
2 changes: 1 addition & 1 deletion datar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .core.defaults import f

__all__ = ('f', 'get_versions')
__version__ = "0.4.3"
__version__ = "0.4.4"

def get_versions(
prnt: bool = True
Expand Down
13 changes: 10 additions & 3 deletions datar/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
log,
log2,
log10,
log1p
log1p,
)
from .bessel import bessel_i, bessel_j, bessel_k, bessel_y
from .casting import as_double, as_float, as_int, as_integer, as_numeric
Expand All @@ -50,7 +50,14 @@
is_factor,
levels,
)
from .funs import cut, data_context, expandgrid, identity
from .funs import (
cut,
data_context,
expandgrid,
identity,
make_unique,
make_names,
)
from .logical import (
FALSE,
TRUE,
Expand All @@ -75,7 +82,7 @@
seq_along,
seq_len,
unique,
match
match,
)
from .special import (
beta,
Expand Down
50 changes: 48 additions & 2 deletions datar/base/funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
registered by `register_verb` and should be placed in `./verbs.py`
"""
import itertools
from typing import Any, Callable, Iterable, Union
from typing import Any, Callable, Iterable, List, Union
import numpy

import pandas
from pandas import Categorical, DataFrame
from pipda import register_func

from ..core.middlewares import WithDataEnv
from ..core.types import NumericType
from ..core.types import NumericType, is_scalar
from ..core.contexts import Context
from ..core.names import repair_names


@register_func(None, context=Context.EVAL)
Expand Down Expand Up @@ -131,3 +132,48 @@ def data_context(data: DataFrame) -> Any:
The original or modified data
"""
return WithDataEnv(data)

def make_names(names: Any, unique: bool = False) -> List[str]:
"""Make names available as columns and can be accessed by `df.<name>`
The names will be transformed using `python-slugify` with
`lowercase=False` and `separator="_"`. When the first character is
a digit, preface it with "_".
If `unique` is True, the results will be fed into
`datar.core.names.repair_names(names, "unique")`
Args:
names: The names
if it is scalar, will make it into a list.
Then all elements will be converted into strings
unique: Whether to make the names unique
Returns:
Converted names
"""
from slugify import slugify
from . import as_character
if is_scalar(names):
names = [names]
names = as_character(names)
names = [slugify(name, separator="_", lowercase=False) for name in names]
names = [f"_{name}" if name[0].isdigit() else name for name in names]
if unique:
return repair_names(names, "unique")
return names

def make_unique(names: Any) -> List[str]:
"""Make the names unique.
It's a shortcut for `make_names(names, unique=True)`
Args:
names: The names
if it is scalar, will make it into a list.
Then all elements will be converted into strings
Returns:
Converted names
"""
return make_names(names, unique=True)
22 changes: 14 additions & 8 deletions datar/tibble/tibble.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Union, Callable, Mapping, Iterable, Tuple

from pandas import DataFrame
from varname import argname2, varname, VarnameRetrievingError
from varname import argname, varname, VarnameException

import pipda
from pipda import register_func, evaluate_expr
Expand Down Expand Up @@ -59,22 +59,25 @@ def tibble(
args = tuple((arg for arg in args if arg is not None))
if not args and not kwargs:
df = DataFrame() if not _rows else DataFrame(index=range(_rows))
df.__dfname__ = varname(raise_exc=False, ignore=pipda)
try:
df.__dfname__ = varname(ignore=pipda)
except VarnameException:
pass
return df

names = [None] * len(args)
values = list(args)
try:
argnames = argname2(
argnames = argname(
"*args",
frame=frame_,
ignore=pipda,
func=tibble,
vars_only=False,
)
if len(argnames) != len(args):
raise VarnameRetrievingError
except VarnameRetrievingError:
raise VarnameException
except VarnameException:
argnames = None

if argnames:
Expand All @@ -93,7 +96,10 @@ def tibble(
names, values, _name_repair=_name_repair, base0_=base0_, dtypes_=dtypes_
)

out.__dfname__ = varname(raise_exc=False, frame=frame_, ignore=pipda)
try:
out.__dfname__ = varname(frame=frame_, ignore=pipda)
except VarnameException:
pass

if not kwargs and len(args) == 1 and isinstance(args[0], DataFrame):
copy_attrs(out, args[0])
Expand Down Expand Up @@ -139,8 +145,8 @@ def tibble_row(
if df.shape[0] > 1:
raise ValueError("All arguments must be size one, use `[]` to wrap.")
try:
df.__dfname__ = varname(raise_exc=False)
except VarnameRetrievingError: # pragma: no cover
df.__dfname__ = varname()
except VarnameException: # pragma: no cover
df.__dfname__ = None

apply_dtypes(df, dtypes_)
Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.4.4

- Adopt `varname` `v0.8.0`
- Add `base.make_names()` and `base.make_unique()`

## 0.4.3

- Adopt `pipda` `0.4.5`
Expand Down
Loading

0 comments on commit 4052194

Please sign in to comment.