Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
138 commits
Select commit Hold shift + click to select a range
02bb027
Delete copyied content from MI-PYT
hroncok Mar 21, 2017
3194965
requests a click
hroncok Sep 7, 2016
34b99ed
requests: Přidání příkladu s hlavičkou
hroncok Sep 8, 2016
7955746
requests: Jiný tweet
hroncok Sep 8, 2016
55ddc92
requests: Přidán odkaz na dokumentaci GH API
hroncok Sep 8, 2016
e010fb6
requests: .gitignore
hroncok Sep 8, 2016
5ca210c
requests: Použití input() pro zadání uživatelského jména
hroncok Sep 8, 2016
73d6683
requests: Přidán tip na využití Sirius API
jirutka Sep 8, 2016
b22def4
Flask
hroncok Sep 8, 2016
4827cad
Flask: Telefonní číslo
hroncok Sep 13, 2016
2fa6846
Flask: Filtry
hroncok Sep 13, 2016
5ff324a
Flask: Víc o Markup a escaping
encukou Sep 14, 2016
71f69c5
Odebrán zbytečně komplikovaný markup (nefunguje na Eduxu)
hroncok Sep 14, 2016
e126dba
Zadrátovat verzi do vytvoření virtualenvu
hroncok Oct 5, 2016
fd14140
Switch from OpenShift to PythonAnywhere
encukou Oct 11, 2016
092f22a
Flask deployment: Reword to use 2nd person
encukou Oct 11, 2016
8cfa7bd
Flask deployment: Generate SSH keys to clone private repos
encukou Oct 11, 2016
d943662
Flask deployment: Add info about updates
encukou Oct 11, 2016
8428fd4
Fix link
hroncok Oct 11, 2016
67f5fdc
Typos
hroncok Oct 11, 2016
24032ea
Actually fix the link
hroncok Oct 11, 2016
558f026
Typo
hroncok Oct 11, 2016
1b4f1b7
WSGI Configuration File more verbose
hroncok Oct 14, 2016
bd7c0d4
Flask: GitHub bot: Use HTML
hroncok Oct 16, 2016
6d7e5e6
information about securing github hooks
tulinkry Oct 18, 2016
7aeaf91
Moduly
hroncok Sep 13, 2016
077d119
Povídání o requirements.txt
encukou Oct 19, 2016
19d80ec
Do nothing but import and call a funtion in __main__.py
encukou Oct 19, 2016
23d0523
Povídání o if __name__ == '__main':
encukou Oct 19, 2016
251428c
Jak najít .pypirc na Windows
encukou Oct 19, 2016
aea7333
Minivystětlení __init__.py a relativních importů
encukou Oct 19, 2016
be64706
Doplnění čárek za argumenty setup()u
encukou Oct 19, 2016
b0a72ff
Instalce z testovací PyPI
ggljzr Oct 22, 2016
b38e4ae
tests: First attempt
hroncok Oct 21, 2016
9c4891d
tests: Rewordings & additions
encukou Oct 24, 2016
bf7cd3c
tests: Typo
hroncok Oct 24, 2016
eaf3ac9
Testovani: python -m pytest
encukou Oct 25, 2016
7120377
Víc o hledání testů + varování o testech ve venvu
encukou Oct 26, 2016
1ceac9c
Parametrizace s více hodnotami
encukou Oct 26, 2016
b162d51
Typo
encukou Oct 26, 2016
9d8bdd6
Trochu lepší vysvětlení, jak se fixtury hledají
encukou Oct 26, 2016
96434cc
Konkrétnější příklad yield fixture
encukou Oct 26, 2016
8597425
Parametrizace fixtures
encukou Oct 26, 2016
7ecfe5d
Rozepsání odkazů "Jak to zapadá do našeho balíčku"
encukou Oct 26, 2016
728dfe0
Import builtins rather than using sys.modules
encukou Oct 26, 2016
5e28bea
Name the tests directory "tests" everywhere
encukou Oct 26, 2016
f83ab41
Poznámka k zip_safe
encukou Oct 26, 2016
7ba3aed
Betamax: komprimované zprávy
MarekSuchanek Oct 31, 2016
2dbf66e
Docs
hroncok Nov 1, 2016
c188554
Edits for the Documentation chapter
encukou Nov 2, 2016
c1f8dfd
Úkol k dokumentaci: Doctesty taky musí procházet
encukou Nov 2, 2016
87dcec3
Dokumentace: typo
encukou Nov 8, 2016
a761db7
Dokumentace: typo
encukou Nov 8, 2016
6bcbc57
First commit for Pandas tutorial
encukou Oct 17, 2016
0ce4091
More work on Pandas
encukou Oct 19, 2016
d8b1aa2
Examples of indexing
encukou Oct 25, 2016
ad101ff
Add sections on indexes, reshaping, and graphs
encukou Nov 7, 2016
978b9d4
Pandas: Introduction, and minor edits
encukou Nov 8, 2016
b58c442
Complete Pandas text, add assignment & link to a MD file
encukou Nov 9, 2016
88202dc
Pandas: Typos etc.
hroncok Nov 9, 2016
dd3cc78
Pandas: Fix the notebook link for master
hroncok Nov 9, 2016
fee6ac7
Pandas heading
hroncok Nov 9, 2016
1417da6
Pandas: Seznam potřebných souborů
encukou Nov 9, 2016
bcb55e0
Pandas: Deadline
hroncok Nov 9, 2016
46954af
Pandas: Use pandas.read_csv instead of DataFrame.from_csv
encukou Nov 11, 2016
35e2b89
Initial content for NumPy
encukou Nov 22, 2016
63d69ac
Examples and corrections for NumPy
encukou Nov 22, 2016
2dc9788
Add forgotten logo file
encukou Nov 22, 2016
53c51e8
Add instructions
encukou Nov 22, 2016
2b3e0b3
Make it more Edux friendly
hroncok Nov 22, 2016
22ae2f4
Typo
hroncok Nov 22, 2016
2e5efdd
NumPy: detaily, vyčištění SVG, hezčí grafy, frekvence ze zvuku
encukou Nov 22, 2016
e6858b7
NumPy: Homework update
hroncok Nov 23, 2016
acbb823
NumPy: Task: One finish
hroncok Nov 23, 2016
c9a1571
NumPy: Windows troubles :(
hroncok Nov 23, 2016
06f4f8a
NumPy: Task: GitHub link
hroncok Nov 23, 2016
9a09fed
NumPy homework clarification
hroncok Nov 25, 2016
0468dd3
First draft/notes for C API/Cython
encukou Nov 29, 2016
5e1bd1f
Cython: Edits from the Etherpad
encukou Nov 30, 2016
e800115
Syntax, oh Edux, Edux
hroncok Nov 30, 2016
3304ee8
Solution for NumPy task
hroncok Nov 30, 2016
c8f4fa5
Deadline
hroncok Nov 30, 2016
20f2426
Cython: C array to list
hroncok Nov 30, 2016
9e975e5
Cython: include_dirs for multiple platforms
hroncok Nov 30, 2016
3531ae6
Cython: Fix some TODOs
hroncok Nov 30, 2016
dece866
Typo
hroncok Nov 30, 2016
7604d9f
PyQt: Installation
hroncok Dec 1, 2016
0a61e62
PyQt: Easy app (for smoke test)
hroncok Dec 1, 2016
6826ede
Qt: Images and initial version of the text (without image links)
encukou Dec 6, 2016
91559db
Qt: Add links to Github-hosted images
encukou Dec 6, 2016
bf23494
Qt: Add README to the pics directory
encukou Dec 6, 2016
28de393
Qt: Typo in installation command
vknaisl Dec 6, 2016
a17e945
PyQt: Be consistent with keyboard shortcuts style
hroncok Dec 6, 2016
1e8bcde
PyQt: Missing )
hroncok Dec 6, 2016
b55c29c
PyQt: Tweaks
hroncok Dec 6, 2016
6d0575f
PyQt: More tweaks
hroncok Dec 6, 2016
55e39c8
Add missing images
hroncok Dec 7, 2016
ce8c058
PyQt: Direct image links
hroncok Dec 7, 2016
6953b5c
PyQt: Debian package with Designer
hroncok Dec 7, 2016
a390e91
PyQt: Use setMaximumSize() on grid as well
hroncok Dec 7, 2016
b1df61d
PyQt: Markdown Syntax Fix
hroncok Dec 7, 2016
b3ba06c
PyQt: Full venv command with --system-site-packages
hroncok Dec 7, 2016
fce452e
PyQt: Use rows/cols instead of height/width
hroncok Dec 7, 2016
2d8c3ed
PyQt: Task: Display *the shortest* path
hroncok Dec 7, 2016
70a629c
PyQt: Task speed
hroncok Dec 7, 2016
540e47d
PyQt: Qt, not QT
hroncok Dec 7, 2016
e440fbb
First draft for asyncio
encukou Dec 13, 2016
91b6cb7
Async: Sync from Etherpad
encukou Dec 14, 2016
aab8a2c
Async: Sync with Etherpad
hroncok Dec 14, 2016
c6645ad
Async: Quamash example
hroncok Dec 14, 2016
d57d19c
Async: Add missing .run() call
hroncok Dec 14, 2016
191a05d
Async: class Actor
hroncok Dec 14, 2016
12e88c7
Public domain Actor
hroncok Dec 14, 2016
0c60139
First draft of Magic
encukou Dec 20, 2016
b9ec7c5
Magic: Sync to Etherpad
encukou Dec 21, 2016
332e207
Magic: Markdown typos
hroncok Dec 21, 2016
a8fd131
Magic: Fixed formatting, better wording and examples
encukou Dec 21, 2016
37a53cf
Magic: Add metaclasses
encukou Dec 21, 2016
8a73d6f
Magic: Add two minor tricks at the end
encukou Dec 21, 2016
2c924e2
Magic: Add __init__
encukou Dec 21, 2016
f672d96
Magic: Typo fix
encukou Dec 21, 2016
d1c21a6
MicroPython: Sync from Etherpad
encukou Jan 4, 2017
d7c47df
MicroPython: add last code examples
encukou Jan 4, 2017
d3ed9f2
MicroPython: Group names
hroncok Jan 4, 2017
4c53640
MicroPython: Group hint
hroncok Jan 4, 2017
0db2538
MicorPython: English vars
hroncok Jan 4, 2017
fc11f44
MicroPython: Relative path for flashing
hroncok Jan 4, 2017
7543f0e
MicroPython: esptool 1.3 was released
hroncok Jan 10, 2017
02f5430
MicroPython: Markdown Typo
hroncok Jan 28, 2017
dd66cf5
Requests: Tweak the condition for custom task a bit
hroncok Jan 28, 2017
1b32f6d
Typo
honzajavorek Feb 11, 2017
0f64034
Typo
andrenvk Feb 12, 2017
1bbc3d2
cython: Fix Markdown syntax in link to Extension
encukou Feb 16, 2017
83e2270
"pythonní" vs "pythoní"
hroncok Feb 20, 2017
1a9923c
Upload images to Edux
hroncok Feb 28, 2017
528abc3
Use ANSI color codes for a diff
hroncok Mar 21, 2017
5fb8571
Fix all github.com/cvut/MI-PYT links, use static or lesson_url
hroncok Mar 21, 2017
58dabfd
PyQt: Different semantics for a search query
encukou Mar 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
845 changes: 463 additions & 382 deletions lessons/intro/async/index.md

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions lessons/intro/async/static/actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
"""This file has been placed in the public domain"""
import asyncio
import contextlib
import time


