41. ### View
#!/usr/bin/env python
from django import http
"""
from django.template import Template, Context
WSW - the World's Shittiest Wiki.
"""
def wsw(request, title):
import re
title = urllib.unquote_plus(title) if title else "Home"
import urllib
p, created = Page.objects.get_or_create(title=title)
from django.conf import settings
form = PageForm(instance=p)
if request.method == "POST":
### Config
form = PageForm(request.POST, instance=p)
settings.configure(
form.save()
DEBUG = True,
return http.HttpResponseRedirect(request.get_full_path())
DATABASE_ENGINE = "doj.backends.zxjdbc.postgresql",
else:
DATABASE_NAME = "wsw",
form = PageForm(instance=p)
DATABASE_USER = "wsw",
t = Template("""<h1>{{ p.title }}</h1><ul>{% for p in
DATABASE_PASSWORD = "wsw",
allpages %}<li><a href="{{ p }}">{{ p }}</a></li>{% endfor %}
ROOT_URLCONF = __name__)
</ul><form action="{{ request.get_full_path }}" method="POST">
<p>{{ form.content }}</p><input type="submit"></form>""")
### Model
wikiwords = re.findall(
from django.db import models
'[A-Z][a-z]+[A-Z][a-z]+(?:[A-Z][a-z]+)*', p.content)
allpages = [
class Page(models.Model):
page.title for page in Page.objects.order_by('title')]
title = models.CharField(max_length=300,
allpages.extend(
primary_key=True)
word for word in wikiwords if word not in allpages)
content = models.TextField()
return http.HttpResponse(t.render(Context(locals())))
class Meta:
### Main
app_label = "wsw"
if __name__ == '__main__':
from django.core import management
### URLs
from django.db import connection
from django.conf.urls.defaults import *
from django.core.management.color import no_style
cursor = connection.cursor()
urlpatterns = patterns(__name__, ('(.*)', 'wsw'))
statements, _ = connection.creation.sql_create_model(
Page, no_style())
### Form
try:
from django.forms import ModelForm
for sql in statements:
cursor.execute(sql)
class PageForm(ModelForm):
connection.connection.commit()
class Meta:
except:
model = Page
pass
fields = ['content']
management.call_command('runserver')
45. def factorial(n):
It must also not be ridiculously large:
"""Return the factorial of n, an
>>> factorial(1e100)
exact integer >= 0.
Traceback (most recent call last):
...
If the result is small enough to
OverflowError: n too large
fit in an int, return an int.
"""
Else return a long.
import math
>>> [factorial(n)
if not n >= 0:
... for n in range(6)]
raise ValueError("n must be >= 0")
[1, 1, 2, 6, 24, 120]
if math.floor(n) != n:
>>> [factorial(long(n))
raise ValueError(
... for n in range(6)]
"n must be exact integer")
[1, 1, 2, 6, 24, 120]
if n+1 == n: # catch a value like 1e300
>>> factorial(30)
raise OverflowError("n too large")
265252859812191058636308480000000L
result = 1
>>> factorial(30L)
factor = 2
265252859812191058636308480000000L
while factor <= n:
>>> factorial(-1)
result *= factor
Traceback (most recent call last):
factor += 1
...
return result
ValueError: n must be >= 0
Factorials of floats are OK, but
the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000L