Re: Java singletonMap in Python

2012-09-24 Thread Duncan Booth
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:

 On Mon, 24 Sep 2012 00:14:23 +0100, Mark Lawrence wrote:
 
 Purely for fun I've been porting some code to Python and came across
 the singletonMap[1].  I'm aware that there are loads of recipes on
 the web for both singletons e.g.[2] and immutable dictionaries
 e.g.[3].  I was wondering how to combine any of the recipes to
 produce the best implementation, where to me best means cleanest and
 hence most maintainable.  I then managed to muddy the waters for
 myself by recalling the Alex Martelli Borg pattern[4].  Possibly or
 even probably the latter is irrelevant, but I'm still curious to know
 how you'd code this beast.
 
 First prize for the best solution is a night out with me, no guesses
 what the second prize is :)
 
 [1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/
 Collections.html
 
 Copied from that page:
 
 static Map singletonMap(Object key, Object value) 
 Returns an immutable map, mapping only the specified key to the
 specified value.
 
 I don't see the point of this. It takes a single key, with a single 
 value, and is immutable so you can't change it or add new keys. What's
 the point? Why bother storing the key:value pair in a data structure, 
 then look up the same data structure to get the same value every time?
 
 # Pseudo-code
 d = singletonMap(key, calculate(key))
 # later:
 value = d[key]  # there's only one key this could be
 process(value)
 
 
 Why not just store the value, instead of key, value and mapping?
 
 value = calculate(key)
 # later
 process(value)
 
 
 
Google is your friend. Searching for java singletonMap gives this as
the second hit:

http://stackoverflow.com/questions/7125536/when-would-i-use-java-collections-singletonmap-method

The answers seem to be that it's for all those cases in Java where you have a 
method that takes a map as an argument and you want to pass in a map with a 
single
kep/value pair. In that case it lets you replace 3 lines of Java with 1.

e.g. from the comments:
If you have a simple select statement like select foo from bar where id = 
:barId 
then you would need a parameter map with a single key-value pair, barId=123. 
That's a great place to use singletonMap()

Of course in Python you just use a dict literal in that case so it's pointless.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-24 Thread 88888 Dihedral
Duncan Booth於 2012年9月25日星期二UTC+8上午1時33分31秒寫道:
 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
 
 
 
  On Mon, 24 Sep 2012 00:14:23 +0100, Mark Lawrence wrote:
 
  
 
  Purely for fun I've been porting some code to Python and came across
 
  the singletonMap[1].  I'm aware that there are loads of recipes on
 
  the web for both singletons e.g.[2] and immutable dictionaries
 
  e.g.[3].  I was wondering how to combine any of the recipes to
 
  produce the best implementation, where to me best means cleanest and
 
  hence most maintainable.  I then managed to muddy the waters for
 
  myself by recalling the Alex Martelli Borg pattern[4].  Possibly or
 
  even probably the latter is irrelevant, but I'm still curious to know
 
  how you'd code this beast.
 
  
 
  First prize for the best solution is a night out with me, no guesses
 
  what the second prize is :)
 
  
 
  [1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/
 
  Collections.html
 
  
 
  Copied from that page:
 
  
 
  static Map singletonMap(Object key, Object value) 
 
  Returns an immutable map, mapping only the specified key to the
 
  specified value.
 
  
 
  I don't see the point of this. It takes a single key, with a single 
 
  value, and is immutable so you can't change it or add new keys. What's
 
  the point? Why bother storing the key:value pair in a data structure, 
 
  then look up the same data structure to get the same value every time?
 
  
 
  # Pseudo-code
 
  d = singletonMap(key, calculate(key))
 
  # later:
 
  value = d[key]  # there's only one key this could be
 
  process(value)
 
  
 
  
 
  Why not just store the value, instead of key, value and mapping?
 
  
 
  value = calculate(key)
 
  # later
 
  process(value)
 
  
 
  
 
  
 
 Google is your friend. Searching for java singletonMap gives this as
 
 the second hit:
 
 
 
 http://stackoverflow.com/questions/7125536/when-would-i-use-java-collections-singletonmap-method
 
 
 
 The answers seem to be that it's for all those cases in Java where you have a 
 
 method that takes a map as an argument and you want to pass in a map with a 
 single
 
 kep/value pair. In that case it lets you replace 3 lines of Java with 1.
 
 
 
 e.g. from the comments:
 
 If you have a simple select statement like select foo from bar where id = 
 :barId 
 
 then you would need a parameter map with a single key-value pair, barId=123. 
 
 That's a great place to use singletonMap()
 
 
 
 Of course in Python you just use a dict literal in that case so it's 
 pointless.
 
 
 
 -- 
 
 Duncan Booth http://kupuguy.blogspot.com

Cheers to those who are programmers that really love  authoring in the high 
level one with tools to translate into other computer languages,
and also collecting  the upgrade fees from clients.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-24 Thread Devin Jeanpierre
On Sun, Sep 23, 2012 at 7:14 PM, Mark Lawrence breamore...@yahoo.co.uk wrote:
 Purely for fun I've been porting some code to Python and came across the
 singletonMap[1].  I'm aware that there are loads of recipes on the web for
 both singletons e.g.[2] and immutable dictionaries e.g.[3].  I was wondering
 how to combine any of the recipes to produce the best implementation

The word singleton usually means thing with only one item. For
example, {a} is a singleton set containing only a, and with matrices,
any dimension of size one is called a singleton dimension, and so on.
In this case, a singleton map is a map with only one key-value pair,
such as {a:b}.

The singleton design antipattern is not relevant here.

-- Devin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-24 Thread Mark Lawrence

On 24/09/2012 18:33, Duncan Booth wrote:

Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:


On Mon, 24 Sep 2012 00:14:23 +0100, Mark Lawrence wrote:


Purely for fun I've been porting some code to Python and came across
the singletonMap[1].  I'm aware that there are loads of recipes on
the web for both singletons e.g.[2] and immutable dictionaries
e.g.[3].  I was wondering how to combine any of the recipes to
produce the best implementation, where to me best means cleanest and
hence most maintainable.  I then managed to muddy the waters for
myself by recalling the Alex Martelli Borg pattern[4].  Possibly or
even probably the latter is irrelevant, but I'm still curious to know
how you'd code this beast.

First prize for the best solution is a night out with me, no guesses
what the second prize is :)

[1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/

Collections.html

Copied from that page:

static Map singletonMap(Object key, Object value)
Returns an immutable map, mapping only the specified key to the
specified value.

I don't see the point of this. It takes a single key, with a single
value, and is immutable so you can't change it or add new keys. What's
the point? Why bother storing the key:value pair in a data structure,
then look up the same data structure to get the same value every time?

# Pseudo-code
d = singletonMap(key, calculate(key))
# later:
value = d[key]  # there's only one key this could be
process(value)


Why not just store the value, instead of key, value and mapping?

value = calculate(key)
# later
process(value)




Google is your friend. Searching for java singletonMap gives this as
the second hit:

http://stackoverflow.com/questions/7125536/when-would-i-use-java-collections-singletonmap-method

The answers seem to be that it's for all those cases in Java where you have a
method that takes a map as an argument and you want to pass in a map with a 
single
kep/value pair. In that case it lets you replace 3 lines of Java with 1.

e.g. from the comments:
If you have a simple select statement like select foo from bar where id = 
:barId
then you would need a parameter map with a single key-value pair, barId=123.
That's a great place to use singletonMap()

Of course in Python you just use a dict literal in that case so it's pointless.



Thank goodness for that, I'd assumed that I'd missed something blatantly 
obvious.  There are two chances of something like this getting into the 
standard library, zero or none.  I think in a way that's a great pity as 
I'm sure that the Python devs would enjoy supporting the little feller 
with code such as this http://tinyurl.com/9v7d7ld :)


--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-24 Thread Mark Lawrence

On 24/09/2012 20:22, Devin Jeanpierre wrote:

On Sun, Sep 23, 2012 at 7:14 PM, Mark Lawrence breamore...@yahoo.co.uk wrote:

Purely for fun I've been porting some code to Python and came across the
singletonMap[1].  I'm aware that there are loads of recipes on the web for
both singletons e.g.[2] and immutable dictionaries e.g.[3].  I was wondering
how to combine any of the recipes to produce the best implementation


The word singleton usually means thing with only one item. For
example, {a} is a singleton set containing only a, and with matrices,
any dimension of size one is called a singleton dimension, and so on.
In this case, a singleton map is a map with only one key-value pair,
such as {a:b}.

The singleton design antipattern is not relevant here.

-- Devin



Java thinks so otherwise there wouldn't also be the singleton which is 
the spelling for singletonSet (don't ask me!!!) and a singletonList. 
From the Python viewpoint I think YAGNI is perfect.  I now understand 
why the BDFL and others fight so hard to keep bloatware out of the 
standard library.


--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Java singletonMap in Python

2012-09-23 Thread Mark Lawrence
Purely for fun I've been porting some code to Python and came across the 
singletonMap[1].  I'm aware that there are loads of recipes on the web 
for both singletons e.g.[2] and immutable dictionaries e.g.[3].  I was 
wondering how to combine any of the recipes to produce the best 
implementation, where to me best means cleanest and hence most 
maintainable.  I then managed to muddy the waters for myself by 
recalling the Alex Martelli Borg pattern[4].  Possibly or even probably 
the latter is irrelevant, but I'm still curious to know how you'd code 
this beast.


First prize for the best solution is a night out with me, no guesses 
what the second prize is :)


[1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html
[2]http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python
[3]http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/
[4]http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/
--
Cheers.

Mark Lawrence.



--
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-23 Thread Oscar Benjamin
On 24 September 2012 00:14, Mark Lawrence breamore...@yahoo.co.uk wrote:

 Purely for fun I've been porting some code to Python and came across the
 singletonMap[1].  I'm aware that there are loads of recipes on the web for
 both singletons e.g.[2] and immutable dictionaries e.g.[3].  I was
 wondering how to combine any of the recipes to produce the best
 implementation, where to me best means cleanest and hence most
 maintainable.  I then managed to muddy the waters for myself by recalling
 the Alex Martelli Borg pattern[4].  Possibly or even probably the latter is
 irrelevant, but I'm still curious to know how you'd code this beast.


What exactly is wanted when an attempt is made to instantiate an instance?
Should it raise an error or return the previously created instance?

This attempt makes all calls to __new__ after the first return the same
instance:

def singleton(cls):
instance = None
class sub(cls):
def __new__(cls_, *args, **kwargs):
nonlocal instance
if instance is None:
instance = super(sub, cls_).__new__(cls_, *args, **kwargs)
return instance
sub.__name__ == cls.__name__
return sub

@singleton
class A(object):
pass

print(A() is A())

Oscar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Java singletonMap in Python

2012-09-23 Thread Steven D'Aprano
On Mon, 24 Sep 2012 00:14:23 +0100, Mark Lawrence wrote:

 Purely for fun I've been porting some code to Python and came across the
 singletonMap[1].  I'm aware that there are loads of recipes on the web
 for both singletons e.g.[2] and immutable dictionaries e.g.[3].  I was
 wondering how to combine any of the recipes to produce the best
 implementation, where to me best means cleanest and hence most
 maintainable.  I then managed to muddy the waters for myself by
 recalling the Alex Martelli Borg pattern[4].  Possibly or even probably
 the latter is irrelevant, but I'm still curious to know how you'd code
 this beast.
 
 First prize for the best solution is a night out with me, no guesses
 what the second prize is :)
 
 [1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/
Collections.html

Copied from that page:

static Map singletonMap(Object key, Object value) 
Returns an immutable map, mapping only the specified key to the specified 
value.

I don't see the point of this. It takes a single key, with a single 
value, and is immutable so you can't change it or add new keys. What's 
the point? Why bother storing the key:value pair in a data structure, 
then look up the same data structure to get the same value every time?

# Pseudo-code
d = singletonMap(key, calculate(key))
# later:
value = d[key]  # there's only one key this could be
process(value)


Why not just store the value, instead of key, value and mapping?

value = calculate(key)
# later
process(value)



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list