OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Implement descriptors to validate parameters

  • Thread starter Thread starter Jack Heathrow
  • Start date Start date
J

Jack Heathrow

Guest
So my task is:

*You must implement the class Book with the attributes price, author, and name.

The author and name fields have to be immutable; The price field may have changes but has to be in the 0 <= price <= 100 range. If a user tries to change the author or name fields after an initialization or set price is out of range, the ValueError should be raised.

Implement descriptors PriceControl and NameControl to validate parameters.*

Example:

Code:
>>> b = Book("William Faulkner", "The Sound and the Fury", 12)
>>> print(f"Author='{b.author}', Name='{b.name}', Price='{b.price}'")
Author='William Faulkner', Name='The Sound and the Fury', Price='12'

>>> b.price = 55
>>> b.price
55
>>> b.price = -12  # => ValueError: Price must be between 0 and 100.
>>> b.price = 101  # => ValueError: Price must be between 0 and 100.

>>> b.author = "new author"  # => ValueError: Author can not be changed.
>>> b.name = "new name"      # => ValueError: Name can not be changed.

My decision is:

Code:
class PriceControl:
    """
    Descriptor which don't allow to set price
    less than 0 and more than 100 included.
    """
    def __init__(self):
        self.price = 0

    def __get__(self, instance, owner):
        return self.price
    def __set__(self, instance, value):
        if value < 0 or value > 100:
            raise ValueError('Price must be between 0 and 100.')
        self.price = value

class NameControl:
    """
    Descriptor which don't allow to change field value after initialization.
    """
    pass

class Book:
    author = NameControl()
    name = NameControl()
    price = PriceControl()
    def __init__(self, author, name, price):
        self.author = author
        self.name = name
        self.price = price

So as you see I implemented a solution for price control but I can't imagine how to make descriptor for author/name control. Can you help me please?


<p>So my task is:</p>
<p>*You must implement the class Book with the attributes price, author, and name.</p>
<p>The author and name fields have to be immutable;
The price field may have changes but has to be in the 0 <= price <= 100 range.
If a user tries to change the author or name fields after an initialization or set price is out of range, the ValueError should be raised.</p>
<p>Implement descriptors PriceControl and NameControl to validate parameters.*</p>
<p>Example:</p>
<pre><code>
>>> b = Book("William Faulkner", "The Sound and the Fury", 12)
>>> print(f"Author='{b.author}', Name='{b.name}', Price='{b.price}'")
Author='William Faulkner', Name='The Sound and the Fury', Price='12'

>>> b.price = 55
>>> b.price
55
>>> b.price = -12 # => ValueError: Price must be between 0 and 100.
>>> b.price = 101 # => ValueError: Price must be between 0 and 100.

>>> b.author = "new author" # => ValueError: Author can not be changed.
>>> b.name = "new name" # => ValueError: Name can not be changed.
</code></pre>
<p>My decision is:</p>
<pre><code>class PriceControl:
"""
Descriptor which don't allow to set price
less than 0 and more than 100 included.
"""
def __init__(self):
self.price = 0

def __get__(self, instance, owner):
return self.price
def __set__(self, instance, value):
if value < 0 or value > 100:
raise ValueError('Price must be between 0 and 100.')
self.price = value

class NameControl:
"""
Descriptor which don't allow to change field value after initialization.
"""
pass

class Book:
author = NameControl()
name = NameControl()
price = PriceControl()
def __init__(self, author, name, price):
self.author = author
self.name = name
self.price = price
</code></pre>
<p>So as you see I implemented a solution for price control but I can't imagine how to make descriptor for author/name control. Can you help me please?</p>
<hr />
 

Latest posts

ن
Replies
0
Views
1
نعمان منذر محمود الجميلي
ن
S
Replies
0
Views
1
Sikandra
S
Top