Reputation: 601
I am pretty new to classes and I already wrote a class for doing some Pandas operations.
However I noticed, that working with objects can result in objects that don't know the original definitions. So I came to @classmethod, but I don't have an idea of how to use them.
Goal of my class:
I want to write a Permission class. It should load/write a json-file and one should be able to add more permissions.
EDIT:
After some reading I ended up with a staticmethod
and a __init__
that load the file during their initialization.
class Permission:
data = None
def __init__(self, path):
if Permission.data is None:
self.read_permissions(path)
@staticmethod
def read_permissions(path):
try:
with open(path, 'r') as read_file:
Permission.data = json.load(read_file)
except FileNotFoundError as e:
logging.error(e)
sys.exit(1)
def add_permissions(self, group, level, permissions):
self.data['groups'].append({
'group': group,
'level': level,
'permissions': permissions
})
So I can use the class like this:
json_path = 'C:\\data\\test.json'
test_perm = Permission(json_path)
print(test_perm.data)
{'groups': [{'group': 'common', 'permissions': {'select_sample_x': True, 'select_sample_y': True, 'assign_x': False, 'request_y': True}}]}
test_perm.add_permissions('guys', 10, {'can_do_icecream': False, 'can_do_nothing': True})
print(test_perm.data)
{'groups': [{'group': 'pkf_common', 'permissions': {'select_sample_debitor': True, 'select_sample_creditor': True, 'assign_third_party_confirmation': False, 'request_reporting': True}}, {'group': 'guys', 'level': 10, 'permissions': {'can_do_icecream': False, 'can_do_nothing': True}}]}
Upvotes: 1
Views: 486
Reputation: 19232
Since most of your methods are @classmethod
, you might find it easy to just use functions.
You can read permissions, and maybe let the calling code decide what to do on an error:
def read_permissions(path):
with open(path, 'r') as read_file:
return json.load(read_file)
Previously, you returned cls(path).data
so the calling code would have to keep hold of the data
.
Once you have one set of permissions, called data
returned from this, you can use your add_permissions
to append to data['groups']
, without using classes at all.
An alternative is to load some data when you make a class instance, and use that instance from then one:
class Permissions:
def __init__(self, path):
self.path = path
self.data = []
try:
with open(path, 'r') as read_file:
self.data = json.load(read_file) #<--- store in instance
except FileNotFoundError as e:
logging.error(e)
sys.exit(1) #slightly extreme, but up to you
def write_permissions(self, filename): #note the new filename
with open(filename, 'w') as outfile:
json.dump(self.data, outfile, indent=4)
def add_permissions(self, path, group, permissions):
self.data['groups'].append({
'group': group,
'level': 0,
'permissions': permissions
}) #now in the instance
You can then use your class:
path_json = r'C:\data\file.json' #raw string
data = Permissions(path_json) #this now reads when you make the class instance
data.add_permissions(json_path, 'guys', {'can_do_icecream': False, 'can_do_nothing': True})
data.write_permissions(r'C:\data\new_file.json') #raw string
Upvotes: 1