Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

inspect-json.py 1.6 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Usage:
  3. inspect-json.py --gr-work [ID...]
  4. inspect-json.py --gr-book [ID...]
  5. """
  6. import sys
  7. import re
  8. import json
  9. from docopt import docopt
  10. from bookdata import tracking, db, script_log
  11. class GR:
  12. def __init__(self, type):
  13. self.type = type
  14. def __call__(self, ids):
  15. with db.connect() as dbc, dbc.cursor() as cur:
  16. if ids:
  17. return [self._id_rec(cur, r) for r in ids]
  18. else:
  19. return [self._top_rec(cur)]
  20. def _id_rec(self, cur, id):
  21. t = self.type
  22. _log.info('fetching %s %s', t, id)
  23. q = f'''
  24. SELECT gr_{t}_data
  25. FROM gr.raw_{t} JOIN gr.{t}_ids USING (gr_{t}_rid)
  26. WHERE gr_{t}_id = %s
  27. '''
  28. cur.execute(q, [id])
  29. rec = cur.fetchone()
  30. if rec is None:
  31. _log.error('%s %s not found', t, id)
  32. else:
  33. return rec[0]
  34. def _top_rec(self, cur):
  35. t = self.type
  36. _log.info('fetching one %s', t)
  37. q = f'SELECT gr_{t}_data FROM gr.raw_{t} LIMIT 1'
  38. cur.execute(q)
  39. data, = cur.fetchone()
  40. _log.debug('got %r', data)
  41. return data
  42. __gr_work = GR('work')
  43. __gr_book = GR('book')
  44. _log = script_log(__name__)
  45. opts = docopt(__doc__)
  46. rec_ids = opts.get('ID', None)
  47. if rec_ids:
  48. rec_ids = [int(r) for r in rec_ids]
  49. recs = None
  50. for k in opts.keys():
  51. fn = k.replace('-', '_')
  52. if k.startswith('--') and opts[k] and fn in globals():
  53. f = globals()[fn]
  54. recs = f(rec_ids)
  55. if recs is None:
  56. _log.error('could not find an operation to perform')
  57. for rec in recs:
  58. print(json.dumps(rec, indent=2))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...