Prevent overriding property for class methods in Python?
Date : March 29 2020, 07:55 AM
this one helps. I suppose this stops developers from accidentally overwriting Test's spam property. Is that why you want this? I not sure that is a good idea (what if a developer wants to override the spam property? why throw up roadblocks?), but... You could use a metaclass. If you don't supply a setter for the metaclass's property, then Test.spam will raise an AttributeError: class MetaTest(type):
@property
def spam(cls):
return cls._spam
class Test(object):
__metaclass__=MetaTest
_spam = 42
@classmethod
def get_spam(cls):
cls._spam
@classmethod
def set_spam(cls, value):
cls._spam = value
spam = property(get_spam, set_spam)
print Test.spam
# 42
Test.spam = 24
AttributeError: can't set attribute
|
JSON serialize a class and change property casing with Python
Date : March 29 2020, 07:55 AM
Hope that helps You just need to create a function to transform the snake_case keys to camelCase. You can easily do that using .split, .lower, and .title. import json
class HardwareProfile:
def __init__(self, vm_size):
self.vm_size = vm_size
self.some_other_thing = 42
self.a = 'a'
def snake_to_camel(s):
a = s.split('_')
a[0] = a[0].lower()
if len(a) > 1:
a[1:] = [u.title() for u in a[1:]]
return ''.join(a)
def serialise(obj):
return {snake_to_camel(k): v for k, v in obj.__dict__.items()}
hp = HardwareProfile('Large')
print(json.dumps(serialise(hp), indent=4, default=serialise))
{
"vmSize": "Large",
"someOtherThing": 42,
"a": "a"
}
|
How to access methods and attributes in derived property class in python
Tag : python , By : user183954
Date : March 29 2020, 07:55 AM
Any of those help How do I access the method "get_memvervalues" or the attributes _name, _tooltip etc? myclass.__dict__['data']
type(a).__dict__['data']
a = myclass()
desc = type(a).__dict__['data']
print(desc.get_membervalues()) # {
# 'name': 'data',
# 'cls_name': 'AnotherClass',
# 'tooltip': 'help'
# }
print(desc._tooltip) # help
print(vars(desc)) # {
# '_name': 'data',
# '_value': 4,
# '_cls_name': 'AnotherClass',
# '_datatype': <class 'int'>,
# '_tooltip': 'help',
# '_kwargs': {}
# }
a = myclass()
b = myclass()
print("Before:") # Before:
print("a:", a.data) # a: 4
print("b:", b.data) # b: 4
a.data = 10
print("After:") # After:
print("a:", a.data) # a: 10
print("b:", b.data) # b: 10 (!!!)
type(a).__dict__['data'] is type(b).__dict__['data']
|
How to apply class methods to my own class' property in Python
Date : March 29 2020, 07:55 AM
Hope that helps Your class should inherit from pygame.Rect. That is the only way to get the Rect methods automatically. However by doing this you will inherits also the int typecasting of the coordinates since that is in the original implementation of pygame.Rect. I'm afraid inheritance won't solve your problem. You know what the Rect methods are supposed to do (The documentation is well written), so I'm afraid the only way is to reimplement them (or at least reimplement those you need) for your own Thing class, to mimic the Rect behaviour with float numbers. class FlRect:
"""Similar to pygame.Rect but uses float numbers.
The class stores internally only coordinates, width and height.
Other attributes are rendered through properties, with getter and setter:
x, y: coordinates of the top-left corner of the rectangle.
top, bottom: y coordinates of the top and bottom edges respectively.
left, right: x coordinates of the left and right edges respectively.
centerx, centery: coordinates of the centre of the rectangle.
width, height: self-explanatory.
"""
def __init__(self, x, y, w, h):
"""Initialization:
x, y - coordinates of top-left corner of the rectangle
w, h - width and height
"""
self._x = x
self._y = y
self._w = w
self._h = h
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = value
@property
def width(self):
return self._w
@width.setter
def width(self, value):
self._w = value
@property
def height(self):
return self._h
@height.setter
def height(self, value):
self._h = value
@property
def top(self):
return self._y
@top.setter
def top(self, value):
self._y = value
@property
def bottom(self):
return self._y + self._h
@bottom.setter
def bottom(self, value):
self._y = value - self._h
@property
def left(self):
return self._x
@left.setter
def left(self, value):
self._x = value
@property
def right(self):
return self._x + self._w
@right.setter
def right(self, value):
self._x = value - self._w
@property
def centerx(self):
return self._x + (self._w / 2)
@centerx.setter
def centerx(self, value):
self._x = value - (self._w / 2)
@property
def centery(self):
return self._y + (self._h / 2)
@centery.setter
def centery(self, value):
self._h = value - (self._h / 2)
def get_rect(self):
"""Return a pygame.Rect object with rounded coordinates"""
return Rect(round(self._x), round(self._y), round(self._w), round(self._h))
|
list @property decorated methods in a python class
Tag : python , By : user179445
Date : March 29 2020, 07:55 AM
|