My Python & co. stuff

Archive for the ‘Python’ Category

Short snippet for settings window as topmost in Pyglet on win32 platform:

    from pyglet.window.win32 import _user32
    from pyglet.window.win32.constants import *

    def set_topmost(window):
        _user32.SetWindowPos(
            window._hwnd, HWND_TOPMOST, 0, 0, 0, 0,
            SWP_NOMOVE | SWP_NOSIZE)
Advertisements

… and settings.py, and urls.py, and so on. Customize as you like.

 

from django.conf import settings
from django.core import management

MY_APP = '' # Your app name here
if not MY_APP_NAME:
    import sys
    MY_APP_NAME = sys.argv[1]

settings.configure(
    DEBUG=True, 
    TEMPLATE_DEBUG=True,
    INSTALLED_APPS = [MY_APP],
    DATABASES = {
        'default': {
            'ENGINE':'django.db.backends.sqlite3',
            'NAME': ':memory:',
            }
        }
    )

management.call_command('test', MY_APP)

Another Python gotcha?

>>> a
12.878
>>> b
Decimal('15.0')
>>> a > b
True
>>> type(a)
<type 'float'>
>>> type(b)
<class 'decimal.Decimal'>
>>> 1.0 > Decimal('1.0')
True
>>> 0.0 > Decimal('0.0')
True
>>> 1.0 > Decimal('1.0') True >>> 0.0 > Decimal('0.0') True

Python has it’s own logging. Twisted has it’s own logging. Unfortunatley, none of them have a convinient way to log to underlying operating system’s facilities (which is, syslog or Windows Event Log in my case). So, I had to wrote one myself. Here you go – this is MultiLog:

class MultiLog(object):
"""
class MultiLog(object): """ Multiplatform logging component. (C) 2009 FHU KAGAMI <info@fhu-kagami.pl> Distributed under the terms of MIT License """ def __init__(self, appname, interactive = None): self.hostname = socket.gethostname() self.appname = appname self.interactive = interactive if self.interactive is None: self.interactive = '--interactive' in sys.argv self.pid = os.getpid() fun = '_init_log_' + sys.platform getattr(self, fun)() def log(self, msg): if type(msg)==unicode: msg = msg.encode('utf8') fun = '_log_' + sys.platform return getattr(self, fun)(msg) __call__ = log def _log_interactive(self, msg): if self.interactive: msg = time.ctime() + ": " + self.hostname + " " + self.appname + "[" + str(self.pid) + "]: " + msg sys.stderr.write(msg) sys.stderr.write('\n') sys.stderr.flush() def _init_log_unix(self): from syslog import openlog openlog(self.appname + "[" + str(self.pid) + "]") def _log_unix(self, msg): from syslog import syslog syslog(msg) self._log_interactive(msg) _init_log_linux2 = _init_log_unix _log_linux2 = _log_unix def _init_log_win32(self): import win32evtlog import win32con self._win32_type = win32con.EVENTLOG_INFORMATION_TYPE self._win32_log = win32evtlog.OpenEventLog(None, self.appname) def _log_win32(self, msg): import win32evtlog win32evtlog.ReportEvent(self._win32_log, self._win32_type, 0, 0, None, (msg, 'PID: %s' % self.pid), None) self._log_interactive(msg) if __name__ == "__main__": log = MultiLog('my application') log('this is a test')

On Ubuntu, look for /var/log/user.log. On Windows, run mmc.exe and open eventwvr – look for Application Logs.