Re: [Tutor] Merge a dictionary into a string

2019-03-17 Thread David L Neil

On 18/03/19 6:05 AM, Mats Wichmann wrote:

On 3/16/19 11:39 AM, Valerio Pachera wrote:

Consider this:
import collections
d = OrderedDict(a='hallo', b='world')
I wish to get a single string like this:
'a "hallo" b "world"'
Notice I wish the double quote to be part of the string.
In other words I want to wrap the value of a and b.



So the question that comes to mind is "why"?  I don't mean that in the
negative sense as in you don't want to do that, but your use case may
drive the choice of possible solutions.


Reading the OP, I immediately recognised the problem - meantime others 
had responded and the f'string' suggestion seemed most apropos.



Somewhat intrigued, and to answer the use-case question, I went looking 
in my personal collection of RDBMS routines and "snippets" (which have 
hardly been updated since Py2, excepting (IIRC) when MySQL's 
Connector-Python extended into dictionary-cursors).


The Connector will automatically delimit field/colNMs passed within a 
variable collection ('escaping' practice, highly recommended!) - a 
SELECT clause (for example). However, such automation is not applied to 
similar appearing in other clauses.


One of my helper-routines creates a comma-separated string by first 
surrounding columnNMs with back-ticks and then .join()ing. It's not 
rocket-surgery, but has been handy and import-ed many, many times.


YAGNI: me being me [puffs-out chest in a most unattractive fashion], one 
of the function's optional arguments offers a choice of delimiter. Can't 
recall ever using it 'elsewhere' though.



Thanks to the OP, and respondents making me think.
Have added to my >=v3.6 Backlog...
--
Regards =dn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-17 Thread Mats Wichmann
On 3/16/19 11:39 AM, Valerio Pachera wrote:
> Consider this:
> 
> import collections
> d = OrderedDict(a='hallo', b='world')
> 
> I wish to get a single string like this:
> 
> 'a "hallo" b "world"'
> 
> Notice I wish the double quote to be part of the string.
> In other words I want to wrap the value of a and b.

So the question that comes to mind is "why"?  I don't mean that in the
negative sense as in you don't want to do that, but your use case may
drive the choice of possible solutions.

For example, I once got asked very nearly this question by someone who
it turned out wanted to serialize the dict into something that could
later be used to load up a dict. String is what he thought of, but in
this case json, or pickle, turned out to be a better solution for what
he wanted than a string.

If you only want to print the string for informational purposes, the
suggestions here will work well.  You can even define your own class
which inherits from OrderedDict and just provides a new definition of
the method which produces a string representation and gives you what you
want without calling anything, like this:

>>> class MyOrderedDict(OrderedDict):
... def __repr__(self):
... return " ".join(f'{k} "{v}"' for k, v in self.items())
...
>>>
>>> d = MyOrderedDict(a='hallo', b='world')
>>> print(d)
a "hallo" b "world"
>>> x = str(d)
>>> x
'a "hallo" b "world"'


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Mats Wichmann
On March 16, 2019 5:57:23 PM MDT, Alan Gauld via Tutor  wrote:
>On 16/03/2019 18:44, Peter Otten wrote:
>>
>> In Python 3.6 and above you can use f-strings:
>> 
> d = dict(a="hello", b="world")
> " ".join(f'{k} "{v}"' for k, v in d.items())
>> 'a "hello" b "world"'
>
>Cool, I'd missed f-strings. Time for some reading
>
>Thanks Peter,

f-strings are great, but a lot of people have to support multiple python 
versions so they're not a big option for everyone... yet.
-- 
Sent from a mobile device with K-9 Mail. Please excuse my brevity.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alan Gauld via Tutor
On 16/03/2019 18:44, Peter Otten wrote:
>
> In Python 3.6 and above you can use f-strings:
> 
 d = dict(a="hello", b="world")
 " ".join(f'{k} "{v}"' for k, v in d.items())
> 'a "hello" b "world"'

Cool, I'd missed f-strings. Time for some reading

Thanks Peter,


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alex Kleider

On 2019-03-16 10:39, Valerio Pachera wrote:

Consider this:

import collections
d = OrderedDict(a='hallo', b='world')

I wish to get a single string like this:

'a "hallo" b "world"'

Notice I wish the double quote to be part of the string.
In other words I want to wrap the value of a and b.

I was thinking to use such function I created:

def mywrap(text, char='"'):
return(char + text + char)

I can't think anything better than

s = ''
for k, v in d.items():
 s += ' '.join( (k, mywrap(v)) ) + ' '

or

s = ''
for k, v in d.items():
s += k + ' ' + mywrap(v) + ' '

What do you think?
It's fine enough but I wonder if there's a better solution.



Would the following not  give you what you want:
(I've not used OrderedDict but I believe it would work for dict so 
assume ok for OrderedDict.)


my_which_string = "a = '{a}'  b = '{b}'".format(**d)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Peter Otten
Valerio Pachera wrote:

> Consider this:
> 
> import collections
> d = OrderedDict(a='hallo', b='world')
> 
> I wish to get a single string like this:
> 
> 'a "hallo" b "world"'
> 
> Notice I wish the double quote to be part of the string.
> In other words I want to wrap the value of a and b.
> 
> I was thinking to use such function I created:
> 
> def mywrap(text, char='"'):
> return(char + text + char)
> 
> I can't think anything better than
> 
> s = ''
> for k, v in d.items():
>  s += ' '.join( (k, mywrap(v)) ) + ' '
> 
> or
> 
> s = ''
> for k, v in d.items():
> s += k + ' ' + mywrap(v) + ' '
> 
> What do you think?
> It's fine enough but I wonder if there's a better solution.

In Python 3.6 and above you can use f-strings:

>>> d = dict(a="hello", b="world")
>>> " ".join(f'{k} "{v}"' for k, v in d.items())
'a "hello" b "world"'

By the way, are you sure that the dictionary contains only strings without 
spaces and '"'?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alan Gauld via Tutor
On 16/03/2019 17:39, Valerio Pachera wrote:

> I wish to get a single string like this:
> 
> 'a "hallo" b "world"'
> 
> Notice I wish the double quote to be part of the string.
> In other words I want to wrap the value of a and b.

When dealing with string layouts I tend to go to
string formatting...

>>> d= {'a':"alpha",'b':"beta"}
>>> ' '.join(['{} "{}"'.format(k,v) for k,v in d.items()])
'a "alpha" b "beta"'
>>>

Or using old C style formatting, it's very slightly shorter:

>>> ' '.join(['%s "%s"'% (k,v) for k,v in d.items()])
'a "alpha" b "beta"'

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor