Python and Ruby

  • Thread starter Jean Guillaume Pyraksos
  • Start date
L

Lou Pecora

John Bokma said:
Not C, but C++ (but there are also C implementations): YAML, see:
http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument

I use YAML now and then with Perl for both reading/writing data and for
debugging purposes (YAML is quite human readable, for programmers at least)

Of course there is also YAML support for Python:
http://pyyaml.org/.

Well, that looks a bit more complicated than I would like, but maybe
it's doing more stuff than I can grok. Here's what I needed and how I
did it in Python:

# Make some variables
x=1.234e-8
y=2
astr="An output string...whatever"
z=4.5+1j*1.3456 # a complex number

# Output them to a file already opened as fp
outlist=[x, y, astr, z]
repvars= repr(outlist)+"\n"
fp.write(repvars)

# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed. 3 lines to write or read a inhomogeneous
collection of variables. I can add more variables, shuffle the order,
whatever without messing with formatting, etc. That's pretty easy for me
and it's easy for anyone to see and understand what's being done. Not
trying to start an argument, just showing how the former messasge I was
replying to made a good point about Python's way of doing things and the
effort to shake off old habits from other languages.
 
L

Lou Pecora

Paul Rubin said:
FYI: I do that too sometimes, but in general using repr/eval that way
is poor style and not very reliable. It's better to use the pickle
module, which is intended for that sort of thing, or the json module
if your datatypes are suitable and you want to follow a semi-standard.

Yeah, I should look into pickle. Haven't messed with that. Most of
what I do is numerical calculations for my consumption/research so quick
and easy comes first. Thanks for the hint.
 
J

John Bokma

Lou Pecora said:
John Bokma said:
Not C, but C++ (but there are also C implementations): YAML, see:
http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument

I use YAML now and then with Perl for both reading/writing data and for
debugging purposes (YAML is quite human readable, for programmers at least)

Of course there is also YAML support for Python:
http://pyyaml.org/.

Well, that looks a bit more complicated than I would like, but maybe
it's doing more stuff than I can grok. Here's what I needed and how I
did it in Python:

# Make some variables
x=1.234e-8
y=2
astr="An output string...whatever"
z=4.5+1j*1.3456 # a complex number

# Output them to a file already opened as fp
outlist=[x, y, astr, z]
repvars= repr(outlist)+"\n"
fp.write(repvars)

# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed. 3 lines to write or read a inhomogeneous
collection of variables. I can add more variables, shuffle the order,
whatever without messing with formatting, etc. That's pretty easy for me
and it's easy for anyone to see and understand what's being done. Not
trying to start an argument, just showing how the former messasge I was
replying to made a good point about Python's way of doing things and the
effort to shake off old habits from other languages.

My C++ is rusty to say the least, so I can't give you an example in C++,
and the C++ version will be longer than the Python version for
sure. I use YAML, YAML::Syck to be precise, now and then in Perl. In
Perl, if $data is a reference to an arbitrary (complex) datastructure:

DumpFile( 'filename', $data );

writes it out and

$data = LoadFile( 'filename' );

reads it back in.

Since YAML is to some extent human readable, I now and then use it for
debugging purposes over Data::Dumper, also because the output is more
compact, e.g.

die Dump $data;

Personally I think it's good to be aware of YAML, since it's supported
by several languages and it should in general be possible to exchange
the generated YAML between them.
 
J

Jonathan Gardner

I agree. If the audience doesn't understand then you haven't explained it..

On the contrary, that explanation would have everything you need. It
would take an hour to read or listen to the explanation, but much more
than that time to truly understand everything that was said.
 
S

Steven D'Aprano

Well, that looks a bit more complicated than I would like, but maybe
it's doing more stuff than I can grok. Here's what I needed and how I
did it in Python: [...]
# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed. 3 lines to write or read a inhomogeneous
collection of variables.

Easy, but also quick and dirty -- good enough for small scripts, but not
really good enough for production applications.

I can add more variables, shuffle the order,
whatever without messing with formatting, etc.

This is nice and easy. But there are at least four catches:


* you can't safely treat the data file as human-editable
(although a sufficiently careful and Python-aware user could edit it)

* you can't use any data that isn't a built-in, or that contains
something that is not a built-in

* there may be reliability issues with floats - you're at the mercy of
changes to the underlying repr of float objects, and it almost certainly
will blow up in your face if you get an inf or nan (at least prior to
Python 2.6)

* you're using eval, which is a security risk if you can't trust the
source of the data file.

However, be aware that neither marshal nor pickle guarantees to be safe
against malicious data either. The docs for both warn against using them
on untrusted data. YAML or JSON *might* be safer, I haven't looked.


That's pretty easy for me
and it's easy for anyone to see and understand what's being done. Not
trying to start an argument, just showing how the former messasge I was
replying to made a good point about Python's way of doing things and the
effort to shake off old habits from other languages.

These are all good points.
 
J

John Bokma

Steven D'Aprano said:
However, be aware that neither marshal nor pickle guarantees to be safe
against malicious data either. The docs for both warn against using them
on untrusted data. YAML or JSON *might* be safer, I haven't looked.