class Actor:
def __init__(self, grid, row, column, kind):
"""Coroutine-based actor on a grid

:param grid:

The grid this actor moves on. Must have the following attributes:

* ``update_actor(actor)``: Called to mark the space currently
occupied by the actor as "dirty" (needs redrawing)
* ``cell_size``: size of a grid cell, in pixels (used to optimize
animations)
* ``directions``: a NumPy array of directions (ASCII bytes),
containing ``b'v'``, ``b'<'``, ``b'>'``, ``b'^'`` depending
on where the actor should move to from a particular cell.
Only used in the default implementation of ``behavior``.

This is stored in an attribute with the same name.

:param row:
:param column:

The initial position of the actor.
These are stored in attributes with the same names, and are updated
regularly.
When the actor is currently moving, the values are floats.

:param kind:

Any data for use by the grid drawing code.
This is stored in an attribute with the same name.

The attribute ``task`` will hold an ``asyncio.Task`` object
corresponding to the actor's behavior. Cancel it when done.
"""
self.row = row
self.column = column
self.kind = kind
self.grid = grid
self.task = asyncio.ensure_future(self.behavior())

async def behavior(self):
"""Coroutine containing the actor's behavior

The base implementation follows directions the actor is standing on.
If there is no directions (e.g. standing on a wall, unreachable space,
or on the goal), the actor jumps repeatedly.

To be reimplemented in subclasses..
"""
while True:
shape = self.grid.directions.shape
row = int(self.row)
column = int(self.column)
if 0 <= row < shape[0] and 0 <= column < shape[1]:
direction = self.grid.directions[row, column]
else:
direction = b'?'

