Skip to content

Commit

Permalink
rewriting qe for easier future implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogila committed Jan 6, 2025
1 parent 7e877b8 commit db60f32
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 371 deletions.
291 changes: 118 additions & 173 deletions aton/interface/qe.py

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
LONG_DESCRIPTION = f.read()

setup(
name='aton',
version=__version__,
author='Pablo Gila-Herranz',
author_email='[email protected]',
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
long_description_content_type='text/markdown',
packages=['aton'],
install_requires=['numpy', 'pandas'],
extras_requires={
'dev': ['pytest', 'twine']
},
python_requires='>=3',
license='AGPL-3.0',
keywords=['Aton', 'Neutron', 'Neutron research', 'Spectra', 'Inelastic Neutron Scattering', 'INS', 'Ab-initio', 'DFT', 'Density Functional Theory', 'MD', 'Molecular Dynamics', 'Quantum ESPRESSO', 'Phonopy', 'CASTEP'],
classifiers= [
name = 'aton',
version = __version__,
author = 'Pablo Gila-Herranz',
author_email = '[email protected]',
description = DESCRIPTION,
long_description = LONG_DESCRIPTION,
long_description_content_type = 'text/markdown',
packages = ['aton'],
install_requires = ['numpy', 'pandas', 'scipy', 'matplotlib'],
extras_requires = {
'dev': ['pytest', 'twine', 'build']
},
python_requires = '>=3',
license = 'AGPL-3.0',
keywords = ['Aton', 'Neutron', 'Neutron research', 'Spectra', 'Inelastic Neutron Scattering', 'INS', 'Ab-initio', 'DFT', 'Density Functional Theory', 'MD', 'Molecular Dynamics', 'Quantum ESPRESSO', 'Phonopy', 'CASTEP'],
classifiers = [
"Development Status :: 5 - Production/Stable",
"Natural Language :: English",
"Intended Audience :: Science/Research",
Expand All @@ -33,3 +33,4 @@
"Operating System :: Other OS",
]
)

105 changes: 105 additions & 0 deletions tests/test_edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from aton.text import edit
from aton import file


folder = 'tests/samples/'
sample = folder + 'sample.txt'
sample_copy = folder + 'sample_copy.txt'


def test_insert_at():
file.copy(sample, sample_copy)
edit.insert_at(filepath=sample_copy, text='MIDDLE', position=1)
edit.insert_at(filepath=sample_copy, text='START', position=0)
edit.insert_at(filepath=sample_copy, text='END', position=-1)
with open(sample_copy, 'r') as f:
assert f.read() == 'START\nline1\nMIDDLE\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nEND'
file.remove(sample_copy)


def test_insert_under():
file.copy(sample, sample_copy)
edit.insert_under(filepath=sample_copy, key='5', text='!!!', skips=0)
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\nline5\n!!!\nline6\nline7\nline8\nline9'
file.copy(sample, sample_copy)
edit.insert_under(filepath=sample_copy, key='5', text='!!!', skips=-1)
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\n!!!\nline5\nline6\nline7\nline8\nline9'
file.copy(sample, sample_copy)
edit.insert_under(filepath=sample_copy, key=r'l[a-z]*5', text='!!!', regex=True)
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\nline5\n!!!\nline6\nline7\nline8\nline9'
file.remove(sample_copy)


def test_replace():
file.copy(sample, sample_copy)
edit.replace(filepath=sample_copy, key='line5', text='!!!')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\n!!!\nline6\nline7\nline8\nline9'
file.remove(sample_copy)


def test_replace_line():
file.copy(sample, sample_copy)
edit.replace_line(filepath=sample_copy, key='line5', text='!!!')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\n!!!\nline6\nline7\nline8\nline9'
file.copy(sample, sample_copy)
edit.replace_line(filepath=sample_copy, key='line5', text='')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\nline6\nline7\nline8\nline9'
file.remove(sample_copy)


def test_replace_between():
file.copy(sample, sample_copy)
edit.replace_between(filepath=sample_copy, key1='line4', key2='line7', text='!!!')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\n!!!\nline7\nline8\nline9'
file.remove(sample_copy)


def test_remove_between():
file.copy(sample, sample_copy)
edit.replace_between(filepath=sample_copy, key1='line4', key2='line7', text='')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\nline7\nline8\nline9'
file.remove(sample_copy)


def test_delete_under():
file.copy(sample, sample_copy)
edit.delete_under(filepath=sample_copy, key='5')
with open(sample_copy, 'r') as f:
assert f.read() == 'line1\nline2\nline3\nline4\nline5'
file.remove(sample_copy)


def test_correct_with_dict():
correct = {'line1': 'text', 'line5': ''}
file.copy(sample, sample_copy)
edit.correct_with_dict(filepath=sample_copy, correct=correct)
with open(sample_copy, 'r') as f:
assert f.read() == 'text\nline2\nline3\nline4\n\nline6\nline7\nline8\nline9'
file.remove(sample_copy)


def test_template():
try:
file.remove(sample_copy)
except:
pass
try:
edit.from_template(old=sample, new=sample_copy, correct={'line':''}, comment='!!!')
with open(sample_copy, 'r') as f:
content = f.read()
assert content == '!!!\n1\n2\n3\n4\n5\n6\n7\n8\n9'
except:
assert False
try:
file.remove(sample_copy)
except:
pass

30 changes: 15 additions & 15 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import thotpy as th
from aton.text import extract


def test_extract_number():
assert th.extract.number(' test = 123 number ', 'test') == 123.0
assert th.extract.number(' test 123 number ', 'test') == 123.0
assert extract.number(' test = 123 number ', 'test') == 123.0
assert extract.number(' test 123 number ', 'test') == 123.0


def test_extract_string():
assert th.extract.string(text=' test = "hello" stop ', name='test', stop='stop', strip=True) == 'hello'
assert th.extract.string(text=' test "hello" stop ', name='test', stop='stop', strip=True) == 'hello'
assert th.extract.string(text=" test 'hello' stop ", name='test', stop='stop', strip=True) == 'hello'
assert th.extract.string(text=" test 'hello' stop ", name='test', stop='stop', strip=False) == "'hello'"
assert extract.string(text=' test = "hello" stop ', name='test', stop='stop', strip=True) == 'hello'
assert extract.string(text=' test "hello" stop ', name='test', stop='stop', strip=True) == 'hello'
assert extract.string(text=" test 'hello' stop ", name='test', stop='stop', strip=True) == 'hello'
assert extract.string(text=" test 'hello' stop ", name='test', stop='stop', strip=False) == "'hello'"


def test_extract_column():
assert th.extract.column(' 123 456.5 789 ', 2) == 789
assert extract.column(' 123 456.5 789 ', 2) == 789


def test_extract_coords():
assert th.extract.coords('coordinates: 1.0, 2.0 and 3 these were the coordinates') ==[1.0, 2.0, 3.0]
assert extract.coords('coordinates: 1.0, 2.0 and 3 these were the coordinates') ==[1.0, 2.0, 3.0]


def test_extract_element():
string = ' element I Lead Pb Nitrogen H2, Xx2 fake element, O Oxygen, He4 isotope Ag Element '
assert th.extract.element(text=string, index=0) == 'I'
assert th.extract.element(text=string, index=1) == 'Pb'
assert th.extract.element(text=string, index=2) == 'H2'
assert th.extract.element(text=string, index=3) == 'O'
assert th.extract.element(text=string, index=4) == 'He4'
assert th.extract.element(text=string, index=5) == 'Ag'
assert extract.element(text=string, index=0) == 'I'
assert extract.element(text=string, index=1) == 'Pb'
assert extract.element(text=string, index=2) == 'H2'
assert extract.element(text=string, index=3) == 'O'
assert extract.element(text=string, index=4) == 'He4'
assert extract.element(text=string, index=5) == 'Ag'

76 changes: 28 additions & 48 deletions tests/test_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'''For this path to be detected properly, the `pytest` must be executed from `ThotPy`!'''

import thotpy as th
from aton import file


folder = 'tests/samples/'
Expand All @@ -14,118 +12,100 @@
def test_get():
# Clean from previous tests
try:
th.file.remove(sample_copy)
file.remove(sample_copy)
except:
pass
# Finds an existing file
try:
assert th.file.get(sample) != None
assert file.get(sample) != None
assert True
except FileNotFoundError:
assert False
# Does not find a non-existing file
try:
th.file.get(sample_copy)
file.get(sample_copy)
assert False
except FileNotFoundError:
assert True
# get_list, 'tests/sample.txt' in 'fullpath/tests/sample.txt'
try:
assert sample in th.file.get_list(folder, filters='sample')[0]
except:
assert False


def test_template():
try:
th.file.remove(sample_copy)
except:
pass
try:
th.file.from_template(old=sample, new=sample_copy, replaces={'line':''}, comment='!!!')
with open(sample_copy, 'r') as f:
content = f.read()
assert content == '!!!\n1\n2\n3\n4\n5\n6\n7\n8\n9'
assert sample in file.get_list(folder, filters='sample')[0]
except:
assert False
try:
th.file.remove(sample_copy)
except:
pass


def test_copy():
try:
th.file.remove(sample_copy)
th.file.remove(sample_copy_2)
file.remove(sample_copy)
file.remove(sample_copy_2)
except:
pass
# Copy files
try:
th.file.copy(sample, sample_copy)
assert th.file.get(sample_copy) != None
file.copy(sample, sample_copy)
assert file.get(sample_copy) != None
except FileNotFoundError:
assert False
# Move files
try:
th.file.move(sample_copy, sample_copy_2)
assert th.file.get(sample_copy_2) != None
file.move(sample_copy, sample_copy_2)
assert file.get(sample_copy_2) != None
except:
assert False
try:
th.file.get(sample_copy)
file.get(sample_copy)
assert False
except FileNotFoundError:
assert True
# Remove
try:
th.file.remove(sample_copy_2)
file.remove(sample_copy_2)
except:
assert False
try:
th.file.get(sample_copy_2)
file.get(sample_copy_2)
assert False
except:
assert True


def test_rename():
try:
th.file.remove(sample_copy)
th.file.remove(sample_ok)
file.remove(sample_copy)
file.remove(sample_ok)
except:
pass
th.file.copy(sample, sample_copy)
th.file.rename_on_folder(old='copy', new='ok', folder=folder)
file.copy(sample, sample_copy)
file.rename_on_folder(old='copy', new='ok', folder=folder)
try:
th.file.remove(sample_ok)
file.remove(sample_ok)
assert True
except:
assert False
try:
th.file.remove(sample_copy)
file.remove(sample_copy)
assert False
except:
assert True


def test_folders():
try:
th.file.remove(sample_copy)
th.file.remove(sample_copy_2)
th.file.remove(sample_ok)
th.file.remove(sample_ok_2)
file.remove(sample_copy)
file.remove(sample_copy_2)
file.remove(sample_ok)
file.remove(sample_ok_2)
except:
pass
th.file.copy_to_folders(extension='.txt', strings_to_delete=['.txt'], folder=folder)
file.copy_to_folders(extension='.txt', strings_to_delete=['.txt'], folder=folder)
try:
assert th.file.get_list(folder=folder+'sample', abspath=False) == ['sample.txt']
assert file.get_list(folder=folder+'sample', abspath=False) == ['sample.txt']
except:
assert False
# Check that the folder is deleted
th.file.remove(folder+'sample')
file.remove(folder+'sample')
try:
th.file.get_list(folder+'sample')
file.get_list(folder+'sample')
assert False
except FileNotFoundError:
assert True
Expand Down
3 changes: 1 addition & 2 deletions tests/test_find.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from thotpy import find
from aton.text import find


sample = 'tests/samples/sample.txt'
'''For this path to be detected properly, the `pytest` must be executed from `ThotPy`!'''


def test_lines():
Expand Down
Loading

0 comments on commit db60f32

Please sign in to comment.