python-2.x

How to print output from a script in gui called in another Tkinter script?

放肆的年华 提交于 2019-11-30 07:42:20
I have tried using several different similar solutions that I have found online, but none seem to quite do what I am aiming for. I want to call an external script (helloworld.py) into my tkinter gui. I want this called script (helloworld.py) to execute all the functions that are contained in it upon a button press in the gui and print the resulting outputs into the gui, not the console. I have found some solutions which will print the output to the console, but I am unable to get it to display in the gui. Any solutions that I have found that print to the gui do not work when I try to get the

Python object cache

不打扰是莪最后的温柔 提交于 2019-11-30 07:35:20
问题 I tried a bit of code but it seems to cause issues: class Page: cache = [] """ Return cached object """ def __getCache(self, title): for o in Page.cache: if o.__searchTerm == title or o.title == title: return o return None """ Initilize the class and start processing """ def __init__(self, title, api=None): o = self.__getCache(title) if o: self = o return Page.cache.append(self) # Other init code self.__searchTerm = title self.title = self.someFunction(title) Then I try: a = Page('test') b =

isinstance() and issubclass() return conflicting results

你说的曾经没有我的故事 提交于 2019-11-30 07:21:33
问题 >>> class Hello: pass and >>> isinstance(Hello,object) True >>> issubclass(Hello,object) False >>> a = Hello() >>> isinstance(a,object) True How do you explain isinstance(Hello,object) returns True whilst issubclass(Hello,object) returns False 回答1: It's because you are using old-style classes so it doesn't derive from object . Try this instead: class Hello(object): pass >>> issubclass(Hello,object) True Old-style classes are deprecated and you shouldn't use them any more. In Python 3.x all

Getting ready to convert from Python 2.x to 3.x

左心房为你撑大大i 提交于 2019-11-30 07:01:32
As we all know by now (I hope), Python 3 is slowly beginning to replace Python 2.x. Of course it will be many MANY years before most of the existing code is finally ported, but there are things we can do right now in our version 2.x code to make the switch easier. Obviously taking a look at what's new in 3.x will be helpful, but what are some things we can do right now to make the upcoming conversion more painless (as well as make it easier to output updates to concurrent versions if needed)? I'm specifically thinking about lines we can start our scripts off with that will make earlier

How do I properly override __setattr__ and __getattribute__ on new-style classes in Python?

给你一囗甜甜゛ 提交于 2019-11-30 06:26:00
问题 I want to override my Python class's __getattribute__ and __setattr__ methods. My use case is the usual one: I have a few special names that I want to handle, and I want the default behavior for anything else. For __getattribute__ , it seems that I can request the default behavior simply by raising AttributeError . However, how can I achieve the same in __setattr__ ? Here is a trivial example, implementing a class with immutable fields "A", "B", and "C". class ABCImmutable(SomeSuperclass):

dicts are not orderable in python 3?

醉酒当歌 提交于 2019-11-30 05:49:09
问题 Why are dicts orderable in python2, but not in python3? I can't find it anywhere in the documentation. Python 3.3.4 (default, Feb 11 2014, 16:14:21) >>> sorted([{'a':'a'},{'b':'b'}]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: dict() < dict() vs. Python 2.7.6 (default, Feb 26 2014, 12:01:28) >>> sorted([{'a':'a'},{'b':'b'}]) [{'a': 'a'}, {'b': 'b'} 回答1: Python 2 uses an undocumented ordering, implemented as a .__cmp__() special method.

Why can't I pass self as a named argument to an instance method in Python?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 05:38:10
问题 This works: >>> def bar(x, y): ... print x, y ... >>> bar(y=3, x=1) 1 3 And this works: >>> class Foo(object): ... def bar(self, x, y): ... print x, y ... >>> z = Foo() >>> z.bar(y=3, x=1) 1 3 And even this works: >>> Foo.bar(z, y=3, x=1) 1 3 But why doesn't this work in Python 2.x? >>> Foo.bar(self=z, y=3, x=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead) This

Check for mutability in Python?

本小妞迷上赌 提交于 2019-11-30 04:23:34
Consider this code : a = {...} # a is an dict with arbitrary contents b = a.copy() What role does mutability play in the keys and values of the dicts? How do I ensure changes to keys or values of one dict are not reflected in the other? How does this relate to the hashable constraint of the dict keys? Are there any differences in behaviour between Python 2.x and Python 3.x? How do I check if a type is mutable in Python? 1) Keys must not be mutable, unless you have a user-defined class that is hashable but also mutable. That's all that's forced upon you. However, using a hashable, mutable

reorder byte order in hex string (python)

我的未来我决定 提交于 2019-11-30 03:55:54
问题 I want to build a small formatter in python giving me back the numeric values embedded in lines of hex strings. It is a central part of my formatter and should be reasonable fast to format more than 100 lines/sec (each line about ~100 chars). The code below should give an example where I'm currently blocked. 'data_string_in_orig' shows the given input format. It has to be byte swapped for each word. The swap from 'data_string_in_orig' to 'data_string_in_swapped' is needed. In the end I need

Why does a classmethod's super need a second argument?

瘦欲@ 提交于 2019-11-30 03:23:17
问题 This works as expected: >>> class Foo(object): ... @classmethod ... def hello(cls): ... print 'hello, foo' ... >>> class Bar(Foo): ... @classmethod ... def hello(cls): ... print 'hello, bar' ... super(Bar, cls).hello() ... >>> b = Bar() >>> b.hello() hello, bar hello, foo I can also call the base class explicitly: >>> class Bar(Foo): ... @classmethod ... def hello(cls): ... print 'hello, bar' ... Foo.hello() ... >>> b = Bar() >>> b.hello() hello, bar hello, foo I was wondering why I can't