def get_ip_infos(ip_address: str = None) -> dict: """ A dict with infos about the ip address of your computer (unless you use a vpn) or a specified ip Args: ip_address (optional): IP from which you want to receive the information Returns: A dict filled with the ip address information Examples: >>> print(get_ip_infos()()) {'ip': '69.69.69.69', 'hostname': 'examplehostname', 'city': 'Suginami City', 'region': 'Tokyo', 'country': 'JP, 'loc': '35.6986, 139.6367', 'org': 'exampleprovider', 'postal': '166-0015', 'timezone': 'Asia/Tokyo', 'readme': 'https://ipinfo.io/missingauth'} """ if ip_address: return _load(_urlopen('http://ipinfo.io/' + ip_address + '/json')) else: return _load(_urlopen('http://ipinfo.io/json'))
def __init__(self, path="./EmberaDB/", hash_method=md5): # WIP import warnings warnings.warn("EmberaDataBase is a Work In Progress. All file formats and indexing is subject to change.") self.path = path if not os.path.isdir(self.path): os.mkdir(self.path) self.bqms_path = os.path.join(self.path,'bqms') if not os.path.isdir(self.bqms_path): os.mkdir(self.bqms_path) self.embeddings_path = os.path.join(self.path,'embeddings') if not os.path.isdir(self.embeddings_path): os.mkdir(self.embeddings_path) self.samplesets_path = os.path.join(self.path,'samplesets') if not os.path.isdir(self.samplesets_path): os.mkdir(self.samplesets_path) self.reports_path = os.path.join(self.path,'reports') if not os.path.isdir(self.reports_path): os.mkdir(self.reports_path) self.aliases_path = os.path.join(self.path,'aliases.json') if os.path.exists(self.aliases_path): with open(self.aliases_path,'r') as fp: self.aliases = _load(fp) self.hash = lambda ser: hash_method(ser).hexdigest()
def load_samplesets(self, bqm, target, embedding, tags=[], unembed_args=None): bqm_id = self.id_bqm(bqm) target_id = self.id_target(target) embedding_id = self.id_embedding(embedding) dir_path = [self.samplesets_path,bqm_id,target_id,embedding_id] samplesets_path = os.path.join(*dir_path) samplesets = [] for root, dirs, files in os.walk(samplesets_path): root_dirs = os.path.normpath(root).split(os.path.sep) if all(tag in root_dirs for tag in tags): for file in files: sampleset_path = os.path.join(root,file) with open(sampleset_path,'r') as fp: sampleset = _load(fp,cls=DimodDecoder) samplesets.append(sampleset) if embedding is "": return samplesets elif not isinstance(embedding,(Embedding,dict)): raise ValueError("Embedding alias or id cannot be used to unembed") if unembed_args is None: return samplesets else: return [unembed_sampleset(s,embedding,bqm,**unembed_args) for s in samplesets]
def get_cached_file(fn, cd=cached_dir): file = f"{_join(cd,fn)}.data.json" if not _isfile(file): return None with open(file) as f: try: return _load(f) except Exception as e: print(e) return None
def from_file(cls, path, alias=None, errors=True): path = _LazyPath(path) if path.exists(): with path.read() as tmp_file: rarg = cls.from_collection(_load(tmp_file)) return rarg.extract(alias) else: if errors: raise FileNotFoundError( "[Errno 2] No such file or directory: '{}'".format(path)) else: return None
def load_config(config_fp): """ Loads an specific config file in json format :param config_fp: Filepath of the config file. :return: Dictionary with the configuration. """ _dict = None with open(config_fp) as c: _dict = _load(c, object_hook=hinted_tuple_hook) return _dict
def load_jsonfile(filepath): """ :param filepath: :return: """ """ Loads an specific json file :param filepath: Filepath of a json file. :return: A dictionary """ with open(filepath) as file: return _load(file)
def load_reports(self, bqm, target, tags=[], dataframe=False): bqm_id = self.id_bqm(bqm) target_id = self.id_target(target) dir_path = [self.reports_path,bqm_id,target_id] reports_path = os.path.join(*dir_path) reports = {} for root, dirs, files in os.walk(reports_path): root_dirs = os.path.normpath(root).split(os.path.sep) if all(tag in root_dirs for tag in tags): for file in files: report_path = os.path.join(root,file) with open(report_path,'r') as fp: report = _load(fp,cls=EmberaDecoder) metric, ext = os.path.splitext(file) if not dataframe: reports[metric] = report else: kwargs = {'columns':list(bqm),'orient':'index'} reports[metric] = pd.DataFrame.from_dict(report,**kwargs) return reports
def load_embeddings(self, source, target, tags=[]): source_id = self.id_source(source) target_id = self.id_target(target) embeddings_path = os.path.join(self.embeddings_path,source_id,target_id) embedding_filenames = [] for root, dirs, files in os.walk(embeddings_path): root_dirs = os.path.normpath(root).split(os.path.sep) if all(tag in root_dirs for tag in tags): for file in files: embedding_filenames.append((root,file)) embeddings = [] for embedding_filename in embedding_filenames: embedding_path = os.path.join(*embedding_filename) with open(embedding_path,'r') as fp: embedding = _load(fp,cls=EmberaDecoder) embeddings.append(embedding) return embeddings
def _json_file_to_args(src_path): with open(src_path, 'rb') as json_data: return _load(json_data, encoding='utf-8')
def load(path) -> object: with open(path, 'r') as f: return _decode(_load(f))
def loadf(file): with open(file, "r") as fs: data = _load(fs) return data
def _init_drm_manager(self, fpga_slot_id): """ Initialize a DRM manager for the specified FPGA slot. Args: fpga_slot_id (int): FPGA slot """ # Get slot configuration slot_config = self._fpga_slots[fpga_slot_id] conf_file_path = self._check_path(slot_config.get( 'conf_file_path', self.DEFAULT_CONF_FILE_PATH)) cred_file_path = self._check_path(slot_config.get( 'cred_file_path', self.DEFAULT_CRED_FILE_PATH)) fpga_driver_name = slot_config.get( 'fpga_driver_name', self.DEFAULT_FPGA_DRIVER_NAME) fpga_image = slot_config.get('fpga_image') # Get configuration files with open(conf_file_path, 'rt') as conf_file: conf = _load(conf_file) drm_conf = conf.get('drm', dict()) # Get FPGA parameters from configuration drm_ctrl_base_addr = drm_conf.get( 'drm_ctrl_base_addr', self.DEFAULT_DRM_CTRL_BASE_ADDR) # Get FPGA driver driver = _get_driver(name=fpga_driver_name)( fpga_slot_id=fpga_slot_id, fpga_image=fpga_image, drm_ctrl_base_addr=drm_ctrl_base_addr) self._drivers.append(driver) # Initialize DRM manager if not slot_config.get('drm_disabled'): drm_manager = _DrmManager( conf_file_path=conf_file_path, cred_file_path=cred_file_path, read_register=driver.read_register_callback, write_register=driver.write_register_callback) drm_manager.set( # Set rotating log file for each slot log_service_path=self.LOG_FILE_BASE_PATH % fpga_slot_id, log_service_type=2, log_service_verbosity=int(_environ.get( 'ACCELIZE_DRM_LOG_VERBOSITY', self.DEFAULT_LOG_VERBOSITY)), # Disable Stdout log output log_verbosity=6, ) drm_manager.set(log_service_create=True) self._lisenced_slots.append(str(fpga_slot_id)) self._drm_managers.append(drm_manager) self._sd_log('FPGA slot %s, configuration file: %s', fpga_slot_id, conf_file_path) self._sd_log('FPGA slot %s, credential file: %s', fpga_slot_id, cred_file_path) self._sd_log('FPGA slot %s, driver: %s', fpga_slot_id, fpga_driver_name) self._sd_log('FPGA slot %s, image: %s', fpga_slot_id, fpga_image) self._sd_log('FPGA slot %s, licensing: %s', fpga_slot_id, 'disabled' if slot_config.get('drm_disabled') else 'enabled')
from pybars import Compiler as _Compiler from ssl_config._helpers import HELPERS as _HELPERS from ssl_config._versions import Version as _Version _DATA_DIR = _join(_dirname(__file__), '_data') #: Supported server software SERVERS = tuple( sorted( _splitext(name)[0] for name in _listdir(_join(_DATA_DIR, 'templates')))) with open(_join(_DATA_DIR, 'guidelines.json'), 'rt') as json_file: #: Guidelines information as dict GUIDELINES = _load(json_file) #: Mozilla SSL configuration levels #: #: Modern: #: Services with clients that support TLS 1.3 and don't need #: backward compatibility. #: #: Intermediate #: General-purpose servers with a variety of clients, recommended for #: almost all systems. #: #: Old: #: Compatible with a number of very old clients, and should be used only as #: a last resort. CONFIGS = tuple(sorted(GUIDELINES['configurations']))
# -*- coding: utf-8 -*- """ Parse PSE and config data. Tries user-config, if not found, falls back to default """ from collections import OrderedDict as _ODict from os.path import dirname as _dirname, expanduser as _expanduser from json import load as _load, dump as _dump with open(_dirname(__file__) + '/default.json') as _f: default = _load(_f, object_pairs_hook=_ODict) try: with open(_expanduser('~/.vipster.json')) as _f: _cfile = _load(_f, object_pairs_hook=_ODict) except: from copy import deepcopy as _deepcopy _cfile = _deepcopy(default) pse = _cfile['PSE'] config = _cfile['General'] _paramdict = _cfile['Parameters'] def saveConfig(): """Write config and PSE to json-file""" with open(_expanduser('~/.vipster.json'), 'w') as f: _dump(_ODict([('PSE', pse), ('General', config), ('Parameters', _paramdict)]), f, indent=2) __all__ = ["pse", "config", "default", "saveConfig"]