Need some advice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Wagner

    Need some advice

    I am in the process of learning Python (obsessively so). I've been through a few tutorials and read
    a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that
    Numerology program I wrote years ago in VB.

    There are times I am totally stuck (for instance, I just had an idea to put the numerical values of
    the alphabet and months of the year in a dictionary located in a function. Then, I can import the
    dictionary I need from that function ... well, I'm getting import errors).

    So the question is this ... when I get stuck like this and seem to be banging into walls with
    everything I try, is it better to continue trying different things or stop, take a break and go back
    to reading a tutorial or a Python book? Or is it better that after I've tried everything I can think
    of, I just post the question here, get the answer and move forward?

    Thanks, Jeff
  • Bruno Desthuilliers

    #2
    Re: Need some advice

    Jeff Wagner wrote:[color=blue]
    > I am in the process of learning Python (obsessively so). I've been through a few tutorials and read
    > a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that
    > Numerology program I wrote years ago in VB.
    >
    > There are times I am totally stuck (for instance, I just had an idea to put the numerical values of
    > the alphabet and months of the year in a dictionary located in a function. Then, I can import the
    > dictionary I need from that function ... well, I'm getting import errors).[/color]

    "import the dictionnary from a function" ????
    [color=blue]
    > So the question is this ... when I get stuck like this and seem to be banging into walls with
    > everything I try, is it better to continue trying different things[/color]

    Definitively, *no*, niet, nein, non, etc... If you don't know what
    you're doing, it's not programming, it's a story about monkeys,
    typewriters and Shakespeare.
    [color=blue]
    > or stop, take a break and go back
    > to reading a tutorial or a Python book?[/color]

    Should be one of the first thing to do. You won't do anything good if
    you don't know enough of the language.
    [color=blue]
    > Or is it better that after I've tried everything I can think
    > of,[/color]

    "trying everything [you] can think of" is not programming. See above...
    [color=blue]
    > I just post the question here, get the answer and move forward?[/color]

    First try to know what you're trying to do, and why you're trying to do
    it this way. Ask yourself if this is the best way (or at least the most
    obvious way) to do it with the language. Then look thru your tutorials,
    books, etc to see how this can be done. Don't forget that the Python
    shell is a great tool.

    If then you're still stuck, you're of course welcome to post your
    question here, with explanation about what you're trying to do and what
    you're stuck with !-)

    My 0.2 cents
    Bruno

    Comment

    • Dennis Lee Bieber

      #3
      Re: Need some advice

      Jeff Wagner fed this fish to the penguins on Saturday 29 November 2003
      16:19 pm:

      [color=blue]
      > There are times I am totally stuck (for instance, I just had an idea
      > to put the numerical values of the alphabet and months of the year in
      > a dictionary located in a function. Then, I can import the dictionary
      > I need from that function ... well, I'm getting import errors).
      >[/color]
      As I mentioned earlier it seems you are trying to modularize the
      program before even getting the logic working...

      Try coding it as one file first, /then/ consider what makes sense as
      an imported module. NOTE: you import modules, modules may contain
      functions, "constants" (Python doesn't really have such), and classes
      -- your months of the year wouldn't need to be a function, just a
      "constant" list, and as for the values of the alphabet? If they are
      sequential (no skips or duplicates) a short function can calculate them
      (in ASCII, EBCDIC would be more difficult as the alphabet is not
      contiguous). See my post of a few hours ago...
      [color=blue]
      > So the question is this ... when I get stuck like this and seem to be
      > banging into walls with everything I try, is it better to continue
      > trying different things or stop, take a break and go back to reading a
      > tutorial or a Python book? Or is it better that after I've tried
      > everything I can think of, I just post the question here, get the
      > answer and move forward?
      >[/color]
      Ignoring classes for now (though I did stick one into my example code)
      I'd also suggest ignoring module import (except for Python supplied
      stuff) at this time... Work on understanding the passing of arguments
      to a function and the return of values from the function (try, above
      all, to avoid relying on global variables -- global "constants" may be
      permissible <G>).


      After all, your piece-meal approach without context did cause some
      confusion (mostly of the "why would you want to even do this?" type).
      As soon as you linked your "master number" stuff to calculating numbers
      from names I was able to envision numerology and hack a quick first
      approach to the entire problem (less your special numbers, and not
      fully tested by hand computations, but still...)

      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Bestiaria Home Page: http://www.beastie.dm.net/ <
      > Home Page: http://www.dm.net/~wulfraed/ <[/color]

      Comment

      • Jay

        #4
        Re: Need some advice


        One lesson that I remember from an old college prof: Often it is better
        to have something that works and isn't 100% correct than a program that
        does nothing 100% correctly.

        Of course, you stated that you goal is to learn Python, so a working
        program is only a side effect.

        I'd get all the info and do it right....

        Jay Bird

        Jeff Wagner wrote:[color=blue]
        > I am in the process of learning Python (obsessively so). I've been through a few tutorials and read
        > a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that
        > Numerology program I wrote years ago in VB.
        >
        > There are times I am totally stuck (for instance, I just had an idea to put the numerical values of
        > the alphabet and months of the year in a dictionary located in a function. Then, I can import the
        > dictionary I need from that function ... well, I'm getting import errors).
        >
        > So the question is this ... when I get stuck like this and seem to be banging into walls with
        > everything I try, is it better to continue trying different things or stop, take a break and go back
        > to reading a tutorial or a Python book? Or is it better that after I've tried everything I can think
        > of, I just post the question here, get the answer and move forward?
        >
        > Thanks, Jeff[/color]

        Comment

        • Bengt Richter

          #5
          Re: Need some advice

          On Sun, 30 Nov 2003 00:19:53 GMT, Jeff Wagner <JWagner@hotmai l.com> wrote:
          [color=blue]
          >I am in the process of learning Python (obsessively so). I've been through a few tutorials and read
          >a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that
          >Numerology program I wrote years ago in VB.[/color]

          My bet is that the VB program will have some ugly things in it, and translating it in too
          fine detail will amount to programming Python in VB, which is perverse.

          I'd suggest you back off and draw a language-neutral flowchart of your old program, and mess
          with that until it's a simple and clear spec of what you want your program to do.

          At that point you should be able to implement it in python without carrying over VB-isms.

          OTOH, you may not be ready to take full advantage of Python, because there are a lot of things
          your don't know about it yet (don't feel bad -- we're all ignorant, just on different parts --
          to paraphrase Will Rogers ;-)
          [color=blue]
          >
          >There are times I am totally stuck (for instance, I just had an idea to put the numerical values of
          >the alphabet and months of the year in a dictionary located in a function. Then, I can import the
          >dictionary I need from that function ... well, I'm getting import errors).[/color]
          Sounds like your concepts of modules and functions and importing still need adjustment ;-)
          [color=blue]
          >
          >So the question is this ... when I get stuck like this and seem to be banging into walls with
          >everything I try, is it better to continue trying different things or stop, take a break and go back[/color]
          If you need a hammer to do the immediate job right, is it better to keep whacking away with pliers
          and crescent wrenches and whatnot, or go back to the booklet that came with your
          almost-everything-in-the-box toolkit and look at the pictures showing uses for all the goodies?
          [color=blue]
          >to reading a tutorial or a Python book? Or is it better that after I've tried everything I can think
          >of, I just post the question here, get the answer and move forward?[/color]
          If you can't think of what to try, chances are you are best off getting up to speed on basics,
          so you know know what's available to you. Otherwise you are wondering how to build a Lego windmill
          with the first dozen pieces that come out of the box ;-)

          Good luck & have fun ;-)

          Regards,
          Bengt Richter

          Comment

          • Jeff Wagner

            #6
            Re: Need some advice

            Thanks for the advise, I appreciate it. Yes, my goal is to learn not only how to write Python code
            but how to write good Python code. This Numerology program is just a way to give me a "real" program
            to write so I don't get stuck doing nothing but small examples.

            I'm trying to look at the VB just to remember what I did but I think I will use Bengt's suggestion
            and just forget the VB code and do this from scratch. Actually, the concept of numerology is very
            simple so I guess I should write it down instead of trying to do it from memory.

            All you do is ask the user for their firstName, middleName, lastName, nickName and presentName (or
            married name). Then you just calculate the numeric equivalent of each of the names individually (by
            adding the value of each letter in each name), and print out the appropriate file.

            Then you do the same with the birthday. Conceptually, it's very simple but writing it cleanly with
            Python isn't that simple for a beginner.

            Some of the examples in these tutorials are pretty basic and when I see some of the suggestions I
            have gotten here, I'm blown away by the simplicity and power of some of the advanced string and list
            "tricks."

            And, as Dennis said, I'm trying to modularize the program too soon. I kept hearing in the lessons
            that part of the power of Python is to use classes and functions and call them as modules. I think I
            will back off that for a while and just get the code to work.

            Bruno, you asked "import the dictionary from a function" ???? Yes, this is what I tried that isn't
            working:

            def chaldeanValues( ):

            dateValues = {
            'January' : '1',
            'February' : '2',
            'March' : '3',
            'April' : '4',
            'May' : '5',
            'June' : '6',
            'July' : '7',
            'August' : '8',
            'September' : '9',
            'October' : '10',
            'November' : '11',
            'December' : '12'
            }
            return dateValues

            numberValues = {
            'a' : '1',
            'b' : '2',
            'c' : '3',
            'd' : '4',
            'e' : '5',
            'f' : '8',
            'g' : '3',
            'h' : '5',
            'i' : '1',
            'j' : '1',
            'k' : '2',
            'l' : '3',
            'm' : '4',
            'n' : '5',
            'o' : '7',
            'p' : '8',
            'q' : '1',
            'r' : '2',
            's' : '3',
            't' : '4',
            'u' : '6',
            'v' : '6',
            'w' : '6',
            'x' : '5',
            'y' : '1',
            'z' : '7'
            }
            return numberValues

            chaldeanValues( )

            I have another function called "def nameRoutine():" which calculates the numerical equivalent of
            each letter in a name and I was going to import that function like this ...

            def nameRoutine():
            from chaldeanValues import dateValues

            and then check the key of say, December, and find out it equals 12 (for the birthday) and then check
            the key of say, "o" and find out it equals 7. I guess I do need to go back and learn more about this
            concept.

            Anyway, thanks for all the help. I'll be back. I'm obsessed with Python. My wife said she hasn't
            seen me this excited in a long time :-) Some of you guys who have been doing this for a while may
            have forgotten the excitment when you find a programming language that you absolutely love. It's
            pretty cool ;)

            Bengt,

            You said,
            "My bet is that the VB program will have some ugly things in it, and translating it in too
            fine detail will amount to programming Python in VB, which is perverse."

            You're absolutely right, there is some very ugly things in my VB code. I even thought about
            rewriting that first but I don't want to go back to VB nor do I want to programming Python in VB,
            which I agree, is perverse.

            Dennis

            You said,
            " After all, your piece-meal approach without context did cause some
            confusion (mostly of the "why would you want to even do this?" type).
            As soon as you linked your "master number" stuff to calculating numbers
            from names I was able to envision numerology and hack a quick first
            approach to the entire problem (less your special numbers, and not
            fully tested by hand computations, but still...)"

            Please feel free to send me your code. That just might be a good way for me to learn some of this.
            I'll be more than happy to test it for you :-)

            Jeff

            Comment

            • Jeff Wagner

              #7
              Re: Need some advice

              On Sun, 30 Nov 2003 03:29:37 GMT, Jeff Wagner <JWagner@hotmai l.com> wrotf:
              [color=blue]
              >All you do is ask the user for their firstName, middleName, lastName, nickName and presentName (or
              >married name). Then you just calculate the numeric equivalent of each of the names individually (by
              >adding the value of each letter in each name), and print out the appropriate file.[/color]

              I forgot to mention that you also have to determine if every letter of each name is a vowel or
              consonant - should be pretty easy.

              Once I'm done writing the code, it is clean and it works, I want to use either PyQt of wxPython to
              build a GUI for it.

              Jeff

              Comment

              • Dennis Lee Bieber

                #8
                Re: Need some advice

                Jeff Wagner fed this fish to the penguins on Saturday 29 November 2003
                19:29 pm:
                [color=blue]
                >
                > And, as Dennis said, I'm trying to modularize the program too soon. I
                > kept hearing in the lessons that part of the power of Python is to use
                > classes and functions and call them as modules. I think I will back
                > off that for a while and just get the code to work.
                >[/color]
                Done properly, it is useful... But properly does require knowing what
                parts are independent from the others, except for a few interface items
                -- IOW, you should be able to change practically everything in the
                module except the interface without having an effect on anything that
                uses the module.

                [color=blue]
                > def chaldeanValues( ):
                >
                > dateValues = {
                > 'January' : '1',
                > 'February' : '2',
                > 'March' : '3',
                > 'April' : '4',
                > 'May' : '5',
                > 'June' : '6',
                > 'July' : '7',
                > 'August' : '8',
                > 'September' : '9',
                > 'October' : '10',
                > 'November' : '11',
                > 'December' : '12'
                > }
                > return dateValues
                >[/color]
                Rather wasteful too -- you don't compute anything in there, so why
                bother calling a function to build a constant dictionary? Just put the
                dictionary at the top level.
                [color=blue]
                > numberValues = {
                > 'a' : '1',
                > 'b' : '2',
                > 'c' : '3',
                > 'd' : '4',
                > 'e' : '5',
                > 'f' : '8',
                > 'g' : '3',
                > 'h' : '5',
                > 'i' : '1',
                > 'j' : '1',
                > 'k' : '2',
                > 'l' : '3',
                > 'm' : '4',
                > 'n' : '5',
                > 'o' : '7',
                > 'p' : '8',
                > 'q' : '1',
                > 'r' : '2',
                > 's' : '3',
                > 't' : '4',
                > 'u' : '6',
                > 'v' : '6',
                > 'w' : '6',
                > 'x' : '5',
                > 'y' : '1',
                > 'z' : '7'
                > }
                > return numberValues
                >[/color]
                Interesting... I don't recall any of my numerology lists using quite
                that odd a sequence... However... Using the code I posted earlier (and
                which you seem to have missed?)... Again, I don't take account of your
                special values, I'm just reducing names to a range of 0..9 (well, since
                there is no way to get a sum of 0, I guess 1..9 is the resultant range).

                -=-=-=-=-=-=-=- main.py
                #
                # main application file for numeralogy exercise
                #

                import Person
                import Number

                import sys

                if len(sys.argv) > 1:
                data = file(sys.argv[1], "r")
                else:
                data = None

                while 1:
                p = Person.Person(d ata)

                if not p.Valid: break

                print p.fName, Number.reductio n(Number.encode (p.fName))
                print p.mName, Number.reductio n(Number.encode (p.mName))
                print p.lName, Number.reductio n(Number.encode (p.lName))
                print p.nName, Number.reductio n(Number.encode (p.nName))
                print p.pName, Number.reductio n(Number.encode (p.pName))

                if data: data.close()

                -=-=-=-=-=-=-=- Person.py
                #
                # Person class file
                #

                class Person:
                def __init__(self, fid = None):
                if fid:
                self.fName = fid.readline(). strip()
                self.mName = fid.readline(). strip()
                self.lName = fid.readline(). strip()
                self.nName = fid.readline(). strip()
                self.pName = fid.readline(). strip()
                self.Valid = ( self.fName
                and self.mName
                and self.lName
                and self.nName
                and self.pName )
                else:
                try:
                self.fName = raw_input("Ente r first name: ").strip()
                self.mName = raw_input("Ente r middle name: ").strip()
                self.lName = raw_input("Ente r last name: ").strip()
                self.nName = raw_input("Ente r nick name: ").strip()
                self.pName = raw_input("Ente r preferred name: ").strip()
                self.Valid = ( self.fName
                and self.mName
                and self.lName
                and self.nName
                and self.pName )
                except:
                self.Valid = 0

                -=-=-=-=-=-=-=- Number.py
                #
                # number cruncher file
                #

                AtoN = { "a" : 1, #why use string values at this stage?
                "b" : 2,
                "c" : 3,
                "d" : 4,
                "e" : 5,
                "f" : 8, #<blink><blink >
                "g" : 3,
                "h" : 5,
                "i" : 1,
                "j" : 1,
                "k" : 2,
                "l" : 3,
                "m" : 4,
                "n" : 5,
                "o" : 7,
                "p" : 8,
                "q" : 1,
                "r" : 2,
                "s" : 3,
                "t" : 4,
                "u" : 6,
                "v" : 6,
                "w" : 6,
                "x" : 5,
                "y" : 1,
                "z" : 7 }

                def encode(aName):
                sum = 0
                for ch in aName.lower():
                sum += AtoN[ch]
                return sum

                def reduction(aNumb er):
                # ignores special values, just reduces to range 0..9
                # recursive definition
                if aNumber > 9:
                sum = 0
                for ch in str(aNumber):
                sum += int(ch)
                return reduction(sum)
                else:
                return aNumber

                -=-=-=-=-=-=-=-

                [color=blue]
                > chaldeanValues( )
                >
                > I have another function called "def nameRoutine():" which calculates
                > the numerical equivalent of each letter in a name and I was going to
                > import that function like this ...
                >
                > def nameRoutine():
                > from chaldeanValues import dateValues
                >[/color]

                Since the only place those lookup tables are used are in the
                conversion routine they should not be in separate module files; and
                especially not if you then have to call a function just to get the data
                into the local namespace. In the above example, AtoN is treated as a
                global constant /inside/ of the module Number. If it was needed by the
                main file, it /could/ be accessed as Number.AtoN
                [color=blue]
                > and then check the key of say, December, and find out it equals 12[/color]

                Do similar for dates as I did for the names (maybe rename my encode()
                to encodeName() and add an encodeDate() -- I think the same reduction()
                function would work for both assuming your special values are valid in
                both names and dates).
                [color=blue]
                >
                > Please feel free to send me your code. That just might be a good way
                > for me to learn some of this. I'll be more than happy to test it for
                > you :-)
                >[/color]
                It was posted, with the same caveats as above -- that I did not handle
                your special values. In fact, the ONLY change from my previous posting
                (wherein I treated the alphabet as a=>1, ... z=>26 [reduction of 26
                would create a value of 8]) is: I added your lookup table, and changed
                the conversion function from

                sum += (ord(ch) - ord("a") + 1)
                {converts each character to ASCII numeric value, subtracts value of
                ASCII "a" ["a" - "a" would give 0], and adds one}

                to

                sum += AtoN(ch)

                the main file required NO changes -- the interface stayed the same.

                The Person class is overkill, since only one person at a time is ever
                used, but I designed it so that if you pass it a reference to an open
                file it will read the next five lines from that file (setting a valid
                flag if it succeeds); without the open file it will instead read from
                the keyboard (a <ctrl-c> will result in an "invalid" exit, just like
                running out of data in the file will).

                --[color=blue]
                > =============== =============== =============== =============== == <
                > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                > wulfraed@dm.net | Bestiaria Support Staff <
                > =============== =============== =============== =============== == <
                > Bestiaria Home Page: http://www.beastie.dm.net/ <
                > Home Page: http://www.dm.net/~wulfraed/ <[/color]

                Comment

                • Francis Avila

                  #9
                  Re: Need some advice

                  Jeff Wagner wrote in message ...[color=blue]
                  >Bruno, you asked "import the dictionary from a function" ???? Yes, this is[/color]
                  what I tried that isn't[color=blue]
                  >working:[/color]


                  Hmm...
                  [color=blue]
                  >def chaldeanValues( ):
                  >
                  > dateValues = {[/color]

                  ....[color=blue]
                  > }
                  > return dateValues[/color]


                  Ok. This is roundabout, but you can do it. (Better simply to put
                  dateValues in the module's global namespace, and get it by
                  <modulename>.da teValues directly.)

                  It's the following I don't get:
                  [color=blue]
                  > numberValues = {[/color]

                  ....[color=blue]
                  > }
                  > return numberValues[/color]


                  That's all dead code (i.e., will never, ever be reached by the flow of
                  execution.) Once you return once from a function, that's it...
                  [color=blue]
                  >chaldeanValues ()[/color]

                  So all the above line will ever return is dateValues.


                  I never did much VB, but it either completely destroyed your mind, or you're
                  approaching Python with the false assumption that it is far more magical and
                  different than what you're used to. (It *is* magical and different, but it
                  hasn't redefined the meaning of "function". ..)

                  Don't worry about modules for now; just stick to the basics.



                  [color=blue]
                  >I have another function called "def nameRoutine():" which calculates the[/color]
                  numerical equivalent of[color=blue]
                  >each letter in a name and I was going to import that function like this ...
                  >
                  >def nameRoutine():
                  > from chaldeanValues import dateValues[/color]



                  Ah! Now I understand your confusion.

                  I just told you not to worry about modules, but I'll break my own advice,
                  since I see that this misconception is easy to fix.

                  [There's a little bit of hand-waving in the following, but nothing you'll
                  need to worry about.]

                  The "import" statement only imports modules. Modules are a special type of
                  object, which containes *names.* (It is not unique in that respect--every
                  object in Python contains names. What's special about modules is that their
                  code comes from separate files.) Module objects contain every name defined
                  in the global space of that module's file.

                  For example, I write a file, 'MyModule.py':

                  --- MyModule.py ---
                  myInt = 1024

                  def myfunc():
                  notglobal = 4021
                  return notglobal
                  --- End ---

                  I fire up Python:
                  [color=blue][color=green][color=darkred]
                  >>> import MyModule[/color][/color][/color]

                  Now I can access any name in MyModule. Well, not *anything*:
                  [color=blue][color=green][color=darkred]
                  >>> MyModule.myInt[/color][/color][/color]
                  1024[color=blue][color=green][color=darkred]
                  >>> MyModule.myfunc ()[/color][/color][/color]
                  4021[color=blue][color=green][color=darkred]
                  >>> MyModule.myfunc .notglobal[/color][/color][/color]
                  Traceback...
                  ....
                  AttributeError: function 'myfunc' has no attribute 'notglobal'...

                  It is this very property of containing names that makes modules handy,
                  because they act as new global *namespaces*.

                  A namespace is a collection of names. There are two kinds: local (which are
                  used inside of blocks of code, like functions) and global (which are
                  outside). When Python encounters a name, it looks for its value in the
                  namespace. First it looks in the local namespace (i.e., above that point in
                  the code, but inside the function) and if it can't find it there, it looks
                  in the global namespace (i.e., outside the function, but not inside any
                  other function.)

                  Modules are neat because they have their own global namespace, which is
                  global *to themselves*, but not *to the one who imports the module*. So any
                  namespace that contains the name of the module, can access the contents of
                  that module.

                  You make *your* namespace contain the name of a module, by using 'import
                  <modulename>' , which means, "Put the name of this module in my namespace, so
                  I can get to its namespace."

                  Sometimes we don't really want everything in a module, but just one or two
                  things. In such cases, we use 'from <modulename> import <nameinthatmodu le>'
                  'Import' here still has the meaning of "put in my namespace", but this time
                  you're extracting a name directly out of the module and placing it in the
                  current namepace, instead of placing the whole module in the current
                  namespace. Think of it as putting a screwdriver on your desk, instead of
                  the whole toolbox.

                  (That said, never use 'from x import y', and especially 'from x import *',
                  which is like throwing the entire contents of your toolbox onto your desk.
                  These two contribute to "namespace pollution", and generally lead to
                  confusion and sloppy programming.)

                  Let's look at your original lines again.
                  [color=blue]
                  >def nameRoutine():
                  > from chaldeanValues import dateValues[/color]

                  What you're doing here is trying to import a local name from a function. So
                  you've done two things wrong: you can only use 'from ... import' or 'import'
                  statements with modules (which is why they're special), and you can't
                  ever[1] import local names, only global names. Local names are only
                  accessable from the inside, not the outside.

                  You've also committed a style error of using an import statement in a local
                  namespace. Generally speaking, you shouldn't do that, but put all your
                  imports at the top of a file.

                  Here's what you should do:

                  --- chaldeanValues. py --
                  dateValues = {...}
                  nameValues = {...}
                  --- END ---

                  --- Numerology.py ---
                  import chaldeanValues
                  #If that hurts your fingers, use:
                  #
                  #import chaldeanValues as cV

                  def nameRoutine():
                  # here, if you need dateValues, get it with
                  # the name "chaldeanValues .dateValues".
                  # ...
                  --- END ---

                  If you don't have enough to warrant using a new module, use a class instead:

                  --- Numerology.py ---
                  class chaldeanValues:
                  dateValues = {..}
                  nameValues = {..}

                  def nameRoutine():
                  # Get it with "chaldeanValues .dateValues", as above.
                  --- END ---

                  Classes are much more than mini-modules, of course, but you can use them
                  that way if you need.

                  An even higher-level concept is a package, which is a collection of modules.
                  You don't need to know about this right now, but for the details see the
                  very end of 6.12 in the Python Language Reference (also the url cited
                  there). Packages are still module objects, however, even though they
                  contain module names.

                  -- Footnotes --
                  [1] Unless you're using dirty tricks.

                  [color=blue]
                  >and then check the key of say, December, and find out it equals 12 (for the[/color]
                  birthday) and then check[color=blue]
                  >the key of say, "o" and find out it equals 7. I guess I do need to go back[/color]
                  and learn more about this[color=blue]
                  >concept.[/color]


                  You seem to understand dictionaries just fine, and you probably understood
                  modules, too (just needed it spelled out for you), but you're starting to
                  run into problems with names and namespaces. These are fundamental, grand,
                  all-unifying concepts in Python, so it's good to get aquainted with them.

                  In the future, when you start asking questions like "Why is this default
                  list argument of my function being shared by everyone?!", we'll give you the
                  "Names are not objects; names point to objects" lecture. :)
                  --
                  Francis Avila



                  Comment

                  • Jeff Wagner

                    #10
                    Re: Need some advice

                    I just wanted to let you guys know that I'm not ignoring your suggestions. I am trying them all and
                    combining them in different ways. I think I'm finally starting to get it.

                    So far, with a little tweaking, they all work and in different, interesting ways.

                    Francis, you said,

                    "I never did much VB, but it either completely destroyed your mind, or you're
                    approaching Python with the false assumption that it is far more magical and
                    different than what you're used to."

                    Maybe a little of both ;)

                    Jeff

                    Comment

                    • Jeff Wagner

                      #11
                      Re: Need some advice

                      On Sun, 30 Nov 2003 01:57:45 -0500, "Francis Avila" <francisgavila@ yahoo.com> wrotf:[color=blue]
                      >The "import" statement only imports modules. Modules are a special type of
                      >object, which containes *names.* (It is not unique in that respect--every
                      >object in Python contains names. What's special about modules is that their
                      >code comes from separate files.) Module objects contain every name defined
                      >in the global space of that module's file.
                      >
                      >For example, I write a file, 'MyModule.py':
                      >
                      >--- MyModule.py ---
                      >myInt = 1024
                      >
                      >def myfunc():
                      > notglobal = 4021
                      > return notglobal
                      >--- End ---
                      >
                      >I fire up Python:
                      >[color=green][color=darkred]
                      >>>> import MyModule[/color][/color]
                      >
                      >Now I can access any name in MyModule. Well, not *anything*:
                      >[color=green][color=darkred]
                      >>>> MyModule.myInt[/color][/color]
                      >1024[color=green][color=darkred]
                      >>>> MyModule.myfunc ()[/color][/color]
                      >4021[color=green][color=darkred]
                      >>>> MyModule.myfunc .notglobal[/color][/color]
                      >Traceback...
                      >...
                      >AttributeError : function 'myfunc' has no attribute 'notglobal'...[/color]

                      Francis,

                      I'm working with your suggestions ... thanks a lot for your time. This is starting to make a lot of
                      sense. I want to make sure I understand this, though.

                      From the example:

                      --- MyModule.py ---
                      myInt = 1024

                      def myfunc():
                      notglobal = 4021
                      return notglobal
                      --- End ---
                      [color=blue][color=green][color=darkred]
                      >>> MyModule.myfunc .notglobal[/color][/color][/color]
                      Traceback...
                      ....
                      AttributeError: function 'myfunc' has no attribute 'notglobal'...

                      Since notglobal is local to my myfunc(), I can not use it outside of that function (not even in the
                      same module, MyModule.py).

                      So, for me to use the return value elsewhere, I would have to do something like this:

                      --- MyModule.py ---
                      def myfunc():
                      notglobal = 4021
                      return notglobal

                      is_now_global = myfunc()
                      --- End ---

                      is_now_global will contain the value in notglobal so I could do something like the following:

                      --- OtherModule.py ---
                      import MyModule

                      print MyModule.is_now _global #or anything else I needed to do with it.
                      --- End ---

                      Are functions in Python another name for subroutines?
                      [color=blue]
                      >In the future, when you start asking questions like "Why is this default
                      >list argument of my function being shared by everyone?!", we'll give you the
                      >"Names are not objects; names point to objects" lecture. :)[/color]

                      That's ok with me ... keep telling me until is sinks in.

                      I know there's magic in Python, I can feel it,
                      Jeff



                      Comment

                      Working...