what is wrong with my code?

Discussion in 'Python' started by Pyenos, Dec 20, 2006.

  1. Pyenos

    Pyenos Guest

    import cPickle, shelve

    could someone tell me what things are wrong with my code?

    class progress:

    PROGRESS_TABLE_ACTIONS=["new","remove","modify"]
    DEFAULT_PROGRESS_DATA_FILE="progress_data"
    PROGRESS_OUTCOMES=["pass", "fail"]


    def unpickleProgressTable(pickled_progress_data_file):

    return unpickled_progress_table

    def pickleProgressTable(progress_table_to_pickle):

    return pickled_progress_data_file

    # Of course, you get progress_table is unpickled progress table.
    def progressTable(progress_table, action, task, pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]):
    pid_column_list=progress_table[0]
    task_column_list=progress_table[1]
    outcome_column_list=progress_table[2]

    # But a task must also come with an outcome!
    def newEntry(new_task, new_outcome):
    new_pid=len(task_column_list)

    pid_column_list.extend(new_pid)
    task_column_list.extend(new_task)
    outcome_column_list.extend(new_outcome)

    def removeEntry(pid_to_remove, task_to_remove):

    if pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove):
    # Must remove all columns for that task
    index_for_removal=pid_column_list.index(pid_to_remove)

    pid_column_list.remove(index_for_removal)
    task_column_list.remove(index_for_removal)
    outcome_column_list.remove(index_for_removal)

    # Default action is to modify to pass
    def modifyEntry(pid_to_modify, outcome_to_modify=PROGRESS_OUTCOMES[0]):
    index_for_modifying=pid_column_list.index(pid_to_modify)

    # Modify the outcome
    outcome_column_list[index_for_modifying]=outcome_to_modify
    Pyenos, Dec 20, 2006
    #1
    1. Advertising

  2. At Wednesday 20/12/2006 19:16, Pyenos wrote:

    >could someone tell me what things are wrong with my code?


    My crystal ball is at the repair shop - could you please tell us:
    - what do you expect the code to do?
    - what do you get instead?
    - any compiler error messages you see?
    - in case of an exception, full traceback as printed by the interpreter?
    - Python version in use?


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
    Gabriel Genellina, Dec 20, 2006
    #2
    1. Advertising

  3. It is hard to determine what is wrong with your code without you
    telling anyone why it is you believe something is wrong with it. Did
    you get an exception? Did it simply not do what it was expected to do?
    There seems to be some apparent indenting problems, but maybe that is
    just from pushing it through the e-mail. I see some general stylistic
    problems as well, but without know what you are actually asking, I
    won't know what questions to answer. "What is wrong with my code?" is
    a container of many many smaller questions, and you need to focus your
    questions better.

    On 21 Dec 2006 09:16:58 +1100, Pyenos <> wrote:
    > import cPickle, shelve
    >
    > could someone tell me what things are wrong with my code?
    >
    > class progress:
    >
    > PROGRESS_TABLE_ACTIONS=["new","remove","modify"]
    > DEFAULT_PROGRESS_DATA_FILE="progress_data"
    > PROGRESS_OUTCOMES=["pass", "fail"]
    >
    >
    > def unpickleProgressTable(pickled_progress_data_file):
    >
    > return unpickled_progress_table
    >
    > def pickleProgressTable(progress_table_to_pickle):
    >
    > return pickled_progress_data_file
    >
    > # Of course, you get progress_table is unpickled progress table.
    > def progressTable(progress_table, action, task, pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]):
    > pid_column_list=progress_table[0]
    > task_column_list=progress_table[1]
    > outcome_column_list=progress_table[2]
    >
    > # But a task must also come with an outcome!
    > def newEntry(new_task, new_outcome):
    > new_pid=len(task_column_list)
    >
    > pid_column_list.extend(new_pid)
    > task_column_list.extend(new_task)
    > outcome_column_list.extend(new_outcome)
    >
    > def removeEntry(pid_to_remove, task_to_remove):
    >
    > if pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove):
    > # Must remove all columns for that task
    > index_for_removal=pid_column_list.index(pid_to_remove)
    >
    > pid_column_list.remove(index_for_removal)
    > task_column_list.remove(index_for_removal)
    > outcome_column_list.remove(index_for_removal)
    >
    > # Default action is to modify to pass
    > def modifyEntry(pid_to_modify, outcome_to_modify=PROGRESS_OUTCOMES[0]):
    > index_for_modifying=pid_column_list.index(pid_to_modify)
    >
    > # Modify the outcome
    > outcome_column_list[index_for_modifying]=outcome_to_modify
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Read my blog! I depend on your acceptance of my opinion! I am interesting!
    http://ironfroggy-code.blogspot.com/
    Calvin Spealman, Dec 20, 2006
    #3
  4. Pyenos wrote:

    > could someone tell me what things are wrong with my code?


    do you want a free code review, or do you have problems with the code
    and just forgot to mention what they are?

    </F>
    Fredrik Lundh, Dec 20, 2006
    #4
  5. Pyenos

    Pyenos Guest

    "Calvin Spealman" <> writes:

    > It is hard to determine what is wrong with your code without you
    > telling anyone why it is you believe something is wrong with it. Did
    > you get an exception? Did it simply not do what it was expected to do?
    > There seems to be some apparent indenting problems, but maybe that is
    > just from pushing it through the e-mail. I see some general stylistic
    > problems as well, but without know what you are actually asking, I
    > won't know what questions to answer. "What is wrong with my code?" is
    > a container of many many smaller questions, and you need to focus your
    > questions better.
    >
    > On 21 Dec 2006 09:16:58 +1100, Pyenos <> wrote:
    > > import cPickle, shelve
    > >
    > > could someone tell me what things are wrong with my code?
    > >
    > > class progress:
    > >
    > > PROGRESS_TABLE_ACTIONS=["new","remove","modify"]
    > > DEFAULT_PROGRESS_DATA_FILE="progress_data"
    > > PROGRESS_OUTCOMES=["pass", "fail"]
    > >
    > >
    > > def unpickleProgressTable(pickled_progress_data_file):
    > >
    > > return unpickled_progress_table
    > >
    > > def pickleProgressTable(progress_table_to_pickle):
    > >
    > > return pickled_progress_data_file
    > >
    > > # Of course, you get progress_table is unpickled progress table.
    > > def progressTable(progress_table, action, task, pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]):
    > > pid_column_list=progress_table[0]
    > > task_column_list=progress_table[1]
    > > outcome_column_list=progress_table[2]
    > >
    > > # But a task must also come with an outcome!
    > > def newEntry(new_task, new_outcome):
    > > new_pid=len(task_column_list)
    > >
    > > pid_column_list.extend(new_pid)
    > > task_column_list.extend(new_task)
    > > outcome_column_list.extend(new_outcome)
    > >
    > > def removeEntry(pid_to_remove, task_to_remove):
    > >
    > > if pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove):
    > > # Must remove all columns for that task
    > > index_for_removal=pid_column_list.index(pid_to_remove)
    > >
    > > pid_column_list.remove(index_for_removal)
    > > task_column_list.remove(index_for_removal)
    > > outcome_column_list.remove(index_for_removal)
    > >
    > > # Default action is to modify to pass
    > > def modifyEntry(pid_to_modify, outcome_to_modify=PROGRESS_OUTCOMES[0]):
    > > index_for_modifying=pid_column_list.index(pid_to_modify)
    > >
    > > # Modify the outcome
    > > outcome_column_list[index_for_modifying]=outcome_to_modify
    > > --
    > > http://mail.python.org/mailman/listinfo/python-list
    > >

    >
    >
    > --
    > Read my blog! I depend on your acceptance of my opinion! I am interesting!
    > http://ironfroggy-code.blogspot.com/


    it says that progress_table is not defined. i don't know why.
    Pyenos, Dec 21, 2006
    #5
  6. Pyenos

    Carl Banks Guest

    Pyenos wrote:
    > "Calvin Spealman" <> writes:
    > > On 21 Dec 2006 09:16:58 +1100, Pyenos <> wrote:
    > > > import cPickle, shelve
    > > >
    > > > could someone tell me what things are wrong with my code?
    > > >
    > > > class progress:
    > > >
    > > > PROGRESS_TABLE_ACTIONS=["new","remove","modify"]
    > > > DEFAULT_PROGRESS_DATA_FILE="progress_data"
    > > > PROGRESS_OUTCOMES=["pass", "fail"]
    > > >
    > > >
    > > > def unpickleProgressTable(pickled_progress_data_file):
    > > >
    > > > return unpickled_progress_table
    > > >
    > > > def pickleProgressTable(progress_table_to_pickle):
    > > >
    > > > return pickled_progress_data_file
    > > >
    > > > # Of course, you get progress_table is unpickled progress table.
    > > > def progressTable(progress_table, action, task, pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]):
    > > > pid_column_list=progress_table[0]
    > > > task_column_list=progress_table[1]
    > > > outcome_column_list=progress_table[2]
    > > >
    > > > # But a task must also come with an outcome!
    > > > def newEntry(new_task, new_outcome):
    > > > new_pid=len(task_column_list)
    > > >
    > > > pid_column_list.extend(new_pid)
    > > > task_column_list.extend(new_task)
    > > > outcome_column_list.extend(new_outcome)
    > > >
    > > > def removeEntry(pid_to_remove, task_to_remove):
    > > >
    > > > if pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove):
    > > > # Must remove all columns for that task
    > > > index_for_removal=pid_column_list.index(pid_to_remove)
    > > >
    > > > pid_column_list.remove(index_for_removal)
    > > > task_column_list.remove(index_for_removal)
    > > > outcome_column_list.remove(index_for_removal)
    > > >
    > > > # Default action is to modify to pass
    > > > def modifyEntry(pid_to_modify, outcome_to_modify=PROGRESS_OUTCOMES[0]):
    > > > index_for_modifying=pid_column_list.index(pid_to_modify)
    > > >
    > > > # Modify the outcome
    > > > outcome_column_list[index_for_modifying]=outcome_to_modify

    > >
    > > It is hard to determine what is wrong with your code without you
    > > telling anyone why it is you believe something is wrong with it. Did
    > > you get an exception? Did it simply not do what it was expected to do?
    > > There seems to be some apparent indenting problems, but maybe that is
    > > just from pushing it through the e-mail. I see some general stylistic
    > > problems as well, but without know what you are actually asking, I
    > > won't know what questions to answer. "What is wrong with my code?" is
    > > a container of many many smaller questions, and you need to focus your
    > > questions better.

    >
    > it says that progress_table is not defined. i don't know why.


    It looks like you have bigger problems than an undefined variable.
    Python does not have implicit access to methods and attributes
    (members) like C++ and Java do. Methods must accept an explicit self
    parameter that refers to the object (analogous to this in C++ and Java)
    to access attributes. Your code defines a class, but self is nowhere
    to be found.

    I suggest getting a good book and/or reading the tutorial
    (http://docs.python.org/tut/) and making sure you can do easy stuff
    first.


    Carl Banks
    Carl Banks, Dec 21, 2006
    #6
  7. Pyenos

    Pyenos Guest

    thanks for your point. so because i have said class blah: i must
    explicitly say self for every instantiation of object?
    Pyenos, Dec 21, 2006
    #7
  8. Pyenos

    Carl Banks Guest

    Pyenos wrote:
    > thanks for your point. so because i have said class blah: i must
    > explicitly say self for every instantiation of object?



    http://docs.python.org/tut/


    Carl Banks
    Carl Banks, Dec 21, 2006
    #8
  9. Pyenos

    rzed Guest

    Pyenos <> wrote in
    news::

    > thanks for your point. so because i have said class blah: i must
    > explicitly say self for every instantiation of object?
    >


    No, for every method within the class.

    Given:

    class Blah(object):
    def method1(self,arg1):
    self.x = arg1


    b = Blah() # b is an instance of class Blah

    b.method1(32) # when you invoke it, just pass arg1

    print b.x # prints 32



    The use of 'self' is just a convention, but a *very* common one,
    and one you should follow if you expect other Python programmers
    to read your code. This is legal, and has the same effect as the
    above class:

    class Blah2(object):
    def method1(spugsl,arg1):
    spugsl.x = arg1

    Spugsl (or self) is just the way to refer to the instance within
    the code.

    In your code, each method wound up with a different name, and none
    of the names would have been associated with what you would have
    expected. So for example, in

    def removeEntry(pid_to_remove, task_to_remove):

    .... your equivalent to 'self' would be pid_to_remove, and the pid
    you passed in would have been associated with task_to_remove.

    --
    rzed
    rzed, Dec 21, 2006
    #9
  10. Hi,

    this is the line that breaks your code:

    def progressTable(progress_table, action, task, pid=len(progress_table)

    your parameter progress_table is known inside the function, not inside its definition. So "pid=len(progress_table)" won't do.

    If you really think that it is possible that pid is anything else but "len(progress_table)", you should use for example -1 as the default value and calculate the length inside your functions if the parameter is not different. Otherwise dump this parameter.

    Hope that helps!

    Greetings

    Nils


    -------- Original-Nachricht --------
    Datum: 21 Dec 2006 09:16:58 +1100
    Von: Pyenos <>
    An:
    Betreff: what is wrong with my code?

    > import cPickle, shelve
    >
    > could someone tell me what things are wrong with my code?
    >
    > class progress:
    >
    > PROGRESS_TABLE_ACTIONS=["new","remove","modify"]
    > DEFAULT_PROGRESS_DATA_FILE="progress_data"
    > PROGRESS_OUTCOMES=["pass", "fail"]
    >
    >
    > def unpickleProgressTable(pickled_progress_data_file):
    >
    > return unpickled_progress_table
    >
    > def pickleProgressTable(progress_table_to_pickle):
    >
    > return pickled_progress_data_file
    >
    > # Of course, you get progress_table is unpickled progress table.
    > def progressTable(progress_table, action, task,
    > pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]):
    > pid_column_list=progress_table[0]
    > task_column_list=progress_table[1]
    > outcome_column_list=progress_table[2]
    >
    > # But a task must also come with an outcome!
    > def newEntry(new_task, new_outcome):
    > new_pid=len(task_column_list)
    >
    > pid_column_list.extend(new_pid)
    > task_column_list.extend(new_task)
    > outcome_column_list.extend(new_outcome)
    >
    > def removeEntry(pid_to_remove, task_to_remove):
    >
    > if
    > pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove):
    > # Must remove all columns for that task
    > index_for_removal=pid_column_list.index(pid_to_remove)
    >
    > pid_column_list.remove(index_for_removal)
    > task_column_list.remove(index_for_removal)
    > outcome_column_list.remove(index_for_removal)
    >
    > # Default action is to modify to pass
    > def modifyEntry(pid_to_modify,
    > outcome_to_modify=PROGRESS_OUTCOMES[0]):
    > index_for_modifying=pid_column_list.index(pid_to_modify)
    >
    > # Modify the outcome
    > outcome_column_list[index_for_modifying]=outcome_to_modify
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    --
    Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
    Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
    =?iso-8859-1?Q?=22Nils_Oliver_Kr=F6ger=22?=, Dec 21, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    2,175
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,254
  3. Matthew
    Replies:
    7
    Views:
    657
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    464
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,100
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page