exportoo/utils.py

69 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-01-17 19:04:33 +01:00
from __future__ import print_function
import os
2022-01-17 19:09:09 +01:00
import sys
2022-01-17 19:04:33 +01:00
import json
2022-01-17 23:56:27 +01:00
from config import *
2022-01-17 19:04:33 +01:00
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
2022-01-17 19:09:09 +01:00
# #from typing import Iterable # > py38
# from collections import Iterable # < py38
2022-01-17 19:04:33 +01:00
# def flatten(items):
# """Yield items from any nested iterable; see Reference."""
# for x in items:
# if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
# for sub_x in flatten(x):
# yield sub_x
# else:
# yield x
def jlog( obj ):
print( json.dumps(obj, indent=4) ) #"\t") )
def ensure_dir(d):
if not os.path.exists(d):
os.makedirs(d)
def args2domains( list ):
domains = []
while len(list) > 0:
str = list[0]
if str == '&' or str == '|' or str == '!':
domains.append( str )
list = list[1:]
else:
domains.append( (list[0], list[1], list[2]) )
list = list[3:]
return domains
# eprint( args2domains(sys.argv[3:]) )
def file_path( muid, format ):
return "%s/%s.%s" % ( DATADIR, muid, format )
2022-01-17 19:04:33 +01:00
def json_path( model, id ):
return "%s/%s.json" % ( DATADIR, Muid(model, id) )
2022-01-17 19:04:33 +01:00
def Muid( model, id ):
return "%s/%s" % ( model, id )
def uniq( arr ):
return list(set(arr))
def groupby_model( arr ):
res = {}
for muid in arr:
model, id = muid.split('/')
if not model in res:
res[model] = []
res[model].append( int(id) )
return res