collapsing all of dr_botzo one directory
This commit is contained in:
0
pi/__init__.py
Normal file
0
pi/__init__.py
Normal file
8
pi/admin.py
Normal file
8
pi/admin.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Manage pi models in the admin interface."""
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from pi.models import PiLog
|
||||
|
||||
|
||||
admin.site.register(PiLog)
|
||||
41
pi/ircplugin.py
Normal file
41
pi/ircplugin.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# coding: utf-8
|
||||
|
||||
import logging
|
||||
|
||||
from ircbot.lib import Plugin
|
||||
from pi.models import PiLog
|
||||
|
||||
|
||||
log = logging.getLogger('pi.ircplugin')
|
||||
|
||||
|
||||
class Pi(Plugin):
|
||||
|
||||
"""Use the Monte Carlo method to simulate pi."""
|
||||
|
||||
def start(self):
|
||||
"""Set up the handlers."""
|
||||
|
||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!pi$',
|
||||
self.handle_pi, -20)
|
||||
|
||||
super(Pi, self).start()
|
||||
|
||||
def stop(self):
|
||||
"""Tear down handlers."""
|
||||
|
||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_pi)
|
||||
|
||||
super(Pi, self).stop()
|
||||
|
||||
def handle_pi(self, connection, event, match):
|
||||
"""Handle the pi command by generating another value and presenting it."""
|
||||
|
||||
newest, x, y, hit = PiLog.objects.simulate()
|
||||
msg = ("({0:.10f}, {1:.10f}) is {2}within the unit circle. π is {5:.10f}. (i:{3:d} p:{4:d})"
|
||||
"".format(x, y, "" if hit else "not ", newest.count_inside, newest.count_total, newest.value()))
|
||||
|
||||
return self.bot.reply(event, msg)
|
||||
|
||||
|
||||
plugin = Pi
|
||||
21
pi/migrations/0001_initial.py
Normal file
21
pi/migrations/0001_initial.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='PiLog',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('count_inside', models.PositiveIntegerField()),
|
||||
('count_total', models.PositiveIntegerField()),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
17
pi/migrations/0002_auto_20150521_2204.py
Normal file
17
pi/migrations/0002_auto_20150521_2204.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pi', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='pilog',
|
||||
options={'get_latest_by': 'created'},
|
||||
),
|
||||
]
|
||||
0
pi/migrations/__init__.py
Normal file
0
pi/migrations/__init__.py
Normal file
67
pi/models.py
Normal file
67
pi/models.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""Karma logging models."""
|
||||
|
||||
import logging
|
||||
import math
|
||||
import pytz
|
||||
import random
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
|
||||
log = logging.getLogger('pi.models')
|
||||
|
||||
|
||||
class PiLogManager(models.Manager):
|
||||
|
||||
"""Assemble some queries against PiLog."""
|
||||
|
||||
def simulate(self):
|
||||
"""Add one more entry to the log, and return it."""
|
||||
|
||||
try:
|
||||
latest = self.latest()
|
||||
except PiLog.DoesNotExist:
|
||||
latest = PiLog(count_inside=0, count_total=0)
|
||||
latest.save()
|
||||
|
||||
inside = latest.count_inside
|
||||
total = latest.count_total
|
||||
|
||||
x = random.random()
|
||||
y = random.random()
|
||||
hit = True if math.hypot(x,y) < 1 else False
|
||||
|
||||
if hit:
|
||||
newest = PiLog(count_inside=inside+1, count_total=total+1)
|
||||
else:
|
||||
newest = PiLog(count_inside=inside, count_total=total+1)
|
||||
newest.save()
|
||||
|
||||
return newest, x, y, hit
|
||||
|
||||
|
||||
class PiLog(models.Model):
|
||||
|
||||
"""Track pi as it is estimated over time."""
|
||||
|
||||
count_inside = models.PositiveIntegerField()
|
||||
count_total = models.PositiveIntegerField()
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
objects = PiLogManager()
|
||||
|
||||
class Meta:
|
||||
get_latest_by = 'created'
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
tz = pytz.timezone(settings.TIME_ZONE)
|
||||
return "({0:d}/{1:d}) @ {2:s}".format(self.count_inside, self.count_total,
|
||||
self.created.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S %Z'))
|
||||
|
||||
def value(self):
|
||||
"""Return this log entry's value of pi."""
|
||||
|
||||
return 4.0 * int(self.count_inside) / int(self.count_total)
|
||||
Reference in New Issue
Block a user