1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
|
%global _empty_manifest_terminate_build 0
Name: python-dol
Version: 0.1.134
Release: 1
Summary: Base builtin tools make and transform data object layers (dols).
License: mit
URL: https://github.com/i2mint/dol
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/6f/68/9c8678c36b6bc4f6d07fadfb6896ccab2a5829c5b8ac422dee51aac946bb/dol-0.1.134.tar.gz
BuildArch: noarch
%description
# dol
Base builtin tools make and transform data object layers (dols).
The main idea comes in many names such as
[Data Access Object (DAO)](https://en.wikipedia.org/wiki/Data_access_object),
[Repository Pattern](https://www.cosmicpython.com/book/chapter_02_repository.html),
[Hexagonal architecture, or ports and adapters architecture](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software))
for data.
But simply put, what `dol` provides is tools to make your interface with data be domain-oriented, simple, and isolated from the underlying data infrastucture. This makes the business logic code simple and stable, enables you to develop and test it without the need of any data infrastructure, and allows you to change this infrastructure independently.
The package is light-weight: Pure python; no third-party dependencies.
To install: ```pip install dol```
[Documentation here](https://i2mint.github.io/dol/)
Note: This project started as [`py2store`](https://github.com/i2mint/py2store).
`dol` is the core of py2store has now been factored out
and many of the specialized data object layers moved to separate packages.
`py2store` is acting more as an aggregator package -- a shoping mall where you can quickly access many (but not all)
functionalities that use `dol`.
It's advised to use `dol` (and/or its specialized spin-off packages) directly when the core functionality is all you need.
# A few highlights of py2store's README
## What is this?
Storage CRUD how and where you want it.
List, read, write, and delete data in a structured data source/target,
as if manipulating simple python builtins (dicts, lists), or through the interface **you** want to interact with,
with configuration or physical particularities out of the way.
Also, being able to change these particularities without having to change the business-logic code.
If you're not a "read from top to bottom" kinda person, here are some tips:
[Quick peek](#quick-peek) will show you a simple example of how it looks and feels.
[Use cases](#use-cases) will give you an idea of how py2store can be useful to you, if at all.
The section with the best bang for the buck is probably
[remove (much of the) data access entropy](#remove-data-access-entropy).
It will give you simple (but real) examples of how to use `py2store` tooling
to bend your interface with data to your will.
[How it works](#a-few-words-about-design) will give you a sense of how it works.
[More examples](https://github.com/i2mint/py2store#more-examples)
will give you a taste of how you can adapt the three main aspects of
storage (persistence, serialization, and indexing) to your needs.
Install it (e.g. `pip install py2store`).
## Quick peek
Think of type of storage you want to use and just go ahead, like you're using a dict.
Here's an example for local storage (you must you string keys only here).
```pydocstring
>>> from py2store import QuickStore
>>>
>>> store = QuickStore() # will print what (tmp) rootdir it is choosing
>>> # Write something and then read it out again
>>> store['foo'] = 'baz'
>>> 'foo' in store # do you have the key 'foo' in your store?
True
>>> store['foo'] # what is the value for 'foo'?
'baz'
>>>
>>> # Okay, it behaves like a dict, but go have a look in your file system,
>>> # and see that there is now a file in the rootdir, named 'foo'!
>>>
>>> # Write something more complicated
>>> store['hello/world'] = [1, 'flew', {'over': 'a', "cuckoo's": map}]
>>> stored_val = store['hello/world']
>>> stored_val == [1, 'flew', {'over': 'a', "cuckoo's": map}] # was it retrieved correctly?
True
>>>
>>> # how many items do you have now?
>>> assert len(store) >= 2 # can't be sure there were no elements before, so can't assert == 2
>>>
>>> # delete the stuff you've written
>>> del store['foo']
>>> del store['hello/world']
```
`QuickStore` will by default store things in local files, using pickle as the serializer.
If a root directory is not specified,
it will use a tmp directory it will create (the first time you try to store something)
It will create any directories that need to be created to satisfy any/key/that/contains/slashes.
Of course, everything is configurable.
## A list of stores for various uses
`py2store` provides tools to create the dict-like interface to data you need.
If you want to just use existing interfaces, build on it, or find examples of how to make such
interfaces, check out the ever-growing list of `py2store`-using projects:
- [mongodol](https://github.com/i2mint/mongodol): For MongoDB
- [hear](https://github.com/otosense/hear): Read/write audio data flexibly.
- [tabled](https://github.com/i2mint/tabled): Data as `pandas.DataFrame` from various sources
- [msword](https://pypi.org/project/msword/): Simple mapping view to docx (Word Doc) elements
- [sshdol](https://github.com/i2mint/sshdol): Remote (ssh) files access
- [haggle](https://github.com/otosense/haggle): Easily search, download, and use kaggle datasets.
- [pyckup](https://github.com/i2mint/pyckup): Grab data simply and define protocols for others to do the same.
- [hubcap](https://pypi.org/project/hubcap/): Dict-like interface to github.
- [graze](https://github.com/thorwhalen/graze): Cache the internet.
- [grub](https://github.com/thorwhalen/grub): A ridiculously simple search engine maker.
Just for fun projects:
- [cult](https://github.com/thorwhalen/cult): Religious texts search engine. 18mn application of `grub`.
- [laugh](https://github.com/thorwhalen/laugh): A (py2store-based) joke finder.
## Use cases
### Interfacing reads
How many times did someone share some data with you in the form of a zip of some nested folders
whose structure and naming choices are fascinatingly obscure? And how much time do you then spend to write code
to interface with that freak of nature? Well, one of the intents of py2store is to make that easier to do.
You still need to understand the structure of the data store and how to deserialize these datas into python
objects you can manipulate. But with the proper tool, you shouldn't have to do much more than that.
### Changing where and how things are stored
Ever have to switch where you persist things (say from file system to S3), or change the way key into your data,
or the way that data is serialized? If you use py2store tools to separate the different storage concerns,
it'll be quite easy to change, since change will be localized. And if you're dealing with code that was already
written, with concerns all mixed up, py2store should still be able to help since you'll be able to
more easily give the new system a facade that makes it look like the old one.
All of this can also be applied to data bases as well, in-so-far as the CRUD operations you're using
are covered by the base methods.
### Adapters: When the learning curve is in the way of learning
Shinny new storage mechanisms (DBs etc.) are born constantly, and some folks start using them, and we are eventually lead to use them
as well if we need to work with those folks' systems. And though we'd love to learn the wonderful new
capabilities the new kid on the block has, sometimes we just don't have time for that.
Wouldn't it be nice if someone wrote an adapter to the new system that had an interface we were familiar with?
Talking to SQL as if it were mongo (or visa versa). Talking to S3 as if it were a file system.
Now it's not a long term solution: If we're really going to be using the new system intensively, we
should learn it. But when you just got to get stuff done, having a familiar facade to something new
is a life saver.
py2store would like to make it easier for you roll out an adapter to be able to talk
to the new system in the way **you** are familiar with.
### Thinking about storage later, if ever
You have a new project or need to write a new app. You'll need to store stuff and read stuff back.
Stuff: Different kinds of resources that your app will need to function. Some people enjoy thinking
of how to optimize that aspect. I don't. I'll leave it to the experts to do so when the time comes.
Often though, the time is later, if ever. Few proof of concepts and MVPs ever make it to prod.
So instead, I'd like to just get on with the business logic and write my program.
So what I need is an easy way to get some minimal storage functionality.
But when the time comes to optimize, I shouldn't have to change my code, but instead just change the way my
DAO does things. What I need is py2store.
## Remove data access entropy
Data comes from many different sources, organization, and formats.
Data is needed in many different contexts, which comes with its own natural data organization and formats.
In between both: A entropic mess of ad-hoc connections and annoying time-consuming and error prone boilerplate.
`py2store` (and it's now many extensions) is there to mitigate this.
The design gods say SOC, DRY, SOLID* and such. That's good design, yes. But it can take more work to achieve these principles.
We'd like to make it _easier_ to do it right than do it wrong.
_(*) Separation (Of) Concerns, Don't Repeat Yourself, https://en.wikipedia.org/wiki/SOLID))_
We need to determine what are the most common operations we want to do on data, and decide on a common way to express these operations, no matter what the implementation details are.
- get/read some data
- set/write some data
- list/see what data we have
- filter
- cache
...
Looking at this, we see that the base operations for complex data systems such as data bases and file systems overlap significantly with the base operations on python (or any programming language) objects.
So we'll reflect this in our choice of a common "language" for these operations. For examples, once projected to a `py2store` object, iterating over the contents of a data base, or over files, or over the elements of a python (iterable) object should look the same, in code. Achieving this, we achieve SOC, but also set ourselves up for tooling that can assume this consistency, therefore be DRY, and many of the SOLID principles of design.
Also mentionable: So far, `py2store` core tools are all pure python -- no dependencies on anything else.
Now, when you want to specialize a store (say talk to data bases, web services, acquire special formats (audio, etc.)), then you'll need to pull in a few helpful packages. But the core tooling is pure.
# A few words about design
By store we mean key-value store. This could be files in a filesystem, objects in s3, or a database. Where and
how the content is stored should be specified, but StoreInterface offers a dict-like interface to this.
__getitem__ calls: _id_of_key _obj_of_data
__setitem__ calls: _id_of_key _data_of_obj
__delitem__ calls: _id_of_key
__iter__ calls: _key_of_id
```pydocstring
>>> from dol import Store
```
A Store can be instantiated with no arguments. By default it will make a dict and wrap that.
```pydocstring
>>> # Default store: no key or value conversion ################################################
>>> s = Store()
>>> s['foo'] = 33
>>> s['bar'] = 65
>>> assert list(s.items()) == [('foo', 33), ('bar', 65)]
>>> assert list(s.store.items()) == [('foo', 33), ('bar', 65)] # see that the store contains the same thing
```
Now let's make stores that have a key and value conversion layer
input keys will be upper cased, and output keys lower cased
input values (assumed int) will be converted to ascii string, and visa versa
```pydocstring
>>>
>>> def test_store(s):
... s['foo'] = 33 # write 33 to 'foo'
... assert 'foo' in s # __contains__ works
... assert 'no_such_key' not in s # __nin__ works
... s['bar'] = 65 # write 65 to 'bar'
... assert len(s) == 2 # there are indeed two elements
... assert list(s) == ['foo', 'bar'] # these are the keys
... assert list(s.keys()) == ['foo', 'bar'] # the keys() method works!
... assert list(s.values()) == [33, 65] # the values() method works!
... assert list(s.items()) == [('foo', 33), ('bar', 65)] # these are the items
... assert list(s.store.items()) == [('FOO', '!'), ('BAR', 'A')] # but note the internal representation
... assert s.get('foo') == 33 # the get method works
... assert s.get('no_such_key', 'something') == 'something' # return a default value
... del(s['foo']) # you can delete an item given its key
... assert len(s) == 1 # see, only one item left!
... assert list(s.items()) == [('bar', 65)] # here it is
>>>
```
We can introduce this conversion layer in several ways.
Here are few...
## by subclassing
```pydocstring
>>> # by subclassing ###############################################################################
>>> class MyStore(Store):
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
>>> s = MyStore(store=dict()) # note that you don't need to specify dict(), since it's the default
>>> test_store(s)
>>>
```
## by assigning functions to converters
```pydocstring
>>> # by assigning functions to converters ##########################################################
>>> class MyStore(Store):
... def __init__(self, store, _id_of_key, _key_of_id, _data_of_obj, _obj_of_data):
... super().__init__(store)
... self._id_of_key = _id_of_key
... self._key_of_id = _key_of_id
... self._data_of_obj = _data_of_obj
... self._obj_of_data = _obj_of_data
...
>>> s = MyStore(dict(),
... _id_of_key=lambda k: k.upper(),
... _key_of_id=lambda _id: _id.lower(),
... _data_of_obj=lambda obj: chr(obj),
... _obj_of_data=lambda data: ord(data))
>>> test_store(s)
>>>
```
## using a Mixin class
```pydocstring
>>> # using a Mixin class #############################################################################
>>> class Mixin:
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
...
>>> class MyStore(Mixin, Store): # note that the Mixin must come before Store in the mro
... pass
...
>>> s = MyStore() # no dict()? No, because default anyway
>>> test_store(s)
```
## adding wrapper methods to an already made Store instance
```pydocstring
>>> # adding wrapper methods to an already made Store instance #########################################
>>> s = Store(dict())
>>> s._id_of_key=lambda k: k.upper()
>>> s._key_of_id=lambda _id: _id.lower()
>>> s._data_of_obj=lambda obj: chr(obj)
>>> s._obj_of_data=lambda data: ord(data)
>>> test_store(s)
```
# Why the name?
- because it's short
- because it's cute
- because it reminds one of "russian dolls" (one way to think of wrappers)
- because we can come up with an acronym the contains "Data Object" in it.
%package -n python3-dol
Summary: Base builtin tools make and transform data object layers (dols).
Provides: python-dol
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-dol
# dol
Base builtin tools make and transform data object layers (dols).
The main idea comes in many names such as
[Data Access Object (DAO)](https://en.wikipedia.org/wiki/Data_access_object),
[Repository Pattern](https://www.cosmicpython.com/book/chapter_02_repository.html),
[Hexagonal architecture, or ports and adapters architecture](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software))
for data.
But simply put, what `dol` provides is tools to make your interface with data be domain-oriented, simple, and isolated from the underlying data infrastucture. This makes the business logic code simple and stable, enables you to develop and test it without the need of any data infrastructure, and allows you to change this infrastructure independently.
The package is light-weight: Pure python; no third-party dependencies.
To install: ```pip install dol```
[Documentation here](https://i2mint.github.io/dol/)
Note: This project started as [`py2store`](https://github.com/i2mint/py2store).
`dol` is the core of py2store has now been factored out
and many of the specialized data object layers moved to separate packages.
`py2store` is acting more as an aggregator package -- a shoping mall where you can quickly access many (but not all)
functionalities that use `dol`.
It's advised to use `dol` (and/or its specialized spin-off packages) directly when the core functionality is all you need.
# A few highlights of py2store's README
## What is this?
Storage CRUD how and where you want it.
List, read, write, and delete data in a structured data source/target,
as if manipulating simple python builtins (dicts, lists), or through the interface **you** want to interact with,
with configuration or physical particularities out of the way.
Also, being able to change these particularities without having to change the business-logic code.
If you're not a "read from top to bottom" kinda person, here are some tips:
[Quick peek](#quick-peek) will show you a simple example of how it looks and feels.
[Use cases](#use-cases) will give you an idea of how py2store can be useful to you, if at all.
The section with the best bang for the buck is probably
[remove (much of the) data access entropy](#remove-data-access-entropy).
It will give you simple (but real) examples of how to use `py2store` tooling
to bend your interface with data to your will.
[How it works](#a-few-words-about-design) will give you a sense of how it works.
[More examples](https://github.com/i2mint/py2store#more-examples)
will give you a taste of how you can adapt the three main aspects of
storage (persistence, serialization, and indexing) to your needs.
Install it (e.g. `pip install py2store`).
## Quick peek
Think of type of storage you want to use and just go ahead, like you're using a dict.
Here's an example for local storage (you must you string keys only here).
```pydocstring
>>> from py2store import QuickStore
>>>
>>> store = QuickStore() # will print what (tmp) rootdir it is choosing
>>> # Write something and then read it out again
>>> store['foo'] = 'baz'
>>> 'foo' in store # do you have the key 'foo' in your store?
True
>>> store['foo'] # what is the value for 'foo'?
'baz'
>>>
>>> # Okay, it behaves like a dict, but go have a look in your file system,
>>> # and see that there is now a file in the rootdir, named 'foo'!
>>>
>>> # Write something more complicated
>>> store['hello/world'] = [1, 'flew', {'over': 'a', "cuckoo's": map}]
>>> stored_val = store['hello/world']
>>> stored_val == [1, 'flew', {'over': 'a', "cuckoo's": map}] # was it retrieved correctly?
True
>>>
>>> # how many items do you have now?
>>> assert len(store) >= 2 # can't be sure there were no elements before, so can't assert == 2
>>>
>>> # delete the stuff you've written
>>> del store['foo']
>>> del store['hello/world']
```
`QuickStore` will by default store things in local files, using pickle as the serializer.
If a root directory is not specified,
it will use a tmp directory it will create (the first time you try to store something)
It will create any directories that need to be created to satisfy any/key/that/contains/slashes.
Of course, everything is configurable.
## A list of stores for various uses
`py2store` provides tools to create the dict-like interface to data you need.
If you want to just use existing interfaces, build on it, or find examples of how to make such
interfaces, check out the ever-growing list of `py2store`-using projects:
- [mongodol](https://github.com/i2mint/mongodol): For MongoDB
- [hear](https://github.com/otosense/hear): Read/write audio data flexibly.
- [tabled](https://github.com/i2mint/tabled): Data as `pandas.DataFrame` from various sources
- [msword](https://pypi.org/project/msword/): Simple mapping view to docx (Word Doc) elements
- [sshdol](https://github.com/i2mint/sshdol): Remote (ssh) files access
- [haggle](https://github.com/otosense/haggle): Easily search, download, and use kaggle datasets.
- [pyckup](https://github.com/i2mint/pyckup): Grab data simply and define protocols for others to do the same.
- [hubcap](https://pypi.org/project/hubcap/): Dict-like interface to github.
- [graze](https://github.com/thorwhalen/graze): Cache the internet.
- [grub](https://github.com/thorwhalen/grub): A ridiculously simple search engine maker.
Just for fun projects:
- [cult](https://github.com/thorwhalen/cult): Religious texts search engine. 18mn application of `grub`.
- [laugh](https://github.com/thorwhalen/laugh): A (py2store-based) joke finder.
## Use cases
### Interfacing reads
How many times did someone share some data with you in the form of a zip of some nested folders
whose structure and naming choices are fascinatingly obscure? And how much time do you then spend to write code
to interface with that freak of nature? Well, one of the intents of py2store is to make that easier to do.
You still need to understand the structure of the data store and how to deserialize these datas into python
objects you can manipulate. But with the proper tool, you shouldn't have to do much more than that.
### Changing where and how things are stored
Ever have to switch where you persist things (say from file system to S3), or change the way key into your data,
or the way that data is serialized? If you use py2store tools to separate the different storage concerns,
it'll be quite easy to change, since change will be localized. And if you're dealing with code that was already
written, with concerns all mixed up, py2store should still be able to help since you'll be able to
more easily give the new system a facade that makes it look like the old one.
All of this can also be applied to data bases as well, in-so-far as the CRUD operations you're using
are covered by the base methods.
### Adapters: When the learning curve is in the way of learning
Shinny new storage mechanisms (DBs etc.) are born constantly, and some folks start using them, and we are eventually lead to use them
as well if we need to work with those folks' systems. And though we'd love to learn the wonderful new
capabilities the new kid on the block has, sometimes we just don't have time for that.
Wouldn't it be nice if someone wrote an adapter to the new system that had an interface we were familiar with?
Talking to SQL as if it were mongo (or visa versa). Talking to S3 as if it were a file system.
Now it's not a long term solution: If we're really going to be using the new system intensively, we
should learn it. But when you just got to get stuff done, having a familiar facade to something new
is a life saver.
py2store would like to make it easier for you roll out an adapter to be able to talk
to the new system in the way **you** are familiar with.
### Thinking about storage later, if ever
You have a new project or need to write a new app. You'll need to store stuff and read stuff back.
Stuff: Different kinds of resources that your app will need to function. Some people enjoy thinking
of how to optimize that aspect. I don't. I'll leave it to the experts to do so when the time comes.
Often though, the time is later, if ever. Few proof of concepts and MVPs ever make it to prod.
So instead, I'd like to just get on with the business logic and write my program.
So what I need is an easy way to get some minimal storage functionality.
But when the time comes to optimize, I shouldn't have to change my code, but instead just change the way my
DAO does things. What I need is py2store.
## Remove data access entropy
Data comes from many different sources, organization, and formats.
Data is needed in many different contexts, which comes with its own natural data organization and formats.
In between both: A entropic mess of ad-hoc connections and annoying time-consuming and error prone boilerplate.
`py2store` (and it's now many extensions) is there to mitigate this.
The design gods say SOC, DRY, SOLID* and such. That's good design, yes. But it can take more work to achieve these principles.
We'd like to make it _easier_ to do it right than do it wrong.
_(*) Separation (Of) Concerns, Don't Repeat Yourself, https://en.wikipedia.org/wiki/SOLID))_
We need to determine what are the most common operations we want to do on data, and decide on a common way to express these operations, no matter what the implementation details are.
- get/read some data
- set/write some data
- list/see what data we have
- filter
- cache
...
Looking at this, we see that the base operations for complex data systems such as data bases and file systems overlap significantly with the base operations on python (or any programming language) objects.
So we'll reflect this in our choice of a common "language" for these operations. For examples, once projected to a `py2store` object, iterating over the contents of a data base, or over files, or over the elements of a python (iterable) object should look the same, in code. Achieving this, we achieve SOC, but also set ourselves up for tooling that can assume this consistency, therefore be DRY, and many of the SOLID principles of design.
Also mentionable: So far, `py2store` core tools are all pure python -- no dependencies on anything else.
Now, when you want to specialize a store (say talk to data bases, web services, acquire special formats (audio, etc.)), then you'll need to pull in a few helpful packages. But the core tooling is pure.
# A few words about design
By store we mean key-value store. This could be files in a filesystem, objects in s3, or a database. Where and
how the content is stored should be specified, but StoreInterface offers a dict-like interface to this.
__getitem__ calls: _id_of_key _obj_of_data
__setitem__ calls: _id_of_key _data_of_obj
__delitem__ calls: _id_of_key
__iter__ calls: _key_of_id
```pydocstring
>>> from dol import Store
```
A Store can be instantiated with no arguments. By default it will make a dict and wrap that.
```pydocstring
>>> # Default store: no key or value conversion ################################################
>>> s = Store()
>>> s['foo'] = 33
>>> s['bar'] = 65
>>> assert list(s.items()) == [('foo', 33), ('bar', 65)]
>>> assert list(s.store.items()) == [('foo', 33), ('bar', 65)] # see that the store contains the same thing
```
Now let's make stores that have a key and value conversion layer
input keys will be upper cased, and output keys lower cased
input values (assumed int) will be converted to ascii string, and visa versa
```pydocstring
>>>
>>> def test_store(s):
... s['foo'] = 33 # write 33 to 'foo'
... assert 'foo' in s # __contains__ works
... assert 'no_such_key' not in s # __nin__ works
... s['bar'] = 65 # write 65 to 'bar'
... assert len(s) == 2 # there are indeed two elements
... assert list(s) == ['foo', 'bar'] # these are the keys
... assert list(s.keys()) == ['foo', 'bar'] # the keys() method works!
... assert list(s.values()) == [33, 65] # the values() method works!
... assert list(s.items()) == [('foo', 33), ('bar', 65)] # these are the items
... assert list(s.store.items()) == [('FOO', '!'), ('BAR', 'A')] # but note the internal representation
... assert s.get('foo') == 33 # the get method works
... assert s.get('no_such_key', 'something') == 'something' # return a default value
... del(s['foo']) # you can delete an item given its key
... assert len(s) == 1 # see, only one item left!
... assert list(s.items()) == [('bar', 65)] # here it is
>>>
```
We can introduce this conversion layer in several ways.
Here are few...
## by subclassing
```pydocstring
>>> # by subclassing ###############################################################################
>>> class MyStore(Store):
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
>>> s = MyStore(store=dict()) # note that you don't need to specify dict(), since it's the default
>>> test_store(s)
>>>
```
## by assigning functions to converters
```pydocstring
>>> # by assigning functions to converters ##########################################################
>>> class MyStore(Store):
... def __init__(self, store, _id_of_key, _key_of_id, _data_of_obj, _obj_of_data):
... super().__init__(store)
... self._id_of_key = _id_of_key
... self._key_of_id = _key_of_id
... self._data_of_obj = _data_of_obj
... self._obj_of_data = _obj_of_data
...
>>> s = MyStore(dict(),
... _id_of_key=lambda k: k.upper(),
... _key_of_id=lambda _id: _id.lower(),
... _data_of_obj=lambda obj: chr(obj),
... _obj_of_data=lambda data: ord(data))
>>> test_store(s)
>>>
```
## using a Mixin class
```pydocstring
>>> # using a Mixin class #############################################################################
>>> class Mixin:
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
...
>>> class MyStore(Mixin, Store): # note that the Mixin must come before Store in the mro
... pass
...
>>> s = MyStore() # no dict()? No, because default anyway
>>> test_store(s)
```
## adding wrapper methods to an already made Store instance
```pydocstring
>>> # adding wrapper methods to an already made Store instance #########################################
>>> s = Store(dict())
>>> s._id_of_key=lambda k: k.upper()
>>> s._key_of_id=lambda _id: _id.lower()
>>> s._data_of_obj=lambda obj: chr(obj)
>>> s._obj_of_data=lambda data: ord(data)
>>> test_store(s)
```
# Why the name?
- because it's short
- because it's cute
- because it reminds one of "russian dolls" (one way to think of wrappers)
- because we can come up with an acronym the contains "Data Object" in it.
%package help
Summary: Development documents and examples for dol
Provides: python3-dol-doc
%description help
# dol
Base builtin tools make and transform data object layers (dols).
The main idea comes in many names such as
[Data Access Object (DAO)](https://en.wikipedia.org/wiki/Data_access_object),
[Repository Pattern](https://www.cosmicpython.com/book/chapter_02_repository.html),
[Hexagonal architecture, or ports and adapters architecture](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software))
for data.
But simply put, what `dol` provides is tools to make your interface with data be domain-oriented, simple, and isolated from the underlying data infrastucture. This makes the business logic code simple and stable, enables you to develop and test it without the need of any data infrastructure, and allows you to change this infrastructure independently.
The package is light-weight: Pure python; no third-party dependencies.
To install: ```pip install dol```
[Documentation here](https://i2mint.github.io/dol/)
Note: This project started as [`py2store`](https://github.com/i2mint/py2store).
`dol` is the core of py2store has now been factored out
and many of the specialized data object layers moved to separate packages.
`py2store` is acting more as an aggregator package -- a shoping mall where you can quickly access many (but not all)
functionalities that use `dol`.
It's advised to use `dol` (and/or its specialized spin-off packages) directly when the core functionality is all you need.
# A few highlights of py2store's README
## What is this?
Storage CRUD how and where you want it.
List, read, write, and delete data in a structured data source/target,
as if manipulating simple python builtins (dicts, lists), or through the interface **you** want to interact with,
with configuration or physical particularities out of the way.
Also, being able to change these particularities without having to change the business-logic code.
If you're not a "read from top to bottom" kinda person, here are some tips:
[Quick peek](#quick-peek) will show you a simple example of how it looks and feels.
[Use cases](#use-cases) will give you an idea of how py2store can be useful to you, if at all.
The section with the best bang for the buck is probably
[remove (much of the) data access entropy](#remove-data-access-entropy).
It will give you simple (but real) examples of how to use `py2store` tooling
to bend your interface with data to your will.
[How it works](#a-few-words-about-design) will give you a sense of how it works.
[More examples](https://github.com/i2mint/py2store#more-examples)
will give you a taste of how you can adapt the three main aspects of
storage (persistence, serialization, and indexing) to your needs.
Install it (e.g. `pip install py2store`).
## Quick peek
Think of type of storage you want to use and just go ahead, like you're using a dict.
Here's an example for local storage (you must you string keys only here).
```pydocstring
>>> from py2store import QuickStore
>>>
>>> store = QuickStore() # will print what (tmp) rootdir it is choosing
>>> # Write something and then read it out again
>>> store['foo'] = 'baz'
>>> 'foo' in store # do you have the key 'foo' in your store?
True
>>> store['foo'] # what is the value for 'foo'?
'baz'
>>>
>>> # Okay, it behaves like a dict, but go have a look in your file system,
>>> # and see that there is now a file in the rootdir, named 'foo'!
>>>
>>> # Write something more complicated
>>> store['hello/world'] = [1, 'flew', {'over': 'a', "cuckoo's": map}]
>>> stored_val = store['hello/world']
>>> stored_val == [1, 'flew', {'over': 'a', "cuckoo's": map}] # was it retrieved correctly?
True
>>>
>>> # how many items do you have now?
>>> assert len(store) >= 2 # can't be sure there were no elements before, so can't assert == 2
>>>
>>> # delete the stuff you've written
>>> del store['foo']
>>> del store['hello/world']
```
`QuickStore` will by default store things in local files, using pickle as the serializer.
If a root directory is not specified,
it will use a tmp directory it will create (the first time you try to store something)
It will create any directories that need to be created to satisfy any/key/that/contains/slashes.
Of course, everything is configurable.
## A list of stores for various uses
`py2store` provides tools to create the dict-like interface to data you need.
If you want to just use existing interfaces, build on it, or find examples of how to make such
interfaces, check out the ever-growing list of `py2store`-using projects:
- [mongodol](https://github.com/i2mint/mongodol): For MongoDB
- [hear](https://github.com/otosense/hear): Read/write audio data flexibly.
- [tabled](https://github.com/i2mint/tabled): Data as `pandas.DataFrame` from various sources
- [msword](https://pypi.org/project/msword/): Simple mapping view to docx (Word Doc) elements
- [sshdol](https://github.com/i2mint/sshdol): Remote (ssh) files access
- [haggle](https://github.com/otosense/haggle): Easily search, download, and use kaggle datasets.
- [pyckup](https://github.com/i2mint/pyckup): Grab data simply and define protocols for others to do the same.
- [hubcap](https://pypi.org/project/hubcap/): Dict-like interface to github.
- [graze](https://github.com/thorwhalen/graze): Cache the internet.
- [grub](https://github.com/thorwhalen/grub): A ridiculously simple search engine maker.
Just for fun projects:
- [cult](https://github.com/thorwhalen/cult): Religious texts search engine. 18mn application of `grub`.
- [laugh](https://github.com/thorwhalen/laugh): A (py2store-based) joke finder.
## Use cases
### Interfacing reads
How many times did someone share some data with you in the form of a zip of some nested folders
whose structure and naming choices are fascinatingly obscure? And how much time do you then spend to write code
to interface with that freak of nature? Well, one of the intents of py2store is to make that easier to do.
You still need to understand the structure of the data store and how to deserialize these datas into python
objects you can manipulate. But with the proper tool, you shouldn't have to do much more than that.
### Changing where and how things are stored
Ever have to switch where you persist things (say from file system to S3), or change the way key into your data,
or the way that data is serialized? If you use py2store tools to separate the different storage concerns,
it'll be quite easy to change, since change will be localized. And if you're dealing with code that was already
written, with concerns all mixed up, py2store should still be able to help since you'll be able to
more easily give the new system a facade that makes it look like the old one.
All of this can also be applied to data bases as well, in-so-far as the CRUD operations you're using
are covered by the base methods.
### Adapters: When the learning curve is in the way of learning
Shinny new storage mechanisms (DBs etc.) are born constantly, and some folks start using them, and we are eventually lead to use them
as well if we need to work with those folks' systems. And though we'd love to learn the wonderful new
capabilities the new kid on the block has, sometimes we just don't have time for that.
Wouldn't it be nice if someone wrote an adapter to the new system that had an interface we were familiar with?
Talking to SQL as if it were mongo (or visa versa). Talking to S3 as if it were a file system.
Now it's not a long term solution: If we're really going to be using the new system intensively, we
should learn it. But when you just got to get stuff done, having a familiar facade to something new
is a life saver.
py2store would like to make it easier for you roll out an adapter to be able to talk
to the new system in the way **you** are familiar with.
### Thinking about storage later, if ever
You have a new project or need to write a new app. You'll need to store stuff and read stuff back.
Stuff: Different kinds of resources that your app will need to function. Some people enjoy thinking
of how to optimize that aspect. I don't. I'll leave it to the experts to do so when the time comes.
Often though, the time is later, if ever. Few proof of concepts and MVPs ever make it to prod.
So instead, I'd like to just get on with the business logic and write my program.
So what I need is an easy way to get some minimal storage functionality.
But when the time comes to optimize, I shouldn't have to change my code, but instead just change the way my
DAO does things. What I need is py2store.
## Remove data access entropy
Data comes from many different sources, organization, and formats.
Data is needed in many different contexts, which comes with its own natural data organization and formats.
In between both: A entropic mess of ad-hoc connections and annoying time-consuming and error prone boilerplate.
`py2store` (and it's now many extensions) is there to mitigate this.
The design gods say SOC, DRY, SOLID* and such. That's good design, yes. But it can take more work to achieve these principles.
We'd like to make it _easier_ to do it right than do it wrong.
_(*) Separation (Of) Concerns, Don't Repeat Yourself, https://en.wikipedia.org/wiki/SOLID))_
We need to determine what are the most common operations we want to do on data, and decide on a common way to express these operations, no matter what the implementation details are.
- get/read some data
- set/write some data
- list/see what data we have
- filter
- cache
...
Looking at this, we see that the base operations for complex data systems such as data bases and file systems overlap significantly with the base operations on python (or any programming language) objects.
So we'll reflect this in our choice of a common "language" for these operations. For examples, once projected to a `py2store` object, iterating over the contents of a data base, or over files, or over the elements of a python (iterable) object should look the same, in code. Achieving this, we achieve SOC, but also set ourselves up for tooling that can assume this consistency, therefore be DRY, and many of the SOLID principles of design.
Also mentionable: So far, `py2store` core tools are all pure python -- no dependencies on anything else.
Now, when you want to specialize a store (say talk to data bases, web services, acquire special formats (audio, etc.)), then you'll need to pull in a few helpful packages. But the core tooling is pure.
# A few words about design
By store we mean key-value store. This could be files in a filesystem, objects in s3, or a database. Where and
how the content is stored should be specified, but StoreInterface offers a dict-like interface to this.
__getitem__ calls: _id_of_key _obj_of_data
__setitem__ calls: _id_of_key _data_of_obj
__delitem__ calls: _id_of_key
__iter__ calls: _key_of_id
```pydocstring
>>> from dol import Store
```
A Store can be instantiated with no arguments. By default it will make a dict and wrap that.
```pydocstring
>>> # Default store: no key or value conversion ################################################
>>> s = Store()
>>> s['foo'] = 33
>>> s['bar'] = 65
>>> assert list(s.items()) == [('foo', 33), ('bar', 65)]
>>> assert list(s.store.items()) == [('foo', 33), ('bar', 65)] # see that the store contains the same thing
```
Now let's make stores that have a key and value conversion layer
input keys will be upper cased, and output keys lower cased
input values (assumed int) will be converted to ascii string, and visa versa
```pydocstring
>>>
>>> def test_store(s):
... s['foo'] = 33 # write 33 to 'foo'
... assert 'foo' in s # __contains__ works
... assert 'no_such_key' not in s # __nin__ works
... s['bar'] = 65 # write 65 to 'bar'
... assert len(s) == 2 # there are indeed two elements
... assert list(s) == ['foo', 'bar'] # these are the keys
... assert list(s.keys()) == ['foo', 'bar'] # the keys() method works!
... assert list(s.values()) == [33, 65] # the values() method works!
... assert list(s.items()) == [('foo', 33), ('bar', 65)] # these are the items
... assert list(s.store.items()) == [('FOO', '!'), ('BAR', 'A')] # but note the internal representation
... assert s.get('foo') == 33 # the get method works
... assert s.get('no_such_key', 'something') == 'something' # return a default value
... del(s['foo']) # you can delete an item given its key
... assert len(s) == 1 # see, only one item left!
... assert list(s.items()) == [('bar', 65)] # here it is
>>>
```
We can introduce this conversion layer in several ways.
Here are few...
## by subclassing
```pydocstring
>>> # by subclassing ###############################################################################
>>> class MyStore(Store):
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
>>> s = MyStore(store=dict()) # note that you don't need to specify dict(), since it's the default
>>> test_store(s)
>>>
```
## by assigning functions to converters
```pydocstring
>>> # by assigning functions to converters ##########################################################
>>> class MyStore(Store):
... def __init__(self, store, _id_of_key, _key_of_id, _data_of_obj, _obj_of_data):
... super().__init__(store)
... self._id_of_key = _id_of_key
... self._key_of_id = _key_of_id
... self._data_of_obj = _data_of_obj
... self._obj_of_data = _obj_of_data
...
>>> s = MyStore(dict(),
... _id_of_key=lambda k: k.upper(),
... _key_of_id=lambda _id: _id.lower(),
... _data_of_obj=lambda obj: chr(obj),
... _obj_of_data=lambda data: ord(data))
>>> test_store(s)
>>>
```
## using a Mixin class
```pydocstring
>>> # using a Mixin class #############################################################################
>>> class Mixin:
... def _id_of_key(self, k):
... return k.upper()
... def _key_of_id(self, _id):
... return _id.lower()
... def _data_of_obj(self, obj):
... return chr(obj)
... def _obj_of_data(self, data):
... return ord(data)
...
>>> class MyStore(Mixin, Store): # note that the Mixin must come before Store in the mro
... pass
...
>>> s = MyStore() # no dict()? No, because default anyway
>>> test_store(s)
```
## adding wrapper methods to an already made Store instance
```pydocstring
>>> # adding wrapper methods to an already made Store instance #########################################
>>> s = Store(dict())
>>> s._id_of_key=lambda k: k.upper()
>>> s._key_of_id=lambda _id: _id.lower()
>>> s._data_of_obj=lambda obj: chr(obj)
>>> s._obj_of_data=lambda data: ord(data)
>>> test_store(s)
```
# Why the name?
- because it's short
- because it's cute
- because it reminds one of "russian dolls" (one way to think of wrappers)
- because we can come up with an acronym the contains "Data Object" in it.
%prep
%autosetup -n dol-0.1.134
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-dol -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 0.1.134-1
- Package Spec generated
|