Python Is Not Java
dirtsimple.org
dirtsimple.org
The language really teaches you the Zen of python.
$ python
>>> import this
from __future__ import braces
I think the effect is what PG was talking about when he discussed the difference between languages designed for the designer to use and languages designed for other people.
I have a question for the Python hackers in the audience: what file format(s) do you use for configuration files? I've been using INI style files up until now, but I've been thinking of switching to XML for more complicated formats. For example, what about something like the following example. What format would you use to store this type of configuration data for a Python application?
<?xml version="1.0" encoding="utf-8" ?>
<directoriesToBackUp>
<directoryInfo>
<directoryName>/media/programming</directoryName>
<excludedDirs>
<dir>/media/programming/log_files</dir>
<dir>/media/programming/temp_files</dir>
</excludedDirs>
</directoryInfo>
<directoryInfo>
<directoryName>/media/documents</directoryName>
<excludedDirs>
<dir>/media/documents/archive</dir>
</excludedDirs>
</directoryInfo>
</directoriesToBackUp> backup = [{
'name': '/media/programming',
'exclude': ['log_files', 'temp_files'],
}, {
'name': '/media/documents',
'exclude': ['archive'],
}]
of the top of my head.*editted for formatting
Are there any security considerations to think about if using that approach?
jeff:disqus devin$ wc settings.py settings.xml
1 15 142 settings.py
16 19 461 settings.xml
17 34 603 total
30% of the words. and, in my opinion, just as readable if not more. <backup>
<dir name="/media/programming" exclude="log_files temp_files" />
<dir name="/media/documents" exclude="archive" />
</backup> config = backup(
directory('/media/programming', exclude=['log_files', 'temp_files']),
directory('/media/documents', exclude='archive')
)I took Python's ConfigParser module and modified it to suit my needs. The config file looks a bit like this:
[directory-details]
path = /media/programming
exclude_dirs = *log_files, *temp_files
[directory-details]
path = /media/documents
exclude_dirs = /media/documents/archive
Honestly, though, xml is a perfectly valid format for storing such configuration.Not when a human has to touch it without tool support.
{
"directories_to_backup": [
{"directory_name": "/media/programming",
"exclude_dirs": ["/media/programming/log_files",
"/media/programming/temp_files" ]
},
{"directory_name": "/media/documents",
"exclude_dirs": ["/media/documents/archive"]
}
}Also, why not just use a python data structure? Do you really need a plain text config file?
If find the need for a config file (I tend to like command line options better) 95% of the time I use python code and import.
The benefits are many; zero work required, known syntax, powerful syntax, arbitrarily complex, no parsing required, nice traceback/errors with screwed up syntax.
#file config.py
debug = True
directories_to_backup = [
# path exclude these subdirs
("/media/programming", ("log_files","temp_files")),
("/media/documents", ("archive",)),
]
# or, if you relish verbosity
directory_info = dict
directories_to_backup = [
directory_info(path="/media/programming", exclude=("log_files","temp_files")),
directory_info(path="/media/documents", exclude=("archive",)),
]
# the options are as endless as your knowledge of Python
Usage import config
for path, excludes in config.directories_to_backup:
garbleblaster(path, excludes)YAML.
directoriesToBackup:
/media/programming:
exclude:
- /media/programming/log_files
- /media/programming/temp_files
/media/documents:
exclude:
- /media/documents/archiveI'm very embarrassed that this beautiful standard has been around for ~5 years without me noticing it.
/me goes off to read http://yaml.org/spec/1.2/
/me goes off to read http://yaml.org/spec/1.2/
There's another problem. YAML is so simple, you don't need to read the damn manual for it. That example already taught you 80%. Ok, the easy 80%. But if you come from the Java (and XML) world, reading the manual is what you do just to get started.To be fair, one equally shouldn't try to program in Java with python on the brain, it's going to turn out just as hideous and slow, and the developer will probably be more furious at the number of recompilations caused because approach the problem with the right mindset.
(As a relative newbie to Python who has in the past coded in a bunch of other languages - including Java - I had actually been meaning to look it up.)
It's patently obvious.
Languages are different for a reason, and if you wish to do meaningful work in a language you would do well to familiarize yourself with the core constructs of a language before embarking on a major project, especially if you have a lot of expertise in some other language, because you may have to unlearn a few things.