osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Casting to a "number" (both int and float)?


On Sat, 29 Aug 2015 02:15 am, Robin Koch wrote:

> Am 28.08.2015 um 18:09 schrieb Sven R. Kunze:
> 
>  >> I'm reading JSON output from an input file, and extracting values.
>  >
>> for proper parsing into native Python types, I would recommend YAML.
> 
> "What's the best way to get from A to B?"
> "I recommend starting at C."
> 
> - Every other usenet-discussion.


"I have a pig's ear. What's the easiest way to turn it into a silk purse?"

"I recommend starting with with something else."

For the non-native English speakers here, we have a proverb "You cannot turn
a pig's ear into a silk purse", a metaphor for the impossibility of making
something (or someone) of good quality from a inferior stock.

As programmers, we must recognise the importance of picking the right data
structures. For instance, if we want fast key lookups, we would pick a
dict, not a large string that needs to be slowly parsed each and every time
we perform a lookup. So the question:

"I have a large string of the form 'key:value', one key per line, how do I
quickly look up a key and get the value?"

would rightly get the answer "You don't. Use a dict." Possibly that means a
once-off conversion from string to dict, or possibly it means convincing
the source of the data to return a dict instead.

In this case, I have no opinion on whether JSON or YAML (or indeed something
else) is a better solution. But it is completely legitimate to suggest that
the current solution is sub-optimal and another would be better.




-- 
Steven