python-2.6

inspect.getmembers in order?

谁说我不能喝 提交于 2019-11-29 16:28:45
问题 inspect.getmembers(object[, predicate]) Return all the members of an object in a list of (name, value) pairs sorted by name. I want to use this method, but I don't want the members to be sorted. I want them returned in the same order they were defined. Is there an alternative to this method? Use case: Creating a form like so: class RegisterForm(Form): username = Field(model_field='username', filters=validators.minlength(3)) password1 = Field(model_field='password', widget=widgets

Custom JSON encoder in Python 2.7 to insert plain JavaScript code

大憨熊 提交于 2019-11-29 15:12:21
I'm trying to upgrade a portion of Python 2.6 code to Python 2.7. This code uses the json module to produce some JavaScript (not JSON-compliant), which is then inserted into the rest of a script. The general idea is to be able to insert code or refer to variables that are defined elsewhere: it's not intended to be used as JSON data, but JavaScript code. Here is the custom encoder that works in Python 2.6: import json class RawJavaScriptText: def __init__(self, jstext): self._jstext = jstext def get_jstext(self): return self._jstext class RawJsJSONEncoder(json.JSONEncoder): def _iterencode

AttributeError: 'module' object has no attribute 'pydebug'

风格不统一 提交于 2019-11-29 14:51:40
When trying to run a python script, I get the error AttributeError: 'module' object has no attribute 'pydebug' . I am using Python 2.6. Full error: File "/lib/python2.6/distutils/sysconfig.py", line 238, in get_makefile_filename return os.path.join(lib_dir, "config" + (sys.pydebug and "_d" or ""), "Makefile") AttributeError: 'module' object has no attribute 'pydebug' I think whatever you are trying to run is expecting to be used with a special debug build of python. sys.pydebug is not normally found on the standard release of the sys module, and I believe it would be there if you built a debug

Regular expression to match comma separated list of key=value where value can contain commas

谁都会走 提交于 2019-11-29 13:31:46
I have a naive "parser" that simply does something like: [x.split('=') for x in mystring.split(',')] However mystring can be something like 'foo=bar,breakfast=spam,eggs' Obviously, The naive splitter will just not do it. I am limited to Python 2.6 standard library for this, So for example pyparsing can not be used. Expected output is [('foo', 'bar'), ('breakfast', 'spam,eggs')] I'm trying to do this with regex, but am facing the following problems: My First attempt r'([a-z_]+)=(.+),?' Gave me [('foo', 'bar,breakfast=spam,eggs')] Obviously, Making .+ non-greedy does not solve the problem. So, I

In a Python object, how can I see a list of properties that have been defined with the @property decorator?

拥有回忆 提交于 2019-11-29 12:25:54
问题 I can see first-class member variables using self.__dict__ , but I'd like also to see a dictionary of properties, as defined with the @property decorator. How can I do this? 回答1: You could add a function to your class that looks something like this: def properties(self): class_items = self.__class__.__dict__.iteritems() return dict((k, getattr(self, k)) for k, v in class_items if isinstance(v, property)) This looks for any properties in the class and then creates a dictionary with an entry

Python 2.6 on Windows: how to terminate subprocess.Popen with “shell=True” argument?

我与影子孤独终老i 提交于 2019-11-29 10:31:36
Is there a way to terminate a process started with the subprocess.Popen class with the "shell" argument set to "True"? In the working minimal example below (uses wxPython) you can open and terminate a Notepad process happily, however if you change the Popen "shell" argument to "True" then the Notepad process doesn't terminate. import wx import threading import subprocess class MainWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.main_panel = wx.Panel(self, -1) self.border_sizer = wx.BoxSizer() self.process_button = wx.Button(self.main

Length-wise-sorted list but, same length in alphabetical-order in a step

 ̄綄美尐妖づ 提交于 2019-11-29 07:21:06
My Python List of string is something like x but long enough: x = ['aaa','ab','aa','c','a','b','ba'] I wants to sort this list as: ['a', 'b', 'c', 'aa', 'ab', 'ba', 'aaa'] and I did as follows in two steps: >>> x.sort() >>> x.sort(key=len) >>> x ['a', 'b', 'c', 'aa', 'ab', 'ba', 'aaa'] But I need in one-step: I also tied using lambda function ( taken help ): >>> x.sort(key=lambda item: (item, len(item))) >>> x ['a', 'aa', 'aaa', 'ab', 'b', 'ba', 'c'] But not as I desired: Is it possible in one-step? Please me. My Python: ~$ python --version Python 2.6.6 Gareth Latty You got the order of the

How to install python2.6-devel package under CentOs 5

两盒软妹~` 提交于 2019-11-29 07:06:09
I need to install mysql-python under python2.6. mysql-python package needs python2.6-devel package that depends on the libpython2.6.so.1.0(64bit) I found on the net some python2.6-devel packages, but can't find libpython2.6 Server architecture is x86_64. Maybe someone have this lib, or know where i can find it. Thanks for help) I have the same issue and this wonderful link solved it for me... http://blog.milford.io/2010/08/new-method-for-installing-python-2-6-4-with-mysql-python-on-centos-5-5/ I followed some of the steps out of order because I was a little antsy and got some interesting error

Python 2.6 TreeMap/SortedDictionary?

蓝咒 提交于 2019-11-29 05:40:17
Is there a built-in sorted dictionary implementation in Python 2.6, or are hashtables the only kind? Clarifications: I'm asking about sorted dictionarys, not ordered dictionaries! James Hurford I think the answer here is no. There is a Treemap but it isn't in the python standard library. http://pypi.python.org/pypi/treemap/ I think to those who don't like my answer, may think it's wrong due to a recent update. Please note this is for Python 2.6, not 2.7 or Python 3 Please add the correct answer if you still think it's wrong, or unhelpful, or at least give a reason why you think it's a bad

Difference between python - getmtime() and getctime() in unix system

≡放荡痞女 提交于 2019-11-29 05:34:29
Can someone please specify what is the difference between os.path.getmtime(path) and os.path.getctime(path) in unix systems . As per the defnition in python docs: os.path.getmtime(path) Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise os.error if the file does not exist or is inaccessible. os.path.getctime(path) Return the system’s ctime which, on some systems (like Unix) is the time of the last change, and, on others (like Windows), is the creation time for path. The return value is a number