[Tutor] Two Problems (databases, and branching)

dman dman@dman.ddts.net
Sat, 27 Apr 2002 00:38:36 -0500


--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Apr 27, 2002 at 01:29:12AM -0300, Spotacus Macell wrote:

| Now, I have two problems, both of which I can't find answers to. First of=
=20
| all: I've been reading through the archived messages on Tutor, and what I=
'm=20
| getting is that you can't make databases right in Python, that you need t=
o=20
| use something else (I was seeing SQL being said a bit). If it IS possible=
=20
| to make databases right in Python, could someone point me at some=20
| documentation or some other such thing so I could figure it out?

What are your requirements for this database?  There are many choices
out there.  If you want a pure-python implementation, gadfly is the
only one I know of.  It implements a subset of SQL.  SQL is the
industry-standard language for interacting with a Relational database.
If you want a more heavy-duty database there are python modules for
connecting to a PostgreSQL or MySQL or other relational databases (all
involve SQL).  There are also bindings to dbm and bsddb
implementations, but I know little of these database systems.  There's
the ZODB (Zope Object Database) which can be used if you want to store
a hierarchical collection of objects.  On the simpler side there's the
standard 'pickle' module, or you could create your own plain-text
format.  It all depends on what your needs are and what your
restrictions are.

| And now for my second problem. From my (albeit minimal) work with #RPGCod=
e,=20
| I found a very useful command, but I can't find it in Python. I'm sure=20
| there'd be something at least similar, but I can't find it. Anyway, what =
it=20
| is is the branch command. In RPGCode, it basically looks like this:
|=20
| -------------
| #branch(:here)
|=20
| #mwin("Text in a message window")
|=20
| :here
| -------------

<shudder>.  You really don't want to start writing spaghetti-code :-).
Python, like C and other modern languages, is structured.  I presume
that before your #branch statement you have a condition to determine
whether or not to branch this time?  Structured languages do it this
way :

    # decide which way to branch
    if cond :
        mwin("Text in a message window")
    else :
        # :here
        print "the else block"

The improvement is that the text flow much more clearly indicates how
the execution will flow.  With the "goto" example all the code looks
like it flows in one line.  It is also very easy to start putting
"gotos" all over the code, and then it can become impossible to
follow.

| Combined with an if statement, it's rather useful. But I can't find
| anything like it in Python, and it perturbs me. I've used while
| loops where they were un-needed, to imitate it to go back _up_
| through a program (my first program was 80-odd lines, and only about
| 5 of which were outside of a giant while loop), but there's no way
| to dopplegang it in the down direction.

You need to refactor the code into "sub programs" (functions, in
python).  Put the statements that are executed together into a
separate function so that you can call it from multiple places and
have manageable code.  Break the "goto" habit as fast as you can!


How about posting that 80-odd line script and letting someone with
some spare cycles refactor it to give you a concrete example of how it
works?


-D

--=20

I tell you the truth, everyone who sins is a slave to sin.  Now a slave
has no permanent place in the family, but a son belongs to it forever.
So if the Son sets you free, you will be free indeed.
        John 8:34-36
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--BXVAT5kNtrzKuDFl
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzKOVwACgkQO8l8XBKTpRQXFACfV2AID85G8e6e+iXk6/DwcVfv
m8gAnRKExk5+vrwRVeqHqUcY0a2OdBt7
=4fYC
-----END PGP SIGNATURE-----

--BXVAT5kNtrzKuDFl--