Skip to content

Commit aff66bb

Browse files
committed
fixes #773
1 parent 2266f11 commit aff66bb

3 files changed

Lines changed: 129 additions & 17 deletions

File tree

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
'fastcore.basics.in_': ('basics.html#in_', 'fastcore/basics.py'),
160160
'fastcore.basics.instantiate': ('basics.html#instantiate', 'fastcore/basics.py'),
161161
'fastcore.basics.is_array': ('basics.html#is_array', 'fastcore/basics.py'),
162+
'fastcore.basics.joins': ('basics.html#joins', 'fastcore/basics.py'),
162163
'fastcore.basics.last': ('basics.html#last', 'fastcore/basics.py'),
163164
'fastcore.basics.last_index': ('basics.html#last_index', 'fastcore/basics.py'),
164165
'fastcore.basics.last_match': ('basics.html#last_match', 'fastcore/basics.py'),

fastcore/basics.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle',
1616
'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'renumerate', 'first', 'last', 'only',
1717
'nested_attr', 'nested_setdefault', 'nested_callable', 'nested_idx', 'set_nested_idx', 'val2idx',
18-
'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'fastuple', 'bind',
19-
'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'negate', 'spread',
18+
'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'joins', 'fastuple',
19+
'bind', 'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'negate', 'spread',
2020
'dspread', 'copy_func', 'patch_to', 'patch', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'ValEnum',
2121
'Stateful', 'NotStr', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'str2bool', 'str2int',
2222
'str2float', 'str2list', 'str2date', 'to_bool', 'to_int', 'to_float', 'to_list', 'to_date', 'typed',
@@ -851,6 +851,11 @@ def last_match(lst, f, default=None):
851851
"Last element of `lst` matching predicate `f`, or `default` if none"
852852
return next((i for i in range(len(lst)-1, -1, -1) if f(lst[i])), default)
853853

