Skip to content

Commit 52143e0

Browse files
committed
fixes #705
1 parent 60c02e0 commit 52143e0

3 files changed

Lines changed: 24 additions & 73 deletions

File tree

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'fastcore.basics.NotStr.__bool__': ('basics.html#notstr.__bool__', 'fastcore/basics.py'),
4343
'fastcore.basics.NotStr.__contains__': ('basics.html#notstr.__contains__', 'fastcore/basics.py'),
4444
'fastcore.basics.NotStr.__eq__': ('basics.html#notstr.__eq__', 'fastcore/basics.py'),
45+
'fastcore.basics.NotStr.__getitem__': ('basics.html#notstr.__getitem__', 'fastcore/basics.py'),
4546
'fastcore.basics.NotStr.__hash__': ('basics.html#notstr.__hash__', 'fastcore/basics.py'),
4647
'fastcore.basics.NotStr.__init__': ('basics.html#notstr.__init__', 'fastcore/basics.py'),
4748
'fastcore.basics.NotStr.__iter__': ('basics.html#notstr.__iter__', 'fastcore/basics.py'),

fastcore/basics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ def __hash__(self): return hash(self.s)
11521152
def __bool__(self): return bool(self.s)
11531153
def __contains__(self, b): return b in self.s
11541154
def __iter__(self): return iter(self.s)
1155+
def __getitem__(self, i): return NotStr(self.s[i])
11551156

11561157
# %% ../nbs/01_basics.ipynb
11571158
class PrettyString(str):