Regarding malicious data, from the Loading YAML section of PyYAML:

Warning: It is not safe to call yaml.load with any data received from
an untrusted source! yaml.load is as powerful as pickle.load and so
may call any Python function. Check the yaml.safe_load function
though.

http://pyyaml.org/wiki/PyYAMLDocumentation#LoadingYAML

yaml.safe_load however, limits to simple Python objects and Python
objects you mark as safe.
 
R

Robert Kern

On the contrary, that explanation would have everything you need. It
would take an hour to read or listen to the explanation, but much more
than that time to truly understand everything that was said.

Like I said, that's exposition, not explanation. There is an important
distinction between the two words. Simply providing information is not
explanation. If it takes four hours for your audience to understand it, then you
explained it in four hours no matter when you stopped talking.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
E

Ethan Furman

Robert said:
Like I said, that's exposition, not explanation. There is an important
distinction between the two words. Simply providing information is not
explanation. If it takes four hours for your audience to understand it,
then you explained it in four hours no matter when you stopped talking.

And if it takes six months? Would you seriously say it took you six
months to explain something because it took that long for your audience
to understand it?

At some point you have to make the transition from person A explaining
and person(s) B understanding -- they don't necessarily happen
synchronously.

As a real-life example, I've read several Python books, tutorials, and
this list for quite some time, some of which has very good explanatory
material, and yet some of the points I didn't fully comprehend until
much, much later. Every time, though, it's still the same reaction: I
*love* Python! :D

~Ethan~
 
L

Lou Pecora

Steven D'Aprano said:
Well, that looks a bit more complicated than I would like, but maybe
it's doing more stuff than I can grok. Here's what I needed and how I
did it in Python: [...]
# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed. 3 lines to write or read a inhomogeneous
collection of variables.

Easy, but also quick and dirty -- good enough for small scripts, but not
really good enough for production applications.

I can add more variables, shuffle the order,
whatever without messing with formatting, etc.

This is nice and easy. But there are at least four catches:


* you can't safely treat the data file as human-editable
(although a sufficiently careful and Python-aware user could edit it)

* you can't use any data that isn't a built-in, or that contains
something that is not a built-in

* there may be reliability issues with floats - you're at the mercy of
changes to the underlying repr of float objects, and it almost certainly
will blow up in your face if you get an inf or nan (at least prior to
Python 2.6)

* you're using eval, which is a security risk if you can't trust the
source of the data file.

However, be aware that neither marshal nor pickle guarantees to be safe
against malicious data either. The docs for both warn against using them
on untrusted data. YAML or JSON *might* be safer, I haven't looked.

I understand where you are coming from: Production Code. I was just
making a point about Python and my code is only used by me. I can edit
the file for the simple I/O I do. I am not recommending this way for
everyone. Just an example.
 
R

Robert Kern

And if it takes six months? Would you seriously say it took you six
months to explain something because it took that long for your audience
to understand it?

At some point you have to make the transition from person A explaining
and person(s) B understanding -- they don't necessarily happen
synchronously.

Then it's exposition and understanding, not explanation and understanding.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
E

Ethan Furman

Robert said:
Then it's exposition and understanding, not explanation and understanding.

Hmm. Well, I can see your point -- after all, if are "explaining" but
your audience is not understanding, are you really explaining? Okay,
looking in the dictionary...

ex⋅plain –verb (used with object)
1. to make plain or clear; render understandable or intelligible: to
explain an obscure point.
2. to make known in detail: to explain how to do something.

un⋅der⋅stand –verb (used with object)
1. to perceive the meaning of; grasp the idea of; comprehend: to
understand Spanish; I didn't understand your question.
2. to be thoroughly familiar with; apprehend clearly the character,
nature, or subtleties of: to understand a trade.
3. to assign a meaning to; interpret: He understood her suggestion as a
complaint.
4. to grasp the significance, implications, or importance of: He does
not understand responsibility.

For me, at least, it boils down to this feeling that understanding is
not a True/False item, but more of a scale (like all the the numbers
between 0.0 and 1.0 [not including 1.0 of course -- this *is* Python!
;)]). As a personal example, decorators are not that difficult to grasp
-- you take your function and wrap it in another function; but what you
can do with them! They are truly impressive once your understanding
deepens.

And at the end of the day (or this thread, whichever comes first ;)
Python is awesome, and that's what counts.

~Ethan~
 
S

Steven D'Aprano

On Fri, 05 Feb 2010 09:22:03 -0500, Lou Pecora wrote:

[...]
Easy, but also quick and dirty -- good enough for small scripts, but
not really good enough for production applications.
[...]
I understand where you are coming from: Production Code. I was just
making a point about Python and my code is only used by me. I can edit
the file for the simple I/O I do. I am not recommending this way for
everyone. Just an example.

We're in violent agreement then :)
 
W

waku

