Skip to content

Commit

Permalink
Made basic implementation of OpenFOAM List in pyvnt
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaHobbyist committed May 24, 2024
1 parent 33b8240 commit 7895718
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyvnt/Reference/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class EnumProp(ValueProperty):
'''

__slots__ = ('_EnumProp__items', '_EnumProp__default')
__slots__ = ('_ValueProperty__name', '_EnumProp__items', '_EnumProp__default')

def __init__(self, name: str, items: {str}, default: str):
super(EnumProp, self).__init__()
Expand Down
30 changes: 30 additions & 0 deletions pyvnt/Reference/errorClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,33 @@ def __init__(self, item):

def __str__(self):
return f"{self.item} not an tuple type"

class SizeError(Exception):
def __init__(self, size: int):
self.size = size

def __str__(self):
return f"Size of values should be {self.size}"

class NoPlaceholdersError(Exception):
def __init__(self, msg: str):
self.msg = msg

def __str__(self):
return f"{self.msg}"

class NoValueError(Exception):
def __init__(self, msg: str):
self.msg = msg

def __str__(self):
return f"{self.msg}"

class KeyRepeatError(Exception):
def __init__(self, key: str):
self.key = key

def __str__(self):
return f"{self.key} Already exists"


108 changes: 108 additions & 0 deletions pyvnt/Reference/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from pyvnt.Reference.basic import *
from pyvnt.Reference.errorClasses import SizeError, NoPlaceholdersError, NoValueError, KeyRepeatError
import warnings

class PropertyList(ValueProperty):
'''
A property that holds a list of values.
Constructor Parameters:
name: The name of the property.
size: The size of the list.
values: The values of the list.
default: The default value of the list.
Class constructor can be called in the following ways:
PropertyList(name, size, values)
PropertyList(name, values)
PropertyList(name, size, default)
'''

__slots__ = ['_ValuePorperty__name', '_PropertyList__values']

def __init__(self, name: int, size: int = None, values: [ValueProperty] = [], default: ValueProperty = None):
super().__init__(name, default)
self.setProperties(name, size, values, default)


def setProperties(self, name: int, size: int, values: [ValueProperty], default: ValueProperty = None):
'''
Sets the values of the list.
'''
self._ValueProperty__name = name

if size and values != []:
if default:
warnings.warn("Default value will be ignored")
else:
pass

if size != len(values):
raise SizeError(size)
else:
self._PropertyList__values = values

elif not size and values != []:
if default:
warnings.warn("Default value will be ignored")
else:
pass

self._PropertyList__values = values

elif size and values == []:
if default:
warnings.warn("Default value will be ignored")
else:
pass

if not default:
raise NoPlaceholdersError("No default value")
else:
self._PropertyList__values = [default] * size

else:
raise NoValueError("No values given for list construction")

def getItem(self, index: int):
'''
Returns the value at the given index.
'''
return self._PropertyList__values[index]

def append_value(self, val: ValueProperty):
'''
Appends the value to the list.
'''
self._PropertyList__values.append(val)

def append_uniq_value(self, val: ValueProperty):
'''
Appends the value to the list if it is not already present.
'''
if val not in self._PropertyList__values:
self._PropertyList__values.append(val)
else:
raise KeyRepeatError(val)

def __repr__(self):
return f"{self._ValueProperty__name}: {self._PropertyList__values}"

def size(self):
'''
Returns the size of the list.
'''
return len(self._PropertyList__values)

def giveVal(self):
'''
Returns the list.
'''
return self._PropertyList__values

def __eq__(self, other):
return self._PropertyList__values == other.giveVal()



0 comments on commit 7895718

Please sign in to comment.