[Tutor] Here's something to talk about
Paul McGuire
ptmcg at austin.rr.com
Wed Apr 15 21:09:51 CEST 2009
1. Python is not Java (see Philip Eby's blog entry
http://dirtsimple.org/2004/12/python-is-not-java.html). Let go of your
concepts that only Items can go into an ItemCollection - Python already has
some perfectly good collection builtins. Instead of writing a custom
ItemCollection, why not write a generic Exporter that takes any Python
sequence (such as a list, set, tuple, etc., anything that supports iter)?
2. SQLExporter does not use recommended form for substituting values into an
SQL statement. It is preferred that one use the 2-argument form of execute,
in which the first argument contains '?' or '%s' placeholders, and the
second argument is a tuple of values. That is, instead of:
first, last = "Bobby", "Tables"
cursor.execute("INSERT into STUDENT (firstname, lastname) values
('"+first+"','"+last+"')")
Do:
first, last = "Bobby", "Tables"
cursor.execute("INSERT into STUDENT (firstname, lastname) values (?,?)",
(first,last))
No need for wrapping in quotes, already handles values with embedded quotes,
and no SQL injection jeopardy (http://xkcd.com/327/). This slightly
complicates your SQL exporter, it would have to return a tuple containing
the INSERT statement and the tuple of substitution values, instead of just a
string to be passed to execute.
3. The Pythonic way to create a comma-separated string of values from a list
of strings is: ','.join(value_list). If you have a list of tuples and want
two comma-separated strings, do: keystring, valstring = (','.join(s) for s
in zip(*key_value_tuple_list))
4. While I am not a slave to PEP8, your mixed case method names with leading
capitals look too much like class names and/or .Net method names.
In general, I find your approach too intrusive into the objects you would
like to export or load. I would prefer to see a Loader and/or Exporter
class that takes my own application object, configured with table name and
field names, and then creates the proper XML or SQL representation.
-- Paul
More information about the Tutor
mailing list