python-2.x

How to force PyYAML to load strings as unicode objects?

北城以北 提交于 2019-12-03 04:20:49
The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force PyYAML to always strings load unicode objects? I do not want to clutter my YAML with !!python/unicode tags. # Encoding: UTF-8 import yaml menu= u"""--- - spam - eggs - bacon - crème brûlée - spam """ print yaml.load(menu) Output: ['spam', 'eggs', 'bacon', u'cr\xe8me br\xfbl\xe9e', 'spam'] I would like: [u'spam', u'eggs', u'bacon', u'cr\xe8me br\xfbl

Use of input/raw_input in python 2 and 3 [duplicate]

天涯浪子 提交于 2019-12-03 04:13:06
This question already has an answer here: Backwards-compatible input calls in Python 4 answers I would like to set a user prompt with the following question: save_flag is not set to 1; data will not be saved. Press enter to continue. input() works in python3 but not python2. raw_input() works in python2 but not python3. Is there a way to do this so that the code is compatible with both python 2 and python 3? Ashwini Chaudhary Bind raw_input to input in Python 2: try: input = raw_input except NameError: pass Now input will return a string in Python 2 as well. If you're using six to write 2/3

How can I optimize this Python code to generate all words with word-distance 1?

和自甴很熟 提交于 2019-12-03 03:55:09
问题 Profiling shows this is the slowest segment of my code for a little word game I wrote: def distance(word1, word2): difference = 0 for i in range(len(word1)): if word1[i] != word2[i]: difference += 1 return difference def getchildren(word, wordlist): return [ w for w in wordlist if distance(word, w) == 1 ] Notes: distance() is called over 5 million times, majority of which is from getchildren, which is supposed to get all words in the wordlist that differ from word by exactly 1 letter.

How to implement user_loader callback in Flask-Login

那年仲夏 提交于 2019-12-03 03:08:13
问题 I'm attempting to use Flask and the Flask-Login extension to implement user authentication in a Flask app. The goal is to pull user account information from a database and then log in a user, but I'm getting stuck; however, I've narrowed it down to a particular part of Flask-Login behavior. According to the Flask-Login documentation, I need to create a user_loader "callback" function. The actual purpose and implementation of this function has had me confused for a few days now: You will need

Python re match only letters from word

时间秒杀一切 提交于 2019-12-02 21:44:18
问题 I am new to Python re, but I need help. I searched here, google, documentation, but nothing worked. So here is what I am trying to do. I have word (for example) "string" then I have word list: strings, string, str, ing, in, ins, rs, stress And I want to matches like: string, str, ing, in, ins, rs. I don't want to match: stress, strings (because there are 2x s, and in word string, there is only 1) Simply match only the letters which are in word string . Sorry for bad english and if I didnt

Convert unicode with utf-8 string as content to str

柔情痞子 提交于 2019-12-02 21:04:25
I'm using pyquery to parse a page: dom = PyQuery('http://zh.wikipedia.org/w/index.php', {'title': 'CSS', 'printable': 'yes', 'variant': 'zh-cn'}) content = dom('#mw-content-text > p').eq(0).text() but what I get in content is a unicode string with utf-8 encoded content: u'\xe5\xb1\x82\xe5\x8f\xa0\xe6\xa0\xb7\xe5\xbc\x8f\xe8\xa1\xa8...' how could I convert it to str without lost the content? to make it clear: I want conent == '\xe5\xb1\x82\xe5\x8f\xa0\xe6\xa0\xb7\xe5\xbc\x8f\xe8\xa1\xa8' not conent == u'\xe5\xb1\x82\xe5\x8f\xa0\xe6\xa0\xb7\xe5\xbc\x8f\xe8\xa1\xa8' If you have a unicode value

Set literal gives different result from set function call

梦想与她 提交于 2019-12-02 19:56:02
Why does the set function call wipe out the dupes, but parsing a set literal does not? >>> x = Decimal('0') >>> y = complex(0,0) >>> set([0, x, y]) {0} >>> {0, x, y} {Decimal('0'), 0j} (Python 2.7.12. Possibly same root cause as for this similar question) Sets test for equality, and until there are new Python releases, the order in which they do this can differ based on the form you hand the values to the set being constructed, as I'll show below. Since 0 == x is true and 0 == y is true, but x == y is false , the behaviour here is really undefined , as the set assumes that x == y must be true

How to organize Python modules for PyPI to support 2.x and 3.x

断了今生、忘了曾经 提交于 2019-12-02 19:15:34
I have a Python module that I would like to upload to PyPI. So far, it is working for Python 2.x. It shouldn't be too hard to write a version for 3.x now. But, after following guidelines for making modules in these places: Distributing Python Modules The Hitchhiker’s Guide to Packaging it's not clear to me how to support multiple source distributions for different versions of Python, and it's not clear if/how PyPI could support it. I envisage I would have separate code for: 2.x 2.6 (maybe, as a special case to use the new buffer API) 3.x How is it possible to set up a Python module in PyPI so

How to implement user_loader callback in Flask-Login

心不动则不痛 提交于 2019-12-02 17:41:23
I'm attempting to use Flask and the Flask-Login extension to implement user authentication in a Flask app. The goal is to pull user account information from a database and then log in a user, but I'm getting stuck; however, I've narrowed it down to a particular part of Flask-Login behavior. According to the Flask-Login documentation , I need to create a user_loader "callback" function. The actual purpose and implementation of this function has had me confused for a few days now: You will need to provide a user_loader callback. This callback is used to reload the user object from the user ID

How do I undo True = False in python interactive mode? [duplicate]

[亡魂溺海] 提交于 2019-12-02 17:22:05
This question already has an answer here: Naming conflict with built-in function 7 answers So I tried the "evil" thing Ned Deily mentioned in his answer here . Now I have that the type True is now always False. How would I reverse this within the interactive window? Thing to not do: True = False Since True has now been completely overridden with False, there doesn't seem to be an obvious way to back-track. Is there a module that True comes from that I can do something like: True = <'module'>.True You can simply del your custom name to set it back to the default: >>> True = False >>> True False