nbs/01_basics.ipynb

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,7 @@
31193119
"text/markdown": [
31203120
"---\n",
31213121
"\n",
3122-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L525){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
3122+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L542){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
31233123
"\n",
31243124
"#### GetAttr\n",
31253125
"\n",
@@ -3128,12 +3128,6 @@
31283128
"*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*"
31293129
],
31303130
"text/plain": [
3131-
"---\n",
3132-
"\n",
3133-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L525){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
3134-
"\n",
3135-
"#### GetAttr\n",
3136-
"\n",
31373131
"> GetAttr ()\n",
31383132
"\n",
31393133
"*Inherit from this to have all attr accesses in `self._xtra` passed down to `self.default`*"
@@ -5054,7 +5048,7 @@
50545048
"text/markdown": [
50555049
"---\n",
50565050
"\n",
5057-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L861){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5051+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L878){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
50585052
"\n",
50595053
"#### fastuple\n",
50605054
"\n",
@@ -5063,12 +5057,6 @@
50635057
"*A `tuple` with elementwise ops and more friendly __init__ behavior*"
50645058
],
50655059
"text/plain": [
5066-
"---\n",
5067-
"\n",
5068-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L861){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5069-
"\n",
5070-
"#### fastuple\n",
5071-
"\n",
50725060
"> fastuple (x=None, *rest)\n",
50735061
"\n",
50745062
"*A `tuple` with elementwise ops and more friendly __init__ behavior*"
@@ -5144,7 +5132,7 @@
51445132
"text/markdown": [
51455133
"---\n",
51465134
"\n",
5147-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L880){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5135+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L897){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
51485136
"\n",
51495137
"##### fastuple.add\n",
51505138
"\n",
@@ -5153,12 +5141,6 @@
51535141
"*`+` is already defined in `tuple` for concat, so use `add` instead*"
51545142
],
51555143
"text/plain": [
5156-
"---\n",
5157-
"\n",
5158-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L880){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5159-
"\n",
5160-
"##### fastuple.add\n",
5161-
"\n",
51625144
"> fastuple.add (*args)\n",
51635145
"\n",
51645146
"*`+` is already defined in `tuple` for concat, so use `add` instead*"
@@ -5196,7 +5178,7 @@
51965178
"text/markdown": [
51975179
"---\n",
51985180
"\n",
5199-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L876){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5181+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L893){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
52005182
"\n",
52015183
"##### fastuple.mul\n",
52025184
"\n",
@@ -5205,12 +5187,6 @@
52055187
"*`*` is already defined in `tuple` for replicating, so use `mul` instead*"
52065188
],
52075189
"text/plain": [
5208-
"---\n",
5209-
"\n",
5210-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L876){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5211-
"\n",
5212-
"##### fastuple.mul\n",
5213-
"\n",
52145190
"> fastuple.mul (*args)\n",
52155191
"\n",
52165192
"*`*` is already defined in `tuple` for replicating, so use `mul` instead*"
@@ -5369,7 +5345,7 @@
53695345
"text/markdown": [
53705346
"---\n",
53715347
"\n",
5372-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L907){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5348+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L924){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
53735349
"\n",
53745350
"### bind\n",
53755351
"\n",
@@ -5378,12 +5354,6 @@
53785354
"*Same as `partial`, except you can use `arg0` `arg1` etc param placeholders*"
53795355
],
53805356
"text/plain": [
5381-
"---\n",
5382-
"\n",
5383-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L907){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
5384-
"\n",
5385-
"### bind\n",
5386-
"\n",
53875357
"> bind (func, *pargs, **pkwargs)\n",
53885358
"\n",
53895359
"*Same as `partial`, except you can use `arg0` `arg1` etc param placeholders*"
@@ -6478,23 +6448,17 @@
64786448
"text/markdown": [
64796449
"---\n",
64806450
"\n",
6481-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1080){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6451+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L1097){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
64826452
"\n",
64836453
"#### ImportEnum\n",
64846454
"\n",
6485-
"> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n",
6455+
"> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
64866456
"> start=1, boundary=None)\n",
64876457
"\n",
64886458
"*An `Enum` that can have its values imported*"
64896459
],
64906460
"text/plain": [
6491-
"---\n",
6492-
"\n",
6493-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1080){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6494-
"\n",
6495-
"#### ImportEnum\n",
6496-
"\n",
6497-
"> ImportEnum (value, names=None, module=None, qualname=None, type=None,\n",
6461+
"> ImportEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
64986462
"> start=1, boundary=None)\n",
64996463
"\n",
65006464
"*An `Enum` that can have its values imported*"
@@ -6546,23 +6510,17 @@
65466510
"text/markdown": [
65476511
"---\n",
65486512
"\n",
6549-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1088){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6513+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L1105){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
65506514
"\n",
65516515
"#### StrEnum\n",
65526516
"\n",
6553-
"> StrEnum (value, names=None, module=None, qualname=None, type=None,\n",
6517+
"> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
65546518
"> start=1, boundary=None)\n",
65556519
"\n",
65566520
"*An `ImportEnum` that behaves like a `str`*"
65576521
],
65586522
"text/plain": [
6559-
"---\n",
6560-
"\n",
6561-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1088){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6562-
"\n",
6563-
"#### StrEnum\n",
6564-
"\n",
6565-
"> StrEnum (value, names=None, module=None, qualname=None, type=None,\n",
6523+
"> StrEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
65666524
"> start=1, boundary=None)\n",
65676525
"\n",
65686526
"*An `ImportEnum` that behaves like a `str`*"
@@ -6614,23 +6572,17 @@
66146572
"text/markdown": [
66156573
"---\n",
66166574
"\n",
6617-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1098){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6575+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L1115){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
66186576
"\n",
66196577
"#### ValEnum\n",
66206578
"\n",
6621-
"> ValEnum (value, names=None, module=None, qualname=None, type=None,\n",
6579+
"> ValEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
66226580
"> start=1, boundary=None)\n",
66236581
"\n",
66246582
"*An `ImportEnum` that stringifies using values*"
66256583
],
66266584
"text/plain": [
6627-
"---\n",
6628-
"\n",
6629-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1098){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6630-
"\n",
6631-
"#### ValEnum\n",
6632-
"\n",
6633-
"> ValEnum (value, names=None, module=None, qualname=None, type=None,\n",
6585+
"> ValEnum (new_class_name, names, module=None, qualname=None, type=None,\n",
66346586
"> start=1, boundary=None)\n",
66356587
"\n",
66366588
"*An `ImportEnum` that stringifies using values*"
@@ -6706,7 +6658,7 @@
67066658
"text/markdown": [
67076659
"---\n",
67086660
"\n",
6709-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1103){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6661+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L1120){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
67106662
"\n",
67116663
"#### Stateful\n",
67126664
"\n",
@@ -6715,12 +6667,6 @@
67156667
"*A base class/mixin for objects that should not serialize all their state*"
67166668
],
67176669
"text/plain": [
6718-
"---\n",
6719-
"\n",
6720-
"[source](https://github.com/AnswerDotAI/fastcore/blob/master/fastcore/basics.py#L1103){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
6721-
"\n",
6722-
"#### Stateful\n",
6723-
"\n",
67246670
"> Stateful (*args, **kwargs)\n",
67256671
"\n",
67266672
"*A base class/mixin for objects that should not serialize all their state*"
@@ -6816,7 +6762,7 @@
68166762
{
68176763
"cell_type": "code",
68186764
"execution_count": null,
6819-
"id": "8b4b2755",
6765+
"id": "5094ae23",
68206766
"metadata": {},
68216767
"outputs": [],
68226768
"source": [
@@ -6835,21 +6781,24 @@
68356781
" def __hash__(self): return hash(self.s)\n",
68366782
" def __bool__(self): return bool(self.s)\n",
68376783
" def __contains__(self, b): return b in self.s\n",
6838-
" def __iter__(self): return iter(self.s)"
6784+
" def __iter__(self): return iter(self.s)\n",
6785+
" def __getitem__(self, i): return NotStr(self.s[i])"
68396786
]
68406787
},
68416788
{
68426789
"cell_type": "code",
68436790
"execution_count": null,
6844-
"id": "1c296d2b",
6791+
"id": "cff43774",
68456792
"metadata": {},
68466793
"outputs": [],
68476794
"source": [
68486795
"s = NotStr(\"hello\")\n",
68496796
"assert not isinstance(s, str)\n",
68506797
"test_eq(s, 'hello')\n",
68516798
"test_eq(s*2, 'hellohello')\n",
6852-
"test_eq(len(s), 5)"
6799+
"test_eq(len(s), 5)\n",
6800+
"test_eq(s[:2], \"he\")\n",
6801+
"test_eq(s[2], \"l\")"
68536802
]
68546803
},
68556804
{

0 commit comments

Comments
 (0)