How to implement user_loader callback in Flask-Login

You will need to load the user object from the DB upon every request. The strongest reason for that requirement is that Flask-Login will check the authentication token every time to ensure its continuing validity. The calculation of this token may require parameters stored on the user object. For example, suppose a user has two … Read more

Parsing HTML to get text inside an element

I searched “python parse html” and this was the first result: https://docs.python.org/2/library/htmlparser.html This code is taken from the python docs from HTMLParser import HTMLParser # create a subclass and override the handler methods class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print “Encountered a start tag:”, tag def handle_endtag(self, tag): print “Encountered an end tag :”, tag … 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

How to write Python 2.x as much compatible with Python 3.x as possible?

I’m putting the finishing touches on an approximately 5000 line, deduplicating backup program (http://stromberg.dnsalias.org/~strombrg/backshift/) that runs on CPython 2.[567], CPython 3.[0123] (3.3 is still alpha 0), Pypy 1.7 and Jython trunk. I also tried IronPython, but it was a pretty different thing – it had no standard library, so no backshift love. Oh, and it … Read more

Why is math.factorial much slower in Python 2.x than 3.x?

Python 2 uses the naive factorial algorithm: 1121 for (i=1 ; i<=x ; i++) { 1122 iobj = (PyObject *)PyInt_FromLong(i); 1123 if (iobj == NULL) 1124 goto error; 1125 newresult = PyNumber_Multiply(result, iobj); 1126 Py_DECREF(iobj); 1127 if (newresult == NULL) 1128 goto error; 1129 Py_DECREF(result); 1130 result = newresult; 1131 } Python 3 uses the … Read more

__str__ versus __unicode__

__str__() is the old method — it returns bytes. __unicode__() is the new, preferred method — it returns characters. The names are a bit confusing, but in 2.x we’re stuck with them for compatibility reasons. Generally, you should put all your string formatting in __unicode__(), and create a stub __str__() method: def __str__(self): return unicode(self).encode(‘utf-8’) … Read more

Dangers of sys.setdefaultencoding(‘utf-8’)

The original poster asked for code which demonstrates that the switch is harmful—except that it “hides” bugs unrelated to the switch. Updates [2020-11-01]: pip install setdefaultencoding Eradicates the need to reload(sys) (from Thomas Grainger). [2019]: Personal experience with python3: No unicode en/decoding problems. Reasons: Got used to writing .encode(‘utf-8’) .decode(‘utf-8’) a (felt) 100 times a … Read more

tech