Python Forum
Using my REPL to bisect numbers and lists with classes (PyBite #181)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using my REPL to bisect numbers and lists with classes (PyBite #181)
#1
Here is what the PyBite #181 exercise is calling for:

Quote: Complete the add method of the OrderedList class which takes a num argument and adds that to the self._numbers list keeping it ordered upon insert.
Using a manual .sort() or .sorted() each time is not allowed. Look into the bisect module how to do it ...

I solved the problem (see script below). To come up with my script, I leveraged Python.org’s bisect doc.

The solution I wrote passes all the unit tests but as a ‘victory lap’ I’m exploring how to call classes in general within my script using my trusty REPL and I am doing something wrong because my output doesn’t process as expected.

Here is the script I am working with:

import bisect
 
 
class OrderedList:
 
   def __init__(self):
       self._numbers = []
 
   def add(self, num):
       bisect.insort_right(self._numbers, num)
 
   def __str__(self):
       return ', '.join(str(num) for num in self._numbers)
In my script’s directory, I activate my REPL, import the script and instantiate:

 $ bpython
bpython version 0.19 on top of Python 3.8.5 /usr/bin/python
>>> import order
>>> dummy = order.OrderedList
>>> 
Here are some inputs with my expected outputs:

dummy.add(10)
print(dummy)  
10
dummy.add(1)
print(dummy)
1, 10
dummy.add(16)
print(dummy)
1, 10, 16
dummy.add(5)
print(dummy)
1, 5, 10, 16
But here is my actual output:

 >>> dummy.add(10)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    dummy.add(10) 
The AttributeError says that the module (order.py) does not contain an attribute (a variable within a class) but as you can see in my script, an attribute is clearly declared at line 7. I believe the problem is not with my script, but with how I am instantiating it in my REPL.

How do I achieve the expected output in my REPL? How do I instantiate and properly pass in integers on the fly in my REPL?

I realize in the PyBite example REPL commands use:

order = OrderedList()
order.add(10)
That seems to work. But when I replace 'order' with 'dummy' (because I am experimenting with an alternate variable name) the output falls apart.
Reply
#2
you need to create an instance of OrderedList:
>>> dummy = order.OrderedList()
Reply
#3
(Sep-24-2020, 01:35 PM)mlieqo Wrote: you need to create an instance of OrderedList:
>>> dummy = order.OrderedList()

@milego, what you have suggested is fairly similar to what I tried myself. To quote my original post:

>>> import order
>>> dummy = order.OrderedList
>>> 
My problem is that I'm missing the parentheses after OrderedList. Wall I guess when it comes to programming, attention to detail is essential. I will be more careful next time and ensure that I check to include parentheses.

Thanks mlieqo for your answer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help for use print in REPL jip31 10 4,126 Apr-30-2021, 03:52 PM
Last Post: bowlofred
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,683 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 2,739 Sep-25-2020, 12:14 PM
Last Post: ndc85430
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Counting vowels in a string (PyBite #106) Drone4four 4 2,205 Jul-07-2020, 05:29 AM
Last Post: theknowshares
  Using matplotlib in Spyder v/s REPL peterjv26 2 2,520 Jul-04-2020, 10:57 AM
Last Post: snippsat
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 2,313 Jun-11-2020, 08:31 PM
Last Post: Drone4four
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,925 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Do lists of classes take up more space in memory? DreamingInsanity 1 1,614 Apr-10-2020, 01:50 PM
Last Post: deanhystad
  Online python repl? Gribouillis 4 33,193 Apr-09-2020, 12:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020