1
- # lazyops
2
- A collection of Lazy Python Functions that aim to balance functionality, performance, and utility.
1
+ # Lazy Op(eration)s or lazyops
3
2
4
- ** Why**
5
- After the nth time of rewriting and/or copying existing functions, I figured it was easier to maintain them all in a single library.
6
-
7
- ### Dependencies
8
- - Python 3.7+
9
- - [ pysimdjson] ( https://github.com/TkTech/pysimdjson ) : simdjson's C++ JSON parser can handle 2 GB/s+ of throughput, allowing millions of JSON docs to be parsed per sec/per core.
10
- - [ fileio] ( https://github.com/trisongz/fileio ) : My personal file lib that wraps many useful I/O ops, allowing you to work with cloud objects faster, and in less lines of code.
11
- - [ aiohttp] ( https://github.com/aio-libs/aiohttp ) : Enables async http calls, maximizing throughput for web requests.
12
- - [ dateparser] ( https://github.com/scrapinghub/dateparser ) : Makes working with natural query dates easier ` 5 days ago ` .
13
- - [ six] ( ) : Enables functionality in ` retryable ` decorator.
3
+ A collection Python modules and submodules focused on balancing functionality,
4
+ performance, utility and Lazyiness.
14
5
15
6
---
16
7
@@ -19,11 +10,149 @@ Install directory from pypi
19
10
20
11
` pip install --upgrade lazyops `
21
12
13
+ Install from Github
14
+
15
+ ` pip install --upgrade git+https://github.com/trisongz/lazyops `
16
+
17
+ ---
18
+
19
+ ## The ` LazyOps ` Philosophy
20
+
21
+ ** Minimalistic** : Minimal External Libraries/Dependencies. Minimal imports.
22
+
23
+ ** Automatic and JIT** : Use a ` Just-in-Time ` approach for loading dependencies.
24
+ Auto-Install external libraries ` as needed ` for submodules.
25
+
26
+ ** Minimal Lines of Code to Accomplish Goal** : Tries to handle as many use-cases
27
+ as possible with minimal limitations on end-user in how its defined.
28
+
29
+ ** High Performant** : Carefully selected, battle-tested libraries providing the
30
+ highest level of performance, while consuming as little resources as needed (RAM)
31
+ or scale to as many resources allowed (Threads)
32
+
33
+ ** (Mostly) Filesystem Agnostic** : Cloud Object _ (GCS atm)_ or Local files are
34
+ supported automatically.
35
+
36
+ ** Reduce Redudancy** : Handle redundant user logic automatically unless explicitly
37
+ defined.
38
+
39
+ ** Object Oriented** : Clean Classes and Objects allow for out-of-the-box usage or
40
+ for customizability.
41
+
42
+ ** Builtin Async** : If it needs to be done fast or many times, ` async ` should probably
43
+ be available.
44
+
45
+ Counter-intuitively, building ` lazyops ` was anything-but-lazy. To (try) to accomplish
46
+ all the above, every class, object, or function had to be designed to be written once
47
+ with the idea that they can stand on their own for as many use cases as possible.
48
+
49
+ This project is a work in process and will continue to evolve over time.
50
+
51
+
52
+ ---
53
+
54
+ ### The Minimal Dependencies (and rationale)
55
+
56
+ - Python 3.7+
57
+ - [ pysimdjson] ( https://github.com/TkTech/pysimdjson ) : simdjson's C++ JSON parser can
58
+ handle 2 GB/s+ of throughput, allowing millions of JSON docs to be parsed per sec/per core.
59
+ - [ fileio] ( https://github.com/trisongz/fileio ) : My personal file lib that wraps many
60
+ useful I/O ops, allowing you to work with cloud objects faster, and in less lines of code.
61
+ - [ aiohttp] ( https://github.com/aio-libs/aiohttp ) : Enables async http calls, maximizing
62
+ throughput for web requests.
63
+ - [ dateparser] ( https://github.com/scrapinghub/dateparser ) : Makes working with natural
64
+ query dates easier ` 5 days ago ` .
65
+ - [ six] ( ) : Enables functionality in ` retryable ` decorator.
66
+
22
67
---
23
68
24
- ### API Usage
69
+ ### LazyOps API Usage
70
+
71
+ - [ LazyOps Core] ( #lazyops-core )
72
+ - [ LazyClassses] ( #lazyclasses )
73
+ - [ LazyAPI] ( #lazyapi )
74
+ - [ LazyDatabase] ( # ) - WIP
75
+ - [ LazyRPC] ( # ) - WIP
76
+ - [ LazyIO] ( # ) - WIP
77
+ - [ LazySources] ( # ) - WIP
78
+ - [ LazyBloom] ( # ) - WIP
79
+
80
+
81
+ ---
82
+
83
+ #### LazyOps Core
84
+
85
+ LazyOps' Core APIs that can be used to handle most common functionalities that can often
86
+ be redundant to constantly define.
87
+
88
+ ** Utilities**
89
+
90
+ Useful functions that speed up development.
91
+
92
+ ``` python
93
+ from lazyops import lazy_init
94
+
95
+ # Lazily initialize Tensorflow and ensure Tensorflow == 2.5.0
96
+ # before importing
97
+ # tf can now be used as you would if you called `import tensorflow as tf`
98
+
99
+ tf = lazy_init(' tensorflow==2.5.0' , ' tensorflow' )
100
+ physical_devices = tf.config.list_physical_devices(' GPU' )
101
+ print (" Num GPUs:" , len (physical_devices))
102
+
103
+ from lazyops import get_logger
104
+
105
+ # This will create a logger that handles stderr / stdout for Notebook envs
106
+ # This logger is also threadsafe as it creates a threadlock to prevent duplication
107
+
108
+ logger = get_logger(' MyLib' , submodule = __file__ )
109
+
110
+ from lazyops import timed_cache, retryable, lazymultiproc, lazyproc
111
+
112
+ def get_dataset (* args , ** kwargs ):
113
+ # do stuff
114
+ return dataset
115
+
116
+ # default num_processes = CPU Cores
117
+ @lazymultiproc (dataset_callable = get_dataset, num_procs = 10 )
118
+ def process_dataset (item , * args , ** kwargs ):
119
+ # do stuff
120
+ return item
121
+
122
+ # LRU Cache with timed expiration
123
+ @timed_cache (seconds = 60 , maxsize = 600 )
124
+ def load_dataset (* args , ** kwargs ):
125
+ # do stuff
126
+ return dataset
25
127
26
- ** Time/Timer/Date Functionalities**
128
+ # turns this function into a thread.Thread function, returning thread obj.
129
+ @lazyproc (name = ' monitoring_x' , start = True , daemon = False )
130
+ def monitor_x (* args , ** kwargs ):
131
+ # do stuff
132
+ return
133
+
134
+ # Retries this function call
135
+ @retryable (stop_max_attempt_number = 5 )
136
+ def call_api (* args , ** kwargs ):
137
+ # do stuff
138
+ return response
139
+
140
+ # Utils for running shell commands and cloning repo projects
141
+ from lazyops import run_cmd, clone_repo
142
+
143
+ # clones lazyops to relative /cwd/lops.
144
+ # if add_to_syspath = True, will append repo path to $PATH
145
+ clone_repo(repo = ' https://github.com/trisongz/lazyops' , path = ' lops' , absl = False , add_to_syspath = True )
146
+
147
+ # returns system response of `df -h` as string
148
+ res = run_cmd(' df -h' )
149
+
150
+ ```
151
+
152
+
153
+ ** Time/Timer/Date**
154
+
155
+ Handle time, timers and datetime mostly automatically.
27
156
28
157
``` python
29
158
import time
@@ -63,13 +192,17 @@ timer.stop_timer('benchmark_1')
63
192
timer.timers()
64
193
65
194
```
195
+ ---
66
196
67
- ** Lazyclass Functionalities ** [ WIP ]
197
+ #### LazyClasses
68
198
69
- * To Do:*
70
- - Implement ` many=True ` found in ` dataclasses_json `
199
+ LazyClasses are an extension of ` dataclasses ` , building on top of [ fastclasses-json] ( https://github.com/cakemanny/fastclasses-json ) ,
200
+ and inspired by heavy usage of [ dataclasses-json] ( https://github.com/lidatong/dataclasses-json ) . ` LazyClass ` utilizes ` simdjson ` as
201
+ the default ` JSON ` serializer for faster performance.
71
202
72
203
204
+ Example Usage:
205
+
73
206
``` python
74
207
from dataclass import dataclass
75
208
from lazyops import lazyclass
@@ -113,12 +246,24 @@ hero_config = {
113
246
}
114
247
115
248
Batman = NewHero.load(hero_config)
249
+ # Or directly
250
+ Batman = NewHero(** hero_config)
116
251
117
252
```
118
253
119
- ** LazyAPI Functionalities**
254
+ ** Roadmap**
255
+ - Implement ` many=True ` found in ` dataclasses_json `
256
+
257
+ ---
258
+
259
+ #### LazyAPI
260
+
261
+ Define and Create API Endpoints as a Class with just a ` Dict ` with
262
+ builtin ` async ` support. API Models are compiled ` JIT ` with endpoints
263
+ set as the ` LazyAPI ` class attribute, enabling them to be called as
264
+ a function.
120
265
121
- Build JIT API Models from a JSON / Dict, leveraging lazyclass
266
+ Example Usage
122
267
123
268
``` python
124
269
from lazyops import LazyAPI
@@ -158,5 +303,53 @@ res = api.predict(data='api model task data ...')
158
303
print (res)
159
304
160
305
```
306
+ ---
307
+
308
+ #### LazyRPC
309
+
310
+ WIP
311
+
312
+ ---
313
+
314
+ #### LazyDatabase
315
+
316
+ WIP
317
+
318
+ ---
319
+
320
+ #### LazyIO
321
+
322
+ WIP
323
+
324
+ ---
325
+
326
+ #### LazySources
327
+
328
+ WIP
329
+
330
+ ---
331
+
332
+ #### LazyBloom
333
+
334
+ WIP
335
+
336
+ ---
337
+
338
+
339
+ #### Credits & Acknowledgements
340
+
341
+ ` LazyOps ` heavily builds & borrows on the work of others in many instances.
342
+ In order to maintain minimalism and not have unneeded libraries added to dependencies,
343
+ some parts of the library have been added into ` LazyOps ` . The original works often inspired
344
+ new ideas from studying the code and are definitely worth checking out.
345
+
346
+
347
+ In no particular order:
348
+
349
+ - [ retrying] ( https://github.com/rholder/retrying )
350
+ - [ fastapi-jsonrpc] ( https://github.com/smagafurov/fastapi-jsonrpc )
351
+ - [ fastclasses-json] ( https://github.com/cakemanny/fastclasses-json )
352
+ - [ dataclasses-json] ( https://github.com/lidatong/dataclasses-json )
353
+ - [ gdelt-doc-api] ( https://github.com/alex9smith/gdelt-doc-api )
161
354
162
355
TBC
0 commit comments