Skip to content

Commit 4052194

Browse files
authored
0.4.4 (#49)
* Adopt varname 0.8.0 * Add `base.make_names` and `base.name_unique` * 0.4.4 * Install python-slugify in CI
1 parent a5697fb commit 4052194

File tree

12 files changed

+240
-157
lines changed

12 files changed

+240
-157
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
poetry install -v
4040
pip install wcwidth
4141
pip install scipy
42+
pip install python-slugify
4243
# reinstall pandas to specific version
4344
pip install $PANDAS
4445
env:

.pre-commit-config.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ repos:
1717
pass_filenames: false
1818
always_run: true
1919
language: system
20-
- repo: https://github.com/pre-commit/mirrors-pylint
21-
rev: v2.4.4
20+
- repo: local
2221
hooks:
2322
- id: pylint
23+
name: Run pylint
2424
files: ^datar/.+$
2525
pass_filenames: false
26+
entry: pylint datar
2627
types: [python]
27-
args: [datar]
28-
- repo: local
29-
hooks:
28+
language: system
3029
- id: poetry2setuppy
3130
name: Convert pyproject.toml to setup.py
3231
entry: dephell deps convert --from=poetry --to=setup.py

datar/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .core.defaults import f
88

99
__all__ = ('f', 'get_versions')
10-
__version__ = "0.4.3"
10+
__version__ = "0.4.4"
1111

1212
def get_versions(
1313
prnt: bool = True

datar/base/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
log,
3434
log2,
3535
log10,
36-
log1p
36+
log1p,
3737
)
3838
from .bessel import bessel_i, bessel_j, bessel_k, bessel_y
3939
from .casting import as_double, as_float, as_int, as_integer, as_numeric
@@ -50,7 +50,14 @@
5050
is_factor,
5151
levels,
5252
)
53-
from .funs import cut, data_context, expandgrid, identity
53+
from .funs import (
54+
cut,
55+
data_context,
56+
expandgrid,
57+
identity,
58+
make_unique,
59+
make_names,
60+
)
5461
from .logical import (
5562
FALSE,
5663
TRUE,
@@ -75,7 +82,7 @@
7582
seq_along,
7683
seq_len,
7784
unique,
78-
match
85+
match,
7986
)
8087
from .special import (
8188
beta,

datar/base/funs.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
registered by `register_verb` and should be placed in `./verbs.py`
55
"""
66
import itertools
7-
from typing import Any, Callable, Iterable, Union
7+
from typing import Any, Callable, Iterable, List, Union
88
import numpy
99

1010
import pandas
1111
from pandas import Categorical, DataFrame
1212
from pipda import register_func
1313

1414
from ..core.middlewares import WithDataEnv
15-
from ..core.types import NumericType
15+
from ..core.types import NumericType, is_scalar
1616
from ..core.contexts import Context
17+
from ..core.names import repair_names
1718

1819

1920
@register_func(None, context=Context.EVAL)
@@ -131,3 +132,48 @@ def data_context(data: DataFrame) -> Any:
131132
The original or modified data
132133
"""
133134
return WithDataEnv(data)
135+
136+
def make_names(names: Any, unique: bool = False) -> List[str]:
137+
"""Make names available as columns and can be accessed by `df.<name>`
138+
139+
The names will be transformed using `python-slugify` with
140+
`lowercase=False` and `separator="_"`. When the first character is
141+
a digit, preface it with "_".
142+
143+
If `unique` is True, the results will be fed into
144+
`datar.core.names.repair_names(names, "unique")`
145+
146+
Args:
147+
names: The names
148+
if it is scalar, will make it into a list.
149+
Then all elements will be converted into strings
150+
unique: Whether to make the names unique
151+
152+
Returns:
153+
Converted names
154+
"""
155+
from slugify import slugify
156+
from . import as_character
157+
if is_scalar(names):
158+
names = [names]
159+
names = as_character(names)
160+
names = [slugify(name, separator="_", lowercase=False) for name in names]
161+
names = [f"_{name}" if name[0].isdigit() else name for name in names]
162+
if unique:
163+
return repair_names(names, "unique")
164+
return names
165+
166+
def make_unique(names: Any) -> List[str]:
167+
"""Make the names unique.
168+
169+
It's a shortcut for `make_names(names, unique=True)`
170+
171+
Args:
172+
names: The names
173+
if it is scalar, will make it into a list.
174+
Then all elements will be converted into strings
175+
176+
Returns:
177+
Converted names
178+
"""
179+
return make_names(names, unique=True)

datar/tibble/tibble.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Any, Union, Callable, Mapping, Iterable, Tuple
44

55
from pandas import DataFrame
6-
from varname import argname2, varname, VarnameRetrievingError
6+
from varname import argname, varname, VarnameException
77

88
import pipda
99
from pipda import register_func, evaluate_expr
@@ -59,22 +59,25 @@ def tibble(
5959
args = tuple((arg for arg in args if arg is not None))
6060
if not args and not kwargs:
6161
df = DataFrame() if not _rows else DataFrame(index=range(_rows))
62-
df.__dfname__ = varname(raise_exc=False, ignore=pipda)
62+
try:
63+
df.__dfname__ = varname(ignore=pipda)
64+
except VarnameException:
65+
pass
6366
return df
6467

6568
names = [None] * len(args)
6669
values = list(args)
6770
try:
68-
argnames = argname2(
71+
argnames = argname(
6972
"*args",
7073
frame=frame_,
7174
ignore=pipda,
7275
func=tibble,
7376
vars_only=False,
7477
)
7578
if len(argnames) != len(args):
76-
raise VarnameRetrievingError
77-
except VarnameRetrievingError:
79+
raise VarnameException
80+
except VarnameException:
7881
argnames = None
7982

8083
if argnames:
@@ -93,7 +96,10 @@ def tibble(
9396
names, values, _name_repair=_name_repair, base0_=base0_, dtypes_=dtypes_
9497
)
9598

96-
out.__dfname__ = varname(raise_exc=False, frame=frame_, ignore=pipda)
99+
try:
100+
out.__dfname__ = varname(frame=frame_, ignore=pipda)
101+
except VarnameException:
102+
pass
97103

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

146152
apply_dtypes(df, dtypes_)

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.4.4
2+
3+
- Adopt `varname` `v0.8.0`
4+
- Add `base.make_names()` and `base.make_unique()`
5+
16
## 0.4.3
27

38
- Adopt `pipda` `0.4.5`

0 commit comments

Comments
 (0)