title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
MVC and django fundamentals | 364,015 | <p>Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a fr... | 8 | 2008-12-12T20:22:29Z | 364,074 | <p>"data / architectural models so that we can get a better idea of the approach we should use using a framework we've never made use of before"</p>
<p>Django imposes best practices on you. You don't have a lot of choices and can't make a lot of mistakes.</p>
<p>MVC (while a noble aspiration) is implemented as follo... | 15 | 2008-12-12T20:37:36Z | [
"python",
"django",
"django-models",
"django-templates"
] |
MVC and django fundamentals | 364,015 | <p>Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a fr... | 8 | 2008-12-12T20:22:29Z | 364,157 | <p><a href="http://martinfowler.com/eaaDev/uiArchs.html" rel="nofollow">This link</a> will explain the MVC and the likes. <a href="http://djangoproject.com" rel="nofollow">This link</a> has all the django related documentation, starter kits etc.</p>
| 1 | 2008-12-12T21:11:13Z | [
"python",
"django",
"django-models",
"django-templates"
] |
MVC and django fundamentals | 364,015 | <p>Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a fr... | 8 | 2008-12-12T20:22:29Z | 364,161 | <p>first, forget all MVC mantra. it's important to have a good layered structure, but MVC (as defined originally) isn't one, it was a <strong>modular</strong> structure, where each <strong>GUI</strong> module is split in these tree submodules. nothing to use on the web here.</p>
<p>in web development, it really pays... | 5 | 2008-12-12T21:12:11Z | [
"python",
"django",
"django-models",
"django-templates"
] |
MVC and django fundamentals | 364,015 | <p>Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a fr... | 8 | 2008-12-12T20:22:29Z | 364,433 | <p>To understand django fundementals and the django take on MVC, consult the following:
<a href="http://www.djangobook.com/" rel="nofollow">http://www.djangobook.com/</a></p>
<p>As a starting point to getting your hands dirty with ...
"...trying to find some comparable data / architectural models"</p>
<p>Here is a q... | 0 | 2008-12-12T22:56:05Z | [
"python",
"django",
"django-models",
"django-templates"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 364,338 | <p>It's XML, not Python, but look at <a href="http://www.openlaszlo.org/" rel="nofollow">Open Laszlo</a></p>
| -1 | 2008-12-12T22:14:46Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 364,340 | <p>You should look into Qt, which you can use from Python using the excellent PyQt interface (why they didn't name it QtPy --- cutiepie, get it ? --- I will never understand).</p>
<p>With Qt, you can have the choice of constructing your GUI's programmatically (which you don't want), or using XML markup. This XML file ... | 3 | 2008-12-12T22:15:19Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 364,363 | <p>windows?</p>
<p>you can use the WinForms editor in Visual Studio and then talk to the assembly from IronPython.</p>
| -2 | 2008-12-12T22:23:33Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 364,393 | <p>How about wxPython? I'm just now beginning to work with it, but there's a tool -- XRC Resource Editor -- that allows you to assemble your GUI, which is then written to an XML file. As I understand it, your Python application loads the XML file, rather than having a whole bunch of GUI-layout code mixed in with your... | 2 | 2008-12-12T22:36:16Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 364,398 | <p>If you use <a href="http://www.pygtk.org/" rel="nofollow">GTK</a>, you can use <a href="http://glade.gnome.org/" rel="nofollow">Glade</a>, which is an XML file. </p>
| 1 | 2008-12-12T22:38:32Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 768,465 | <p>You can try Mozilla's XUL. It supports Python via XPCOM.</p>
<p>See this project: <a href="http://pyxpcomext.mozdev.org/no_wrap/tutorials/pyxulrunner/python_xulrunner_about.html">pyxpcomext</a></p>
<p>XUL isn't compiled, it is packaged and loaded at runtime. Firefox and many other great applications use it, but mo... | 7 | 2009-04-20T14:18:21Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 768,701 | <p>As a GUI programmer who has gained some <strong>experience</strong>, you should probably just roll your own sweet little toolkit for automating tasks you find yourself doing over and over again.</p>
| 0 | 2009-04-20T15:09:56Z | [
"python",
"user-interface",
"markup"
] |
Markup-based GUI for python | 364,327 | <p>I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them. </p>
<p>I think GUI design should be done in a separate text... | 7 | 2008-12-12T22:10:08Z | 768,777 | <p>If you choose a language like Tcl or Python and Tk for your application development it becomes fairly trivial to write your own DSL for describing the interface. You can, for instance, write a DSL that lets you create menus like this:</p>
<pre><code>menubar {
File => {
Open => cmd.open
Sav... | 2 | 2009-04-20T15:26:43Z | [
"python",
"user-interface",
"markup"
] |
Threaded code on mod_python | 364,358 | <p>I have written a Django app that makes use of Python threading to create a web spider, the spider operates as a series of threads to check links.</p>
<p>When I run this app using the django test server (built in), the app runs fine and the threads seem to start and stop on time.</p>
<p>However, running the app on ... | 1 | 2008-12-12T22:21:53Z | 364,427 | <p>Most likely, you are missing the creation of new processes. Apache will not run in a single process, but fork new processes for requests every now and then (depending on a <a href="http://httpd.apache.org/docs/2.2/mod/worker.html" rel="nofollow">dozen or so configuration parameters</a>). If you run django in each pr... | 3 | 2008-12-12T22:51:37Z | [
"python",
"django",
"multithreading",
"apache"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 364,494 | <p>Maybe try Aptana (<a href="http://www.aptana.com/" rel="nofollow">http://www.aptana.com/</a>) - you can then plug in as many extra modules as you require.</p>
| 3 | 2008-12-12T23:31:45Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 364,502 | <p>PDT 2.0 is vey good Eclipse <a href="http://www.eclipse.org/pdt/" rel="nofollow">PHP Ide</a> . It is created with cooperation with Zend, so there a lot of featuers from Zend Studio.
I also use it with Subeclipse(for SVN) and Target Management (for work on FTP remotely).
I will also advice to use Aptana plugins fot ... | 1 | 2008-12-12T23:40:19Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 364,515 | <p>I develop Python applications at work and find Eclipse Classic and Eclipse for Java Devs a good fit because I don't do any plugin or Java EE Stuff. We use Eclipse for Java, Python, and NSIS (nullsoft installer) scripting. </p>
<p>The Python development I do requires the pydev plugin (see: <a href="http://pydev.sour... | 1 | 2008-12-12T23:52:43Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 364,517 | <p>PyDev is pretty decent as I'm sure you know. It can fit on top of all the Eclipse distributions (provided they meet the minimum version requirements). If you're doing webdev stuff, you'll probably find the closest fit with Aptana. </p>
<p>That said, I find Aptana hideously clunky when compared to a decent text edit... | 0 | 2008-12-12T23:54:48Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 367,322 | <p><a href="http://www.epic-ide.org/" rel="nofollow">EPIC</a> is the only Eclipse Perl plugin I know for Perl. </p>
<p>The integration is okay. Offers a graphical debugger, but watch out for inspecting data that contains cycles, as the perl exec could just go into an infinite loop</p>
| 0 | 2008-12-15T02:11:48Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 6,481,753 | <p>I develop in PHP, python, C(python modules), SQL and JS/HTML/CSS all on eclipse. I do this
by installing PDT, CDT, pydev and SQL tools onto the eclipse-platform, and then using different workspaces for mixed projects. </p>
<p>Two workspaces to be specific, one for PHP web development and another for Python/C. I do ... | 0 | 2011-06-26T02:17:07Z | [
"php",
"python",
"perl",
"eclipse"
] |
Which Eclipse distribution is good for web development using Python, PHP, or Perl? | 364,486 | <p>I'd like to try out Eclipse, but I'm a bit baffled with all the different distributions of it. I mainly program in Python doing web development, but I also need to maintain PHP and Perl apps. It looks like EasyEclipse is a bit behind. Should I just grab the base Eclipse and start loading plug-ins?</p>
| 4 | 2008-12-12T23:26:53Z | 18,023,885 | <p>I use the javascript eclipse helios and added pydev plugin to it for django support it seems to do everything I need.</p>
| 0 | 2013-08-02T18:36:53Z | [
"php",
"python",
"perl",
"eclipse"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 364,521 | <p>Use the <a href="http://pydoc.org/2.5.1/__builtin__.html#-sorted"><code>sorted()</code></a> function:</p>
<pre><code>return sorted(dict.iteritems())
</code></pre>
<p>If you want an actual iterator over the sorted results, since <code>sorted()</code> returns a list, use:</p>
<pre><code>return iter(sorted(dict.iter... | 63 | 2008-12-12T23:57:53Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 364,524 | <p>Greg's answer is right. Note that in Python 3.0 you'll have to do </p>
<pre><code>sorted(dict.items())
</code></pre>
<p>as <code>iteritems</code> will be gone.</p>
| 25 | 2008-12-13T00:00:20Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 364,588 | <p>A dict's keys are stored in a hashtable so that is their 'natural order', i.e. psuedo-random. Any other ordering is a concept of the consumer of the dict.</p>
<p>sorted() always returns a list, not a dict. If you pass it a dict.items() (which produces a list of tuples), it will return a list of tuples [(k1,v1), (k2... | 35 | 2008-12-13T00:44:32Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 364,599 | <p>Haven't tested this very extensively, but works in Python 2.5.2.</p>
<pre><code>>>> d = {"x":2, "h":15, "a":2222}
>>> it = iter(sorted(d.iteritems()))
>>> it.next()
('a', 2222)
>>> it.next()
('h', 15)
>>> it.next()
('x', 2)
>>>
</code></pre>
| 100 | 2008-12-13T00:49:38Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 364,627 | <p>sorted returns a list, hence your error when you try to iterate over it,
but because you can't order a dict you will have to deal with a list.</p>
<p>I have no idea what the larger context of your code is, but you could try adding an
iterator to the resulting list.
like this maybe?:</p>
<pre><code>return iter(sor... | 3 | 2008-12-13T01:27:02Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 8,444,119 | <p>If you want to sort by the order that items were inserted instead of of the order of the keys, you should have a look to Python's <a href="http://docs.python.org/py3k/library/collections.html?highlight=collections.ordereddict#collections.OrderedDict" rel="nofollow">collections.OrderedDict</a>. (Python 3 only)</p>
| 3 | 2011-12-09T10:25:31Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 11,080,546 | <p>Assuming you are using CPython 2.x and have a large dictionary mydict, then using sorted(mydict) is going to be slow because sorted builds a sorted list of the keys of mydict.</p>
<p>In that case you might want to look at my ordereddict package which includes a C implementation of <code>sorteddict</code> in C. Espe... | 2 | 2012-06-18T09:57:20Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 15,257,786 | <p>In general, one may sort a dict like so:</p>
<pre><code>for k in sorted(d):
print k, d[k]
</code></pre>
<p>For the specific case in the question, having a "drop in replacement" for d.iteritems(), add a function like:</p>
<pre><code>def sortdict(d, **opts):
# **opts so any currently supported sorted() opti... | 5 | 2013-03-06T20:53:01Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 15,940,903 | <pre><code>>>> import heapq
>>> d = {"c": 2, "b": 9, "a": 4, "d": 8}
>>> def iter_sorted(d):
keys = list(d)
heapq.heapify(keys) # Transforms to heap in O(N) time
while keys:
k = heapq.heappop(keys) # takes O(log n) time
yield (k, d[k])
>>... | 3 | 2013-04-11T04:39:35Z | [
"python",
"dictionary"
] |
In Python, how do I iterate over a dictionary in sorted order? | 364,519 | <p>There's an existing function that ends in:</p>
<pre><code>return dict.iteritems()
</code></pre>
<p>that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items in sorted order. How do I do that?</p>
| 136 | 2008-12-12T23:57:05Z | 17,532,913 | <p>You can now use <code>OrderedDict</code> in Python 2.7 as well:</p>
<pre><code>>>> from collections import OrderedDict
>>> d = OrderedDict([('first', 1),
... ('second', 2),
... ('third', 3)])
>>> d.items()
[('first', 1), ('second', 2), ('third', 3)]
</cod... | 5 | 2013-07-08T17:49:52Z | [
"python",
"dictionary"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 364,638 | <pre><code>for i in xrange(len(testlist)):
if testlist[i] == 1:
print i
</code></pre>
<p>xrange instead of range as requested (see comments).</p>
| 8 | 2008-12-13T01:33:30Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 364,641 | <p>Use enumerate:</p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for position, item in enumerate(testlist):
if item == 1:
print position
</code></pre>
| 36 | 2008-12-13T01:37:46Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 364,742 | <p>If your list got large enough and you only expected to find the value in a sparse number of indices, consider that this code <em>could</em> execute much faster because you don't have to iterate every value in the list.</p>
<pre><code>lookingFor = 1
i = 0
index = 0
try:
while i < len(testlist):
index = test... | 0 | 2008-12-13T03:14:50Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 364,769 | <p>Hmmm. There was an answer with a list comprehension here, but it's disappeared.</p>
<p>Here:</p>
<pre><code> [i for i,x in enumerate(testlist) if x == 1]
</code></pre>
<p>Example:</p>
<pre><code>>>> testlist
[1, 2, 3, 5, 3, 1, 2, 1, 6]
>>> [i for i,x in enumerate(testlist) if x == 1]
[0, 5, 7]... | 168 | 2008-12-13T03:28:26Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 1,799,718 | <pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for id, value in enumerate(testlist):
if id == 1:
print testlist[id]
</code></pre>
<p>I guess that it's exacly what you want. ;-)
'id' will be always the index of the values on the list.</p>
| -1 | 2009-11-25T20:24:35Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 10,125,837 | <p>Here is another way to do this:</p>
<pre><code>try:
id = testlist.index('1')
print testlist[id]
except:
print "Not Found"
</code></pre>
| 3 | 2012-04-12T14:36:57Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 10,266,829 | <p>What about the following?</p>
<pre><code>print testlist.index(element)
</code></pre>
<p>If you are not sure whether the element to look for is actually in the list, you can add a preliminary check, like</p>
<pre><code>if element in testlist:
print testlist.index(element)
</code></pre>
<p>or</p>
<pre><code>p... | 82 | 2012-04-22T09:35:15Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 12,731,878 | <pre><code>[x for x in range(len(testlist)) if testlist[x]==1]
</code></pre>
| 1 | 2012-10-04T16:41:33Z | [
"python",
"list"
] |
How to get item's position in a list? | 364,621 | <p>I am iterating over a list and I want to print out the index of the item if it meets a certain condition. How would I do this?</p>
<p>Example: </p>
<pre><code>testlist = [1,2,3,5,3,1,2,1,6]
for item in testlist:
if item == 1:
print position
</code></pre>
| 103 | 2008-12-13T01:20:32Z | 17,224,675 | <p>I think that it might be useful to use the curselection() method from thte Tkinter library:</p>
<pre><code>from Tkinter import *
listbox.curselection()
</code></pre>
<p>This method works on Tkinter listbox widgets, so you'll need to construct one of them instead of a list.</p>
<p>This will return a position like... | 0 | 2013-06-20T22:06:41Z | [
"python",
"list"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 364,691 | <p>A quick but hackish way is to have a wrapper script for python.</p>
<pre><code>cat > $HOME/bin/mypython << EOF
#!/usr/bin/python
import os
os.path = ['/list/of/paths/you/want']
EOF
</code></pre>
<p>and then start all your XCode scripts with</p>
<pre><code>#!/Users/you/bin/mypython
</code></pre>
| 1 | 2008-12-13T02:16:06Z | [
"python",
"xcode",
"osx",
"path"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 364,746 | <p>Just add the paths to sys,path.</p>
<pre><code>>>> import sys
>>> sys.path
['', ... lots of stuff deleted....]
>>> for i in sys.path:
... print i
...
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/li... | 1 | 2008-12-13T03:18:11Z | [
"python",
"xcode",
"osx",
"path"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 364,759 | <p>Forgive me if my answer seems ignorant, I'm not totally familiar with Mac and I also may have misunderstood your question.</p>
<p>On Windows and Linux, when I want to refer to a script I've written, I set the PYTHONPATH environment variable. It is what os.sys.path gets its values from, if I remember correctly.</p>
... | 0 | 2008-12-13T03:22:09Z | [
"python",
"xcode",
"osx",
"path"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 364,835 | <p>On the mac, environment variables in your .profile aren't visible to applications outside of the terminal. </p>
<p>If you want an environment variable (like PATH, PYTHONPATH, etc) to be available to xcode apps, you should add it to a new plist file that you create at ~/.MacOSX/environment.plist.</p>
<p>See the <a ... | 5 | 2008-12-13T04:23:46Z | [
"python",
"xcode",
"osx",
"path"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 1,685,231 | <p>I tend to use <a href="http://docs.python.org/install/index.html" rel="nofollow">pth</a> files. From the docs.</p>
<blockquote>
<p>The most convenient way is to add a
path configuration file to a directory
thatâs already on Pythonâs path,
usually to the .../site-packages/
directory. Path configuratio... | 1 | 2009-11-06T03:28:58Z | [
"python",
"xcode",
"osx",
"path"
] |
How do I use my standard python path when running python scripts from xcode macros | 364,655 | <p>I'm trying to run Python scripts using Xcode's User Scripts menu.</p>
<p>The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which mean... | 3 | 2008-12-13T01:50:38Z | 8,054,281 | <p>Not sure if Xcode counts as launching the script through Finder or not, but if it does, apparently Finder doesn't read <code>.profile</code> or <code>.cshrc</code> files when it starts for a user the way Terminal does. </p>
<p>If your question is unanswered still, check out Apple's knowledge base: <a href="http://d... | 0 | 2011-11-08T17:06:47Z | [
"python",
"xcode",
"osx",
"path"
] |
generator comprehension | 364,802 | <p>What does generator comprehension do? How does it work? I couldn't find a tutorial about it.</p>
| 37 | 2008-12-13T03:55:44Z | 364,816 | <p>A generator comprehension is the lazy version of a list comprehension.</p>
<p>It is just like a list comprehension except that it returns an iterator instead of the list ie an object with a next() method that will yield the next element.</p>
<p>If you are not familiar with list comprehensions see <a href="http://d... | 10 | 2008-12-13T04:08:34Z | [
"python"
] |
generator comprehension | 364,802 | <p>What does generator comprehension do? How does it work? I couldn't find a tutorial about it.</p>
| 37 | 2008-12-13T03:55:44Z | 364,818 | <p>List/generator comprehension is a construct which you can use to create a new list/generator from an existing one.</p>
<p>Let's say you want to generate the list of squares of each number from 1 to 10. You can do this in Python:</p>
<pre><code>>>> [x**2 for x in range(1,11)]
[1, 4, 9, 16, 25, 36, 49, 64, ... | 2 | 2008-12-13T04:09:29Z | [
"python"
] |
generator comprehension | 364,802 | <p>What does generator comprehension do? How does it work? I couldn't find a tutorial about it.</p>
| 37 | 2008-12-13T03:55:44Z | 364,824 | <p>Do you understand list comprehensions? If so, a generator expression is like a list comprehension, but instead of finding all the items you're interested and packing them into list, it waits, and yields each item out of the expression, one by one.</p>
<pre><code>>>> my_list = [1, 3, 5, 9, 2, 6]
>>>... | 76 | 2008-12-13T04:14:04Z | [
"python"
] |
generator comprehension | 364,802 | <p>What does generator comprehension do? How does it work? I couldn't find a tutorial about it.</p>
| 37 | 2008-12-13T03:55:44Z | 20,820,350 | <p>Generator comprehension is an easy way of creating generators with a certain structure. Lets say you want a <code>generator</code> that outputs one by one all the even numbers in <code>your_list</code>. If you create it by using the function style it would be like this:</p>
<pre><code>def allEvens( L ):
for num... | 0 | 2013-12-28T23:06:29Z | [
"python"
] |
generator comprehension | 364,802 | <p>What does generator comprehension do? How does it work? I couldn't find a tutorial about it.</p>
| 37 | 2008-12-13T03:55:44Z | 27,446,011 | <p>Generator comprehension is an approach to create iterables, something like a cursor which moves on a resource. If you know mysql cursor or mongodb cursor, you may be aware of that the whole actual data never gets loaded into the memory at once, but one at a time. Your cursor moves back and forth, but there is alway... | 0 | 2014-12-12T14:42:21Z | [
"python"
] |
Detect windows logout in Python | 365,058 | <p>How can I detect, or be notified, when windows is logging out in python?</p>
<p>Edit:
Martin v. Löwis' answer is good, and works for a full logout but it does not work for a 'fast user switching' event like pressing win+L which is what I really need it for.
<br /><br />Edit: im not using a gui this is running as a... | 5 | 2008-12-13T09:55:16Z | 365,096 | <p>In a console application, you can use win32api.SetConsoleCtrlHandler and look for CTRL_LOGOFF_EVENT. In a GUI application, you need a window open and wait for the WM_QUERYENDSESSION message. How precisely that works (and if it works at all) depends on your GUI library.</p>
| 3 | 2008-12-13T10:50:02Z | [
"python",
"winapi"
] |
Detect windows logout in Python | 365,058 | <p>How can I detect, or be notified, when windows is logging out in python?</p>
<p>Edit:
Martin v. Löwis' answer is good, and works for a full logout but it does not work for a 'fast user switching' event like pressing win+L which is what I really need it for.
<br /><br />Edit: im not using a gui this is running as a... | 5 | 2008-12-13T09:55:16Z | 365,232 | <p>You can detect fast user switching events using the Terminal Services API, which you can access from Python using the <code>win32ts</code> module from <a href="http://sourceforge.net/projects/pywin32/">pywin32</a>. In a GUI application, call <a href="http://msdn.microsoft.com/en-us/library/aa383841.aspx">WTSRegister... | 6 | 2008-12-13T13:53:12Z | [
"python",
"winapi"
] |
Can you change a field label in the Django Admin application? | 365,082 | <p>As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?</p>
| 19 | 2008-12-13T10:30:41Z | 365,236 | <p>the <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#verbose-field-names">verbose name</a> of the field is the (optional) first parameter at field construction.</p>
| 37 | 2008-12-13T13:59:40Z | [
"python",
"django",
"django-admin",
"django-forms"
] |
Can you change a field label in the Django Admin application? | 365,082 | <p>As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?</p>
| 19 | 2008-12-13T10:30:41Z | 365,431 | <p>Building on Javier's answer; if you need one label in forms (on the front-end) and another label on admin it is best to set internal (admin) one in the model and overwrite it on forms. Admin will of course use the label in the model field automatically.</p>
| 4 | 2008-12-13T16:56:52Z | [
"python",
"django",
"django-admin",
"django-forms"
] |
Can you change a field label in the Django Admin application? | 365,082 | <p>As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?</p>
| 19 | 2008-12-13T10:30:41Z | 14,743,532 | <p>If your field is a property (a method) then you should use short_description:</p>
<pre><code>class Person(models.Model):
...
def address_report(self, instance):
...
# short_description functions like a model field's verbose_name
address_report.short_description = "Address"
</code></pre>
| 12 | 2013-02-07T04:25:28Z | [
"python",
"django",
"django-admin",
"django-forms"
] |
Can you change a field label in the Django Admin application? | 365,082 | <p>As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?</p>
| 19 | 2008-12-13T10:30:41Z | 24,121,475 | <p>As Javier suggested you can use verbose name in your fields in model.py. Example as below,</p>
<pre><code>class Employee(models.Model):
name = models.CharField(max_length = 100)
dob = models.DateField('Date Of Birth')
doj = models.DateField(verbose_name='Date Of Joining')
mobile=models.IntegerFi... | 2 | 2014-06-09T13:36:37Z | [
"python",
"django",
"django-admin",
"django-forms"
] |
Can you change a field label in the Django Admin application? | 365,082 | <p>As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?</p>
| 19 | 2008-12-13T10:30:41Z | 40,016,382 | <p><strong>Meta options</strong>¶</p>
<p>Give your model metadata by using an inner class Meta, like so:</p>
<pre><code>from django.db import models
class MyClassName(models.Model):
class Meta:
verbose_name = "Question"
verbose_name_plural = "Questions"
</code></pre>
<p>human-readable singular... | 0 | 2016-10-13T08:51:27Z | [
"python",
"django",
"django-admin",
"django-forms"
] |
Cross platform keylogger | 365,110 | <p>I'm looking for ways to watch mouse and keyboard events on Windows, Linux and Mac from Python.</p>
<p>My application is a time tracker. I'm not looking into the event, I just record the time when it happens. If there are no events for a certain time, say 10 minutes, I assume that the user has left and stop the curr... | 9 | 2008-12-13T11:10:29Z | 365,225 | <p>There are couple of open source apps that might give you some pointers:</p>
<ul>
<li><a href="http://sourceforge.net/p/pykeylogger/wiki/Main_Page/" rel="nofollow">PyKeylogger</a> is python keylogger for windows and linux</li>
<li><a href="http://code.google.com/p/logkext/" rel="nofollow">logKext</a> is a c++ keylog... | 10 | 2008-12-13T13:46:12Z | [
"python",
"cross-platform",
"time-management",
"keylogger"
] |
Cross platform keylogger | 365,110 | <p>I'm looking for ways to watch mouse and keyboard events on Windows, Linux and Mac from Python.</p>
<p>My application is a time tracker. I'm not looking into the event, I just record the time when it happens. If there are no events for a certain time, say 10 minutes, I assume that the user has left and stop the curr... | 9 | 2008-12-13T11:10:29Z | 2,074,117 | <p>There's a great article on <b>Writing Linux Kernel Keyloggers</b> <br>
<a href="http://www.phrack.com/issues.html?issue=59&id=14#article">http://www.phrack.com/issues.html?issue=59&id=14#article</a></p>
<p>If you are attempting to run a honeypot, then definitely give Sebek a try:<br>
<a href="https://projec... | 7 | 2010-01-15T19:16:24Z | [
"python",
"cross-platform",
"time-management",
"keylogger"
] |
run a function in another function in N times | 365,384 | <p>I have ask this kind of question before, but it seems my previous question is a bit misleading due to my poor English. I'm asking again to make clear. I am really confused about it. Thanks in advance.</p>
<p>Suppose I have a function A for generating the state of a cell in a certain rule, and I have another functio... | 0 | 2008-12-13T16:24:07Z | 365,404 | <pre><code>def 1st_funtion(a_matrixA)
#apply some rule on a_matrixA and return a new matrix(next state of the cell)
return new_matrix
def 2nd_funtion(a_matrixB,repeat_times)
for i in range(repeat_times):
a_matrixB = 1st_funtion(a_matrixB)
return a_matrixB
</code></pre>
| 2 | 2008-12-13T16:37:41Z | [
"python"
] |
How to truncate matrix using NumPy (Python) | 365,395 | <p>just a quick question, if I have a matrix has n rows and m columns, how can I cut off the 4 sides of the matrix and return a new matrix? (the new matrix would have n-2 rows m-2 columns).</p>
<p>Thanks in advance</p>
| 0 | 2008-12-13T16:30:16Z | 365,399 | <pre><code>a[1:-1, 1:-1]
</code></pre>
| 10 | 2008-12-13T16:32:17Z | [
"python",
"numpy"
] |
How to truncate matrix using NumPy (Python) | 365,395 | <p>just a quick question, if I have a matrix has n rows and m columns, how can I cut off the 4 sides of the matrix and return a new matrix? (the new matrix would have n-2 rows m-2 columns).</p>
<p>Thanks in advance</p>
| 0 | 2008-12-13T16:30:16Z | 365,983 | <p>A more general answer is:</p>
<pre><code>a[[slice(1, -1) for _ in a.shape]]
</code></pre>
| 3 | 2008-12-14T00:20:57Z | [
"python",
"numpy"
] |
best way to print data in columnar format? | 365,601 | <p>I am using Python to read in data in a user-unfriendly format and transform it into an easier-to-read format. The records I am outputting are usually going to be just a last name, first name, and room code. I</p>
<p>I would <em>like</em> to output a series of pages, each containing a contiguous subset of the tota... | 0 | 2008-12-13T19:33:09Z | 367,099 | <p>"If I knew for certain that the printable area of the paper would hold 20 records vertically and five horizontally"</p>
<p>You do know that.</p>
<p>You know the size of your paper. You know the size of your font. You can easily do the math.</p>
<p>"almost certainly limited to HTML..." doesn't make much sense. ... | 3 | 2008-12-14T22:39:53Z | [
"python",
"html",
"css",
"formatting"
] |
Tracking redirects and cookies with Python | 366,037 | <p>I would like to do be able to follow and track redirects and the cookies that are set by the different webpages with Python (a bit like the tamper plugin for Firefox).</p>
<p>So if website1 redirects to website2 which then redirects to website3, I would like to follow that and also see what cookies each website set... | 5 | 2008-12-14T01:26:04Z | 366,424 | <p>there are detailed turorial on this.
In <a href="http://diveintopython.net/http_web_services/redirects.html" rel="nofollow">dive into python</a> and in <a href="http://www.voidspace.org.uk/python/articles/urllib2.shtml" rel="nofollow">voidspace</a>. The short version is that urllib2 provide handlers (That you can ov... | 2 | 2008-12-14T11:25:19Z | [
"python",
"redirect",
"urllib2"
] |
how to browse to a external url from turbogears/cherrypy application? | 366,421 | <p>I am writing a tinyurl clone to learn turbogears. I am wondering how do i redirect my browser to the external website (say www.yahoo.com) from my cherrypy/turbogears app?</p>
<p>I googled about it, but could not find much useful info.</p>
| 0 | 2008-12-14T11:23:21Z | 366,433 | <p>Just raise a HTTPRedirect exception, which lives in the cherrypy namespace. Like this:</p>
<pre><code>raise cherrypy.HTTPRedirect("http://www.yahoo.com")
</code></pre>
| 2 | 2008-12-14T11:32:32Z | [
"python",
"url-routing",
"cherrypy",
"turbogears"
] |
What is the pythonic way to avoid default parameters that are empty lists? | 366,422 | <p>Sometimes it seems natural to have a default parameter which is an empty list. Yet Python gives unexpected behavior in these situations. </p>
<p>If for example, I have a function:</p>
<pre><code>def myFunc(working_list = []):
working_list.append("a")
print working_list
</code></pre>
<p>The first time it ... | 34 | 2008-12-14T11:23:54Z | 366,430 | <pre><code>def myFunc(working_list=None):
if working_list is None:
working_list = []
working_list.append("a")
print working_list
</code></pre>
<p>is how I do it.</p>
| 68 | 2008-12-14T11:27:30Z | [
"python"
] |
What is the pythonic way to avoid default parameters that are empty lists? | 366,422 | <p>Sometimes it seems natural to have a default parameter which is an empty list. Yet Python gives unexpected behavior in these situations. </p>
<p>If for example, I have a function:</p>
<pre><code>def myFunc(working_list = []):
working_list.append("a")
print working_list
</code></pre>
<p>The first time it ... | 34 | 2008-12-14T11:23:54Z | 366,446 | <p>Not that it matters in this case, but you can use object identity to test for None:</p>
<pre><code>if working_list is None: working_list = []
</code></pre>
<p>You could also take advantage of how the boolean operator or is defined in python:</p>
<pre><code>working_list = working_list or []
</code></pre>
<p>Thoug... | 9 | 2008-12-14T11:43:57Z | [
"python"
] |
What is the pythonic way to avoid default parameters that are empty lists? | 366,422 | <p>Sometimes it seems natural to have a default parameter which is an empty list. Yet Python gives unexpected behavior in these situations. </p>
<p>If for example, I have a function:</p>
<pre><code>def myFunc(working_list = []):
working_list.append("a")
print working_list
</code></pre>
<p>The first time it ... | 34 | 2008-12-14T11:23:54Z | 367,774 | <p>I might be off-topic, but remember that if you just want to pass a variable number of arguments, the pythonic way is to pass a tuple <code>*args</code> or a dictionary <code>**kargs</code>. These are optional and are better than the syntax <code>myFunc([1, 2, 3])</code>.</p>
<p>If you want to pass a tuple:</p>
<pr... | 0 | 2008-12-15T08:33:39Z | [
"python"
] |
What is the pythonic way to avoid default parameters that are empty lists? | 366,422 | <p>Sometimes it seems natural to have a default parameter which is an empty list. Yet Python gives unexpected behavior in these situations. </p>
<p>If for example, I have a function:</p>
<pre><code>def myFunc(working_list = []):
working_list.append("a")
print working_list
</code></pre>
<p>The first time it ... | 34 | 2008-12-14T11:23:54Z | 2,021,717 | <p>If the intent of the function is to <em>modify</em> the parameter passed as <code>working_list</code>, see HenryR's answer (=None, check for None inside).</p>
<p>But if you didn't intend to mutate the argument, just use it as starting point for a list, you can simply copy it:</p>
<pre><code>def myFunc(starting_lis... | 7 | 2010-01-07T16:09:07Z | [
"python"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 366,745 | <p>You don't have to use threads. You can use another process to do the blocking work, for instance, maybe using the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module. If you want to share data structures between different parts of your program then <a href="http://twistedmat... | 3 | 2008-12-14T17:13:12Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 366,754 | <p>The only "safe" way to do this, in any language, is to use a secondary process to do that timeout-thing, otherwise you need to build your code in such a way that it will time out safely by itself, for instance by checking the time elapsed in a loop or similar. If changing the method isn't an option, a thread will no... | 4 | 2008-12-14T17:20:18Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 366,763 | <p>I'm not sure how cross-platform this might be, but using signals and alarm might be a good way of looking at this. With a little work you could make this completely generic as well and usable in any situation.</p>
<p><a href="http://docs.python.org/library/signal.html">http://docs.python.org/library/signal.html</a... | 29 | 2008-12-14T17:27:50Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 367,490 | <p>Here's a timeout function I think I found via google and it works for me.</p>
<p>From:
<a href="http://code.activestate.com/recipes/473878/" rel="nofollow">http://code.activestate.com/recipes/473878/</a></p>
<pre><code>def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
'''This function wi... | 2 | 2008-12-15T04:41:19Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 601,168 | <p>An improvement on @rik.the.vik's answer would be to use the <a href="http://www.python.org/dev/peps/pep-0343/"><code>with</code> statement</a> to give the timeout function some syntactic sugar:</p>
<pre><code>from __future__ import with_statement # Required in 2.5
import signal
from contextlib import contextmanager... | 46 | 2009-03-02T03:14:25Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 1,114,567 | <p>Doing this from within a signal handler is dangerous: you might be inside an exception handler at the time the exception is raised, and leave things in a broken state. For example,</p>
<pre><code>def function_with_enforced_timeout():
f = open_temporary_file()
try:
...
finally:
here()
unlink(f.filena... | 7 | 2009-07-11T20:30:32Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 26,664,130 | <p>Here's a Linux/OSX way to limit a function's running time. This is in case you don't want to use threads, and want your program to wait until the function ends, or the time limit expires.</p>
<pre><code>from multiprocessing import Process
from time import sleep
def f(time):
sleep(time)
def run_with_limited_t... | 13 | 2014-10-30T22:05:02Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 35,038,906 | <p>I would usually prefer using a contextmanager as suggested by @josh-lee</p>
<p>But in case someone is interested in having this implemented as a decorator, here's an alternative.</p>
<p>Here's how it would look like:</p>
<pre class="lang-py prettyprint-override"><code>import time
from timeout import timeout
clas... | 0 | 2016-01-27T13:38:35Z | [
"python",
"multithreading"
] |
How to limit execution time of a function call in Python | 366,682 | <p>There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.</p>
| 37 | 2008-12-14T16:20:24Z | 37,648,512 | <p>I prefer a context manager approach because it allows the execution of multiple python statements within a <code>with time_limit</code> statement. Because windows system does not have <code>SIGALARM</code>, a more portable and perhaps more straightforward method could be using a <code>Timer</code></p>
<pre><code>fr... | 0 | 2016-06-06T01:41:39Z | [
"python",
"multithreading"
] |
Bitwise subtraction in Python | 366,706 | <p>This is a follow-up to <a href="http://stackoverflow.com/questions/365522/what-is-the-best-way-to-add-two-numbers-without-using-the-operator">my question yesterday</a>:</p>
<p>CMS kindly provided this example of using bitwise operators to add two numbers in C:</p>
<pre><code>#include<stdio.h>
int add(int x,... | 9 | 2008-12-14T16:41:28Z | 366,734 | <p>Shifting negative numbers doesn't have consistent interpretation between python and C.</p>
| 2 | 2008-12-14T17:04:34Z | [
"python",
"low-level"
] |
Bitwise subtraction in Python | 366,706 | <p>This is a follow-up to <a href="http://stackoverflow.com/questions/365522/what-is-the-best-way-to-add-two-numbers-without-using-the-operator">my question yesterday</a>:</p>
<p>CMS kindly provided this example of using bitwise operators to add two numbers in C:</p>
<pre><code>#include<stdio.h>
int add(int x,... | 9 | 2008-12-14T16:41:28Z | 366,735 | <p>As I pointed out in my response to CMS' answer yesterday, left-shifting a negative number is undefined behavior in C so this isn't even guaranteed to work in C (the problem is how to handle the signed bit, do you shift it like a value bit or is it not affected by a shift? The standards committee couldn't agree on a... | 9 | 2008-12-14T17:06:34Z | [
"python",
"low-level"
] |
Bitwise subtraction in Python | 366,706 | <p>This is a follow-up to <a href="http://stackoverflow.com/questions/365522/what-is-the-best-way-to-add-two-numbers-without-using-the-operator">my question yesterday</a>:</p>
<p>CMS kindly provided this example of using bitwise operators to add two numbers in C:</p>
<pre><code>#include<stdio.h>
int add(int x,... | 9 | 2008-12-14T16:41:28Z | 3,487,601 | <p>if <code>i</code>, <code>j</code> are two integers:</p>
<p>addition:</p>
<pre><code>printf("%d",(i^j)|((i&j)<<1));
</code></pre>
| 1 | 2010-08-15T13:43:38Z | [
"python",
"low-level"
] |
Bitwise subtraction in Python | 366,706 | <p>This is a follow-up to <a href="http://stackoverflow.com/questions/365522/what-is-the-best-way-to-add-two-numbers-without-using-the-operator">my question yesterday</a>:</p>
<p>CMS kindly provided this example of using bitwise operators to add two numbers in C:</p>
<pre><code>#include<stdio.h>
int add(int x,... | 9 | 2008-12-14T16:41:28Z | 30,786,721 | <p>I've noticed that you're assuming that python works with numbers the same way as C does.<br>
Thats not entirely true. Meaning C's int numbres have a fixed length of 16 bits. For detailed info on C datatypes you can refer to <a href="http://en.wikipedia.org/wiki/C_data_types" rel="nofollow">C_data_types on en.wikiped... | 0 | 2015-06-11T16:46:23Z | [
"python",
"low-level"
] |
How to organize python test in a way that I can run all tests in a single command? | 366,720 | <p>Currently my code is organized in the following tree structure:</p>
<pre><code>src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
</code></pre>
<p>Where the <code>modul... | 14 | 2008-12-14T16:52:50Z | 366,770 | <p>This is probably a hotly-contested topic, but I would suggest that you separate your tests out from your modules. Set up something like this...</p>
<p>Use <code>setup.py</code> to install these into the system path (or you may be able to modify environment variables to avoid the need for an "install" step).</p>
<... | 2 | 2008-12-14T17:37:18Z | [
"python",
"unit-testing",
"python-nose"
] |
How to organize python test in a way that I can run all tests in a single command? | 366,720 | <p>Currently my code is organized in the following tree structure:</p>
<pre><code>src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
</code></pre>
<p>Where the <code>modul... | 14 | 2008-12-14T16:52:50Z | 366,819 | <p>I'll give a <a href="http://www.testoob.org" rel="nofollow">Testoob</a> answer.</p>
<p>Running tests in a single file is like Nose:</p>
<pre><code>testoob test_foo.py
</code></pre>
<p>To run tests in many files you can create suites with the Testoob collectors (in each subpackage)</p>
<pre><code># src/subpackage... | 0 | 2008-12-14T18:32:47Z | [
"python",
"unit-testing",
"python-nose"
] |
How to organize python test in a way that I can run all tests in a single command? | 366,720 | <p>Currently my code is organized in the following tree structure:</p>
<pre><code>src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
</code></pre>
<p>Where the <code>modul... | 14 | 2008-12-14T16:52:50Z | 366,828 | <p>Whether you seperate or mix tests and modules is probably a matter of taste, although I would strongly advocate for keeping them apart (setup reasons, code stats etc).</p>
<p>When you're using nosetests, make sure that all directories with tests are real packages:</p>
<pre><code>src/
module1.py
module2.py
... | 10 | 2008-12-14T18:41:31Z | [
"python",
"unit-testing",
"python-nose"
] |
How to organize python test in a way that I can run all tests in a single command? | 366,720 | <p>Currently my code is organized in the following tree structure:</p>
<pre><code>src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
</code></pre>
<p>Where the <code>modul... | 14 | 2008-12-14T16:52:50Z | 367,748 | <p>I don't know about nosetests, but you can achieve that with the standard unittest module. You just need to create a <code>test_all.py</code> file under your root directory, then import all your test modules. In your case:</p>
<pre><code>import unittest
import test_module1
import test_module2
import subpackage1
if _... | 4 | 2008-12-15T08:15:09Z | [
"python",
"unit-testing",
"python-nose"
] |
How to organize python test in a way that I can run all tests in a single command? | 366,720 | <p>Currently my code is organized in the following tree structure:</p>
<pre><code>src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
</code></pre>
<p>Where the <code>modul... | 14 | 2008-12-14T16:52:50Z | 373,150 | <p>If they all begin with <code>test</code> then just <code>nosetest</code> should work. Nose automatically searches for any files beginning with 'test'.</p>
| 7 | 2008-12-16T23:28:39Z | [
"python",
"unit-testing",
"python-nose"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 367,026 | <p>It depends on who will provide the translations. If you want to provide a web interface to translation, then you need to develop that yourself, and also represent the translations in the database.</p>
<p>If the same translators who translated the site will also translate the data, you can provide them with the same... | 0 | 2008-12-14T21:43:39Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 367,120 | <p>"i must provide a way of translating theses names and descriptions to the users."</p>
<p>"Is there a natural way in django to do this?"</p>
<p>Are you asking if Django can translate from language to language? Are you asking about something like <a href="http://translate.google.com/" rel="nofollow">http://translat... | 1 | 2008-12-14T23:00:27Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 367,698 | <p>I would suggest checking out <a href="http://code.google.com/p/django-multilingual/">django-multilingual</a>. It is a third party app that lets you define translation fields on your models.</p>
<p>Of course, you still have to type in the actual translations, but they are stored transparently in the database (as op... | 10 | 2008-12-15T07:40:50Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 367,700 | <p>There are two projects of note for translatable content in Django:
<a href="http://code.google.com/p/django-multilingual/" rel="nofollow">http://code.google.com/p/django-multilingual/</a>
<a href="http://code.google.com/p/transdb/" rel="nofollow">http://code.google.com/p/transdb/</a></p>
| 4 | 2008-12-15T07:42:14Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 370,889 | <p>I use <a href="http://code.google.com/p/django-multilingual/" rel="nofollow">django-multilingual</a> for localize content and <a href="http://code.google.com/p/django-localeurl/" rel="nofollow">django-localeurl</a> for choosing language based on url (for example mypage/en/).</p>
<p>You can see how multilingua and l... | 5 | 2008-12-16T10:15:17Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 1,325,190 | <p>Also looking for content localization plugin, or how to write it.
Can add to the list <a href="http://github.com/foxbunny/django-i18n-model/tree/master" rel="nofollow">django-i18n-model</a></p>
| 0 | 2009-08-24T22:46:21Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 3,870,634 | <p>I think you should operate in two steps:</p>
<ol>
<li>Get translations</li>
<li>Show translated strings</li>
</ol>
<p>For the first step, you should tell Django that the user-inserted strings are to be translated. I think there is no native way to do so. Maybe you can extract the strings from your db putting them ... | 0 | 2010-10-06T08:22:18Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 3,896,455 | <p>Or try this:</p>
<p><a href="http://packages.python.org/django-easymode/i18n/index.html" rel="nofollow">http://packages.python.org/django-easymode/i18n/index.html</a></p>
<p>It stays very close to how you would normally do a django model, you just add 1 decorator above your model. It has admin support for the tran... | 0 | 2010-10-09T07:32:44Z | [
"python",
"django",
"localization",
"internationalization"
] |
How to localize Content of a Django application | 366,838 | <p>Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to translate the dynamic content of the application.
Users can save "products" in the database and give them names and descriptions, but since the whole ... | 9 | 2008-12-14T18:48:49Z | 12,482,331 | <p>I have 2 languages on my site: English and Arabic
Users can switch between languages clicking on a flag.
In models i use a proxy model:</p>
<pre><code>class Product(models.Model):
name=models.CharField(max_length=100)
name_ar=models.CharField(max_length=100, default='')
def __unicode__(self):
r... | 1 | 2012-09-18T17:49:24Z | [
"python",
"django",
"localization",
"internationalization"
] |
Deleting erroneous ReferenceProperty properties in AppEngine | 367,029 | <p>Most of the time, the errors you get from your model properties will happen when you're saving data. For instance, if you try saving a string as an IntegerProperty, that will result in an error.</p>
<p>The one exception (no pun intended) is ReferenceProperty. If you have lots of references and you're not completely... | 0 | 2008-12-14T21:45:21Z | 367,334 | <p>I'm having similar difficulties for my project. As I code the beta version of my application, I do create a lot of dead link and its trully a pain to untangle things afterward. Ideally, this tool would have to also report of the offending reference so that you could pin-point problems in the code.</p>
| 1 | 2008-12-15T02:18:47Z | [
"python",
"google-app-engine",
"model",
"referenceproperty"
] |
Deleting erroneous ReferenceProperty properties in AppEngine | 367,029 | <p>Most of the time, the errors you get from your model properties will happen when you're saving data. For instance, if you try saving a string as an IntegerProperty, that will result in an error.</p>
<p>The one exception (no pun intended) is ReferenceProperty. If you have lots of references and you're not completely... | 0 | 2008-12-14T21:45:21Z | 374,241 | <p>You could extend and customize ReferenceProperty to not throw this exception, but then it'll need to return something - presumably None - in which case your template will simply throw an exception when it attempts to access properties on the returned object.</p>
<p>A better approach is to fetch the referencepropert... | 0 | 2008-12-17T11:07:12Z | [
"python",
"google-app-engine",
"model",
"referenceproperty"
] |
Deleting erroneous ReferenceProperty properties in AppEngine | 367,029 | <p>Most of the time, the errors you get from your model properties will happen when you're saving data. For instance, if you try saving a string as an IntegerProperty, that will result in an error.</p>
<p>The one exception (no pun intended) is ReferenceProperty. If you have lots of references and you're not completely... | 0 | 2008-12-14T21:45:21Z | 2,152,183 | <p>That exception is actually a bug that's been waiting to be fixed for a while (see <a href="http://code.google.com/p/googleappengine/issues/detail?id=426" rel="nofollow">http://code.google.com/p/googleappengine/issues/detail?id=426</a>). Ideally you should be able to test whether the reference is valid like this(from... | 0 | 2010-01-28T03:51:19Z | [
"python",
"google-app-engine",
"model",
"referenceproperty"
] |
No print output from child multiprocessing.Process unless the program crashes | 367,053 | <p>I am having trouble with the Python multiprocessing module. I am using the <code>Process</code> class to spawn a new process in order to utilize my second core. This second process loads a bunch of data into RAM and then waits patiently instead of consuming. </p>
<p>I wanted to see what that process printed with th... | 12 | 2008-12-14T22:07:56Z | 367,065 | <p>Have you tried flushing stdout?</p>
<pre><code>import sys
print "foo"
sys.stdout.flush()
</code></pre>
| 21 | 2008-12-14T22:18:22Z | [
"python",
"multithreading",
"io",
"multiprocessing"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.