In [1]:
import json
import pymongo
import subprocess
import os
from collections import defaultdict
from bson import BSON

In [14]:
base_dir = '/home/mohitsharma44/assignments/python_core/'
# Every folder has a Submission attachment(s) subfolder
sub_dir2 = 'Submission_attachment_s_'
dirs = os.listdir(base_dir)

In [15]:
dirs[0][:-1]


Out[15]:
'Bisht_Bhagwat_bsb342'

In [16]:
os.listdir(os.path.join(base_dir,dirs[0]))


Out[16]:
['Submission_attachment_s_', 'timestamp.txt']

In [17]:
# Store output to a variable with whitespaces removed.
#files = subprocess.check_output(['ls', os.path.join(base_dir,dirs[0],sub_dir2)]).rstrip()
files = os.listdir(os.path.join(base_dir,dirs[0],sub_dir2))

In [24]:
all_files = []
all_users = []
for root, dirs, files in os.walk(base_dir):
    if files:
        for i in files:
            if i.endswith('py'):
                all_users.append(root.split('/')[5])
                all_files.append(os.path.join(root,i))

In [19]:
a = subprocess.Popen(['pylint', all_files[0], '--rcfile=pylintrc.txt','-f', 'json'], stdout=subprocess.PIPE).stdout.read()

In [20]:
j_a = json.loads(a)

In [21]:
d_a = defaultdict(list)
for i in j_a:
    for k, v in i.iteritems():
        d_a[k].append(v)
d_a['name'].append(all_users[0])

In [22]:
d_a.keys()


Out[22]:
[u'obj',
 'name',
 u'column',
 u'symbol',
 u'module',
 u'path',
 u'message',
 u'type',
 u'line']

In [1]:
from patoolib import extract_archive

In [ ]:
extract_archive

In [60]:
j_b = json.dumps(d_a, sort_keys=True, indent=4)

In [61]:
j_c = json.loads(j_b)

In [39]:
temp_f = []
for root, dirs, files in os.walk(base_dir):
    temp_f.append(files)

In [47]:



---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-47-1b69d37bf03b> in <module>()
----> 1 print [x for x in y for y in temp_f if len(y)> 0]

NameError: name 'y' is not defined

In [ ]:


In [ ]:


In [2]:
conn = pymongo.MongoClient()

In [3]:
conn.database_names()


Out[3]:
[u'local', u'test_1', u'test']

In [4]:
db = conn.test_1

In [5]:
db.collection_names()


Out[5]:
[u'system.indexes', u'testcollection']

In [6]:
collection = db.testcollection

In [7]:
collection.count()


Out[7]:
1

In [34]:
collection.insert(j_c)


/home/mohitsharma44/.local/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
  if __name__ == '__main__':
Out[34]:
ObjectId('55d9bb29cff3d4263a27c362')

In [35]:
for i in collection.find():
    temp_id = i['_id']

In [244]:
collection.remove({'_id':temp_id})


/home/mohitsharma44/.local/lib/python2.7/site-packages/ipykernel/__main__.py:1: DeprecationWarning: remove is deprecated. Use delete_one or delete_many instead.
  if __name__ == '__main__':
Out[244]:
{u'ok': 1, u'n': 1}

In [39]:
for i in collection.find({'name':'TestUser'}):
    print i['name']


[u'TestUser']

In [13]:
collection.find().sort([{'_id',-1}]).limit(10)


Out[13]:
<pymongo.cursor.Cursor at 0x7fcdb1cb5450>

In [25]:
os.path.join(root,,i)


Out[25]:
'/home/mohitsharma44/assignments/python_core/Feinglass_Maxwell_mf3319_/Submission_attachment_s_/Max_Feinglass_CUSP_PythonLab_Core_Challenge.py'

In [ ]: