Skip to content

Commit

Permalink
Drop support for Python <3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Aug 27, 2022
1 parent 41b5f98 commit ffe2a14
Show file tree
Hide file tree
Showing 25 changed files with 63 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-20.04]
python-version: ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
include:
- os: windows-2019
python-version: "3.8"
Expand Down
2 changes: 1 addition & 1 deletion examples/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

print("Atoms with x < 5: ")
for i in less_than_five:
print(" - {}".format(i))
print(f" - {i}")
3 changes: 0 additions & 3 deletions src/chemfiles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from .atom import Atom
from .cell import CellShape, UnitCell
from .frame import Frame
Expand Down
1 change: 0 additions & 1 deletion src/chemfiles/_c_lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -* coding: utf-8 -*
import os
import sys
from ctypes import POINTER, c_double, cdll
Expand Down
25 changes: 10 additions & 15 deletions src/chemfiles/atom.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import c_char_p, c_double, c_uint64

from .misc import ChemfilesError
from .property import Property
from .utils import CxxPointer, _call_with_growing_buffer, string_type
from .utils import CxxPointer, _call_with_growing_buffer


class Atom(CxxPointer):
Expand Down Expand Up @@ -38,12 +35,10 @@ def __copy__(self):
return Atom.from_mutable_ptr(None, self.ffi.chfl_atom_copy(self.ptr))

def __repr__(self):
name = self.name
type = self.type
if type == name:
return "Atom('{}')".format(name)
if self.type == self.name:
return f"Atom('{self.name}')"
else:
return "Atom('{}', '{}')".format(name, type)
return f"Atom('{self.name}', '{self.type}')"

@property
def mass(self):
Expand Down Expand Up @@ -150,10 +145,11 @@ def __getitem__(self, name):
Get a property of this atom with the given ``name``, or raise an error
if the property does not exists.
"""
if not isinstance(name, string_type):
if not isinstance(name, str):
raise ChemfilesError(
"Invalid type {} for an atomic property name".format(type(name))
f"Invalid type {type(name)} for an atomic property name"
)

ptr = self.ffi.chfl_atom_get_property(self.ptr, name.encode("utf8"))
return Property.from_mutable_ptr(self, ptr).get()

Expand All @@ -162,10 +158,9 @@ def __setitem__(self, name, value):
Set a property of this atom, with the given ``name`` and ``value``.
The new value overwrite any pre-existing property with the same name.
"""
if not isinstance(name, string_type):
raise ChemfilesError(
"invalid type {} for a property name".format(type(name))
)
if not isinstance(name, str):
raise ChemfilesError(f"invalid type {type(name)} for a property name")

property = Property(value)
self.ffi.chfl_atom_set_property(self.mut_ptr, name.encode("utf8"), property.ptr)

Expand Down
7 changes: 1 addition & 6 deletions src/chemfiles/cell.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import ARRAY, c_double
from enum import IntEnum

Expand Down Expand Up @@ -63,9 +60,7 @@ def __init__(self, lengths, angles=(90.0, 90.0, 90.0)):
else:
if lengths.shape != (3, 3):
raise ChemfilesError(
"expected the cell matrix to have 3x3 shape, got {}".format(
lengths.shape
)
f"expected the cell matrix to have 3x3 shape, got {lengths.shape}"
)
matrix = ARRAY(chfl_vector3d, (3))()
matrix[0][0] = lengths[0, 0]
Expand Down
25 changes: 9 additions & 16 deletions src/chemfiles/frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import POINTER, c_bool, c_char_p, c_double, c_uint64

import numpy as np
Expand All @@ -11,7 +8,7 @@
from .misc import ChemfilesError
from .property import Property
from .topology import Topology
from .utils import CxxPointer, string_type
from .utils import CxxPointer


class FrameAtoms(object):
Expand All @@ -32,9 +29,7 @@ def __getitem__(self, index):
associated :py:class:`Frame`.
"""
if index >= len(self):
raise IndexError(
"atom index ({}) out of range for this frame".format(index)
)
raise IndexError(f"atom index ({index}) out of range for this frame")
else:
ptr = self.frame.ffi.chfl_atom_from_frame(
self.frame.mut_ptr, c_uint64(index)
Expand Down Expand Up @@ -68,7 +63,7 @@ def __copy__(self):
return Frame.from_mutable_ptr(None, self.ffi.chfl_frame_copy(self.ptr))

def __repr__(self):
return "Frame with {} atoms".format(len(self.atoms))
return f"Frame with {len(self.atoms)} atoms"

@property
def atoms(self):
Expand Down Expand Up @@ -325,10 +320,9 @@ def __getitem__(self, name):
Get a property of this frame with the given ``name``, or raise an error
if the property does not exists.
"""
if not isinstance(name, string_type):
raise ChemfilesError(
"Invalid type {} for a frame property name".format(type(name))
)
if not isinstance(name, str):
raise ChemfilesError(f"Invalid type {type(name)} for a frame property name")

ptr = self.ffi.chfl_frame_get_property(self.ptr, name.encode("utf8"))
return Property.from_mutable_ptr(self, ptr).get()

Expand All @@ -337,10 +331,9 @@ def __setitem__(self, name, value):
Set a property of this frame, with the given ``name`` and ``value``.
The new value overwrite any pre-existing property with the same name.
"""
if not isinstance(name, string_type):
raise ChemfilesError(
"Invalid type {} for a frame property name".format(type(name))
)
if not isinstance(name, str):
raise ChemfilesError(f"Invalid type {type(name)} for a frame property name")

property = Property(value)
self.ffi.chfl_frame_set_property(
self.mut_ptr, name.encode("utf8"), property.ptr
Expand Down
54 changes: 18 additions & 36 deletions src/chemfiles/misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import warnings
from ctypes import POINTER, c_uint64, create_string_buffer

Expand Down Expand Up @@ -82,43 +79,28 @@ def _set_from_c(self, c_format_metadata):
self.residues = c_format_metadata.residues

def __repr__(self):
return """
FormatMetadata for {name}
-------------------{name_underline}
{description}
return f"""
FormatMetadata for {self.name}
-------------------{"-" * len(self.name)}
{self.description}
name = {name}
extension = {extension}
reference = {reference}
name = {self.name}
extension = {self.extension}
reference = {self.reference}
Capacities:
-----------
read = {read}
write = {write}
memory = {memory}
positions = {positions}
velocities = {velocities}
unit_cell = {unit_cell}
atoms = {atoms}
bonds = {bonds}
residues = {residues}
""".format(
name=self.name,
name_underline="-" * len(self.name),
description=self.description,
extension=self.extension,
reference=self.reference,
read=self.read,
write=self.write,
memory=self.memory,
positions=self.positions,
velocities=self.velocities,
unit_cell=self.unit_cell,
atoms=self.atoms,
bonds=self.bonds,
residues=self.residues,
)
read = {self.read}
write = {self.write}
memory = {self.memory}
positions = {self.positions}
velocities = {self.velocities}
unit_cell = {self.unit_cell}
atoms = {self.atoms}
bonds = {self.bonds}
residues = {self.residues}
"""


def formats_list():
Expand Down Expand Up @@ -162,7 +144,7 @@ def callback(message):
try:
function(message.decode("utf8"))
except Exception as e:
message = "exception raised in warning callback: {}".format(e)
message = f"exception raised in warning callback: {e}"
warnings.warn(message, ChemfilesWarning)

global _CURRENT_CALLBACK
Expand Down
12 changes: 3 additions & 9 deletions src/chemfiles/property.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import sys
from ctypes import c_bool, c_double

import numpy as np

from ._c_api import chfl_property_kind, chfl_vector3d
from .misc import ChemfilesError
from .utils import CxxPointer, _call_with_growing_buffer, string_type
from .utils import CxxPointer, _call_with_growing_buffer


class Property(CxxPointer):
Expand All @@ -26,16 +22,14 @@ def __init__(self, value):
ptr = self.ffi.chfl_property_bool(c_bool(value))
elif isinstance(value, (float, int)):
ptr = self.ffi.chfl_property_double(c_double(value))
elif isinstance(value, string_type):
elif isinstance(value, str):
ptr = self.ffi.chfl_property_string(value.encode("utf8"))
elif _is_vector3d(value):
value = chfl_vector3d(value[0], value[1], value[2])
ptr = self.ffi.chfl_property_vector3d(value)
else:
raise ChemfilesError(
"can not create a Property with a value of type '{}'".format(
type(value)
)
f"can not create a Property with a value of type '{type(value)}'"
)

super(Property, self).__init__(ptr, is_const=False)
Expand Down
17 changes: 8 additions & 9 deletions src/chemfiles/residue.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import c_bool, c_char_p, c_int64, c_uint64

import numpy as np

from .misc import ChemfilesError
from .property import Property
from .utils import CxxPointer, _call_with_growing_buffer, string_type
from .utils import CxxPointer, _call_with_growing_buffer


class ResidueAtoms(object):
Expand Down Expand Up @@ -90,7 +87,7 @@ def __copy__(self):
return Residue.from_mutable_ptr(None, self.ffi.chfl_residue_copy(self.ptr))

def __repr__(self):
return "Residue('{}') with {} atoms".format(self.name, len(self.atoms))
return f"Residue('{self.name}') with {len(self.atoms)} atoms"

@property
def name(self):
Expand Down Expand Up @@ -120,10 +117,11 @@ def __getitem__(self, name):
Get a property of this residude with the given ``name``, or raise an
error if the property does not exists.
"""
if not isinstance(name, string_type):
if not isinstance(name, str):
raise ChemfilesError(
"Invalid type {} for a residue property name".format(type(name))
f"Invalid type {type(name)} for a residue property name"
)

ptr = self.ffi.chfl_residue_get_property(self.ptr, name.encode("utf8"))
return Property.from_mutable_ptr(self, ptr).get()

Expand All @@ -132,10 +130,11 @@ def __setitem__(self, name, value):
Set a property of this residue, with the given ``name`` and ``value``.
The new value overwrite any pre-existing property with the same name.
"""
if not isinstance(name, string_type):
if not isinstance(name, str):
raise ChemfilesError(
"Invalid type {} for a residue property name".format(type(name))
f"Invalid type {type(name)} for a residue property name"
)

property = Property(value)
self.ffi.chfl_residue_set_property(
self.mut_ptr, name.encode("utf8"), property.ptr
Expand Down
5 changes: 1 addition & 4 deletions src/chemfiles/selection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import c_uint64

import numpy as np
Expand Down Expand Up @@ -33,7 +30,7 @@ def __copy__(self):
return Selection.from_mutable_ptr(None, self.ffi.chfl_selection_copy(self.ptr))

def __repr__(self):
return "Selection('{}')".format(self.string)
return f"Selection('{self.string}')"

@property
def size(self):
Expand Down
18 changes: 5 additions & 13 deletions src/chemfiles/topology.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from ctypes import c_bool, c_uint64
from enum import IntEnum

Expand Down Expand Up @@ -54,9 +51,7 @@ def __getitem__(self, index):
associated :py:class:`Topology`.
"""
if index >= len(self):
raise IndexError(
"atom index ({}) out of range for this topology".format(index)
)
raise IndexError(f"atom index ({index}) out of range for this topology")
else:
ptr = self.topology.ffi.chfl_atom_from_topology(
self.topology.mut_ptr, c_uint64(index)
Expand Down Expand Up @@ -110,9 +105,7 @@ def __getitem__(self, index):
topology does not necessarily match the residue id.
"""
if index >= len(self):
raise IndexError(
"residue index ({}) out of range for this topology".format(index)
)
raise IndexError(f"residue index ({index}) out of range for this topology")
else:
ptr = self.topology.ffi.chfl_residue_from_topology(
self.topology.ptr, c_uint64(index)
Expand Down Expand Up @@ -151,7 +144,7 @@ def __copy__(self):
return Topology.from_mutable_ptr(None, self.ffi.chfl_topology_copy(self.ptr))

def __repr__(self):
return "Topology with {} atoms".format(len(self.atoms))
return f"Topology with {len(self.atoms)} atoms"

@property
def atoms(self):
Expand Down Expand Up @@ -186,9 +179,8 @@ def residue_for_atom(self, index):
atom is not part of a residue.
"""
if index >= len(self.atoms):
raise IndexError(
"residue index ({}) out of range for this topology".format(index)
)
raise IndexError(f"residue index ({index}) out of range for this topology")

ptr = self.ffi.chfl_residue_for_atom(self.ptr, c_uint64(index))
if ptr:
return Residue.from_const_ptr(self, ptr)
Expand Down
Loading

0 comments on commit ffe2a14

Please sign in to comment.