General > General Technical Chat
How many people code in C these days, and if so, why?
paulca:
The one thing that puzzled me with Python was that it lacks basic JSON support built in. The is the "json" package and a few hacks you can do with the __dict__ member, but I honestly expected it to have amazing built in JSON marshalling support.
bd139:
It does have basic JSON stuff built in. I use it all the time. Part of the core libraries: https://github.com/python/cpython/tree/3.8/Lib/json
Berni:
--- Quote from: paulca on May 19, 2020, 02:00:35 pm ---The one thing that puzzled me with Python was that it lacks basic JSON support built in. The is the "json" package and a few hacks you can do with the __dict__ member, but I honestly expected it to have amazing built in JSON marshalling support.
--- End quote ---
That's because Python is designed to keep actual features packaged in libraries while the core language just provides some basic core data types. Not everyone likes json, maybe they want xml instead, or maybe they like pythons own proprietary binary format. But it is part of the standard library so technically its included in the language. And if you need to do something not included in the standard library its typical practice so simply "pip install x" some X library that does that thing.
But yeah if i need performance on a PC i typically just resort to C# since it can run almost as fast as C in most cases but still provide all the handy libraries for easily getting things done, even if its just boring string manipulation. Yes i know oldschool C also provides string manipulation in its standard library, but its really lacking compared to modern languages (Really feels like a chore working with strings there)
Nominal Animal:
--- Quote from: paulca on May 19, 2020, 02:00:35 pm ---The one thing that puzzled me with Python was that it lacks basic JSON support built in. The is the "json" package and a few hacks you can do with the __dict__ member, but I honestly expected it to have amazing built in JSON marshalling support.
--- End quote ---
What do you mean? The json module is built-in, a part of Python standard library. (That is, I do not understand what the problem is – unless it is that Python does not use JSON as the native serialization format – and a quick glance at the python-list mailing list didn't immediately show any discussion related to this. I'd like to know more.)
(Ninja'd by bd139 above! ;D)
Std 90, also known as RFC 8259, is only 2.5 years old at this point, and the native serialization support in Python uses the pickle module (the marshal module is used for compiled .pyc files). The pickle format itself, as of this writing, is in its sixth variant already. :-//
In general, I've never trusted pickling, because I do not like to serialize code: it tweaks my paranoia way too much, being such a risky thing to do unless completely sandboxed. Using the eval() function (with custom globals and locals, to evaluate user-specified expressions – especially math) is my limit. In that sense, JSON serialization is better, because it does not support serializing Python code, unless you add such support yourself.
paulca:
Have you tried to use the json package? It's completely rubbish. Generates pages of boiler plate code and is pretty much just a bare bones parsing thing where it's up to you to do the actual marshalling. Often involving hacks with __dict__ and namedtuples.
Or maybe I used it wrong. All I wanted was to say. "Here is some JSON, please give me a Python object with that data populated."
In Java, while much heavier weight, things like JacksonBind or Sparks Encoders will serialise in and out of JSON with nothing but annotations on a value class.
Example of python:
--- Code: --- data_str=data.decode("utf-8")
datums = Datum.jsonToDict(data_str)
--- End code ---
Fine, but I have to implement jsonToDict() as:
--- Code: --- @staticmethod
def jsonToDict(data):
ts = json.loads(data, object_hook=Datum._json_object_hook)
datums = {}
for t in ts:
if "timestamp" in t._fields:
timestamp = t.timestamp
else:
timestamp = "0"
datum = Datum(t.key, t.name, t.shortName, t.value, t.type, t.units, timestamp )
datums[t.key] = datum
return datums
--- End code ---
I also have to implement the object_hook
--- Code: --- @staticmethod
def _json_object_hook(d):
return namedtuple('Dict', d.keys())(*d.values())
--- End code ---
So only one line is actually helped by "json".
For an untyped language with dynamic member creation at run time it's a bit shit TBH. Going a few more steps to ORM would have been nice.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version