GithubHelp home page GithubHelp logo

pyyaml.org's Introduction

pyyaml.org's People

Contributors

akasurde avatar ingydotnet avatar nitzmahone avatar perlpunk avatar taion avatar victoriaroan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pyyaml.org's Issues

Documentation for load() is wrong

The documentation has many instances of load being called with one argument, which is invalid usage since release 6.0. For instance, one example looks like this:

>>> stream = file('document.yaml', 'r')    # 'document.yaml' contains a single YAML document.
>>> yaml.load(stream)

This call of load does not work in release 6.0.

Also, the signature in the Reference section is incorrect. The signature is given as load(stream, Loader=Loader) but the correct signature is load(stream, Loader).

safe_load_all() raises ConstructorError where safe_load() does not.

Description

safe_load_all() raises yaml.constructor.ConstructorError for tag !include where safe_load() does not.

Steps to reproduce

  1. Install Miniconda
  2. Create conda environment:
...$ conda create -n foo python=3 PyYAML
...$ conda activate foo
  1. Create file foo.py:
import yaml

# This works fine
try:
    yaml.safe_load(
        """
        includes:
          - !include some_file.yaml
        """
    )
except yaml.YAMLError as e:
    pass

# This does not
try:
    docs = yaml.safe_load_all(
        """
        includes:
          - !include some_file.yaml
        """
    )
except yaml.YAMLError as e:
    pass

for doc in docs:
    pass

4: Run: (foo) ...$ python foo.py

Error

Traceback (most recent call last):
  File "foo.py", line 25, in <module>
    for doc in docs:
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/__init__.py", line 130, in load_all
    yield loader.get_data()
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 37, in get_data
    return self.construct_document(self.get_node())
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 52, in construct_document
    for dummy in generator:
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 399, in construct_yaml_seq
    data.extend(self.construct_sequence(node))
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 121, in construct_sequence
    return [self.construct_object(child, deep=deep)
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 121, in <listcomp>
    return [self.construct_object(child, deep=deep)
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 92, in construct_object
    data = constructor(self, node)
  File "/home/moss/miniconda3/envs/test/lib/python3.8/site-packages/yaml/constructor.py", line 418, in construct_undefined
    raise ConstructorError(None, None,
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!include'
  in "<unicode string>", line 3, column 13:
              - !include some_file.yaml
                ^

Clarify that load_all methods return a generator, not a sequence

The documentation for load_all and safe_load_all states (emphasis mine):

... returns a sequence of Python objects ...

In reality, those methods return a generator, which is not the same thing as a sequence. For example:

with open(filename) as f:
    docs = yaml.safe_load_all(f)
for d in docs:
    # do stuff

will raise ValueError: I/O operation on closed file. which if docs was really a sequence doesn't make sense.

(Moved from yaml/pyyaml#762)

error in representer.py

Hi,

I'm using pyyaml 5.4.1, installed from pypi, and came across the error below. Could you perhaps help me understand what goes wrong?

Many thanks in advance.

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml_init_.py in dump(data, stream, Dumper, **kwds)
288 If stream is None, return the produced string instead.
289 """
--> 290 return dump_all([data], stream, Dumper=Dumper, **kwds)
291
292 def safe_dump_all(documents, stream=None, **kwds):

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml_init_.py in dump_all(documents, stream, Dumper, default_style, default_flow_style, canonical, indent, width, allow_unicode, line_break, encoding, explicit_start, explicit_end, version, tags, sort_keys)
276 dumper.open()
277 for data in documents:
--> 278 dumper.represent(data)
279 dumper.close()
280 finally:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent(self, data)
25
26 def represent(self, data):
---> 27 node = self.represent_data(data)
28 self.serialize(node)
29 self.represented_objects = {}

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml_init_.py in to_yaml(cls, dumper, data)
424 """
425 return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
--> 426 flow_style=cls.yaml_flow_style)
427

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_yaml_object(self, tag, data, cls, flow_style)
226 else:
227 state = data.dict.copy()
--> 228 return self.represent_mapping(tag, state, flow_style=flow_style)
229
230 def represent_undefined(self, data):

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_mapping(self, tag, mapping, flow_style)
116 for item_key, item_value in mapping:
117 node_key = self.represent_data(item_key)
--> 118 node_value = self.represent_data(item_value)
119 if not (isinstance(node_key, ScalarNode) and not node_key.style):
120 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_list(self, data)
197 # break
198 #if not pairs:
--> 199 return self.represent_sequence('tag:yaml.org,2002:seq', data)
200 #value = []
201 #for item_key, item_value in data:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_sequence(self, tag, sequence, flow_style)
90 best_style = True
91 for item in sequence:
---> 92 node_item = self.represent_data(item)
93 if not (isinstance(node_item, ScalarNode) and not node_item.style):
94 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_dict(self, data)
205
206 def represent_dict(self, data):
--> 207 return self.represent_mapping('tag:yaml.org,2002:map', data)
208
209 def represent_set(self, data):

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_mapping(self, tag, mapping, flow_style)
116 for item_key, item_value in mapping:
117 node_key = self.represent_data(item_key)
--> 118 node_value = self.represent_data(item_value)
119 if not (isinstance(node_key, ScalarNode) and not node_key.style):
120 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_list(self, data)
197 # break
198 #if not pairs:
--> 199 return self.represent_sequence('tag:yaml.org,2002:seq', data)
200 #value = []
201 #for item_key, item_value in data:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_sequence(self, tag, sequence, flow_style)
90 best_style = True
91 for item in sequence:
---> 92 node_item = self.represent_data(item)
93 if not (isinstance(node_item, ScalarNode) and not node_item.style):
94 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml_init_.py in to_yaml(cls, dumper, data)
424 """
425 return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
--> 426 flow_style=cls.yaml_flow_style)
427

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_yaml_object(self, tag, data, cls, flow_style)
226 else:
227 state = data.dict.copy()
--> 228 return self.represent_mapping(tag, state, flow_style=flow_style)
229
230 def represent_undefined(self, data):

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_mapping(self, tag, mapping, flow_style)
116 for item_key, item_value in mapping:
117 node_key = self.represent_data(item_key)
--> 118 node_value = self.represent_data(item_value)
119 if not (isinstance(node_key, ScalarNode) and not node_key.style):
120 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
46 data_types = type(data).mro
47 if data_types[0] in self.yaml_representers:
---> 48 node = self.yaml_representers[data_types[0]](self, data)
49 else:
50 for data_type in data_types:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_list(self, data)
197 # break
198 #if not pairs:
--> 199 return self.represent_sequence('tag:yaml.org,2002:seq', data)
200 #value = []
201 #for item_key, item_value in data:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_sequence(self, tag, sequence, flow_style)
90 best_style = True
91 for item in sequence:
---> 92 node_item = self.represent_data(item)
93 if not (isinstance(node_item, ScalarNode) and not node_item.style):
94 best_style = False

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_data(self, data)
50 for data_type in data_types:
51 if data_type in self.yaml_multi_representers:
---> 52 node = self.yaml_multi_representers[data_type](self, data)
53 break
54 else:

c:\users\gpipeleers.conda\envs\bp\lib\site-packages\yaml\representer.py in represent_object(self, data)
315 reduce = copyreg.dispatch_tablecls
316 elif hasattr(data, 'reduce_ex'):
--> 317 reduce = data.reduce_ex(2)
318 elif hasattr(data, 'reduce'):
319 reduce = data.reduce()

TypeError: reduce_ex() takes exactly one argument (0 given)

Separate options for Key and Value representation

Hello:

Is there an option to set separate formatting/representation for keys and values in the yaml.dump call?

The closest thing to setting the values separately that I have found is this snippet from represent_mapping in the class BaseRepresenter. Since represent_data is called for both key and value, I'm guessing the answer is "no", but thought I'd be thorough.

 for item_key, item_value in mapping:
      node_key = self.represent_data(item_key)
      node_value = self.represent_data(item_value)
      if not (isinstance(node_key, ScalarNode) and not node_key.style):
          best_style = False
      if not (isinstance(node_value, ScalarNode) and not node_value.style):
          best_style = False
      value.append((node_key, node_value))

For context, my requirement is the yaml file must quote every value, irrespective of key or depth as they are all sql code fragments that are being passed into the AutomateDV package for dbt.

Many thanks,
Simon Norton

Wheels?

I'm trying to install a linux binary on my mac just so I can copy said binary into a docker image. During the process of doing so, I realize pyyaml does not distribute wheels, I was wondering if it's possible to distribute manylinux wheels?

“<<:” merge list ERROR

Why doesn't it support 2 list merge,The final result looks like this
{'w52': [5200, 5220], 'w53': [5300, 5320], 'w52_53': [5200, 5220, 5300, 5300]}

example 1
w52: &w52

  • 5200
  • 5300
    w53: &w53
  • 5300
  • 5320
    w52_53:
    *w52
    *w53

example 2
w52: &w52

  • 5200
  • 5300
    w53: &w53
  • 5300
  • 5320
    w52_53:
    <<: *w52
    <<: *w53

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.