Skip to content

Commit

Permalink
Merge branch 'dev' into extended_data
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaHobbyist authored May 27, 2024
2 parents 7895718 + 52ca0fd commit c02c268
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 34 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
- name: Install dependencies and build package
run: |
python -m pip install --upgrade pip
python -m pip install pytest anytree setuptools wheel
python setup.py sdist bdist_wheel
python -m pip install .
python -m pip install pytest anytree setuptools wheel build
python -m build
pip install dist/*.whl
- name: Test with pytest
run: |
pytest -v
pytest -v tests/
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"files.associations": {
"dictionaryFile.C": "cpp"
},
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
18 changes: 10 additions & 8 deletions manual_tests/test_pyvnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# set up automated tests for CI/CD in github

# test for KeyData and Foam classes
'''

key1 = KeyData('solver', prop1, prop2)
print(key1.giveVal())
head = Foam("test_head",None, None)
Expand All @@ -19,19 +19,21 @@
child1 = Foam('test_child', head, None)
child2 = Foam('test_child2', child1, None, key1)
child3 = Foam('test_child3', child1, None, key1)
'''


# Display tests
'''
print(head)

# print(head)
showTree(head)
print(RenderTree(child1).by_attr())
'''
# print(RenderTree(child1).by_attr())


# Test for Keydata class singularily

key1 = KeyData('solver', prop1)
print(key1)
# key1 = KeyData('solver', prop1)
# print(key1)

writeTo(head, 'testFile.txt')



10 changes: 5 additions & 5 deletions manual_tests/treeTest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .pyvnt.DictionaryElement.foamDS import *
from .pyvnt.DictionaryElement.keyData import *
from .pyvnt.Reference.basic import *
from pyvnt import *

prop1 = EnumProp('val1', items={'PCG', 'PBiCG', 'PBiCGStab'}, default='PCG')

key1 = KeyData('solver', prop1)

head = Foam(name="test_head", children = [key1])
head = Foam("test_head", None, None, key1)

head.dispTree()
# head.dispTree()

showTree(head)
Empty file.
13 changes: 13 additions & 0 deletions pyvnt/Converter/Writer/writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pyvnt.DictionaryElement import *

def writeTo(root, path):
'''
Function to write the dictionary object to the file
Parameters:
Foam: Dictionary object to be written
path: Path to the file where the dictionary object is to be written
'''
with open(path, "w") as file:
root.writeOut(file)
Empty file added pyvnt/Converter/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions pyvnt/DictionaryElement/foamDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Type
from pyvnt.DictionaryElement.keyData import KeyData
from pyvnt.Reference.errorClasses import *
from pyvnt.utils.makeIndent import makeIndent

'''
Criteria for classes:
Expand Down Expand Up @@ -134,5 +135,41 @@ def reorderData(self, data: KeyData, pos: int):
self.data.insert(data, pos)
except:
raise AttributeError(f"{data.name} does not exist in this node")

def writeOut(self, file, indent = 0):
'''
Function to write the current node to the file
'''

'''
if self.parent == None:
# TODO: Add the header to the file
pass
else:
file.write(f"{self.name}\n")
file.write("{\n")
for d in self.data:
file.write("\t")
d.writeOut(file)
file.write("}\n")
'''

makeIndent(file, indent)
file.write(f"{self.name}\n")

makeIndent(file, indent)
file.write("{\n")

for d in self.data:
d.writeOut(file, indent+1)

# makeIndent(file, indent)

for child in self.children:
child.writeOut(file, indent+1)
file.write("\n")

makeIndent(file, indent)
file.write("}\n")


33 changes: 28 additions & 5 deletions pyvnt/DictionaryElement/keyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict
from anytree import NodeMixin
from pyvnt.Reference.basic import *
from pyvnt.utils.makeIndent import makeIndent


'''
Expand Down Expand Up @@ -71,7 +72,7 @@ def __setattr__(self, key, value):
def appendVal(self, key: "str", val: ValueProperty):
self._privateDict[key] = val

"""
'''
# TODO: Take input of the object to be replaced or the obejct name instead of the variable name as the string. -- done in replaceVal2
This piece of code is here to remind devs about what not to do
Expand Down Expand Up @@ -107,7 +108,7 @@ def replaceVal2(self, old: ValueProperty | str, new: ValueProperty):
for k, v in list(self.__dict__.items()):
self.__dict__[replacement.get(k, k)] = self.__dict__.pop(k)
self.__dict__[newKey] = new
"""
'''

def replaceVal(self, old: ValueProperty or str, new: ValueProperty): # uses orderedDict instead of regular Dictionary
'''
Expand Down Expand Up @@ -147,9 +148,12 @@ def delVal(self, key: str):
del self._privateDict[key]

def __repr__(self):
last_elem = list(self._privateDict.keys())[-1]
res_str = f"KeyData("
for key, val in self._privateDict.items():
res_str = res_str + f"{key} : {val}, "
res_str = res_str + f"{key} : {val}"
if key != last_elem:
res_str = res_str + ", "
res_str = res_str + ")"

return res_str
Expand All @@ -158,11 +162,30 @@ def giveVal(self):
'''
Function to get all the keys and values stored in the object in a text format
'''
last_elem = list(self._privateDict.keys())[-1]

res = f"{self.name} : "
for key, val in self._privateDict.items():
if key == 'name':
continue
else:
res = res + f"{val.giveVal()}, "
res = res + f"{val.giveVal()}"
if key != last_elem:
res = res + ", "

return res
return res

def writeOut(self, file, indent = 0):
'''
Function to write the object to a file
'''
col_width = 16
last_elem = list(self._privateDict.keys())[-1]

makeIndent(file, indent)
file.write(f"{self.name.ljust(col_width)}")
for key, val in self._privateDict.items():
val.writeOut(file)
if key != last_elem:
file.write(" ")
file.write(";\n")
22 changes: 21 additions & 1 deletion pyvnt/Reference/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def __eq__(self, other):
def __ne__(self, other):
return self.__default != other._PropertyFloat__default

def writeOut(self, file):
'''
Function to write the object to a file
'''
file.write(f"{self.__default}")



class PropertyFloat(ValueProperty):
'''
Expand Down Expand Up @@ -187,6 +194,13 @@ def __eq__(self, other):

def __ne__(self, other):
return self.__default != other._PropertyFloat__default

def writeOut(self, file):
'''
Function to write the object to a file
'''
file.write(f"{self.__default}")



class PropertyString(ValueProperty): # for testing purposes only, to be scrapped
Expand Down Expand Up @@ -326,4 +340,10 @@ def giveVal(self):
Funciton to return the current value of the property
'''
res = self.__default
return res
return res

def writeOut(self, file):
'''
Function to write the object to a file
'''
file.write(f"{self.__default}")
4 changes: 3 additions & 1 deletion pyvnt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pyvnt.Reference.basic import *
from pyvnt.DictionaryElement.foamDS import *
from pyvnt.DictionaryElement.showTree import *
from pyvnt.DictionaryElement.keyData import *
from pyvnt.Converter.Writer.writer import *
from pyvnt.utils import *
from pyvnt.utils.showTree import *
Empty file added pyvnt/utils/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions pyvnt/utils/makeIndent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


def makeIndent(file, indent: int):
for i in range(indent):
file.write("\t")
File renamed without changes.
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
author="",
author_email="<[email protected]>",
description=DESCRIPTION,
packages_dir={"": "pyvnt"},
packages=find_packages(exclude=['test']),
# packages_dir={"": "pyvnt"},
# data_files=[('Shared_Objects', [
# './pyvnt/Converter/cpp_src/dictionaryFile/lib/dictionaryFile.so',
# './pyvnt/Converter/cpp_src/dictionaryFileIterator/lib/dictionaryFileIterator.so'
# ])],
packages=find_packages(include=['pyvnt', 'pyvnt.*']),
# py_modules=['pyvnt'],
# include_package_data=True,
# package_data={'': ['./pyvnt/Converter/cpp_src/dictionaryFile/lib/dictionaryFile.so', './pyvnt/Converter/cpp_src/dictionaryFileIterator/lib/dictionaryFileIterator.so']},
install_requires=['anytree', 'dataclasses'],
keywords=['python'],
classifiers=[
Expand Down
17 changes: 17 additions & 0 deletions testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_head
{
test_child
{
test_child2
{
solver PCG PBiCG;
}

test_child3
{
solver PCG PBiCG;
}

}

}
12 changes: 6 additions & 6 deletions tests/test_keyData.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ def teardown_method(self, method):
del self.items

def test_keyData_print(self):
assert str(self.key1) == f"KeyData(val1 : {str(self.prop1)}, val2 : {str(self.prop2)}, )"
assert str(self.key1) == f"KeyData(val1 : {str(self.prop1)}, val2 : {str(self.prop2)})"

def test_keyData_val(self):
assert self.key1.giveVal() == f"solver : {self.prop1.giveVal()}, {self.prop2.giveVal()}, "
assert self.key1.giveVal() == f"solver : {self.prop1.giveVal()}, {self.prop2.giveVal()}"

def test_keyData_edit(self):
tmp_prop1 = PropertyInt('tmpval1', 2, 1, 10)
tmp_prop2 = PropertyInt('tmpval2', 3, 1, 10)

self.key1.replaceVal('val1', tmp_prop1)
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {self.prop2.giveVal()}, "
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {self.prop2.giveVal()}"

self.key1.replaceVal(self.prop2, tmp_prop2)
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop2.giveVal()}, "
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop2.giveVal()}"

tmp_prop3 = PropertyInt('tmpval2', 4, 1, 10)

self.key1.replaceVal(tmp_prop2, tmp_prop3)
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop3.giveVal()}, "
assert self.key1.giveVal() == f"solver : {tmp_prop1.giveVal()}, {tmp_prop3.giveVal()}"

def test_keyData_edit_fail(self):
tmp_prop1 = PropertyInt('tmpval1', 2, 1, 10)
Expand All @@ -52,6 +52,6 @@ def test_keyData_edit_fail(self):

def test_keyData_del(self):
self.key1.delVal('val1')
assert self.key1.giveVal() == f"solver : {self.prop2.giveVal()}, "
assert self.key1.giveVal() == f"solver : {self.prop2.giveVal()}"


0 comments on commit c02c268

Please sign in to comment.