Getting JSON output from within Python #20

Closed
opened 2021-06-26 11:46:33 +00:00 by fingoldo · 3 comments
fingoldo commented 2021-06-26 11:46:33 +00:00 (Migrated from gitlab.com)

Hi! Is there a native way to get a JSON output from within Python directly?
For now, I go with

from subprocess import check_output
check_output(["pylspci", "-nn"]).decode()
Hi! Is there a native way to get a JSON output from within Python directly? For now, I go with ```python from subprocess import check_output check_output(["pylspci", "-nn"]).decode() ```
Lucidiot commented 2021-09-21 17:10:12 +00:00 (Migrated from gitlab.com)

Hello there, sorry for the delay!

The JSON output was a bit of a hack, using the Device._asdict() method added by namedtuple and just vars for the NameWithID or Slot classes. You can see how it did its output right here.

I'm working on adding a bunch of as_dict() methods, so that you could go with something like this:

[device.as_dict() for device in CommandBuilder().with_ids().with_default_parser()]

This would give you a list of dicts with the same structure as what python3 -m pylspci can output. I'm putting the finishing touches and releasing 0.4.0 in a few hours!

Hello there, sorry for the delay! The JSON output was a bit of a hack, using the `Device._asdict()` method added by `namedtuple` and just `vars` for the `NameWithID` or `Slot` classes. You can see how it did its output [right here](https://gitlab.com/Lucidiot/pylspci/-/blob/96cbe0e72c4ee4403e27b24ca74b7a5315f25da4/pylspci/__main__.py#L199-207). I'm working on adding a bunch of `as_dict()` methods, so that you could go with something like this: ```python [device.as_dict() for device in CommandBuilder().with_ids().with_default_parser()] ``` This would give you a list of dicts with the same structure as what `python3 -m pylspci` can output. I'm putting the finishing touches and releasing 0.4.0 in a few hours!
Lucidiot commented 2021-09-21 18:02:09 +00:00 (Migrated from gitlab.com)

mentioned in merge request !16

mentioned in merge request !16
Lucidiot commented 2021-09-21 18:02:34 +00:00 (Migrated from gitlab.com)

mentioned in commit 7cebd6202d

mentioned in commit 7cebd6202d6018959f6088ca6df3e4415b1574c3
Sign in to join this conversation.
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: lucidiot/pylspci#20
No description provided.