Skip to content

Commit 5453eab

Browse files
author
Keiron Pizzey
committed
Implement basic Question object for querying.
1 parent c44b527 commit 5453eab

File tree

8 files changed

+148
-53
lines changed

8 files changed

+148
-53
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,5 @@ crashlytics-build.properties
128128

129129
### Virtualenv
130130

131-
venv/*
131+
venv/*
132+
dev.py

kesh/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Empty __init__.py
1+
__version__ = '0.1.dev'

kesh/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .core import Connection
1+
from .connection import MongoConnection

kesh/api/connection.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from pymongo import MongoClient
2+
3+
4+
class Connection(object):
5+
6+
def __init__(self):
7+
super().__init__()
8+
9+
10+
class MongoConnection(Connection):
11+
"""Connection object for connecting to the mongodb database and retrieving data."""
12+
13+
def __init__(self, db, mongo_options={}):
14+
super().__init__()
15+
16+
self.client = MongoClient(**mongo_options)
17+
self.db = self.client[db]
18+
19+
20+
def query(self, d):
21+
22+
coll_name = d.pop('collection', None)
23+
if coll_name is None:
24+
raise Exception('Collection param not found in query.')
25+
26+
coll = self.db[coll_name]
27+
28+
if 'id' in d:
29+
return coll.find_one(d)
30+
31+
if 'ids' in d.keys():
32+
return list(coll.find({'id':{'$in':d['ids']}}))
33+
34+
if 'all' in d and d['all']:
35+
return coll.find()
36+
37+
38+
def __enter__(self):
39+
"""For use with the "with" statement. Will create an open db connection.
40+
41+
:return: Client connection.
42+
"""
43+
44+
return self
45+
46+
def __exit__(self, exc_type, exc_val, exc_tb):
47+
"""For use with the "with" statement. Will disconnect from db connection.
48+
:param exc_type:
49+
:param exc_val:
50+
:param exc_tb:
51+
:return:
52+
"""
53+
54+
self.client.close()
55+
56+
57+
if __name__ == '__main__':
58+
59+
with MongoConnection() as conn:
60+
print(conn)

kesh/api/core.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

kesh/api/post.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
class Post(object):
4+
5+
def __init__(self, db):
6+
super().__init__()
7+
8+
self.db = db

kesh/api/question.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from .post import Post
2+
3+
class Question(Post):
4+
5+
def __init__(self, db):
6+
super().__init__(db=db)
7+
8+
9+
def get_question_by_id(self, id):
10+
d = {'collection':'questions', 'id':id}
11+
12+
result = self.db.query(d)
13+
return result
14+
15+
def get_question_by_ids(self, ids):
16+
d = {'collection':'questions', 'ids':ids}
17+
18+
result = self.db.query(d)
19+
return result
20+
21+
def get_all_questions(self):
22+
d = {'collection':'questions', 'all':True}
23+
24+
result = self.db.query(d)
25+
return result

setup.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import re
2+
import os
3+
4+
from setuptools import setup, find_packages
5+
6+
try:
7+
with open('requirements.txt') as f:
8+
requirements = f.read().splitlines()
9+
except FileNotFoundError:
10+
requirements = []
11+
12+
try:
13+
with open(os.path.join(os.path.dirname(__file__), 'kesh', '__init__.py')) as f:
14+
version = re.search(r"__version__ = '(.*)'", f.read()).group(1)
15+
except FileNotFoundError:
16+
version = 'test'
17+
18+
classifiers = [
19+
'Development Status :: 1 - Planning',
20+
21+
'Operating System :: OS Independent',
22+
'Operating System :: POSIX',
23+
'Operating System :: MacOS :: MacOS X',
24+
'Operating System :: Microsoft :: Windows',
25+
26+
'Programming Language :: Python',
27+
'Programming Language :: Python :: 3',
28+
'Programming Language :: Python :: 3.3',
29+
'Programming Language :: Python :: 3.4',
30+
'Programming Language :: Python :: 3 :: Only',
31+
32+
'Intended Audience :: Developers',
33+
'Intended Audience :: Science/Research',
34+
35+
'Topic :: Internet',
36+
'Topic :: Scientific/Engineering :: Information Analysis',
37+
'Topic :: Software Development :: Libraries :: Python Modules',
38+
]
39+
40+
setup(
41+
name='kesh',
42+
packages=find_packages(),
43+
url='http://sopython.com',
44+
license='BSD',
45+
author='Keiron Pizzey',
46+
author_email='[email protected]',
47+
description='Database program for accessing Stack Overflow information.',
48+
install_requires=requirements,
49+
classifiers=classifiers,
50+
version=version
51+
)

0 commit comments

Comments
 (0)