Skip to content

Commit 84366f9

Browse files
Add ruff (libAtoms#120)
* Add pre-commit and ruff dependencies * Add pre-commit * Add pre-commit CI * Apply pre-commit * Apply ruff format and fixes * Apply ruff unsafe fixes * Update ruff * Update ruff * Apply ruff fixes --------- Co-authored-by: ElliottKasoar <[email protected]>
1 parent b6cd0cb commit 84366f9

File tree

132 files changed

+1261759
-1261919
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+1261759
-1261919
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [ push, pull_request ]
44

55
jobs:
66

7-
build:
7+
tests:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
@@ -35,3 +35,25 @@ jobs:
3535
with:
3636
token: ${{ secrets.CODECOV_TOKEN }}
3737

38+
pre-commit:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- name: Set up Python 3.11
44+
uses: actions/setup-python@v5
45+
with:
46+
python-version: '3.11'
47+
48+
- name: Install poetry
49+
run: pipx install poetry
50+
51+
- name: Install python dependencies
52+
run: |
53+
poetry env use 3.11
54+
poetry install --with pre-commit,dev
55+
56+
- name: Run pre-commit
57+
run: |
58+
poetry run pre-commit install
59+
poetry run pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )

.pre-commit-config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Install pre-commit hooks via:
2+
# pre-commit install
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v5.0.0
6+
hooks:
7+
- id: end-of-file-fixer
8+
- id: mixed-line-ending
9+
- id: trailing-whitespace
10+
- id: check-json
11+
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
# Ruff version.
14+
rev: v0.9.6
15+
hooks:
16+
# Run the linter.
17+
- id: ruff
18+
args: [ --fix ]
19+
# Run the formatter.
20+
- id: ruff-format

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@ RUN python setup.py develop
1313
#CMD ["/bin/bash"]
1414
CMD [ "python", "abcd/server/__init__.py" ]
1515
#CMD ["gunicorn", "-w 4", "-b 0.0.0.0:8000", "server:app"]
16-
17-

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
[![Doc](https://img.shields.io/badge/docs-master-green.svg)](https://libatoms.github.io/abcd/)
44
[![Build Status](https://travis-ci.org/libAtoms/abcd.svg?branch=master)](https://travis-ci.org/libAtoms/abcd)
55

6-
Database storage and discovery of atomistic data.
6+
Database storage and discovery of atomistic data.
77

88
Take a look at the `examples.md` file for.. examples!
99

1010
Main features:
1111

12-
- Configurations that consist of atom positions, elements, forces, and various metadata are stored as a dictionary by a MongoDB backend.
13-
- There is no predefined schema, any combination of keys are allowed for all configurations.
14-
- Two modes: "discovery" and "download". Both use filter-type queries, but in "discovery" mode, summary statistics of the configurations that pass the filter are reported. In "download" mode, the matching configurations are downloaded and exported to a file.
15-
- The "discovery" mode can be used to learn what keys exist in the set of configurations that have passed the current quiery filter. The user can use this to refine the query.
16-
- Complex queries on dictionary key-value pairs are allowed, and their logical combinations.
12+
- Configurations that consist of atom positions, elements, forces, and various metadata are stored as a dictionary by a MongoDB backend.
13+
- There is no predefined schema, any combination of keys are allowed for all configurations.
14+
- Two modes: "discovery" and "download". Both use filter-type queries, but in "discovery" mode, summary statistics of the configurations that pass the filter are reported. In "download" mode, the matching configurations are downloaded and exported to a file.
15+
- The "discovery" mode can be used to learn what keys exist in the set of configurations that have passed the current quiery filter. The user can use this to refine the query.
16+
- Complex queries on dictionary key-value pairs are allowed, and their logical combinations.
1717

1818
## Installation
1919

@@ -24,13 +24,13 @@ $ pip install git+https://github.com/libAtoms/abcd.git
2424

2525
## Setup
2626

27-
If you have an already running mongo server, or install your own, they you are ready to go. Alternatively,
27+
If you have an already running mongo server, or install your own, they you are ready to go. Alternatively,
2828

2929
```
3030
docker run -d --rm --name abcd-mongodb -v <path-on-your-machine-to-store-database>:/data/db -p 27017:27017 mongo
3131
```
3232

33-
will download and install a docker and run a database in it.
33+
will download and install a docker and run a database in it.
3434

3535
To connect to a mongodb that is already running, use
3636
```
@@ -56,7 +56,7 @@ You can set up an `abcd` user on your machine where the database is running, and
5656
command="/path/to/abcd --remote ${SSH_ORIGINAL_COMMAND}",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa <public-key> your@email
5757
```
5858

59-
Then you'll be able to access the database remotely using, e.g.
59+
Then you'll be able to access the database remotely using, e.g.
6060
```
6161
ssh [email protected] summary
6262
```
@@ -74,18 +74,18 @@ To access it:
7474
docker pull stenczelt/projection-abcd:latest
7575
```
7676
77-
2. create a docker network, which enables the containers to communicate with each other and the outside world as well
77+
2. create a docker network, which enables the containers to communicate with each other and the outside world as well
7878
```
7979
docker network create --driver bridge abcd-network
8080
```
8181
8282
3. run the mongo (ABCD) and the visualiser as well
8383
```
8484
docker run -d --rm --name abcd-mongodb-net -v <path-on-your-machine-to-store-database>:/data/db -p 27017:27017 --network abcd-network mongo
85-
85+
8686
docker run -it --rm --name visualiser-dev -p 9999:9999 --network abcd-network stenczelt/projection-abcd
8787
```
88-
NB: You need a a directory where the database files are kept locally and you need to connect this to the mongo
88+
NB: You need a a directory where the database files are kept locally and you need to connect this to the mongo
8989
container. More info about this can be found in the original ABCD repo
9090
9191
This will start the visualiser with ABCD integration! Have fun!

abcd/__init__.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from enum import Enum
12
import logging
23
from urllib import parse
3-
from enum import Enum
44

55
logger = logging.getLogger(__name__)
66

@@ -10,7 +10,7 @@ class ConnectionType(Enum):
1010
http = 2
1111

1212

13-
class ABCD(object):
13+
class ABCD:
1414
@classmethod
1515
def from_config(cls, config):
1616
# Factory method
@@ -24,7 +24,6 @@ def from_url(cls, url, **kwargs):
2424
logger.info(r)
2525

2626
if r.scheme == "mongodb":
27-
2827
conn_settings = {
2928
"host": r.hostname,
3029
"port": r.port,
@@ -39,20 +38,19 @@ def from_url(cls, url, **kwargs):
3938
from abcd.backends.atoms_pymongo import MongoDatabase
4039

4140
return MongoDatabase(db_name=db, **conn_settings, **kwargs)
42-
elif r.scheme == "mongodb+srv":
41+
if r.scheme == "mongodb+srv":
4342
db = r.path.split("/")[1] if r.path else None
4443
db = db if db else "abcd"
4544
from abcd.backends.atoms_pymongo import MongoDatabase
4645

4746
return MongoDatabase(db_name=db, host=r.geturl(), uri_mode=True, **kwargs)
48-
elif r.scheme == "http" or r.scheme == "https":
47+
if r.scheme == "http" or r.scheme == "https":
4948
raise NotImplementedError("http not yet supported! soon...")
50-
elif r.scheme == "ssh":
49+
if r.scheme == "ssh":
5150
raise NotImplementedError("ssh not yet supported! soon...")
52-
else:
53-
raise NotImplementedError(
54-
"Unable to recognise the type of connection. (url: {})".format(url)
55-
)
51+
raise NotImplementedError(
52+
f"Unable to recognise the type of connection. (url: {url})"
53+
)
5654

5755

5856
if __name__ == "__main__":
@@ -62,11 +60,3 @@ def from_url(cls, url, **kwargs):
6260
url = "mongodb://mongoadmin:secret@localhost:27017/abcd_new"
6361
abcd = ABCD.from_url(url)
6462
abcd.print_info()
65-
66-
# from ase.io import iread
67-
# for atoms in iread('../tutorials/data/bcc_bulk_54_expanded_2_high.xyz', index=slice(1)):
68-
# # Hack to fix the representation of forces
69-
# atoms.calc.results['forces'] = atoms.arrays['force']
70-
#
71-
# abcd.push(atoms)
72-
# print(atoms)

abcd/backends/atoms_http.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import json
22
import logging
3-
import requests
43
from os import linesep
5-
from typing import List
64

75
import ase
6+
import requests
7+
88
from abcd.backends.abstract import Database
99

1010
logger = logging.getLogger(__name__)
@@ -44,17 +44,15 @@ def pull(self, query=None, properties=None):
4444
def query(self, query_string):
4545
pass
4646

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

5150
def get_atoms(self, id: str) -> Atoms:
52-
data = requests.get(self.url + "/calculation/{}".format(id)).json()
53-
atoms = Atoms.from_dict(data)
54-
return atoms
51+
data = requests.get(self.url + f"/calculation/{id}").json()
52+
return Atoms.from_dict(data)
5553

5654
def __repr__(self):
57-
return "ABCD(type={}, url={}, ...)".format(self.__class__.__name__, self.url)
55+
return f"ABCD(type={self.__class__.__name__}, url={self.url}, ...)"
5856

5957
def _repr_html_(self):
6058
"""jupyter notebook representation"""
@@ -67,9 +65,7 @@ def print_info(self):
6765
[
6866
"{:=^50}".format(" ABCD Database "),
6967
"{:>10}: {}".format("type", "remote (http/https)"),
70-
linesep.join(
71-
"{:>10}: {}".format(k, v) for k, v in self.db.info().items()
72-
),
68+
linesep.join(f"{k:>10}: {v}" for k, v in self.db.info().items()),
7369
]
7470
)
7571

0 commit comments

Comments
 (0)