Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Created May 12, 2015 01:55
Show Gist options
  • Save NotTheEconomist/52351b173a08930ae42b to your computer and use it in GitHub Desktop.
Save NotTheEconomist/52351b173a08930ae42b to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class MyRow(Base):
__tablename__ = "m"
id = Column(Integer, primary_key=True)
m1 = Column(Integer)
m2 = Column(Integer)
engine = create_engine("sqlite:///")
Base.metadata.create_all(bind=engine)
session = sessionmaker(bind=engine)()
file1 = """1
2
3
4
5
6
"""
file2 = """2
4
6
8
10
3
"""
conditionals = """m1 >=3 && (m1 + m2) >= 11
"""
for c1, c2 in zip(*map(str.splitlines, (file1, file2))):
if not all(map(str.strip, [c1, c2])):
continue # skips blank lines
row = MyRow(m1=int(c1.strip()), m2=int(c2.strip()))
session.add(row)
session.commit()
map_table = {"||": "OR",
"&&": "AND"} # etc
results = {}
for conditional in conditionals.splitlines():
if not conditional.strip():
continue # skips blank lines
for orig, new in map_table.items():
conditional = conditional.replace(orig, new)
query = "SELECT m1, m2 FROM m WHERE " + conditional
result = session.execute(query)
results[conditional] = result.fetchall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment