Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
JSON (JavaScript Object Notation) aka the Fat Free Alternative to XML is a lightweight data exchange format inspired by JavaScript object literals. It is often used with JavaScript, Ajax, and RESTful web services but is completely language independent.
0
votes
Currency converter in Python 2.7
Requests
The responses from api.fixer.io are json strings, not Python dictionaries. You can convert back and forth between the two using the json module. … base={}"
def get_rate(base, symbol):
url = URL.format(base.upper())
content = requests.get(url).json()
return content['rates'][symbol.upper()]
if __name__ == '__main__':
parser = argparse.ArgumentParser …
3
votes
Accepted
Type-safe JSON deserialization
You need to:
Get the annotated signature of a target_class' __init__ method;
Apply whatever arguments come from the provided JSON string;
Check that all arguments are present;
Check that all arguments … All except the last step is pretty straightforward using the inspect module:
import json
import inspect
from typing import Callable, Any, T
def deserialize_json(target_class: Callable[[Any], T], object_repr …
6
votes
Accepted
Store nested json repsonses in relational database
Or you could use it properly so that Country(**json_data) will build the whole thing; but:
Keys in the JSON data are not valid python identifiers, you would need to convert them;
You would need to handle … We could use dataclasses.astuple to convert or objects to proper parameters for our query, but we would still need a specific parser to recursively traverse nested JSON layers. …
2
votes
Accepted
JSON pretty print
formatted = format_pretty(source)
if filename is None:
print formatted
else:
with open(filename) as f:
f.write(formatted)
if __name__ == "__main__":
import json …
5
votes
Accepted
Extracting the IP addresses of Docker containers using JSON API
So it should be more obvious using a binary choice:
def container_status(include_stopped=False):
url = 'http://127.0.0.1:6000/containers/json? … Full code would look like:
import requests
from prettytable import PrettyTable
def container_status(include_stopped=False):
url = 'http://127.0.0.1:6000/containers/json? …
13
votes
Accepted
Minecraft block generator
Lastly, you should avoid keeping code at the top-level of the file, protect it with an if __name__ == '__main__' guard:
import os
import json
def delete_files(files):
for filename in files: … )
create_file(
f'models/block/{block_name}.json',
models_block(modid, block_name),
created_files)
if __name__ == '__main__':
block_name = input("block name …
2
votes
Program to edit an address book stored as a JSON file
All in all, you code can become:
import json
class NotAPhoneNumberException(ValueError):
pass
def save_contacts(contacts, filename):
with open(filename, 'w') as f:
json.dump(contacts …
2
votes
JSON Cache Program
Other than that, I would change a few other things:
"""TODO: Module docstring"""
import os
import json
import requests
class cache_json(object):
"""Manage a JSON object through the cache. … (self):
"""The JSON data associated to the given URL. …