import sqlalchemy as SA from sqlalchemy import orm import re from date

 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
import sqlalchemy as SA
from sqlalchemy import orm
import re
from datetime import datetime
engine = SA.create_engine('sqlite:///prox.db')
meta= SA.MetaData()
meta.bind = engine
PROXY_BAD = 'b'
PROXY_TRANSPARENT = 't'
PROXY_ANONYMOUS = 'a'
PROXY_ELITE = 'e'
PROXY_TYPES_ANONYMOUS = (PROXY_ANONYMOUS, PROXY_ELITE)
PROXY_TYPES_VALID = (PROXY_TRANSPARENT, PROXY_ANONYMOUS, PROXY_ELITE)
RE_PROXY = re.compile(r'([-a-z0-9.]{5,30}:\d{1,4})', re.I)
MIN_DATE = datetime(1900, 1, 1)
proxy_table = SA.Table('proxy', meta,
SA.Column('proxy', SA.String(100), primary_key=True),
SA.Column('type', SA.String(1), default=''),
SA.Column('reason', SA.String(255)),
SA.Column('speed', SA.Float),
SA.Column('created', SA.DateTime, default=datetime.now),
SA.Column('worked', SA.DateTime, default=MIN_DATE, index=True),
SA.Column('checked', SA.DateTime, default=MIN_DATE, index=True),
SA.Column('searched', SA.DateTime, default=MIN_DATE, index=True),
)
class Base(object):
def __init__(self, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)
class Proxy(Base):
def __repr__(self):
return self.proxy
orm.mapper(Proxy, proxy_table)
meta.create_all()
Session = SA.session_maker(bind=engine)
session = Session()
print session.query(Proxy).count()