Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Immutable Book Class
#1
from copy import deepcopy


class Book:

    def __init__(self, book_title, authors):
        self.book_title = book_title
        self.authors = deepcopy(authors)
        self.logs = []

    def update_logs(self, log):
        self.logs.append(log)[b][/b]
I have a Book class, with a self.logs = [], the utility I'm creating helps people keep notes of the book they are currently reading. Now for learning purposes, I intend to keep this class immutable, it's meant for a book that the user bought, so I don't see how it could change(updating and re-printing the book is another matter, for my simple program, it's not a problem).

When modelling data that is really mutable(in my case, the logs), I've seen patterns where it updates the mutable data and returns an object reflecting the change.

In my case, it's only the list that needs updating, how would I go about keeping the list immutable?

What I tried:

class Book:

    def __init__(self, book_title, authors):
        self.book_title = book_title
        self.authors = deepcopy(authors)
        self._internal_logs = []
        self.logs = []

    @property
    def get_logs(self):
        return deepcopy(self._internal_logs)

    def update_logs(self, log):
        self.logs.append(log)
        self._internal_logs = deepcopy(self.logs)
Questions:
  1. If you wanted to keep an a list immutable, is this the best way to do it?, If not, how?

  2. When creating the list(self.logs = []), I do so in the constructor, the client code doesn't pass a list(IMO, they shouldn't, because no logs have been written yet), is this considered bad practice? Should I allow them to pass in an empty list? For example, the user may create a book file, but they haven't started reading the book yet. This means a book object is created without the logs.
Reply
#2
why not use tuple, instead of list, if you want it immutable? Note that I don't think logs should be immutable - user may want to edit an old log
Reply
#3
(Nov-27-2017, 06:37 PM)buran Wrote: why not use tuple, instead of list, if you want it immutable? Note that I don't think logs should be immutable - user may want to edit an old log

I wasn't thinking, I should have thought of tuple!

That being said, how would go about modelling the logs while keeping it thread safe? The data changes, as we both pointed out.
Reply
#4
(Nov-27-2017, 06:44 PM)QueenSvetlana Wrote: That being said, how would go about modelling the logs while keeping it thread safe?
from your description I don't see where/why multi-thread approach will be used. Also I don't see how you plan to store the book data, incl. logs. I would expect a log would be a class on its own with properties like id, time created/last edit, text, book it refers to, etc.. and logs in Book class would be much better as dict (and mutable)
Reply
#5
(Nov-27-2017, 06:50 PM)buran Wrote:
(Nov-27-2017, 06:44 PM)QueenSvetlana Wrote: That being said, how would go about modelling the logs while keeping it thread safe?
from your description I don't see where/why multi-thread approach will be used

This might not be a great example of when to use multi-threading, but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it?
Reply
#6
https://opensource.com/article/17/4/grok-gil
http://effbot.org/zone/thread-synchronization.htm
Reply
#7
(Nov-27-2017, 07:04 PM)buran Wrote: https://opensource.com/article/17/4/grok-gil

what happened to the other link?
Reply
#8
I edited the post, because it's too old. but anyway - now I reverted it back
Reply
#9
(Nov-27-2017, 06:53 PM)QueenSvetlana Wrote: but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it? 

With a lock. https://docs.python.org/3/library/thread...ck-objects

import threading

class Data:
    def __init__(self, value):
        self.value = value
        self.lock = threading.Lock()

    def get(self):
        with self.lock:
            return self.value

    def set(self, new_value):
        with self.lock:
            self.value = new_value

spam = Data("eggs")
print(spam.get())
spam.set("foo bar")
print(spam.get())
Reply
#10
(Nov-27-2017, 07:19 PM)nilamo Wrote:
(Nov-27-2017, 06:53 PM)QueenSvetlana Wrote: but suppose if you had to model data that changes but wanted to keep it thread-safe how would you do it? 

With a lock. https://docs.python.org/3/library/thread...ck-objects

import threading

class Data:
    def __init__(self, value):
        self.value = value
        self.lock = threading.Lock()

    def get(self):
        with self.lock:
            return self.value

    def set(self, new_value):
        with self.lock:
            self.value = new_value

spam = Data("eggs")
print(spam.get())
spam.set("foo bar")
print(spam.get())

@ buran as well, Thanks for the help Smile.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a contact book - a class made to store data apollo 2 1,957 Jun-12-2019, 04:33 PM
Last Post: apollo
  Correct way to implement immutable class QueenSveta 3 7,563 Jun-20-2018, 07:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020