Skip to content

Commit

Permalink
fix exercise 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheukting committed May 13, 2024
1 parent 6ed9135 commit 3c09868
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ Then we will store the counter of the number of registration as a class attribut

```
#[classattr]
fn reg_num() -> u32 {
fn cur_reg_num() -> u32 {
0
}
```
Expand All @@ -745,11 +745,11 @@ fn new(cls: &Bound<'_, PyType>, name: String, speaker: bool) -> PyResult<Self> {
if name.len() == 0 {
Err(PyValueError::new_err("Please enter a name"))
} else {
let cur_num: u32 = cls.getattr("reg_num")?.extract()?;
cls.setattr("reg_num", cur_num + 1)?;
let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?;
cls.setattr("cur_reg_num", cur_reg_num + 1)?;
Ok(
Attendee{
reg_num: cur_num,
reg_num: cur_reg_num,
name: name,
speaker: speaker,
}
Expand All @@ -758,7 +758,7 @@ fn new(cls: &Bound<'_, PyType>, name: String, speaker: bool) -> PyResult<Self> {
}
```

As a class method, the first argument is a pointer to the Python class `Attendee` itself. We can then use `getattr` to get back the value of the attribute `reg_num` and then convert it to a Rust integer using `extract`. Both methods will return a `Result` there for we add the `?` to get back the value if `Ok`. We then use `setattr` to increate the class attribute by 1 and then add the number to our new attendee created.
As a class method, the first argument is a pointer to the Python class `Attendee` itself. We can then use `getattr` to get back the value of the attribute `cur_reg_num` and then convert it to a Rust integer using `extract`. Both methods will return a `Result` therefore we add the `?` to get back the value if `Ok`. We then use `setattr` to increate the class attribute by 1 and then add the number to our new attendee created.

Also, don't forget to add this:
```
Expand All @@ -771,21 +771,21 @@ Now, lets test it out to see if it works as expected:

```
# test Attendee
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")
me = p1.Attendee('Cheuk', True)
print(me.name, me.speaker, me.reg_num)
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")
keynote = p1.Attendee('John', True)
print(keynote.name, keynote.speaker, keynote.reg_num)
keynote.name = 'Jon'
print(keynote.name, keynote.speaker, keynote.reg_num)
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")
```

You may found it strange why the first attendee created will have `reg_num` 1 instead of 0. I suspect this has something to do with Python interpret the code at run time while Rust is compiled before hand and the class we were referencing is a Python object... this will goes into a rabbit hole of what's happening behind the hood, so let's park this mystery for now and move on to the next exercise. But if you have time, feel free to play with it some more and look into this.
Feel free to create another class attribute and class methods before moving to the next exercise.

---

Expand Down
8 changes: 4 additions & 4 deletions ex_6/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct Attendee {
#[pymethods]
impl Attendee {
#[classattr]
fn reg_num() -> u32 {
fn cur_reg_num() -> u32 {
0
}
#[new]
Expand All @@ -80,11 +80,11 @@ impl Attendee {
if name.len() == 0 {
Err(PyValueError::new_err("Please enter a name"))
} else {
let cur_num: u32 = cls.getattr("reg_num")?.extract()?;
cls.setattr("reg_num", cur_num + 1)?;
let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?;
cls.setattr("cur_reg_num", cur_reg_num + 1)?;
Ok(
Attendee{
reg_num: cur_num,
reg_num: cur_reg_num,
name: name,
speaker: speaker,
}
Expand Down
6 changes: 3 additions & 3 deletions ex_6/try.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
print(p1.travel_avg(budget_dict))

# test Attendee
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

me = p1.Attendee('Cheuk', True)
print(me.name, me.speaker, me.reg_num)
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

keynote = p1.Attendee('John', True)
print(keynote.name, keynote.speaker, keynote.reg_num)
keynote.name = 'Jon'
print(keynote.name, keynote.speaker, keynote.reg_num)

print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

# test Fibonacci
# fib = p1.Fibonacci()
Expand Down
8 changes: 4 additions & 4 deletions ex_7/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct Attendee {
#[pymethods]
impl Attendee {
#[classattr]
fn reg_num() -> u32 {
fn cur_reg_num() -> u32 {
0
}
#[new]
Expand All @@ -82,11 +82,11 @@ impl Attendee {
if name.len() == 0 {
Err(PyValueError::new_err("Please enter a name"))
} else {
let cur_num: u32 = cls.getattr("reg_num")?.extract()?;
cls.setattr("reg_num", cur_num + 1)?;
let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?;
cls.setattr("cur_reg_num", cur_reg_num + 1)?;
Ok(
Attendee{
reg_num: cur_num,
reg_num: cur_reg_num,
name: name,
speaker: speaker,
}
Expand Down
6 changes: 3 additions & 3 deletions ex_7/try.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
print(p1.travel_avg(budget_dict))

# test Attendee
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

me = p1.Attendee('Cheuk', True)
print(me.name, me.speaker, me.reg_num)
print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

keynote = p1.Attendee('John', True)
print(keynote.name, keynote.speaker, keynote.reg_num)
keynote.name = 'Jon'
print(keynote.name, keynote.speaker, keynote.reg_num)

print(f"Number of attendees are {p1.Attendee.reg_num}")
print(f"Number of attendees are {p1.Attendee.cur_reg_num}")

# test Fibonacci
# fib = p1.Fibonacci()
Expand Down

0 comments on commit 3c09868

Please sign in to comment.