473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

scalar references


I have some (potentially mutable) scalar values that I want to share
among many class instances. Changes in one instance should be
reflected across the many instances. A classic Borg is not
appropriate because I have more than one instance.

Use case: in a plotting library I have several figures. Each figure
has a DPI parameter, possibly different between figures, which I want
to share with all the figure components (Axes, Ticks, etc...). I want
to update the DPI in one place and have it shared across all the
relevant components of the figure.

My current approach (which works) is

class RRef:
'A read only ref'
def __init__(self, val):
self._val = val

def get(self):
return self._val

class RWRef(RRef):
'A readable and writable ref'
def set(self, val):
self._val = val

Then I can instantiate a DPI instance as

dpi = RWRef(72)

and __init__ all the figure components with it, which store

self.dpi = dpi

When a figure changes it's DPI via the set method, all the components
have a reference to the mutated value.

I don't really have a problem with this approach, but am wondering if
this is the best (most pythonic!) way to share a mutable scalar value
among several objects.

In addition, I want to do basic binary op arithmetic on read only
references, eg

def bintimes(x,y): return x*y
def binadd(x,y): return x+y
def binsub(x,y): return x-y

class RRef:
'A read only ref'
def __init__(self, val):
self._val = val

def get(self):
return self._val

def __add__(self, other):
return BinOp(self, other, binadd)

def __mul__(self, other):
return BinOp(self, other, bintimes)

def __sub__(self, other):
return BinOp(self, other, binsub)

class BinOp(RRef):
'A read only ref that handles binary ops of refs'
def __init__(self, ref1, ref2, func=binadd):
self.ref1 = ref1
self.ref2 = ref2
self.func = func

def get(self):
return self.func(self.ref1.get(), self.ref2.get())

Thanks for any suggestions!
John Hunter

Jul 18 '05 #1
2 1555
John Hunter wrote:
My current approach (which works) is

class RRef:
'A read only ref'
def __init__(self, val):
self._val = val

def get(self):
return self._val

class RWRef(RRef):
'A readable and writable ref'
def set(self, val):
self._val = val


Yep, this is the kind of thing I usually do. If you'd like to write
shorter, but less self-documenting code, you can just use a one-element
list.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ Perfect situations must go wrong
-- Florence, _Chess_
Jul 18 '05 #2
John Hunter wrote:
I have some (potentially mutable) scalar values that I want to share
among many class instances. Changes in one instance should be
reflected across the many instances. A classic Borg is not
appropriate because I have more than one instance.


Below is a slight variation of your approach. I think both the beauty and
the danger is that, for read access, attributes defined in the client
instance are indistinguishable from attributes defined in the template
object. I've separated them here, but you could even use a UseTemplate
instance as the template for another instance.

class Template:
def __init__(self, **kwd):
self.__dict__.update(kwd)
german = Template(color="blue", language="german")
french = Template(color="yellow", language="french")

class UseTemplate:
def __init__(self, name, template=german):
self.name = name
self.template = template
def __getattr__(self, name):
"Attributes not found in UseTemplate are looked up in the template"
return getattr(self.template, name)

def printit(u):
print "name=%s, color=%s, language=%s" % (u.name, u.color, u.language)

first = UseTemplate("first", german)
second = UseTemplate("second", french)
special = UseTemplate("special", french)

def printThem():
for u in [first, second, special]:
printit(u)
print

printThem()

# instance attributes shade template attributes
special.color = "black"

# changes in template affect all instances using it
french.color = "red"

printThem()

del special.color # the template attr will reappear
printThem()

Peter
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mountain Man | last post by:
Hi, I have an array derived from a set of radio buttons that I want to break down into a single variable for use with a database. How can I do this? $gender is the array, and I want $gender2 to...
7
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop...
4
by: Tyler Hudson | last post by:
Is it ill-advised to have columns whose values pull from scalar functions using other fields in the record as parameters? For example, if I have create table a(iID int primary key) create table...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
5
by: Eli | last post by:
Hi, I want to check whether a value is a scalar. A scalar can be: - None (null) - string - number (integer, float) - boolean How can I validate a value is one of these types? I care about...
11
by: tthunder | last post by:
Hi @all, My small example does not compile... I know, that this (as always) has reasons, but I want to know WHY? BTW: I only get errors with g++ (4.x), BCB (6.0),... VS C++ (2005) works...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
2
by: nshishir | last post by:
In oracle, there is a performance improvement if scalar subqueries are used instead of joins. Does this hold good for Db2 (8.2) too?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.