python-2.6

RethinkDB: multiple comparisons filtering

流过昼夜 提交于 2019-12-11 02:44:50
问题 By the docs, it seems that in order to filter all users that are 30 years old OR 40 years old, I can do this (with python): r.table("users").filter((r.row["age"].eq(30)) | (r.row["age"].eq(40))).run(conn) Say I have a list based on input / request: [12, 14, 18, 88, 33 ...], how do I filter all the users that are in the age of one of the elements in the list above by iterating it (and not doing it hard coded)? 回答1: That's one way to do it valid_ages = [12, 14, 18, 88, 33] r.table("users")

Most pythonic way to convert a string to a octal number

浪子不回头ぞ 提交于 2019-12-10 12:31:15
问题 I am looking to change permissions on a file with the file mask stored in a configuration file. Since os.chmod() requires an octal number, I need to convert a string to an octal number. For example: '000' ==> 0000 (or 0o000 for you python 3 folks) '644' ==> 0644 (or 0o644) '777' ==> 0777 (or 0o777) After an obvious first attempt of creating every octal number from 0000 to 0777 and putting it in a dictionary lining it up with the string version, I came up with the following: def new_oct(octal

Can I sort a list of objects by 2 keys?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 10:38:43
问题 I have the following class (trimmed down): class DiskInstance(object): def __init__(self, name, epoch, size) self.name = name self.epoch = epoch self.size = size Then I define a external function (external to the class above): def getepoch(object): return object.epoch I then instantiate several objects of this class and append to a list called DISKIMAGES. I am currently sorting like this: for image in sorted(DISKIMAGES, key=getedate, reverse=True): Is there any way I can sort first by

writing back into the same file after reading from the file

让人想犯罪 __ 提交于 2019-12-10 03:19:37
问题 My aim is to read line from the file , strip the blank spaces at the end of it and write back into the same file. I have tried the following code: with open(filename, 'r+') as f: for i in f: f.write(i.rstrip()+"\n") This seems to write at the end of the file, keeping initial data in the file intact . I know that using f.seek(0) would take the pointer back to start of the file , which I am assuming would be somehow required for this solution. Can you please advise if there is different

How can I set a subparser to be optional in argparse?

混江龙づ霸主 提交于 2019-12-09 22:35:23
问题 import argparse parser_sub = subparsers.add_parser('files') parser_sub.add_argument( '--file-name', action='store', dest='filename', nargs='*') options = parser.parse_args() Output: error: too few arguments. As per this link: https://bugs.python.org/issue9253 it states that subparsers cant be optional. Can this behaviour be changed? I would like my subcommands to be optional. How can I achieve this through argparse in python 2.6? 回答1: There's not much that can be added to that bug/issue https

PYTHON 2.6 XML.ETREE to output single quote for attributes instead of double quote

夙愿已清 提交于 2019-12-09 21:49:08
问题 i got the following code : #!/usr/bin/python2.6 from lxml import etree n = etree.Element('test') n.set('id','1234') print etree.tostring(n) the output generate is <test id="1234"/> but i want <test id='1234'/> can someone help ? 回答1: I checked the documentation and found no reference for single/double-quote option. I think your only recourse is print etree.tostring(n).replace('"', "'") Update Given: from lxml import etree n = etree.Element('test') n.set('id', "Zach's not-so-good answer") my

how to use distutils to create executable .zip file?

巧了我就是萌 提交于 2019-12-09 17:58:09
问题 Python 2.6 and beyond has the ability to directly execute a .zip file if the zip file contains a __main__.py file at the top of the zip archive. I'm wanting to leverage this feature to provide preview releases of a tool I'm developing that won't require users to install anything beyond copying the .zip file to their disk. Is there a standard way to create such a zip file? I'm looking for a solution that works with python 2.6 and python 2.7. Ideally I would like to use distutils, since I

How to get in python the key pressed without press enter?

放肆的年华 提交于 2019-12-09 13:39:59
问题 I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed. 回答1: The related question may help you, as @S.Lott mentioned: Detect in python which keys are pressed I am writting in, though to give yu advice: don't worry about that. What kind of program are you trying to produce? Programas running on a terminal usually don't have an interface in which getting "live" keystrokes is interesting. Not nowadays. For programs running in the terminal, you

Can bin() be overloaded like oct() and hex() in Python 2.6?

 ̄綄美尐妖づ 提交于 2019-12-09 04:39:21
问题 In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__ special functions. However there is not a __bin__ special function for overloading the behaviour of Python 2.6's new bin() built-in function. I want to know if there is any way of flexibly overloading bin() , and if not I was wondering why the inconsistent interface? I do know that the __index__ special function can be used, but this isn't flexible as it can only

Creating dictionary of dictionaries in python 2.6

坚强是说给别人听的谎言 提交于 2019-12-09 02:07:24
问题 I have a line of code in python2.7 that generates a dictionary of empty dictionaries: values=[0,1,2,4,5,8] value_dicts={x:{} for x in values} which throws a syntax error when run on python2.6. I can do the same thing using a for loop: values_dicts={} values=[0,1,2,4,5,8] for value in values : values_dicts[value]={} values_dicts Out[25]: {0: {}, 1: {}, 2: {}, 4: {}, 5: {}, 8: {}} But that seems silly. Why does the list comprehension (in the first block) not work in python2.6? 回答1: You can use