FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #17 from swfiua/dice · swfiua/matplotlib@ec97d73 · GitHub

Repository navigation

Commit ec97d73

Browse files
Zär'a Cardell
authored
Merge pull request matplotlib#17 from swfiua/dice
Dice
2 parents 889ace2 + c70459f commit ec97d73

11 files changed

Lines changed: 329 additions & 25 deletions

File tree

‎README.md‎

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ Karma Phy - Looks better spelt with a K
6969

7070
Karma Pi - Phy for physics, pi for math.
7171

72-
# Install
73-
74-
To install from source:
75-
76-
python setup.py install
77-
78-
79-
To install in development mode:
80-
81-
python setup.py develop --user
82-
83-
8472
## Ubuntu on Pi
8573

8674

@@ -140,15 +128,62 @@ Docs need a fair bit of work, but better docstrings in the code turn into pretty
140128

141129
## Release
142130

131+
pip3.x install karmapi
132+
133+
x = 5 or 6, may be just 3.6 soon.
134+
135+
### Making a new release
136+
137+
Install some stuff needed to help with building releases:
138+
139+
pip3.6 install twine wheel
140+
141+
Build a source releas (this just creates a tarball in the dists/ subfolder):
142+
143+
python setup.py sdist
144+
145+
Build a binary release as a wheel (:
146+
147+
python setup.py bdist_wheel
148+
149+
Upload to PyPi with twine (you will need a username and password that
150+
has access to the project you are trying to update):
151+
152+
twine upload dist/*
143153

144154
## Develop
145155

146156
git clone https://github.com/openbermuda/karmapi
147157

148158
cd karmapi
149159

150-
python3 setup.py develop --user
160+
pip3.6 -e ,
161+
162+
## Python3.6 on Ubuntu 16.04
163+
164+
git clone https://github.com/python/cpython
165+
166+
checkout 3.6 branch, then:
167+
168+
./configure
169+
make
170+
make install
171+
172+
Note: python3.6 can be installed (from universe) with apt on Ubuntu >= 16.10.
173+
174+
### Things to do first
175+
176+
Some dev libraries need installing into Ubuntu for certain features to be built into the python.
177+
178+
tkinter is one part you need to make sure *tk-8.6-dev* is installed before ./configure.
179+
180+
The good news is that after the intitial build it all runs pretty fast.
181+
182+
* tk-8.6-dev -- for tkinter
183+
184+
* libbz2-dev -- compression library
151185

186+
* libssl-dev -- for ssl support required for pip3.6 to work.
152187

153188
## TODO
154189

‎docs/eric.rst‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
===========
2+
Eric Idle
3+
===========
4+
5+
Eric Idle was a python.
6+
7+
So when Guido needed a simple development environment to show people
8+
python, IDLE was born,
9+
10+
11+
IDLE gives you a python prompt and a simple editor that knows a little
12+
about python,
13+
14+
15+
Python Console
16+
==============
17+
18+
Since *currie* is currently running using tkinter and *currie* could
19+
do with a console, time to take a look at IDLE.
20+
21+
22+
Editor
23+
======
24+
25+
An editor might be handy too,
26+
27+
It might be easy to get these working in a *PigFarm*.
28+
29+
30+
idlelib
31+
=======
32+
33+
The code for IDLE comes with python, you can import it with *idlelib*.
34+
35+
A quick look at the code and pig farm integration does not look too
36+
hard.
37+
38+
The main obstacle is that *idlelib* had solved a lot of what *PigFarm*
39+
and *piglets* are doing here.
40+
41+
Who knew that a library called *idlelib* written 20 years ago might
42+
have saved some work?
43+
44+
async idle
45+
==========
46+
47+
On a PigFarm the eventloop will be run by *curio*.
48+
49+

‎docs/index.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Astro pi
4949
astro_pi
5050

5151

52+
Eric
53+
====
54+
55+
.. toctree::
56+
57+
eric
58+
59+
5260
Indices and tables
5361
==================
5462

‎karmapi/currie.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616
from collections import deque
1717
import curio
18+
from pathlib import Path
1819

1920
from karmapi import hush
2021

@@ -249,7 +250,7 @@ def main():
249250

250251
parser.add_argument('--monitor', action='store_true')
251252
parser.add_argument('--nomon', action='store_false', default=True)
252-
253+
parser.add_argument('--words', default='diceware.wordlist.asc')
253254

254255
args = parser.parse_args()
255256

@@ -302,12 +303,19 @@ def main():
302303
im_info.update(im)
303304
farm.add(piglet.Image, im_info.copy())
304305

306+
307+
words = Path(args.words)
308+
if words.exists():
309+
words = words.open()
310+
else:
311+
words = None
312+
305313
farm.add(talk.Talk)
306-
farm.add(dice.StingingBats)
314+
farm.add(dice.StingingBats, dict(words=words))
307315
farm.add(StingingBats)
308316

309317
farm.add(TankRain)
310-
farm.add(sunny.Sunspot)
318+
#farm.add(sunny.Sunspot)
311319
farm.add(sonogram.SonoGram)
312320
farm.add(piglet.XKCD)
313321
farm.add(widgets.InfinitySlalom)

‎karmapi/diceware.py‎

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@
1414
import random
1515
import math
1616

17+
BIGLY = 'helvetica 20 bold'
1718

1819
class StingingBats(pig.Canvas):
1920

20-
def __init__(self, parent):
21+
def __init__(self, parent, words=None):
2122

2223
super().__init__(parent)
2324

2425
self.width = self.height = 200
26+
27+
if words:
28+
self.words = load_words(words)
29+
else:
30+
self.words = None
31+
32+
self.size = 5
33+
self.sides = 6
34+
self.canvas_text = None
35+
2536
self.minswarms = 20
2637
self.maxswarms = 50
2738
self.themes = []
@@ -44,7 +55,21 @@ def create_event_map(self):
4455
self.add_event_map('k', self.more)
4556
self.add_event_map('f', self.fast)
4657
self.add_event_map('s', self.slow)
58+
self.add_event_map('r', self.roll)
4759
self.add_event_map('t', self.next_theme)
60+
self.add_event_map('u', self.up)
61+
self.add_event_map('d', self.down)
62+
63+
async def up(self):
64+
""" Increase the number of dice """
65+
66+
self.size += 1
67+
68+
69+
async def down(self):
70+
""" Decrease the number of dice """
71+
72+
self.size -= 1
4873

4974

5075
async def next_theme(self):
@@ -77,9 +102,24 @@ async def slow(self):
77102

78103
self.sleep += 0.05
79104

105+
async def roll(self):
106+
''' roll the dice '''
107+
108+
self.data = [random.randint(1, self.sides) for die in range(self.size)]
109+
110+
total = 0
111+
for die in self.data:
112+
total += (total * self.size) + die
113+
114+
self.total = total
115+
116+
print(f'{self.data} {total}')
117+
118+
self.canvas_text = f'{self.data} {total} {self.sides ** self.size}'
119+
120+
80121
def create_swarms(self):
81122

82-
print('new swarms')
83123
self.swarms = [Swarm()
84124
for x in range(random.randint(self.minswarms, self.maxswarms))]
85125

@@ -95,6 +135,80 @@ def recalc(self, width, height):
95135
self.canvas.configure(scrollregion=(0, 0, width, height))
96136

97137

138+
def draw_digit(self, x, y, size, die):
139+
""" Draw a digit like a die """
140+
digits = {
141+
1: ((0,0,0), (0,1,0), (0,0,0)),
142+
2: ((1,0,0), (0,0,0), (0,0,1)),
143+
3: ((1,0,0), (0,1,0), (0,0,1)),
144+
4: ((1,0,1), (0,0,0), (1,0,1)),
145+
5: ((1,0,1), (0,1,0), (1,0,1)),
146+
6: ((1,0,1), (1,0,1), (1,0,1))}
147+
148+
149+
self.draw_pixel(digits[die], x, y, size, size, 10)
150+
151+
def draw_pixel(self, data, x, y, width, height, size, colour=None):
152+
153+
#colour = 'red'
154+
colour = colour or self.random_colour()
155+
156+
for ix, row in enumerate(data):
157+
for jx, col in enumerate(row):
158+
159+
xx = (ix * width) + x
160+
161+
yy = (jx * height) + x
162+
163+
#print(f'zzzz {xx} {yy} {size}, {colour}')
164+
165+
if col:
166+
self.canvas.create_arc(xx-size, yy-size, xx+size, yy+size,
167+
start=0, extent=180, fill=colour)
168+
169+
def random_colour(self):
170+
171+
colours = self.theme.colours
172+
173+
return colours[random.randint(0, len(colours) - 1)]
174+
175+
176+
def dice_ware_text(self):
177+
""" Return password for current data """
178+
179+
# FIXME get a diceware list of words
180+
# Also, remind me to write a bit about why real dice are better
181+
182+
try:
183+
return self.words[tuple(self.data)]
184+
except:
185+
return "no dice"
186+
187+
def draw_dice(self):
188+
""" Draw the dice """
189+
190+
#FIXME make the tkinter part async
191+
totsize = min(self.width, self.height)
192+
193+
gap = totsize / (1 + self.size)
194+
195+
xx = gap / 2
196+
yy = gap / 2
197+
198+
ygap = (self.height / 3) / (self.size - 1)
199+
200+
text = self.dice_ware_text()
201+
self.canvas.create_text(totsize - (2 * gap), yy, fill=self.random_colour(),
202+
text=text, font=BIGLY)
203+
204+
for die in self.data:
205+
self.draw_digit(xx, yy, gap * 0.4, die)
206+
207+
xx += gap
208+
yy += gap
209+
210+
211+
98212
async def run(self):
99213
self.sleep = 0.1
100214

@@ -112,6 +226,13 @@ async def run(self):
112226
ray.draw(self.canvas, self.width, self.height,
113227
self.theme.colours)
114228

229+
if self.canvas_text:
230+
self.canvas.create_text((self.width * 0.3, self.height * 0.9),
231+
text=self.canvas_text, fill='yellow',
232+
font=BIGLY)
233+
self.draw_dice()
234+
235+
115236
await curio.sleep(self.sleep)
116237

117238

@@ -132,7 +253,6 @@ def __init__(self):
132253
self.step = 0
133254
self.this_step = random.randint(40, 100)
134255

135-
136256
def draw(self, canvas, width, height, colours):
137257

138258

@@ -176,3 +296,18 @@ def draw(self, canvas, width, height, colours):
176296
self.this_step = random.randint(40, 100)
177297

178298
# FIXME: draw tail -- sine wave angle of dangle based on dx, dy
299+
300+
301+
def load_words(infile):
302+
303+
words = {}
304+
six = set([str(x) for x in range(1, 7)])
305+
for row in infile:
306+
if row[0] in six:
307+
308+
fields = row.split()
309+
key = tuple(int(x) for x in fields[0])
310+
311+
words[key] = fields[-1]
312+
313+
return words

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL