Skip to content

Commit

Permalink
Examples in docs + api change: add_participants
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Jan 2, 2017
1 parent 8ee8f32 commit 0353f75
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
4 changes: 2 additions & 2 deletions challonge/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ async def add_participant(self, display_name: str = None, username: str = None,
self._add_participant(new_p)
return new_p

async def add_participants(self, names: list) -> list:
params = {'name': names}
async def add_participants(self, *names: str) -> list:
params = {'name': list(names)}
res = await self.connection('POST',
'tournaments/{}/participants/bulk_add'.format(self._id),
'participants[]',
Expand Down
20 changes: 20 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. currentmodule:: challonge
.. module:: challonge


Examples
========


Simple list of tournaments
--------------------------

Let's fetch and print all names and url of the organizer's tournaments

.. literalinclude:: ../examples/listing.py


Simple creation of a tournament
-------------------------------

.. literalinclude:: ../examples/create.py
14 changes: 8 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ achallonge

async challonge for python 3.5+!

.. _github: https://github.com/fp12/achallonge
.. _GitHub: https://github.com/fp12/achallonge
.. highlight:: python


Features
Expand Down Expand Up @@ -44,10 +45,10 @@ Simple example::
Dependencies
------------

- *python 3.5+*
- *aiohttp*
- *cchardet*
- *aiodns*
- python 3.5+
- aiohttp
- cchardet
- aiodns


Author and License
Expand All @@ -57,7 +58,7 @@ The ``achallonge`` package is written by Fabien Poupineau (fp12).

It's covered under the *MIT* license.

Feel free to improve this package and send a pull request to github_.
Feel free to improve this package and send a pull request to GitHub_.



Expand All @@ -66,6 +67,7 @@ Feel free to improve this package and send a pull request to github_.
:caption: Contents:

api
examples



Expand Down
36 changes: 36 additions & 0 deletions examples/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import challonge
import asyncio

my_username = 'challonge_username'
my_api_key = 'challonge_api_key'


async def main(loop):
my_user = challonge.get_user(my_username, my_api_key)
new_tournament = await my_user.create_tournament(name='my super tournament',
url='super-tournament-url')

john = await new_tournament.add_participant('john')
bob = await new_tournament.add_participant('bob')
steve = await new_tournament.add_participant('steve')
franck = await new_tournament.add_participant('franck')
# or simply new_tournament.add_participants('john', 'bob', 'steve', 'franck')

await new_tournament.start()

matches = await new_tournament.get_matches()

# match 1: john (p1) Vs bob (p2)
await matches[0].report_winner(john, '2-0,1-2,2-1')
# match 2: steve (p1) Vs franck (p2)
await matches[1].report_winner(franck, '2-0,1-2,0-2')

# finals: john (p1) Vs franck (p2)
await matches[2].report_winner(franck, '2-1,0-2,1-2')

await new_tournament.finalize()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
17 changes: 17 additions & 0 deletions examples/listing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import challonge
import asyncio

my_username = 'challonge_username'
my_api_key = 'challonge_api_key'


async def main(loop):
my_user = challonge.get_user(my_username, my_api_key)
my_tournaments = await my_user.get_tournaments()
for t in my_tournaments:
print(t.name, t.full_challonge_url)


if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
10 changes: 5 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_d_update_participants(self):
def test_e_bulk(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.add_participants(['p1', 'p2', 'p3', 'p4'])
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
self.assertEqual(len(t.participants), 4)
yield from self.user.destroy_tournament(t)

Expand All @@ -161,7 +161,7 @@ def setUp(self):
def test_a_report_live_scores(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.add_participants(['p1', 'p2', 'p3', 'p4'])
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
yield from t.start()
self.assertEqual(t.state, 'underway', random_name)
m = yield from t.get_matches()
Expand All @@ -179,7 +179,7 @@ def test_a_report_live_scores(self):
def test_b_report_winner(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.add_participants(['p1', 'p2', 'p3', 'p4'])
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
p1 = yield from t.search_participant('p1')
self.assertEqual(p1.name, 'p1')
yield from t.start()
Expand All @@ -201,7 +201,7 @@ def test_a_url(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.allow_attachments()
yield from t.add_participants(['p1', 'p2', 'p3', 'p4'])
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
yield from t.start()
m = yield from t.get_matches()
a = yield from m[0].attach_url('https://github.com/fp12/achallonge')
Expand All @@ -216,7 +216,7 @@ def test_b_text(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.allow_attachments()
yield from t.add_participants(['p1', 'p2', 'p3', 'p4'])
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
yield from t.start()
m = yield from t.get_matches()
random_text = get_random_name()
Expand Down

0 comments on commit 0353f75

Please sign in to comment.