Skip to content

Commit

Permalink
Merge pull request #22 from Sangarshanan/add-success-tracks
Browse files Browse the repository at this point in the history
Add onsuccess tracks
  • Loading branch information
Sangarshanan authored Sep 20, 2020
2 parents 5dccb2c + 9546b84 commit 16dbd91
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 40 deletions.
79 changes: 76 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# JazzIt 🎷

[![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Sangarshanan/jazzit/blob/master/notebook.ipynb)
[![License](https://img.shields.io/pypi/l/jazzit.svg)](https://github.com/Sangarshanan/jazzit/blob/master/LICENSE)
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/[email protected])

Ever wanted your scripts to play music while running/ on erroring out?

Of course you didn't
Expand Down Expand Up @@ -64,26 +68,95 @@ if __name__ == "__main__":
run(1000)
```

The in-build tracks are bruh_moment, elevator and curb_your_enthusiasm but you can add your custom tracks
To satisfy your WHAT !!! IT WORKED !!! moments


```python
from jazzit import success_track

@success_track("anime-wow.mp3")
def add(a,b):
print(a+b)

if __name__ == "__main__":
add(12, 42)
```

The in-build tracks are bruh_moment, elevator and curb_your_enthusiasm but you can add your custom tracks and urls

Check out some of the [examples](https://github.com/Sangarshanan/jazzit/tree/master/examples)


> :warning: **Use wisely**: Best suited for scripts you run non-professionally!
### Jupyter Notebook

Check out the colab link above or the example [notebook](https://github.com/Sangarshanan/jazzit/blob/master/notebook.ipynb)

### Use Jupyter Magic:

Load inside a Jupyter notebook

``` python
%load_ext jazzit
```

Running away from the PEP8 police 🚲🚓

```python
%%waiting_track -t elevator.mp3

def fibonacci(n):
if n < 0:
raise Exception("BE POSITIVE!!!")
elif n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
for num in range(1, 35):
print(fibonacci(num))
```

For those BRUH moments in your life

```python
%%error_track -t bruh.mp3 -w 3

for num in reversed(range(10)):
print(10/num)
```

It worked !!!

```python
%%success -t anime-wow.mp3 -w 3

for num in range(10):
print(num ** 2)
```

There are two arguments in play here

`-t` or `--track` that needs the sound track (Can be a file path, url, inbuilt tracks)

`-w` or `--wait` (Optional, default is 3 seconds) Wait for x seconds before cutting the music


### Dependencies

Jazz added with [playsound](https://github.com/TaylorSMarks/playsound)

If you are ubuntu and getting hit with `ModuleNotFoundError: No module named 'gi`
If you getting hit with `ModuleNotFound` errors while running jazzit

You might need to install Pygobject https://pygobject.readthedocs.io/en/latest/getting_started.html


> :warning: **Use wisely**: Best suited for scripts you run non-professionally!

### Profiling

TL;DR It is definitely slower with music

I did a lil profiling on bin/profiler.py

Expand Down
127 changes: 113 additions & 14 deletions notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"import jazzit\n",
"from jazzit import error_track\n",
"from jazzit import waiting_track"
"from jazzit import waiting_track\n",
"from jazzit import success_track"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"%load_ext jazzit"
"%reload_ext jazzit"
]
},
{
Expand All @@ -38,7 +39,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -120,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -145,7 +146,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-cbe8749008a5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mreversed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-16-cbe8749008a5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mreversed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
},
Expand All @@ -171,22 +172,71 @@
}
],
"source": [
"%%error_track -t https://www.myinstants.com/media/sounds/no-god-please-no-noooooooooo.mp3 -w 3\n",
"%%error_track -t bruh.mp3 -w 1\n",
"\n",
"for num in reversed(range(10)):\n",
" print(10/num)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"4\n",
"9\n",
"16\n",
"25\n",
"36\n",
"49\n",
"64\n",
"81\n"
]
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%success_track -t anime-wow.mp3\n",
"\n",
"for num in range(10):\n",
" print(num ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Decorator"
"## Decorator 🎍"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand All @@ -209,9 +259,9 @@
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/sangarshanan/Music/jazzit/src/jazzit/jazz.py\", line 52, in wrapped_function\n",
" File \"/home/sangarshanan/Music/jazzit/src/jazzit/jazz.py\", line 47, in wrapped_function\n",
" original_func(*args)\n",
" File \"<ipython-input-5-95f0645fd7b3>\", line 4, in run\n",
" File \"<ipython-input-21-95f0645fd7b3>\", line 4, in run\n",
" print(10/num)\n",
"ZeroDivisionError: division by zero\n"
]
Expand All @@ -238,7 +288,7 @@
}
],
"source": [
"@error_track(\"https://www.myinstants.com/media/sounds/hellodarknessmyoldfriend.mp3\", wait=7)\n",
"@error_track(\"curb_your_enthusiasm.mp3\", wait=7)\n",
"def run():\n",
" for num in reversed(range(10)):\n",
" print(10/num)\n",
Expand All @@ -248,7 +298,49 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54\n"
]
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@success_track(\"anime-wow.mp3\")\n",
"def add(a,b):\n",
" print(a+b)\n",
"\n",
"if __name__ == \"__main__\":\n",
" add(12, 42)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -330,6 +422,13 @@
"\n",
"run(35)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
1 change: 1 addition & 0 deletions src/jazzit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ def load_ipython_extension(ipython):

from .jazz import WaitingTrack as waiting_track # noqa
from .jazz import ErrorTrack as error_track # noqa
from .jazz import SuccessTrack as success_track # noqa
36 changes: 31 additions & 5 deletions src/jazzit/jazz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import time
import traceback

from .utils import (
_get_track_path,
_start_music,
_stop_music,
)
from .utils import _get_track_path, _start_music, _stop_music


class WaitingTrack(object):
Expand Down Expand Up @@ -70,3 +66,33 @@ def wrapped_function(*args):
_stop_music(process)

return wrapped_function


class SuccessTrack(object):
"""Use on potential YAYs."""

def __init__(self, track=None, wait=None):
"""Constructor."""
self.track = track
self.wait = wait # (optional) will be infered by track length; defaults to 3

def __call__(self, original_func):
"""Wrap decorator."""

def wrapped_function(*args):
track_path = _get_track_path(self.track)
original_func(*args)
try:
process = _start_music(track_path)
if self.wait is None:
from .utils import _track_length

time.sleep(_track_length(track_path))
else:
time.sleep(self.wait)
except KeyboardInterrupt:
_stop_music(process)
finally:
_stop_music(process)

return wrapped_function
Loading

0 comments on commit 16dbd91

Please sign in to comment.