[...]
there are languages where indentation can be either enforced and allow
one to omit some syntactic nuissance like braces or begin-end clauses,
or made optional, requiring other syntactic means for delimiting
blocks etc.  (consider f# with its #light declaration, for example.)

If you're writing "experimental code", you're doing it wrong. Every
line of code you write may end up on the space shuttle one day (so to
speak!) Why not write the code well-formatted in the first place, so
that any bugs you introduce are as easily visible as possible?

you're missing the large part of the language's customers that use it
specifically for experimenting. they're not developing space
shuttles, but rather explore available data, experiment with
algorithms, etc. (and for space shuttles, i doubt python is the first
choice anyway.)

yes, i am writing lots of experimental code, and i know many who do,
too, and there is *nothing* wrong about it. and then, i sometimes use
ipython to interactively experiment, saving the input to a log file,
and editing it afterwards as needed. and just the mere fact that i
*have* to adapt my editor to ipython's indentation policy (or vice
versa) whenever working with others' code because otherwise the code
fails, is fairly annoying. there is nothing wrong or stupid about the
editor in this respect.

The only reason why you may want to write crap code without proper
formatting is because your text editor is stupid.

'proper' formatting is, in some languages, something achieved by
simply running a pretty formatter. in some, it's the job of the
programmer. it has nothing to do with the claimed stupidity of the
editor.
If that's the case,
get rid of your text editor and find some tools that help you do the
right thing the first time.
[...]

If you're text editor has a problem with indenting, you have a
terrible text editor. Period, end of sentence.

you're just wrong.

You can't screw in bolts with a hammer, and you can't level with a
saw.

.... and you can't freely format you code with python.
Don't try to write code in any language without a real text

'real'? meaning one able to guess how to combine code from multiple
developers with different indentation policies, for example, one using
tabs, another four spaces, and yet another eight? (which is not at
all uncommon, and not quite wrong or stupid.)
editor that can do proper indentation. Don't let your teammates use
deficient text editors either. I wouldn't appreciate it if I delivered
precision components that my teammates tried to install with
sledgehammers.

This is the 21st Century. Good text editors are not hard to find on
any platform.

'stupid', 'wrong', 'deficient', 'terrible', ... you're using strong
words instead of concrete arguments, it might intimidate your
opponents, but is hardly helpful in a fair discussion.

vQ
 
J

Jonathan Gardner

'stupid', 'wrong', 'deficient', 'terrible', ...  you're using strong
words instead of concrete arguments, it might intimidate your
opponents, but is hardly helpful in a fair discussion.

In today's day and age, I don't know how a text editor which cannot do
simple indentation can be considered anything but "stupid", "wrong",
"deficient", and "terrible". There is simply no excuse for this kind
of behavior on the part of the text editor.

I mean, how long has vi had the auto indent feature? How many decades
has emacs supported the same? This isn't new technology, or difficult
technology to implement. It's not even non-standard anymore. Heck, far
more advanced features, such as syntax highlighting, are standard in
all text editors.

Your problem is you're using something like Windows Notepad to edit
your code. Windows Notepad is a terrible tool for writing anything,
let alone writing code. Why in the world would any programming
language adapt itself to the mental deficiencies of Windows Notepad? I
mean, if you were an artist, would you use MS Paint to do anything
serious? If you did, would you complain that the art world has a
problem because they don't accept your paintings?

I strongly suggest you download one of the *hundreds* of free text
editors available for the Windows platform that give you the
indentation features you so sorely lack. I suggest ViM, though it has
a steep learning curve.
 
W

waku

In today's day and age, I don't know how a text editor which cannot do
simple indentation can be considered anything but "stupid", "wrong",
"deficient", and "terrible". There is simply no excuse for this kind
of behavior on the part of the text editor.

i thought we were not discussing text editors, why should you insist
so much on diverting attention from the issue of a programmer being
*forced* to keep perfect indentation to the issue of an editor doing
autoindent or the like.
I mean, how long has vi had the auto indent feature? How many decades
has emacs supported the same? This isn't new technology, or difficult
technology to implement. It's not even non-standard anymore. Heck, far
more advanced features, such as syntax highlighting, are standard in
all text editors.

wow! and then, back to the issue of enforced indentation in python?

Your problem is you're using something like Windows Notepad to edit
your code.

thin ice! you haven't got the faintest idea of what i use to edit my
code, do you.


Windows Notepad is a terrible tool for writing anything,
let alone writing code. Why in the world would any programming
language adapt itself to the mental deficiencies of Windows Notepad? I
mean, if you were an artist, would you use MS Paint to do anything
serious? If you did, would you complain that the art world has a
problem because they don't accept your paintings?

I strongly suggest you download one of the *hundreds* of free text
editors available for the Windows platform that give you the
indentation features you so sorely lack. I suggest ViM, though it has
a steep learning curve.

listen to what people say before responding, and stop showing off.
again, you haven't used a single reasonable argument here.

vQ
 
S

Steve Holden

waku said:
i thought we were not discussing text editors, why should you insist
so much on diverting attention from the issue of a programmer being
*forced* to keep perfect indentation to the issue of an editor doing
autoindent or the like.
It's as sensible to complain about people being "*forced* to keep
perfect indentation" as it is to complain about people being *forced* to
use braces to delimit code blocks.

This is called "syntax", and it's a part of the language, so get over it
- this aspect of Python has been present in the language since its
inception, and it isn't going to change.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top