Skip to content

Commit

Permalink
Apply black formatting (libAtoms#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar authored Jun 13, 2024
1 parent b27de76 commit 25a79ff
Show file tree
Hide file tree
Showing 26 changed files with 1,045 additions and 805 deletions.
35 changes: 19 additions & 16 deletions abcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ABCD(object):
@classmethod
def from_config(cls, config):
# Factory method
url = config['url']
url = config["url"]
return ABCD.from_url(url)

@classmethod
Expand All @@ -23,35 +23,38 @@ def from_url(cls, url, **kwargs):
r = parse.urlparse(url)
logger.info(r)

if r.scheme == 'mongodb':
if r.scheme == "mongodb":

conn_settings = {
'host': r.hostname,
'port': r.port,
'username': r.username,
'password': r.password,
'authSource': 'admin',
"host": r.hostname,
"port": r.port,
"username": r.username,
"password": r.password,
"authSource": "admin",
}

db = r.path.split('/')[1] if r.path else None
db = db if db else 'abcd'
db = r.path.split("/")[1] if r.path else None
db = db if db else "abcd"

from abcd.backends.atoms_pymongo import MongoDatabase

return MongoDatabase(db_name=db, **conn_settings, **kwargs)

elif r.scheme == 'http' or r.scheme == 'https':
raise NotImplementedError('http not yet supported! soon...')
elif r.scheme == 'ssh':
raise NotImplementedError('ssh not yet supported! soon...')
elif r.scheme == "http" or r.scheme == "https":
raise NotImplementedError("http not yet supported! soon...")
elif r.scheme == "ssh":
raise NotImplementedError("ssh not yet supported! soon...")
else:
raise NotImplementedError('Unable to recognise the type of connection. (url: {})'.format(url))
raise NotImplementedError(
"Unable to recognise the type of connection. (url: {})".format(url)
)


if __name__ == '__main__':
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

# url = 'mongodb://mongoadmin:secret@localhost:27017'
url = 'mongodb://mongoadmin:secret@localhost:27017/abcd_new'
url = "mongodb://mongoadmin:secret@localhost:27017/abcd_new"
abcd = ABCD.from_url(url)
abcd.print_info()

Expand Down
35 changes: 20 additions & 15 deletions abcd/backends/atoms_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@


class Atoms(ase.Atoms):

@classmethod
def from_dict(cls, data):
return cls(numbers=data['numbers'], positions=data['positions'])
return cls(numbers=data["numbers"], positions=data["positions"])


class HttpDatabase(Database):
"""client/local interface"""

def __init__(self, url='http://localhost'):
def __init__(self, url="http://localhost"):
super().__init__()

self.url = url

def push(self, atoms: ase.Atoms):
# todo: list of Atoms, metadata(user, project, tags)

message = requests.put(self.url + '/calculation', json=DictEncoder().encode(atoms))
message = requests.put(
self.url + "/calculation", json=DictEncoder().encode(atoms)
)
# message = json.dumps(atoms)
# message_hash = hashlib.md5(message.encode('utf-8')).hexdigest()
logger.info(message)
Expand All @@ -44,29 +45,33 @@ def query(self, query_string):
pass

def search(self, query_string: str) -> List[str]:
results = requests.get(self.url + '/calculation').json()
results = requests.get(self.url + "/calculation").json()
return results

def get_atoms(self, id: str) -> Atoms:
data = requests.get(self.url + '/calculation/{}'.format(id)).json()
data = requests.get(self.url + "/calculation/{}".format(id)).json()
atoms = Atoms.from_dict(data)
return atoms

def __repr__(self):
return 'ABCD(type={}, url={}, ...)'.format(self.__class__.__name__, self.url)
return "ABCD(type={}, url={}, ...)".format(self.__class__.__name__, self.url)

def _repr_html_(self):
"""jupyter notebook representation"""
return '<b>ABCD database</b>'
return "<b>ABCD database</b>"

def print_info(self):
"""shows basic information about the connected database"""

out = linesep.join([
'{:=^50}'.format(' ABCD Database '),
'{:>10}: {}'.format('type', 'remote (http/https)'),
linesep.join('{:>10}: {}'.format(k, v) for k, v in self.db.info().items())
])
out = linesep.join(
[
"{:=^50}".format(" ABCD Database "),
"{:>10}: {}".format("type", "remote (http/https)"),
linesep.join(
"{:>10}: {}".format(k, v) for k, v in self.db.info().items()
),
]
)

print(out)

Expand All @@ -80,6 +85,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
pass


if __name__ == '__main__':
abcd = HttpDatabase(url='http://localhost:8080/api')
if __name__ == "__main__":
abcd = HttpDatabase(url="http://localhost:8080/api")
abcd.print_info()
Loading

0 comments on commit 25a79ff

Please sign in to comment.