-
Errors when installing the Python ‘lxml’ library using pip on Ubuntu
Posted on August 28th, 2011 No comments
Print
I thought I’d post this here, this is the second time I ran into this issue on Ubuntu and forgot what I did the first time.
If you get errors about things missing when you do ‘sudo pip install lxml’ on Ubuntu, you’ll probably need to install the following development packages (source):
- sudo apt-get install python-dev
- sudo apt-get install libxml2-dev
- sudo apt-get install libxslt1-dev
You may also want to do ‘sudo pip install lxml –upgrade’ if pip tells you that it’s already installed but you know it wasn’t properly installed.
-
How to configure the ‘logging’ module using dictionaries in Python 2.6
Posted on August 21st, 2011 No comments
Print
The logging.config module was updated in Python 2.7 and included a function called dictConfig() which takes a dictionary as an argument used to configure the logging module.
I wanted to use this in my new project so I can keep all my configurations/settings in one Python file but we’re not ready to upgrade to Python 2.7 just yet. The good news is you can just get the dictconfig module on its own and add it to your project. I actually just took it from the Django 1.3 package and put it in a package within the project I’m working on.
Here’s a sample configuration/usage for the ‘root’ logger:
Configuration:
LOG_SETTINGS = { 'version': 1, 'root': { 'level': 'NOTSET', 'handlers': ['console', 'file', 'smtp', 'mongodb'], }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'INFO', 'formatter': 'detailed', 'stream': 'ext://sys.stdout', }, 'file': { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'INFO', 'formatter': 'detailed', 'filename': 'logs/MyProject.log', 'mode': 'a', 'maxBytes': 10485760, 'backupCount': 5, }, 'smtp': { 'class': 'logging.handlers.SMTPHandler', 'level': 'ERROR', 'formatter': 'email', 'mailhost': 'localhost', 'fromaddr': 'alerts@calazan.com', 'toaddrs': ['admin@calazan.com', 'support@calazan.com'], 'subject': '[My Project] Error encountered.', }, 'mongodb': { 'class': 'log4mongo.handlers.MongoHandler', 'level': 'DEBUG', 'host': 'localhost', 'port': 27017, 'database_name': 'myproject', 'collection': 'logs', 'username': 'logger', 'password': 'password', }, }, 'formatters': { 'detailed': { 'format': '%(asctime)s %(module)-17s line:%(lineno)-4d ' \ '%(levelname)-8s %(message)s', }, 'email': { 'format': 'Timestamp: %(asctime)s\nModule: %(module)s\n' \ 'Line: %(lineno)d\nMessage: %(message)s', }, }, }Usage:
import logging import dictconfig from settings import LOG_SETTINGS dictconfig.dictConfig(LOG_SETTINGS) logging.debug('This is a debug message!') logging.info('This is an info message!') logging.error('This is an error message!') -
How to print the values of all the fields of an object in Java
Posted on August 17th, 2011 No comments
Print
I was just doing some Java coding and I needed to check the values of a bunch of fields of an object (mostly numbers). Normally I’d just call and print each get() method if I just need to check a few fields, but the class I’m dealing with has over 30 fields and I need to check them all and there’s no way I’m doing over 30 print statements for each get() method!
A quick Google search led me to this StackOverflow post which has exactly what I was looking for:
ClassABC abc = new ClassABC(); for (Field field : abc.getClass().getDeclaredFields()) { field.setAccessible(true); String name = field.getName(); Object value = field.get(abc); System.out.printf("%s: %s%n", name, value); }


Recent Comments