GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-dbtxn/db_txn.py at master · echaozh/python-dbtxn

Expand file tree

/

db_txn.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
102 lines (89 loc) · 2.79 KB
/

db_txn.py

Copy path

File metadata and controls

102 lines (89 loc) · 2.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from collections import namedtuple
from contextlib import closing
from functools import partial
DONE = 0
QUERY = 1
EXECUTE = 2 # can insert, but won't report insert id
INSERT = 3
RECURSE = 4 # recursive generator
def db_result(*val, **named):
assert len(val) == 0 or len(named) == 0
if len(val) == 1:
return (DONE, val[0])
elif len(val) > 1:
return (DONE, val)
elif len(named) != 0:
t = namedtuple('_', named.keys())
return (DONE, t(**named))
else:
return (DONE, None)
def db_input(t, sql, *args):
return (t, (sql, args))
db_query = partial(db_input, QUERY)
db_execute = partial(db_input, EXECUTE)
db_insert = partial(db_input, INSERT)
def db_recurse(g):
return (RECURSE, g)
def for_recurse(f):
def wrapper(*args, **kwargs):
return db_recurse(f(*args, **kwargs))
return wrapper
class _NextIteration(BaseException):
def __init__(value):
super(_NextIteration, self).__init__()
self.value = value
def _exec(cur, g):
def _run_for_gen(f, *args, **kwargs):
try:
return f(*args, **kwargs)
except StopIteration:
raise
except Exception as e:
raise _NextIteration(g.throw(type(e), e))
what, val = next(g)
while True:
try:
if what == DONE:
g.close()
return val
elif what == RECURSE:
try:
r = _run_for_gen(_exec, cur, val)
except StopIteration:
r = None
what, val = g.send(r)
else:
sql, args = val
_run_for_gen(cur.execute, sql, args)
rowc = cur.rowcount
rs = None
if what == QUERY:
rows = _run_for_gen(cur.fetchall)
cols = [c[0] for c in cur.description]
nt = namedtuple('_', cols)
rs = [nt(*list(r)) for r in rows]
rowc = len(rs)
elif what == INSERT:
rs = cur.lastrowid
what, val = g.send((rowc, rs))
except _NextIteration as e:
what, val = e.value
def db_txn(pool, gen, *args, **kwargs):
g = gen(*args, **kwargs)
with closing(pool.connection()) as conn:
try:
conn.begin()
except AttributeError:
pass
try:
with closing(conn.cursor()) as cur:
res = _exec(cur, g)
conn.commit()
return res
except StopIteration: # no val to return
conn.commit()
return None
def in_txn(f):
def wrapper(pool, *args, **kwargs):
return db_txn(pool, f, *args, **kwargs)
return wrapper
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.