Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Oct 01 04:10
    susi132002 opened #140
  • Sep 27 00:45
    jpiedra opened #139
  • Sep 26 19:31
    alex-t555 edited #138
  • Sep 26 19:17
    alex-t555 synchronize #138
  • Sep 26 00:17
    alex-t555 opened #138
  • Sep 14 07:16
    dekusuke closed #137
  • Sep 14 07:15
    dekusuke opened #137
  • Sep 14 03:24
    dekusuke closed #136
  • Sep 14 03:24
    dekusuke opened #136
  • Sep 08 03:39
    alexgphan synchronize #134
  • Sep 08 03:38
    alexgphan synchronize #134
  • Sep 07 03:50
    alexgphan synchronize #134
  • Sep 06 03:26
    alexgphan synchronize #134
  • Sep 04 21:52
    alexgphan synchronize #134
  • Sep 04 03:13
    alexgphan synchronize #134
  • Sep 03 20:14
    alexgphan synchronize #134
  • Aug 31 01:25
    lavaflows closed #135
  • Aug 31 01:24
    lavaflows commented #135
  • Aug 31 01:23
    lavaflows opened #135
  • Aug 29 23:48
    alexgphan synchronize #134
Bob Hiltner
@bobhilt
@kiet112 , what issue are you seeing, and what have you tried ? can you point to a repository or put your code into pastebin.com and post the link here?
Kiet Phan
@kiet112
@bobhilt Hey Bob I'm thinking of implementing an if-statement to stop it but that solution is kinda of a brute force. I read somewhere that I can use built-in max() function too but I don't know how to implement that.
David Beazley
@dabeaz
Will take a look at it (Exercise 1.8)
cirocco
@cirocco
Thank you so much for all the work you put into this course and for making it available to us. It's really good.
Bob Hiltner
@bobhilt
@kiet112 , you'll need some kind of conditional. I don't know that that makes it brute force, but at the end of the term, there will be that odd remainder amount, and you'll have to compare amount paid vs. remainder.
Yubel Escobedo
@yubelec
Hi
David Beazley
@dabeaz
Hello!
Z Z
@ZZ939_gitlab

Dear @dabeaz , thank you for the nice course. From Exercise 7.7 found here Can someone kindly explain what the prop is that is returned in the typedproperty function? It's unclear to me whether the property is returned or whether the property setter function is returned.

# typedproperty.py

def typedproperty(name, expected_type):
    private_name = '_' + name
    @property
    def prop(self):
        return getattr(self, private_name)

    @prop.setter
    def prop(self, value):
        if not isinstance(value, expected_type):
            raise TypeError(f'Expected {expected_type}')
        setattr(self, private_name, value)

    return prop

And in the below block of code, how can the typedproperty('name', str) be assigned to the variable namewhen as far as I can tell, the typedproperty function from above doesn't return any value?

from typedproperty import typedproperty

class Stock:
    name = typedproperty('name', str)
    shares = typedproperty('shares', int)
    price = typedproperty('price', float)

    def __init__(self, name, shares, price):
        self.name = name
        self.shares = shares
        self.price = price

Thanks in advance!

David Beazley
@dabeaz
The typedproperty() function returns a property instance. You could try it at the REPL for instance:
>>> name = typedproperty('name', str)
>>> name
<property object at 0x10e02ab88>
>>>
Z Z
@ZZ939_gitlab

Am I understanding correctly that when a Stock instance is created, e.g.:

>>> stock = Stock('GOOG', 100, 490.1)

then the __init__() for the Stock instance runs, including:

self.name = name

or for this particular instance 'GOOG' as name because that was supplied as the name argument:

self.name = 'GOOG'

If that's all correct, how does the code

name = typedproperty('name', str)

become involved?

David Beazley
@dabeaz

name is a property so this question is really the same as "how does a property" get involved?

class Stock:
    @property
    def name(self):
        return self._name
    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError("Expected a value")
        self._name = value
    def __init__(self, name, shares, price):
        self.name = name
        self.shares = shares
        self.price = price

s  = Stock('GOOG', 100, 490.1)     # Works
t = Stock(123, 100, 490.1)      # Fails

To answer this question, you'll need to do some research on how properties work. That's left as an exercise... (wink ;-).

Z Z
@ZZ939_gitlab

Thank you--I believe I understand now. For simplicity, if only looking at how self.name = name works, when a Stock instance is created, even before the __init__() is run we already have the following established:

name = typedproperty('name', str)

So, the above code has set a property instance to the variable name. Subsequently, when the __init__() runs the following code:

self.name = name

self.name is equivalent to self.prop (prop being the closure assigned to name in the first block of code which has retained the arguments that was given to the typedproperty() function initially). This means that the above statement uses the setter method defined in propto be called which raises an exception if the wrong type of value is passed to it.

Yodo92
@Yodo92
hi guys! if anyone is here, could you please help me? I have a code to walk through and do some refactoring
kim074
@kim074
Hi!!! nice meet you!! everybody! I come here first time! I'd like to be helped from all you! Thanks!!!!!!!!!!!
smilofactor
@smilofactor
Hi, are there any prerequisites for being able to participate in this room? I've followed some of David Beazleys presentations on youtube so I wanted to be able to have a place to ask questions directly with the man himself if possible
David Beazley
@dabeaz
There are no prerequisites that I know of. I do lurk here should anyone have questions. Sometimes I'm teaching a live-course so I'm not always able to offer and immediate response, but for the most part I can be found here.
MalikRumi
@MalikRumi
Greetings, all. New here, first post. No one has posted in a month? Did you all go on vacation?
David Beazley
@dabeaz
It's a quiet group ;-)
MalikRumi
@MalikRumi
I came to see about my answer to mortgage.py. I struggled with it for a long time, and was relieved to discover I was not the only one - see dabeaz-course/practical-python#95. Final Answer: 308 874705.88 3478.83 309 877389.99 809.21 310 878199.2 3.37 311 878202.57 0.01 312 878202.59 0.0, However, my table kept running until month 447: 445 878202.59 0.0 446 878202.59 0.0 447 878202.59 0.0 Total paid: 878202.59 Months: 0.0 Given Dabeaz' response to issue 95, is this "good enough"? I aask because I am moving on to the next problems. I don't want to be stuck here forever. I think my real problem is that I don't know how to calculate a mortgage! ;-)
I also struggle with markdown... give me html any day...
keyblitz
@keyblitz45_twitter
absolute thanks to @dabeaz and everyone else who has provided help
sodiqafolayan
@sodiqafolayan
Hello, i am starting the course today and hoping to upgrade from a beginner to intermediate then expert :)
sodiqafolayan
@sodiqafolayan

Dear all, i need help understanding exercise 1.5 solution
Question:
A rubber ball is dropped from a height of 100 meters and each time it hits the ground, it bounces back up to 3/5 the height it fell. Write a program bounce.py that prints a table showing the height of the first 10 bounces.

Solution:
height = 100
bounce = 1
while bounce <= 10:
height = height * (3/5)
print(bounce, round(height, 4))
bounce += 1

My challenge is i could not comprehend what makes the value of height change at every iteration

Aleksey Tsalolikhin
@atsaloli_gitlab
Hello. Merry Christmas. Where can I find the reference documentation for the python slice notation? I noticed the start and stop defaults change when the step is negative, and I just want to find where that is documented.
Aleksey Tsalolikhin
@atsaloli_gitlab
I found https://docs.python.org/3/reference/expressions.html?highlight=slice#slicings but I don't see anything there about the start/stop defaults changing
Aleksey Tsalolikhin
@atsaloli_gitlab
Another question -- why does python round() x.5 to the nearest even number instead rounding it up like in math? that's surprising...
for example, round(1.5) comes out to 2; round (10.5) comes out to 10.
Aleksey Tsalolikhin
@atsaloli_gitlab
TIL about Banker's Rounding https://wiki.c2.com/?BankersRounding
Z Z
@ZZ939_gitlab
@sodiqafolayan Hello! The value of height changes at every iteration because you are multiplying height by 3/5 each time through the loop. Hope that helps.
Tom Nguyen
@tunggnu
@sodiqafolayan Physically, air resistance and floor friction cause height to be reduced.
sodiqafolayan
@sodiqafolayan

Dear all,

In the mortgage.py code as stated below, please can someone explain why we have (1+rate/12)
I am not able to wrap my head around why we need to add 1 to that line

principal = 500000.0
rate = 0.05
payment = 2684.11
total_paid = 0.0

