Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: modified quickstart simple example #349

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

roynielsen17
Copy link
Contributor

Adding a while loop to the index.md example with the ability to show what happens with looping, seeing multiple signals sent and handled, seeing the changed print statements when the signals are handled, when the inputs are changed and signals emitted.

example tested with python 3.9+ python on macos

Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1863f7d) to head (7b0056f).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #349   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines         2095      2095           
=========================================
  Hits          2095      2095           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@roynielsen17
Copy link
Contributor Author

This submission is deliberately simpler than pyside6/Qt - to give people a very simple example of signals and slots so they don't have to learn the whole PySide/Qt Frameworks before understanding Signals and Slots. (I've programmed with them on and off for over a decade)

Here's a more complex version that has a pytest component:

"""
Two ways to run this file:

% python changing_psygnal.py

% pytest changing_psygnal.py

"""

from psygnal import Signal
import pytest

class MyObject:
# define one or signals as class attributes
value_changed = Signal(str)

create an instance

my_obj = MyObject()

You (or others) can connect callbacks to your signals

@my_obj.value_changed.connect
def on_change(new_value: str):
print(f'Hi {new_value}!') # prints the new string - Hi {new_value}
my_obj.new_value = new_value

def get_name_and_say_hello():
my_input = input("enter a name here: ")
my_obj.value_changed.emit(str(my_input))
return my_obj.new_value

if name == "main":
count = 0
while count < 5:
name = get_name_and_say_hello()
print(f"Said Hi to.... {name}")
count = count + 1

""" TEST """
def test_say_hello(monkeypatch):
names = ['Sally', 'Bob', 'Harriet', 'Bart', 'Erica']
count = 0
while count < 5:
monkeypatch.setattr('builtins.input', lambda _: names[count])
result = get_name_and_say_hello()
assert result == names[count]
count = count + 1

Copy link

codspeed-hq bot commented Jan 24, 2025

CodSpeed Performance Report

Merging #349 will not alter performance

Comparing roynielsen17:main (7b0056f) with main (1863f7d)

Summary

✅ 67 untouched benchmarks

@tlambert03
Copy link
Member

thanks for the suggestion @roynielsen17. If you think this clarifies something about the usage pattern, I'm happy to include it

@tlambert03 tlambert03 enabled auto-merge (squash) January 24, 2025 01:10
@tlambert03 tlambert03 changed the title modified quickstart simple example docs: modified quickstart simple example Jan 24, 2025
@tlambert03 tlambert03 added the documentation Improvements or additions to documentation label Jan 24, 2025
@roynielsen17
Copy link
Contributor Author

roynielsen17 commented Jan 24, 2025 via email

@tlambert03 tlambert03 merged commit 2179994 into pyapp-kit:main Jan 24, 2025
47 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Development

Successfully merging this pull request may close these issues.

2 participants