<commit_before>import os
import re
import sys

import pymongo

dirWithIds = sys.argv[1]

pazansMongo = pymongo.MongoClient("equal.cf")
pazansDb = pazansMongo['pazans']
pazansCollection = pazansDb['pazans']

# gotoMongo = pymongo.MongoClient("goto.reproducible.work")
# gotoDb = gotoMongo['vk']
# gotoUserCollection = gotoDb['users']
usersJsonFile = sys.argv[2]

ids = set()

idsFile = sys.argv[3]
if not os.path.isfile(idsFile):
	idsRegex = re.compile('\\"_id\\": (.+?),')
	with open(usersJsonFile, "r") as fileName:
		for line in fileName:
			groups = idsRegex.search(line)
			id = int(groups.group(1))
			ids.add(id)

	with open(idsFile, "w") as fileName:
		for id in ids:
			fileName.write(str(id) + "\n")
else:
	with open(idsFile, "r") as fileName:
		for line in fileName:
			ids.add(int(line))

for fileName in os.listdir(dirWithIds):
	print("parsing", fileName)

	with open(os.path.join(dirWithIds, fileName), "r") as file:
		for line in file:
			id = int(line)
			if id in ids:
				pazan = pazansCollection.find_one(id)
				if pazan is None:
					pazansCollection.insert_one({"_id": id, "groups": [fileName]})
				elif fileName not in pazan["groups"]:
					pazan["groups"].append(fileName)
					pazansCollection.update_one(
							{"_id": pazan["_id"]},
							{"$set": {"groups": pazan["groups"]}}
					)

	print("- done")<commit_msg>Some refactoring to push-pazans-to-mongo.py (also PEP-8 changes)<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*- 

import os
import re
import sys

import pymongo


dirWithIds = sys.argv[1]

pazansMongo      = pymongo.MongoClient("equal.cf")
pazansDb         = pazansMongo['pazans']
pazansCollection = pazansDb['pazans']

# gotoMongo = pymongo.MongoClient("goto.reproducible.work")
# gotoDb = gotoMongo['vk']
# gotoUserCollection = gotoDb['users']
usersJsonFile = sys.argv[2]

ids = set()

idsFile = sys.argv[3]
if not os.path.isfile(idsFile):
	idsRegex = re.compile('\\"_id\\": (.+?),')
	with open(usersJsonFile, "r") as file_name:
		for line in file_name:
			groups = idsRegex.search(line)
			uid = int(groups.group(1))
			ids.add(uid)

	with open(idsFile, "w") as file_name:
		for uid in ids:
			file_name.write(str(uid) + "\n")
else:
	with open(idsFile, "r") as f_ids:
		for line in f_ids:
			f_ids.add(int(line))

for file_name in os.listdir(dirWithIds):
	print("parsing {}".format(file_name))

	with open(os.path.join(dirWithIds, file_name), "r") as file:
		for line in file:
			id = int(line)
			if id in ids:
				pazan = pazansCollection.find_one(id)
				if pazan is None:
					pazansCollection.insert_one({"_id": id, "groups": [file_name]})
				elif file_name not in pazan["groups"]:
					pazan["groups"].append(file_name)
					pazansCollection.update_one(
							{"_id": pazan["_id"]},
							{"$set": {"groups": pazan["groups"]}}
					)

	print("- done")
{'fraction_non_alphanumeric': 0.16388197653750444, 'fraction_numerical': 0.0035549235691432635, 'mean_word_length': 4.052064631956912, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '626246', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1086, 'n_words': 238}