Commit a68b6d27 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

refacto style

parent 26fc8187
...@@ -14,22 +14,20 @@ logger = logging.getLogger(__name__) ...@@ -14,22 +14,20 @@ logger = logging.getLogger(__name__)
class ImportKEGGKO(object): class ImportKEGGKO(object):
KEGG_KO_LIST_API = "http://rest.kegg.jp/list/ko" KEGG_KO_LIST_API = "http://rest.kegg.jp/list/ko"
ORM_SOURCE_KEY = 'source'
KEGG_SOURCE = 'kegg'
def __init__(self, kegg_ko_list_api=KEGG_KO_LIST_API): def __init__(self, kegg_ko_list_api=KEGG_KO_LIST_API):
self.kegg_ko_list_api = kegg_ko_list_api self.kegg_ko_list_api = kegg_ko_list_api
self.processed_kegg_count = 0 self.processed_count = 0
self.created_kegg_count = 0 self.created_count = 0
self.updated_kegg_count = 0 self.updated_count = 0
self.skipped_kegg_count = 0 self.skipped_count = 0
self.skipped_kegg_ids = [] self.skipped_ids = []
def load_all_kegg_ko(self, chunk_size=1000, test=False): def load_all_kegg_ko(self, chunk_size=1000, test=False):
all_ko_response = requests.get(self.kegg_ko_list_api) all_ko_response = requests.get(self.kegg_ko_list_api)
all_ko_response.raise_for_status() all_ko_response.raise_for_status()
all_ko = all_ko_response.text.splitlines() all_ko = all_ko_response.text.splitlines()
self.total_kegg_nb = len(all_ko) self.total_nb = len(all_ko)
for chunk in generate_chunks(all_ko, chunk_size): for chunk in generate_chunks(all_ko, chunk_size):
ko_chunk = [KEGGLineParser.ko_list(i) for i in chunk] ko_chunk = [KEGGLineParser.ko_list(i) for i in chunk]
for i in ko_chunk: for i in ko_chunk:
...@@ -37,25 +35,25 @@ class ImportKEGGKO(object): ...@@ -37,25 +35,25 @@ class ImportKEGGKO(object):
try: try:
kegg = KeggOrthology(**payload) kegg = KeggOrthology(**payload)
kegg.save() kegg.save()
self.created_kegg_count += 1 self.created_count += 1
except IntegrityError: except IntegrityError:
try: try:
kegg = KeggOrthology.objects.get(function_id=payload.get('function_id')) kegg = KeggOrthology.objects.get(function_id=payload.get('function_id'))
for k, v in payload.items(): for k, v in payload.items():
setattr(kegg, k, v) setattr(kegg, k, v)
kegg.save() kegg.save()
self.updated_kegg_count += 1 self.updated_count += 1
except IntegrityError: except IntegrityError:
self.skipped_kegg_ids.append(payload.get('function_id')) self.skipped_ids.append(payload.get('function_id'))
self.skipped_kegg_count += 1 self.skipped_count += 1
self.processed_kegg_count += len(ko_chunk) self.processed_count += len(ko_chunk)
logger.info("%s/%s KEGG KO processed so far...", self.processed_kegg_count, self.total_kegg_nb) logger.info("%s/%s KEGG KO processed so far...", self.processed_count, self.total_nb)
if test: if test:
break break
logger.info("[DONE] %s/%s KEGG KO created.", self.created_kegg_count, self.total_kegg_nb) logger.info("[DONE] %s/%s KEGG KO created.", self.created_count, self.total_nb)
logger.info("[DONE] %s/%s KEGG KO updated.", self.updated_kegg_count, self.total_kegg_nb) logger.info("[DONE] %s/%s KEGG KO updated.", self.updated_count, self.total_nb)
logger.info("[DONE] %s/%s KEGG KO skipped. List: %s", self.skipped_kegg_count, self.total_kegg_nb, logger.info("[DONE] %s/%s KEGG KO skipped. List: %s", self.skipped_count, self.total_nb,
self.skipped_kegg_ids) self.skipped_ids)
class Command(BaseCommand): class Command(BaseCommand):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment