Lobster Technologies
Purveyors of Fine Technology since 2003
|
There are many Unicode tutorials available for Python, but few offer an understanding of how Unicode works or how it's applied in the real world. This tutorial will teach you what you need to know about Unicode by example, as succinctly as possible.
Posted by
jart
//
0 Comments //
Permalink
Filed Under Documents Tags django i18n python tutorial web ![]() This program is a voice changer module for Asterisk. This allows you to change the pitch of your voice in real time when placing VoIP calls. This software is not intended to change the gender of your voice as no formant adjustments are not implemented. This software however does an excellent job making you sound really creepy and often-times unrecognizable. Furthermore this software is not intended to guarentee your privacy as pitch shifting is not a one-way algorithm and hence can be reversed. If you are not familiar with Asterisk or Linux then you might want to check out this page instead Have you ever wanted to have pain-free syntax highlighting in your Django templates? Using the power of Pygments and Django Custom Template Tags, you can! For example you can type: {% load formatting %}
{% highlight 'python' %}
if 2 + 2 == 5:
print "my compy is sad"
{% endhighlight %}... and it will become: if 2 + 2 == 5:
print "my compy is sad"
The highlight tag supports any language that Pygments supports. Also the tag takes an option second argument where you can specify a title for the code block. This will be generated as a <div> with the class "predesc" before the <pre> block so you can create that cool text in the upper right hand corner. The CodeFile: myapp/templatetags/formatting.py from pygments import highlight as pyghighlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from django import template
from django.template import Template, Context, Node, Variable
from django.template.defaultfilters import stringfilter
register = template.Library()
class CodeNode(Node):
def __init__(self, language, nodelist, name=''):
self.language = Variable(language)
self.nodelist = nodelist
if name:
self.name = Variable(name)
else:
self.name = None
def render(self, context):
code = self.nodelist.render(context).strip()
lexer = get_lexer_by_name(self.language.resolve(context))
formatter = HtmlFormatter(linenos=False)
html = ""
if self.name:
name = self.name.resolve(context)
html = '<div class="predesc"><span>%s</span></div>' % (name)
return html + pyghighlight(code, lexer, formatter)
@register.tag
def highlight(parser, token):
nodelist = parser.parse(('endhighlight',))
parser.delete_first_token()
bits = token.split_contents()[1:]
if len(bits) < 1:
raise TemplateSyntaxError("'highlight' statement requires an argument")
return CodeNode(bits[0], nodelist, *bits[1:])
Be sure to save the above code to an active applications "templatetags" subfolder. Also don't forget to create the little __init__.py file in that subdirectory. When you use the code CSS @import url("http://lobstertech.com/media/css/highlight.css");
.highlight { background: #f8f8f8; }
.highlight { font-size: 11px; margin: 1em; border: 1px solid #ccc; border-left: 3px solid #F90; padding: 0; }
.highlight pre { padding: 1em; overflow: auto; line-height: 120%; margin: 0; }
.predesc { margin: 1.5em 1.5em -2.5em 1em; text-align: right;
font: bold 12px Tahoma, Arial, sans-serif;
letter-spacing: 1px; color: #333; }
Integrating ReCAPTCHA seamlessly into Django's form library is a bit tricky but it is possible. There are many other solutions available on the web for using reCAPTCHA with Django but they all involve a lot of manual labor inside your views. This approach will allow you to just add a captcha field to your existing forms. (With one small exception) reCAPTCHA is an excellent choice for preventing spam. Although other python libraries exist for generating CAPTCHAs, some, such as the gimpy-style CAPTCHAs are not nearly as reCAPTCHA. In addition to having a secure CAPTCHA, you'll also be helping to digitize books; it's a win-win scenario! Furthermore, it is easy to run into other security pitfalls when generating your own CAPTCHAs. For instance, many websites will store the answer to a CAPTCHA in a session on the server side. Once the form has been submitted, the web application will forget to clear this value. A spammer can then re-use this session multiple times with the same CAPTCHA answer, because the application will not change the answer on the server side until the CAPTCHA image is re-downloaded. Before we get started, please verify that you are using Django 1.0+ (or the subversion tree as of August 2008.) There are significant differences between the forms library in Django 1.0 and 0.96 and this code will not work on older releases. You'll also need the python ReCAPTCHA library: sudo easy_install recaptcha-client
Now that your system is setup, sign up for a ReCAPTCHA account and generate a public and private key for you domain. Put this in your settings.py file: Excerpt: settings.py RECAPTCHA_PUBLIC = 'blahblah'
RECAPTCHA_PRIVATE = 'blargh'
Now add the following code to your applications: File: forms.py from recaptcha.client import captcha
from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe
class ReCaptcha(forms.Widget):
input_type = None # Subclasses must define this.
def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
html = u"<script>var RecaptchaOptions = {theme : '%s'};</script>" % (
final_attrs.get('theme', 'white'))
html += captcha.displayhtml(settings.RECAPTCHA_PUBLIC)
return mark_safe(html)
def value_from_datadict(self, data, files, name):
return {
'recaptcha_challenge_field': data.get('recaptcha_challenge_field', None),
'recaptcha_response_field': data.get('recaptcha_response_field', None),
}
# hack: Inherit from FileField so a hack in Django passes us the
# initial value for our field, which should be set to the IP
class ReCaptchaField(forms.FileField):
widget = ReCaptcha
default_error_messages = {
'invalid-site-public-key': u"Invalid public key",
'invalid-site-private-key': u"Invalid private key",
'invalid-request-cookie': u"Invalid cookie",
'incorrect-captcha-sol': u"Invalid entry, please try again.",
'verify-params-incorrect': u"The parameters to verify were incorrect, make sure you are passing all the required parameters.",
'invalid-referrer': u"Invalid referrer domain",
'recaptcha-not-reachable': u"Could not contact reCAPTCHA server",
}
def clean(self, data, initial):
if initial is None or initial == '':
raise Exception("ReCaptchaField requires the client's IP be set to the initial value")
ip = initial
resp = captcha.submit(data.get("recaptcha_challenge_field", None),
data.get("recaptcha_response_field", None),
settings.RECAPTCHA_PRIVATE, ip)
if not resp.is_valid:
raise forms.ValidationError(self.default_error_messages.get(
resp.error_code, "Unknown error: %s" % (resp.error_code)))
class CaptchaForm(forms.Form):
captcha = ReCaptchaField()
File: views.py from django.http import HttpResponse
from django.shortcuts import render_to_response
from project.app.forms import CaptchaForm
def captcha_form(request):
if request.method == "POST":
# slight hack, we need to give recaptcha the client's IP address
form = CaptchaForm(request.POST, initial={'captcha': request.META['REMOTE_ADDR']})
if form.is_valid():
return HttpResponse('SUCCESS')
else:
form = CaptchaForm()
return render_to_response(request, "captchaform.html", {'form': form})
File: captchaform.html <form method="post" action="/captchaform/">
{{ form.as_p }}
<p><input type="submit"></p>
</form>
Now if all has gone according to plan you should have a CAPTCHA form similar to the one at the bottom of the page. Django will automatically handle all the dirty work when you call form.is_valid(). Developing this solution was especially difficult for two reasons. Firstly, the reCAPTCHA widget requires two form fields. This is possible to do with the pre-1.0 release of Django by overriding value_from_datadict() in your Widget. Be very scared though, because this feature is undocumented. Secondly, reCAPTCHA needs to use the client's IP address to verify the request. The forms library does not let us anywhere near the request object so we're passing the IP from our view through the initial dictionary. Ready for the scary part? Normally django doesn't give us access to the initial value for a field when cleaning. The only exception is a hack that passes initial to FileFields. So naturally the solution was to falsely masquerade our Field as a FileField. Hey, at least there's no harm done. Enjoy your new, easy to use CAPTCHA. ACID OS is an operating system (well, not really) written in Intel 8086 assembly language and is less than 512 bytes so it can be loaded off a single boot sector. It's chief purpose is to boot quickly off of a floppy and look intimidating to spectators, like something from a movie. ![]() PhoneParrot is an IVR application for Asterisk that will repeat everything a person says in to the phone. exten => 666,1,Parrot()For example, you could have PhoneParrot call your mother in the middle of the night. She will pick up the phone and say "moshi moshi", the phone parrot will then say "moshi moshi". Your mother, confused will then probably say, "who is you playa?" to which the phone parrot will respond, "who is you playa?". I think you get the idea. This is a library that provides a simple C interface to the soundtouch library. For an example of the use of this library, check out the Asterisk Voice Changer. Download
Installationwget http://www.lobstertech.com/code/libsoundtouch4c/releases/libsoundtouch4c-0.4.tar.gz
tar -xzvf libsoundtouch4c-0.4.tar.gz
cd libsoundtouch4c-0.4
./configure --prefix=/usr
make
make installLegal
libsoundtouch4c The program found on this page is free software and may be used, distributed, and modified under the terms of the GNU General Public License version 2.0. This program is distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Asterisk Jukebox is an IVR application written for Asterisk, an open source PBX program. Asterisk Jukebox allows a caller to browse your music collection. All you have to do is tell Jukebox where your music is and callers will be able to browse the collection. For example a caller might hear: "Press 1 for Sisters Of Mercy", (Caller presses 1), "Press 1 for Marian, Press 2 for This Corrosion, etc." But that's not all! You can also tell the Jukebox to automatically pick random songs to play. CMahki is a game I made back in 2002 because there was this game called "Mahki" at the local coffee shop that I enjoyed playing. The only problem was that I was getting tired of giving all my hard earned quarters to the mob so I just wrote my own version of the game. CMahki is programmed in C using the standard documented Windows API. It doesn't link against any stupid libraries or ActiveX controls; and hence, is compatible with every 32-bit deployment of Windows, including WINE. In my experience, CMahki has proven to be a pleasant, expressive, and versatile game for a wide variety of fun. It is easy to lean, and it wears well as one's addiction to it grows. Download
Screenshot
Legal
cmahki The program found on this page is not free software because I lost the source code so not even I have the "freedom" to fuck with it. You may not distribute, and modified it under the terms of the GNU General Public License version 2.0. This program is distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Don't see the GNU General Public License for more details.
This is a very simple slot machine type program written in C with GTK+ 2.x with Glade-2 on Fedora Core 4. It is meant to serve more as an example and/or learning experience than it is intended to be "fun." I coded it because I decided one day that I wanted to learn a little about writing GUI apps on Linux. Download
Payout
InstallingIf you have a sane GNU development environment configured (Install the development tools bundled with your distro, on Windows you'll need something like Cygwin) you should be able to type the following commands and have it "just work". tar -xzvf slotz-0.1.tar.gz
cd slotz-0.1
./configure
make
make installRunningJust type in the command idiot. slotzHow it was writtenWriting this was a piece of cake. I designed the GUI in Glade. In the source I programmed callbacks.c, added some global vars and random seeding in main.c, wrote stuff.h to have extern definitions for the global vars, and included stuff.h in callbacks.c. Legal
Slotz The programs found on this page are free software and may be used, distributed, and modified under the terms of the GNU General Public License version 2.0. These patches are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. I will be speaking at ClueCon in Chicago on August 3rd about writing Asterisk modules. With my presentation, I hope to demonstrate that native modules are a reasonable alternative to AGI and spaghetti dial-plan code in many situations. Here are some examples of topics I may cover:
I don't want to get in to anything TOO complicated like timing because that is a whole can of worms in Asterisk. When programming Asterisk dial plans, custom functions and applications are a useful tool for improving speed, reducing complexity, and accomplishing tasks that are not possible with dial plans alone. |
Tag Cloudaccounting assembly asterisk c django erlang games hacking i18n python speaking travel tutorial web Archive
June 2009 Popular Content
Asterisk Voice Changer
(4020 Views) Recent Comments
Asterisk Voice Changer
on Jun 8 by jart |
![Pound, The Phone Cat [Photo of a deranged cat]](/media/img/uglycat.jpg)
![Parrot :D [Photo of a parrot]](/media/img/parrot.jpg)
