Remove whitespaces in XML string

The easiest solution is probably using lxml, where you can set a parser option to ignore white space between elements: >>> from lxml import etree >>> parser = etree.XMLParser(remove_blank_text=True) >>> xml_str=””‘<root> >>> <head></head> >>> <content></content> >>> </root>”’ >>> elem = etree.XML(xml_str, parser=parser) >>> print etree.tostring(elem) <root><head/><content/></root> This will probably be enough for your needs, but … Read more

Setup.py: install lxml with Python2.6 on CentOS

I had the same issue, I managed to install it after installing the package libxslt-devel and python-devel which seems to be your problem: yum install libxslt-devel python-devel python setup.py install Installed /usr/lib/python2.6/site-packages/lxml-2.2.8-py2.6-linux-i686.egg Processing dependencies for lxml==2.2.8 Finished processing dependencies for lxml==2.2.8 However since I also installed other packages in the process, you might want to … Read more

Alternative to dict comprehension prior to Python 2.7

Use: gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8])) That’s the dict() function with a generator expression producing (key, value) pairs. Or, to put it generically, a dict comprehension of the form: {key_expr: value_expr for targets in iterable <additional loops or if expressions>} can always be made compatible with Python < 2.7 by using: … Read more

Suppress print output in unittests

Call your unittest with option “-b” – buffer stdout and stderr Foo.py class Foo: def bar(self): print “bar” return 7 test.py import unittest from Foo import Foo class test_Foo(unittest.TestCase): def test_bar(self): obj = Foo() res = obj.bar() self.assertEqual(res, 7) if __name__ == “__main__”: unittest.main() Run it with -b option $ python test.py -b . ———————————————————————- … Read more

If all in list == something

Use: all( type(i) is int for i in lst ) Example: In [1]: lst = range(10) In [2]: all( type(i) is int for i in lst ) Out[2]: True In [3]: lst.append(‘steve’) In [4]: all( type(i) is int for i in lst ) Out[4]: False [Edit]. Made cleaner as per comments.

Extract time from datetime and determine if time (not date) falls within range?

This line: str_time = datetime.strptime(Datetime, “%m/%j/%y %H:%M”) returns a datetime object as per the docs. You can test this yourself by running the following command interactively in the interpreter: >>> import datetime >>> datetime.datetime.strptime(’12/31/13 00:12′, “%m/%j/%y %H:%M”) datetime.datetime(2013, 1, 31, 0, 12) >>> The time portion of the returned datetime can then be accessed using … Read more

Why does Python have a format function as well as a format method

tldr; format just calls obj.__format__ and is used by the str.format method which does even more higher level stuff. For the lower level it makes sense to teach an object how to format itself. It is just syntactic sugar The fact that this function shares the name and format specification with str.format can be misleading. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)