854+
# %% ../nbs/01_basics.ipynb #bd711f0d
855+
def joins(sep, its):
856+
"Sugar for `sep.join(map(str, its))`"
857+
return sep.join(map(str, its))
858+
854859
# %% ../nbs/01_basics.ipynb #39e52ca1
855860
num_methods = """
856861
__add__ __sub__ __mul__ __matmul__ __truediv__ __floordiv__ __mod__ __divmod__ __pow__

nbs/01_basics.ipynb

Lines changed: 121 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -727,18 +727,32 @@
727727
"text/markdown": [
728728
"---\n",
729729
"\n",
730-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L115){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
730+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L116){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
731731
"\n",
732732
"### get_class\n",
733733
"\n",
734-
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, anno=None,\n",
735-
"> **flds)\n",
734+
"```python\n",
735+
"\n",
736+
"def get_class(\n",
737+
" nm, fld_names:VAR_POSITIONAL, sup:NoneType=None, doc:NoneType=None, funcs:NoneType=None, anno:NoneType=None,\n",
738+
" flds:VAR_KEYWORD\n",
739+
"):\n",
740+
"\n",
741+
"\n",
742+
"```\n",
736743
"\n",
737744
"*Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`*"
738745
],
739746
"text/plain": [
740-
"> get_class (nm, *fld_names, sup=None, doc=None, funcs=None, anno=None,\n",
741-
"> **flds)\n",
747+
"```python\n",
748+
"\n",
749+
"def get_class(\n",
750+
" nm, fld_names:VAR_POSITIONAL, sup:NoneType=None, doc:NoneType=None, funcs:NoneType=None, anno:NoneType=None,\n",
751+
" flds:VAR_KEYWORD\n",
752+
"):\n",
753+
"\n",
754+
"\n",
755+
"```\n",
742756
"\n",
743757
"*Dynamically create a class, optionally inheriting from `sup`, containing `fld_names`*"
744758
]
@@ -922,16 +936,30 @@
922936
"text/markdown": [
923937
"---\n",
924938
"\n",
925-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L159){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
939+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L160){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
926940
"\n",
927941
"#### ignore_exceptions\n",
928942
"\n",
929-
"> ignore_exceptions ()\n",
943+
"```python\n",
944+
"\n",
945+
"def ignore_exceptions(\n",
946+
" args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
947+
"):\n",
948+
"\n",
949+
"\n",
950+
"```\n",
930951
"\n",
931952
"*Context manager to ignore exceptions*"
932953
],
933954
"text/plain": [
934-
"> ignore_exceptions ()\n",
955+
"```python\n",
956+
"\n",
957+
"def ignore_exceptions(\n",
958+
" args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
959+
"):\n",
960+
"\n",
961+
"\n",
962+
"```\n",
935963
"\n",
936964
"*Context manager to ignore exceptions*"
937965
]
@@ -1089,12 +1117,26 @@
10891117
"\n",
10901118
"### noop\n",
10911119
"\n",
1092-
"> noop (x=None, *args, **kwargs)\n",
1120+
"```python\n",
1121+
"\n",
1122+
"def noop(\n",
1123+
" x:NoneType=None, args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
1124+
"):\n",
1125+
"\n",
1126+
"\n",
1127+
"```\n",
10931128
"\n",
10941129
"*Do nothing*"
10951130
],
10961131
"text/plain": [
1097-
"> noop (x=None, *args, **kwargs)\n",
1132+
"```python\n",
1133+
"\n",
1134+
"def noop(\n",
1135+
" x:NoneType=None, args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
1136+
"):\n",
1137+
"\n",
1138+
"\n",
1139+
"```\n",
10981140
"\n",
10991141
"*Do nothing*"
11001142
]
@@ -1132,12 +1174,26 @@
11321174
"\n",
11331175
"### noops\n",
11341176
"\n",
1135-
"> noops (x=None, *args, **kwargs)\n",
1177+
"```python\n",
1178+
"\n",
1179+
"def noops(\n",
1180+
" x:NoneType=None, args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
1181+
"):\n",
1182+
"\n",
1183+
"\n",
1184+
"```\n",
11361185
"\n",
11371186
"*Do nothing (method)*"
11381187
],
11391188
"text/plain": [
1140-
"> noops (x=None, *args, **kwargs)\n",
1189+
"```python\n",
1190+
"\n",
1191+
"def noops(\n",
1192+
" x:NoneType=None, args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
1193+
"):\n",
1194+
"\n",
1195+
"\n",
1196+
"```\n",
11411197
"\n",
11421198
"*Do nothing (method)*"
11431199
]
@@ -3101,16 +3157,30 @@
31013157
"text/markdown": [
31023158
"---\n",
31033159
"\n",
3104-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L543){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
3160+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L533){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
31053161
"\n",
31063162
"#### GetAttr\n",
31073163
"\n",
3108-
"> GetAttr ()\n",
3164+
"```python\n",
3165+
"\n",
3166+
"def GetAttr(\n",
3167+
" args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
3168+
"):\n",
3169+
"\n",
3170+
"\n",
3171+
"```\n",
31093172
"\n",
31103173
"*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*"
31113174
],
31123175
"text/plain": [
3113-
"> GetAttr ()\n",
3176+
"```python\n",
3177+
"\n",
3178+
"def GetAttr(\n",
3179+
" args:VAR_POSITIONAL, kwargs:VAR_KEYWORD\n",
3180+
"):\n",
3181+
"\n",
3182+
"\n",
3183+
"```\n",
31143184
"\n",
31153185
"*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*"
31163186
]
@@ -4936,6 +5006,42 @@
49365006
"test_eq(last_match(a, lambda o:o%2), 5)"
49375007
]
49385008
},
5009+
{
5010+
"cell_type": "code",
5011+
"execution_count": null,
5012+
"id": "bd711f0d",
5013+
"metadata": {},
5014+
"outputs": [],
5015+
"source": [
5016+
"#| export\n",
5017+
"def joins(sep, its):\n",
5018+
" \"Sugar for `sep.join(map(str, its))`\"\n",
5019+
" return sep.join(map(str, its))"
5020+
]
5021+
},
5022+
{
5023+
"cell_type": "code",
5024+
"execution_count": null,
5025+
"id": "b8d12e02",
5026+
"metadata": {},
5027+
"outputs": [
5028+
{
5029+
"data": {
5030+
"text/plain": [
5031+
"'1,2,3'"
5032+
]
5033+
},
5034+
"execution_count": null,
5035+
"metadata": {},
5036+
"output_type": "execute_result"
5037+
}
5038+
],
5039+
"source": [
5040+
"# Gives: `TypeError: sequence item 0: expected str…`:\n",
5041+
"# \",\".join([1,2,3])\n",
5042+
"joins(\",\", [1,2,3])"
5043+
]
5044+
},
49395045
{
49405046
"cell_type": "markdown",
49415047
"id": "ebb867d5",

0 commit comments

Comments
 (0)