while principal > 0:
principal = principal * (1+rate/12) - payment
total_paid = total_paid + payment

print('Total paid', total_paid)

Chalermsak Chatdokmaiprai
@Chalermsak-KU
Dear @dabeaz. I'm an old-time C programmer and learned Python basics from books. Last month I found this free online Practical Python Course and decided that this course is precisely what I need to upgrade my Python skills. Now that I've finally finished it, I would like to thank you for creating this superb course. Working myself carefully through the course is a great eye-opener, enlightening experience with lots of fun. It's a bit terse sometimes but, no problems, I can always google things for more details. This course has been a great compass for me in learning Python. Thank you very much to make it available for the world. Eagerly looking forward to your new Python book "Python distilled". :)
David Beazley
@dabeaz
Glad you liked the course!
Zac E.
@zac618
Started the course today...very new to this!
quas
@quas:matrix.org
[m]
Hello. Is there a solution for exercise 1.6 at the end? (https://github.com/dabeaz-course/practical-python/blob/master/Notes/01_Introduction/02_Hello_world.md)
I fixed it and it works, but it's always nice to have a look at the 'official' solution in case something pops up which I haven't thought of.
for the previous exercise there is a solution in the repo
quas
@quas:matrix.org
[m]
I have the same question for 1.11. I'm not sure if the solution give includes the solution there or not.
quas
@quas:matrix.org
[m]
is this channel active at all? does it make sense to reach anyone here?:)
quas
@quas:matrix.org
[m]
:point_up: Edit: I have the same question for 1.11. I'm not sure if the solution given includes the solution there or not.
David Beazley
@dabeaz
I monitor the channel, but I'm not able to give personalized help for course material. The Solutions/ directory has all of the posted solutions to most course exercises.
quas
@quas:matrix.org
[m]
I understand. So what is the purpose of this channel?
David Beazley
@dabeaz
If people want to chat about stuff, they can.
Sarfraaz Ahmed
@asarfraaz
In the Stock class, after we create "name", "shares" and "price" using typedproperty, we end up creating 3 new class variables in Stock. I noticed that slots no longer worked after this. What is the way to make use of slots in this case ? Or, do we no longer need slots because the attributes have now become properties ?
David Beazley
@dabeaz
__slots__ needs to specify the names where attribute values are actually stored. For something like a property, if the data is stored in an attribute like _name, then '_name' would need to appear in __slots__
1 reply
Thomas Ammitzbøll-Bach
@tabacdk
Hello David. Thank you so much for your effort. Do you have any instructions for generating the presentation? I would guess it is something that involves Pandoc or Spinx, but would you like to give some hints of what you do? Do you create Reveal.js slides or what?
David Beazley
@dabeaz
I don't have any presentation generation for the material here. Original source material was adapted from Keynote slides, but it's been reorganized slightly from that.
Thomas Ammitzbøll-Bach
@tabacdk
Hi David. Thanks for your reply. Yes, I will find a way, it is MD after all.
Widad Iqbal Mogral
@widadmogral
Hello, I am trying out Exercise 6.3: Making a more proper container and even after including the def getitem(self, index):
return self._holdings[index]
code in the Portfolio class, I am not able to get indexing to work properly. So for example trying
import report
portfolio = report.read_portfolio('Data/portfolio.csv')
>>>portfolio[0]
<stock.Stock object at 0x7f486b7b0c40>
instead of Stock('AA', 100, 32.2) as expected
>>> portfolio[0:3]
[<stock.Stock object at 0x7f486b7b0c40>, <stock.Stock object at 0x7f486b7b0f70>, <stock.Stock object at 0x7f486b7d4040>]
instead of [Stock('AA', 100, 32.2), Stock('IBM', 50, 91.1), Stock('CAT', 150, 83.44)] as expected.
Other functions like __len__ and __contains__ works as expected.
>>>len(portfolio)
7
>>> 'IBM' in portfolio
True
Anyone else who had the same issue or maybe any clue as to what I am doing wrong? Thanks in advance.
Sarfraaz Ahmed
@asarfraaz
I think you missed implementing __repr__ in Stock class
Widad Iqbal Mogral
@widadmogral
@asarfraaz , Thank you so much, I had indeed missed that.
1 reply