if direction == b'v':
await self.step(1, 0)
elif direction == b'>':
await self.step(0, 1)
elif direction == b'^':
await self.step(-1, 0)
elif direction == b'<':
await self.step(0, -1)
else:
await self.jump()

def _progress(self, duration):
"""Iterator that yields progress from 0 to 1 based on time

In each iteration, yields a number based on the current time:

* 0.0 at the time the generator was started;
* 1.0 at start time plus ``duration`` seconds (end time)
* for the time in between, a linearly interpolated number between 0...1

It is not guaranteed that 1.0 will be yielded on the last iteration.

When using this with a for-loop, you probably need to put
a sleep/delay into each iteration.
"""
start = time.monotonic()
while True:
now = time.monotonic()
p = (now - start) / duration
if p > 1:
return
yield p

@contextlib.contextmanager
def _update_context(self):
"""Context manager for updating the actor's position

Updates the grid widget before and after the contextis entered.
Wrapping any coordinate updates in this context wil ensure the actor
is drawn correctly.
"""
self.grid.update_actor(self)
yield
self.grid.update_actor(self)

async def step(self, dr, dc, duration=1):
"""Coroutine for a step in a given direction

Smoothly moves ``dr`` tiles in the row-direction and ``dc`` tiles in
the column-direction in ``duration`` seconds.
"""
start_row = self.row
start_col = self.column

for p in self._progress(duration):
with self._update_context():
self.row = start_row + dr * p
self.column = start_col + dc * p

# Sleep amount is based on zoom level: we want to sleep for
# about one pixel's worth of movement.
await asyncio.sleep(duration/self.grid.cell_size)

# Final update to the exact ending position (this should use integer
# arithmetic, so it avoids rounding errors)
with self._update_context():
self.row = start_row + dr
self.column = start_col + dc

async def jump(self, duration=0.2):
"""Coroutine for a small jump

Smoothly moves a bit up and down in ``duration`` seconds.
"""
start_row = self.row

for p in self._progress(duration):
with self._update_context():
# jump along a parabola
self.row = start_row - p * (1-p)

await asyncio.sleep(duration/self.grid.cell_size * 2)

with self._update_context():
self.row = start_row
Loading