Nico Schlömer | 12 Jun 2012 21:39
Picon
Gravatar

less verbose Python lib

Hi all,

in C++, typical YAML usage looks like

============ *snip* ============
YAML::Emitter out;
out << YAML::BeginSeq;
out << "eggs";
out << "bread";
out << "milk";
out << YAML::EndSeq;
============ *snap* ============

while the same thing in Python is (as far as I know)
============ *snip* ============
print yaml.emit([yaml.StreamStartEvent(),
                 yaml.DocumentStartEvent(),
                 yaml.SequenceStartEvent(anchor=None, tag=None, implicit=True),
                 yaml.ScalarEvent(anchor=None, tag=None,
implicit=(True, False), value=u'eggs'),
                 yaml.ScalarEvent(anchor=None, tag=None,
implicit=(True, False), value=u'bread'),
                 yaml.ScalarEvent(anchor=None, tag=None,
implicit=(True, False), value=u'milk'),
                 yaml.SequenceEndEvent()
               ])
============ *snap* ============

Is there any way to make this less verbose? (I don't even know why I
need all these options.)
(Continue reading)

Dennis Lissov | 12 Jun 2012 22:52
Picon
Gravatar

Re: less verbose Python lib

Hello Nico,

in Python YAML is typically mapped directly to the trees of objects.
The YAML sequence is mapped to the Python list, so using

    yaml.dump(['eggs', 'bread', 'milk'], default_flow_style=False)

and if the equivalent output of '[eggs, bread, milk]' is acceptable,
the keyword argument may also be omitted. Unless you need to
explicitly deal with the representation and serialization steps, it's
the easiest way to deal with YAML in Python.

Good luck,
Dennis.

On Tue, Jun 12, 2012 at 11:39 PM, Nico Schlömer
<nico.schloemer <at> gmail.com> wrote:
> Hi all,
>
> in C++, typical YAML usage looks like
>
> ============ *snip* ============
> YAML::Emitter out;
> out << YAML::BeginSeq;
> out << "eggs";
> out << "bread";
> out << "milk";
> out << YAML::EndSeq;
> ============ *snap* ============
>
(Continue reading)

Nico Schlömer | 12 Jun 2012 23:01
Picon
Gravatar

Re: less verbose Python lib

Compiling the list is difficult for me as the items take a long time
to be computed (the emitter is supposed to be part of a numerical
code), so I wouldn't want to have to wait for the last item to arrive
until they're printed all together.

On Tue, Jun 12, 2012 at 10:52 PM, Dennis Lissov
<dennis.lissov@...> wrote:
> Hello Nico,
>
> in Python YAML is typically mapped directly to the trees of objects.
> The YAML sequence is mapped to the Python list, so using
>
>    yaml.dump(['eggs', 'bread', 'milk'], default_flow_style=False)
>
> and if the equivalent output of '[eggs, bread, milk]' is acceptable,
> the keyword argument may also be omitted. Unless you need to
> explicitly deal with the representation and serialization steps, it's
> the easiest way to deal with YAML in Python.
>
> Good luck,
> Dennis.
>
> On Tue, Jun 12, 2012 at 11:39 PM, Nico Schlömer
> <nico.schloemer@...> wrote:
>> Hi all,
>>
>> in C++, typical YAML usage looks like
>>
>> ============ *snip* ============
>> YAML::Emitter out;
(Continue reading)

Daniel Goertzen | 12 Jun 2012 23:18
Picon

Re: less verbose Python lib

Perhaps implement your items as a generator instead of building the whole list ahead of time.


Dan.

On Tue, Jun 12, 2012 at 4:01 PM, Nico Schlömer <nico.schloemer <at> gmail.com> wrote:
Compiling the list is difficult for me as the items take a long time
to be computed (the emitter is supposed to be part of a numerical
code), so I wouldn't want to have to wait for the last item to arrive
until they're printed all together.



On Tue, Jun 12, 2012 at 10:52 PM, Dennis Lissov <dennis.lissov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Hello Nico,
>
> in Python YAML is typically mapped directly to the trees of objects.
> The YAML sequence is mapped to the Python list, so using
>
>    yaml.dump(['eggs', 'bread', 'milk'], default_flow_style=False)
>
> and if the equivalent output of '[eggs, bread, milk]' is acceptable,
> the keyword argument may also be omitted. Unless you need to
> explicitly deal with the representation and serialization steps, it's
> the easiest way to deal with YAML in Python.
>
> Good luck,
> Dennis.
>
> On Tue, Jun 12, 2012 at 11:39 PM, Nico Schlömer
> <nico.schloemer <at> gmail.com> wrote:
>> Hi all,
>>
>> in C++, typical YAML usage looks like
>>
>> ============ *snip* ============
>> YAML::Emitter out;
>> out << YAML::BeginSeq;
>> out << "eggs";
>> out << "bread";
>> out << "milk";
>> out << YAML::EndSeq;
>> ============ *snap* ============
>>
>> while the same thing in Python is (as far as I know)
>> ============ *snip* ============
>> print yaml.emit([yaml.StreamStartEvent(),
>>                 yaml.DocumentStartEvent(),
>>                 yaml.SequenceStartEvent(anchor=None, tag=None, implicit=True),
>>                 yaml.ScalarEvent(anchor=None, tag=None,
>> implicit=(True, False), value=u'eggs'),
>>                 yaml.ScalarEvent(anchor=None, tag=None,
>> implicit=(True, False), value=u'bread'),
>>                 yaml.ScalarEvent(anchor=None, tag=None,
>> implicit=(True, False), value=u'milk'),
>>                 yaml.SequenceEndEvent()
>>               ])
>> ============ *snap* ============
>>
>> Is there any way to make this less verbose? (I don't even know why I
>> need all these options.)
>>
>> Cheers,
>> Nico
>>
>> ------------------------------------------------------------------------------
>> Live Security Virtual Conference
>> Exclusive live event will cover all the ways today's security and
>> threat landscape has changed and how IT managers can respond. Discussions
>> will include endpoint security, mobile security and the latest in malware
>> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> _______________________________________________
>> Yaml-core mailing list
>> Yaml-core <at> lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/yaml-core

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Yaml-core mailing list
Yaml-core-5NWGOfrQmnetEtDZOKyKiw@public.gmane.orgrge.net
https://lists.sourceforge.net/lists/listinfo/yaml-core

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Yaml-core mailing list
Yaml-core@...
https://lists.sourceforge.net/lists/listinfo/yaml-core

Gmane