Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29921

Re: Java singletonMap in Python

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'argument': 0.04; 'subject:Python': 0.05; 'parameter': 0.07; 'missed': 0.09; 'python': 0.09; '"if': 0.09; 'assumed': 0.09; 'booth': 0.09; 'dict': 0.09; 'immutable': 0.09; 'lawrence': 0.09; 'literal': 0.09; 'none.': 0.09; 'porting': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'structure,': 0.09; 'cases': 0.15; 'comments:': 0.16; 'd[key]': 0.16; 'devs': 0.16; 'dictionaries': 0.16; 'keys.': 0.16; 'pair,': 0.16; 'pair.': 0.16; 'pity': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recipes': 0.16; 'storing': 0.16; 'subject:Java': 0.16; 'url:java': 0.16; 'later': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'alex': 0.17; 'copied': 0.17; 'library,': 0.17; '>>>': 0.18; 'java': 0.21; 'friend.': 0.22; 'latter': 0.22; 'lets': 0.22; "i'd": 0.22; 'specified': 0.23; 'statement': 0.23; "i've": 0.23; 'second': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'select': 0.26; 'wondering': 0.26; '[1]': 0.27; 'possibly': 0.27; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; '+0100,': 0.29; "d'aprano": 0.29; 'key,': 0.29; 'loads': 0.29; 'steven': 0.29; 'value)': 0.29; 'waters': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'e.g.': 0.30; 'code': 0.31; 'point': 0.31; 'from:addr:yahoo.co.uk': 0.32; 'structure': 0.32; 'could': 0.32; 'getting': 0.33; 'curious': 0.33; 'zero': 0.33; 'to:addr :python-list': 0.33; 'that,': 0.34; "can't": 0.34; 'mapping': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'thank': 0.36; 'supporting': 0.37; 'two': 0.37; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'store': 0.38; 'object': 0.38; 'some': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'gives': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'google': 0.39; 'little': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'map': 0.61; 'first': 0.61; 'night': 0.62; 'fun': 0.64; 'great': 0.64; 'url:tinyurl': 0.66; 'enjoy': 0.67; 'url:4': 0.72; 'prize': 0.78; 'obvious.': 0.84; 'url:api': 0.84; 'received:2': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Java singletonMap in Python
Date Mon, 24 Sep 2012 20:28:14 +0100
References <mailman.1165.1348442059.27098.python-list@python.org> <505fc372$0$1612$c3e8da3$76491128@news.astraweb.com> <XnsA0D8BCC3B9239duncanbooth@127.0.0.1>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-2-100-238-176.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
In-Reply-To <XnsA0D8BCC3B9239duncanbooth@127.0.0.1>
X-Antivirus avast! (VPS 120924-0, 24/09/2012), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1203.1348514868.27098.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348514868 news.xs4all.nl 6953 [2001:888:2000:d::a6]:59573
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29921

Show key headers only | View raw


On 24/09/2012 18:33, Duncan Booth wrote:
> Steven D'Aprano <steve+comp.lang.python@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Java singletonMap in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-24 00:14 +0100
  Re: Java singletonMap in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-24 02:20 +0000
    Re: Java singletonMap in Python Duncan Booth <duncan.booth@invalid.invalid> - 2012-09-24 17:33 +0000
      Re: Java singletonMap in Python 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-24 12:03 -0700
      Re: Java singletonMap in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-24 20:28 +0100
      Re: Java singletonMap in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-25 00:29 -0400
      Re: Java singletonMap in Python Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-25 00:38 -0400

csiph-web