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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
how to get content of a small ascii file in python? | 1,432,126 | <p>Let's say we want to implement an equivalent of the PHP's <a href="http://fr2.php.net/manual/en/function.file-get-contents.php"><code>file_get_content</code></a>.</p>
<p>What is the best practice? (elegant and reliable)</p>
<p>Here are some proposition, are they correct?</p>
<p>using <code>with</code> statement:<... | 20 | 2009-09-16T10:13:28Z | 1,432,235 | <p>Using the with statement is actually the nicest way to be <em>sure</em> that the file is really closed.</p>
<p>Depending on the garbage collector behavior for this task might work, but in this case, there is a nice way to be sure in all cases, so...</p>
| 3 | 2009-09-16T10:47:45Z | [
"python",
"file"
] |
how to get content of a small ascii file in python? | 1,432,126 | <p>Let's say we want to implement an equivalent of the PHP's <a href="http://fr2.php.net/manual/en/function.file-get-contents.php"><code>file_get_content</code></a>.</p>
<p>What is the best practice? (elegant and reliable)</p>
<p>Here are some proposition, are they correct?</p>
<p>using <code>with</code> statement:<... | 20 | 2009-09-16T10:13:28Z | 1,432,238 | <p>In the current implementation of CPython, both will generally immediately close the file. However, Python the language makes no such guarantee for the second one - the file will eventually be closed, but the finaliser may not be called until the next gc cycle. Implementations like Jython and IronPython will work l... | 29 | 2009-09-16T10:48:20Z | [
"python",
"file"
] |
how to get content of a small ascii file in python? | 1,432,126 | <p>Let's say we want to implement an equivalent of the PHP's <a href="http://fr2.php.net/manual/en/function.file-get-contents.php"><code>file_get_content</code></a>.</p>
<p>What is the best practice? (elegant and reliable)</p>
<p>Here are some proposition, are they correct?</p>
<p>using <code>with</code> statement:<... | 20 | 2009-09-16T10:13:28Z | 10,930,035 | <p>You can also use Python's v3 feature:</p>
<pre><code>>>> ''.join(open('htdocs/config.php', 'r').readlines())
"This is the first line of the file.\nSecond line of the file"
</code></pre>
<p>Read more here <a href="http://docs.python.org/py3k/tutorial/inputoutput.html" rel="nofollow">http://docs.python.org/... | -1 | 2012-06-07T10:30:05Z | [
"python",
"file"
] |
Returning a list | 1,432,358 | <p>I have the following code:</p>
<pre><code>def foo(*args)
print len(args)
print args
</code></pre>
<p>now I'd like to know how to return that same args list. I guess it should be simple?</p>
<p>Thanks</p>
| 0 | 2009-09-16T11:16:39Z | 1,432,361 | <p>It is indeed simple:</p>
<pre><code>return args
</code></pre>
<p><a href="http://docs.python.org/tutorial/index.html" rel="nofollow">Here</a> is the Python tutorial:
There are also many resources on beginners python on the net. Some are listed <a href="http://stackoverflow.com/questions/774325/programming-tutoria... | 6 | 2009-09-16T11:17:35Z | [
"python"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,432,505 | <p><code>os.system('clear')</code> works on linux. If you are running windows try <code>os.system('CLS')</code> instead.</p>
<p>You need to import os first like this: </p>
<pre><code>import os
</code></pre>
| 41 | 2009-09-16T11:54:59Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,433,135 | <p>The way to execute commands in Python 2.4+ is to use the <a href="http://docs.python.org/library/subprocess.html">subprocess</a> module. You can use it in the same way that you use <code>os.system</code>.</p>
<pre><code>import subprocess
subprocess.call("clear") # linux/mac
subprocess.call("cls", shell=True) # wind... | 7 | 2009-09-16T13:56:11Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,433,818 | <p>The "cls" and "clear" are commands which will clear a terminal (ie a DOS prompt, or terminal window). From your screenshot, you are using the shell within IDLE, which won't be affected by such things. Unfortunately, I don't think there is a way to clear the screen in IDLE. The best you could do is to scroll the s... | 64 | 2009-09-16T15:44:21Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,433,849 | <p>Your screenshot shows you're using IDLE. <code>cls</code>/<code>clear</code> won't work for you.</p>
| 1 | 2009-09-16T15:50:56Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,435,237 | <p>There does not appear to be a way to clear the IDLE 'shell' buffer.</p>
| 6 | 2009-09-16T20:19:45Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 1,576,881 | <p>As mark.ribau said, it seems that there is no way to clear the Text widget in idle. One should edit the <code>EditorWindow.py</code> module and add a method and a menu item in the EditorWindow class that does something like:</p>
<pre><code>self.text.tag_remove("sel", "1.0", "end")
self.text.delete("1.0", "end")
</c... | 0 | 2009-10-16T08:40:54Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 4,550,807 | <p>File -> New Window</p>
<p><strong>In the new window**</strong></p>
<p>Run -> Python Shell</p>
<p>The problem with this method is that it will clear all the things you defined, such as variables.</p>
<p>Alternatively, you should just use command prompt. </p>
<p>open up command prompt</p>
<p>type "cd c:\python27... | 2 | 2010-12-29T01:11:27Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 5,888,489 | <p>If you want to clear the shell buffer go to shell > restart shell in IDLE. Your question is a little vague. Do you want to clear the shell buffer or are you trying to call the OS command?</p>
| 1 | 2011-05-04T18:57:19Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 8,352,839 | <p>An extension for clearing the shell can be found in <a href="http://bugs.python.org/issue6143">Issue6143</a> as a "feature request". This extension is included with <a href="http://idlex.sourceforge.net">IdleX</a>.</p>
| 14 | 2011-12-02T07:04:16Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 8,846,657 | <pre><code>>>> import os
>>>def cls():
... os.system("clear")
...
>>>cls()
</code></pre>
<p>That does is perfectly. No '0' printed either.</p>
| 6 | 2012-01-13T06:13:07Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 12,491,227 | <p>I do this on my mac like so:</p>
<p>First..
I have a file named fxn.py in a folder I have added to PYTHONPATH so each time I load python, I can just do</p>
<pre><code>>>> from fxn import *
</code></pre>
<p>...all my functions declared are now available. one of the functions declared therein is:</p>
<pre... | 0 | 2012-09-19T08:44:55Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 14,471,376 | <p>There is no need to write your own function to do this! Python has a built in clear function. </p>
<p>Type the following in the command prompt:</p>
<pre><code>shell.clear()
</code></pre>
| -2 | 2013-01-23T02:12:44Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 16,622,317 | <p>The best way to do it on windows is using the command prompt 'cmd' and access python directory the command prompt could be found on the start menu >run>cmd</p>
<pre><code>C:\>cd Python27
C:\Python27>python.exe
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "cop... | 0 | 2013-05-18T08:46:42Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 16,634,647 | <p>If you are using the terminal ( i am using ubuntu ) , then just use the combination of <kbd>CTRL</kbd>+<kbd>l</kbd> from keyboard to clear the python script, even it clears the terminal script...</p>
| -1 | 2013-05-19T12:28:15Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 18,176,163 | <p>You can make an <a href="http://www.autohotkey.com/" rel="nofollow">AutoHotKey</a> script.</p>
<p>To set ctrl-r to a hotkey to clear the shell:</p>
<pre><code>^r::SendInput print '\n' * 50 {Enter}
</code></pre>
<p>Just install AutoHotKey, put the above in a file called idle_clear.ahk, run the file, and your hotke... | 2 | 2013-08-11T20:07:07Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 24,121,785 | <p>This works for me in Windows:</p>
<p><code>print chr(12)</code></p>
| 0 | 2014-06-09T13:54:38Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 24,466,596 | <p>Most of the answers appear here do clearing the DOS prompt screen clearing commands. Which is not the Question asked here. An other answer here posted was the printing blank lines to show a clearing effect of the screen. </p>
<p>The simplest answer of this question is </p>
<blockquote>
<p>It is not possible to c... | 7 | 2014-06-28T11:59:57Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 25,972,072 | <pre><code>from subprocess import call as c
def cls():
c("cls",shell=True)
print([x for x in range(10000)])
cls()
</code></pre>
<p>Whenever you wish to clear console call the function cls, but note that this function will clear the screen in Windows only if you are running Python Script using cmd prompt, it wi... | 1 | 2014-09-22T10:38:01Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 26,911,370 | <p>use this</p>
<pre><code>for num in range(1,100):
print("\n")
</code></pre>
| 0 | 2014-11-13T14:37:56Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 29,189,588 | <p><kbd>ctrl</kbd> + <kbd>L</kbd> clears the screen on Ubuntu Linux.</p>
| 12 | 2015-03-22T00:19:55Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 37,799,264 | <p>I like to use:</p>
<pre><code>import os
clear = lambda : os.system('cls') # or clear for Linux
clear()
</code></pre>
| 0 | 2016-06-13T21:15:39Z | [
"python",
"python-idle"
] |
Any way to clear python's IDLE window? | 1,432,480 | <p>I know there's a similar topic about python console, but I do not know if they are the same. I tried system("clear") and it didn't work here.</p>
<p>How do I clear python's IDLE window?</p>
| 80 | 2009-09-16T11:49:36Z | 38,944,644 | <p>None of these solutions worked for me on Windows 7 and within IDLE. Wound up using PowerShell, running Python within it and exiting to call "cls" in PowerShell to clear the window. </p>
<p>CONS: Assumes Python is already in the PATH variable. Also, this does clear your Python variables (though so does restarting... | 0 | 2016-08-14T17:18:02Z | [
"python",
"python-idle"
] |
Help with Admin forms validation error | 1,432,530 | <p>I am quite new to Django, I'm having few problems with validation
forms in Admin module, more specifically with raising exceptions in the
ModelForm. I can validate and manipulate data in clean methods but
cannot seem to raise any errors. Whenever I include any raise
statement I get this error "'NoneType' object has ... | 0 | 2009-09-16T11:59:07Z | 1,432,695 | <p>You problem might be in the * import.</p>
<pre><code>from proj.models import *
</code></pre>
<p>if proj.models contains any variable named forms (including some module import like "from django import forms), it could trounce your initial import of:</p>
<pre><code>from django import forms
</code></pre>
<p>I would... | 3 | 2009-09-16T12:37:57Z | [
"python",
"django",
"django-models",
"django-admin"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 1,432,910 | <p>Try this:</p>
<p>Persistent data structures are data structures which, unlike arrays or lists, always preserve their original structure and contents.</p>
<p>They are effectively immutable - instead of changing such a structure, any computation will yield a new <em>copy</em> leaving the original structure unchanged... | 6 | 2009-09-16T13:19:13Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 1,433,052 | <p>Explaining "persistent" data structures is much, much easier if you use the word the Python community uses: "immutable".</p>
<p>Everyone understands immutable numbers. <code>2+2</code> can't update 2, it has to create a new value.</p>
<p>Everyone understand mutable lists. <code>[2, 3].append(4)</code> actually u... | 6 | 2009-09-16T13:43:24Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 1,433,202 | <p>Other useful concept words to describe persistent data structures are "<em>Snapshot</em>" or "<em>Version</em>", which you may precede by "virtual" because an efficient implementation of a persistent structure would not merely make multiple copies. Persistent Data Structure libraries strive to store and process the... | 1 | 2009-09-16T14:08:24Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 1,448,892 | <p>The sort of phrasing I would suggest is that the value of the given structure is persistent across function calls, and that any function calls that result in a change to the structure result in a new structure being created based on copy-on-change semantics.</p>
<p>For example, given a cons type structure L defined... | 2 | 2009-09-19T16:30:49Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 1,512,210 | <p>I think the key point to make is that persistent data structures are an optimization for handling immutable values. </p>
<p>But they are an important optimization, even with fast cpu and big memory.</p>
<p>For example, Java strings are immutable but they do NOT have a persistent data structure behind the scenes. T... | 1 | 2009-10-02T23:01:02Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
Explaining persistent data structures in simple terms | 1,432,760 | <p>I'm working on a <a href="http://persistence.kenai.com/">library for Python</a> that implements some persistent data structures (mainly as a learning exercise). However, I'm beginning to learn that explaining persistent data structures to people unfamiliar with them can be difficult. Can someone help me think of a... | 12 | 2009-09-16T12:50:11Z | 2,883,662 | <p>When you alter a mutable data structure (e.g. insert into a hash table) it changes in-place and the previous version is lost. When you alter a persistent data structure (e.g. insert into a <code>Map</code>) you get a new data structure and the previous version remains valid.</p>
| 0 | 2010-05-21T16:08:04Z | [
"python",
"data-structures",
"documentation",
"functional-programming",
"persistence"
] |
python: Change the scripts working directory to the script's own directory | 1,432,924 | <p>I run a python shell from crontab every minute:</p>
<pre><code>* * * * * /home/udi/foo/bar.py
</code></pre>
<p><code>/home/udi/foo</code> has some necessary subdirectories, like <code>/home/udi/foo/log</code> and <code>/home/udi/foo/config</code>, which <code>/home/udi/foo/bar.py</code> refers to.</p>
<p>The prob... | 85 | 2009-09-16T13:21:51Z | 1,432,949 | <p>This will change your current working directory to so that opening relative paths will work:</p>
<pre><code>import os
os.chdir("/home/udi/foo")
</code></pre>
<p>However, you asked how to change into whatever directory your Python script is located, even if you don't know what directory that will be when you're wri... | 100 | 2009-09-16T13:26:24Z | [
"python",
"working-directory"
] |
python: Change the scripts working directory to the script's own directory | 1,432,924 | <p>I run a python shell from crontab every minute:</p>
<pre><code>* * * * * /home/udi/foo/bar.py
</code></pre>
<p><code>/home/udi/foo</code> has some necessary subdirectories, like <code>/home/udi/foo/log</code> and <code>/home/udi/foo/config</code>, which <code>/home/udi/foo/bar.py</code> refers to.</p>
<p>The prob... | 85 | 2009-09-16T13:21:51Z | 1,433,009 | <p>Don't do this.</p>
<p>Your scripts and your data should not be mashed into one big directory. Put your code in some known location (<code>site-packages</code> or <code>/var/opt/udi</code> or something) separate from your data. Use good version control on your code to be sure that you have current and previous ver... | 14 | 2009-09-16T13:34:21Z | [
"python",
"working-directory"
] |
python: Change the scripts working directory to the script's own directory | 1,432,924 | <p>I run a python shell from crontab every minute:</p>
<pre><code>* * * * * /home/udi/foo/bar.py
</code></pre>
<p><code>/home/udi/foo</code> has some necessary subdirectories, like <code>/home/udi/foo/log</code> and <code>/home/udi/foo/config</code>, which <code>/home/udi/foo/bar.py</code> refers to.</p>
<p>The prob... | 85 | 2009-09-16T13:21:51Z | 9,130,405 | <p>You can get a shorter version by using <code>sys.path[0]</code>.</p>
<pre><code>os.chdir(sys.path[0])
</code></pre>
<p>From <a href="http://docs.python.org/library/sys.html#sys.path">http://docs.python.org/library/sys.html#sys.path</a></p>
<blockquote>
<p>As initialized upon program startup, the first item of t... | 16 | 2012-02-03T14:41:28Z | [
"python",
"working-directory"
] |
python: Change the scripts working directory to the script's own directory | 1,432,924 | <p>I run a python shell from crontab every minute:</p>
<pre><code>* * * * * /home/udi/foo/bar.py
</code></pre>
<p><code>/home/udi/foo</code> has some necessary subdirectories, like <code>/home/udi/foo/log</code> and <code>/home/udi/foo/config</code>, which <code>/home/udi/foo/bar.py</code> refers to.</p>
<p>The prob... | 85 | 2009-09-16T13:21:51Z | 14,628,314 | <p>Change your crontab command to</p>
<pre><code>* * * * * (cd /home/udi/foo/ || exit 1; ./bar.py)
</code></pre>
<p>The <code>(...)</code> starts a sub-shell that your crond executes as a single command. The <code>|| exit 1</code> causes your cronjob to fail in case that the directory is unavailable.</p>
<p>Though t... | 9 | 2013-01-31T14:37:45Z | [
"python",
"working-directory"
] |
Generate and parse Python code from C# application | 1,432,998 | <p>I need to generate Python code to be more specific <a href="http://ironpython.codeplex.com/">IronPyton</a>. I also need to be able to parse the code and load it into <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>. I just started looking at some tools. I played with <a href="http://msdn.mi... | 5 | 2009-09-16T13:33:10Z | 1,433,184 | <p>I think you should look at the <a href="http://msdn.microsoft.com/en-us/magazine/cc163344.aspx" rel="nofollow">Dynamic Language Runtime</a>. This will be a standard part of some later version of .Net and C# (.Net 4 from memory).</p>
<p>I've used it to compile and execute Python code generated at runtime, but I hav... | 0 | 2009-09-16T14:04:35Z | [
"c#",
"python",
"code-generation",
"ironpython",
"cocor"
] |
Generate and parse Python code from C# application | 1,432,998 | <p>I need to generate Python code to be more specific <a href="http://ironpython.codeplex.com/">IronPyton</a>. I also need to be able to parse the code and load it into <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>. I just started looking at some tools. I played with <a href="http://msdn.mi... | 5 | 2009-09-16T13:33:10Z | 1,433,330 | <p>The <a href="http://ironpython.codeplex.com/">IronPython</a> implementation itself includes <a href="http://ironpython.codeplex.com/SourceControl/changeset/view/59195#992853">a parser</a> and <a href="http://ironpython.codeplex.com/SourceControl/changeset/view/59195#992331">an AST representation</a> of Python progra... | 7 | 2009-09-16T14:29:00Z | [
"c#",
"python",
"code-generation",
"ironpython",
"cocor"
] |
Generate and parse Python code from C# application | 1,432,998 | <p>I need to generate Python code to be more specific <a href="http://ironpython.codeplex.com/">IronPyton</a>. I also need to be able to parse the code and load it into <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>. I just started looking at some tools. I played with <a href="http://msdn.mi... | 5 | 2009-09-16T13:33:10Z | 1,433,475 | <p>Not really my area of expertise but you might want to try <a href="http://www.antlr.org/" rel="nofollow">ANTLR</a>. It has support for generating <a href="http://www.antlr.org/wiki/display/ANTLR3/Antlr3PythonTarget" rel="nofollow">Python</a>.</p>
| 1 | 2009-09-16T14:48:13Z | [
"c#",
"python",
"code-generation",
"ironpython",
"cocor"
] |
How do I run a sub-process, display its output in a GUI and allow it to be terminated? | 1,433,367 | <p>I have been trying to write an application that runs subprocesses and (among other things) displays their output in a GUI and allows the user to click a button to cancel them. I start the processes like this:</p>
<pre><code>queue = Queue.Queue(500)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE... | 2 | 2009-09-16T14:35:14Z | 1,433,495 | <p>In the code that terminates the process, you could also explicitly <code>os.close()</code> the pipe that your thread is reading from?</p>
| 1 | 2009-09-16T14:50:13Z | [
"python"
] |
How do I run a sub-process, display its output in a GUI and allow it to be terminated? | 1,433,367 | <p>I have been trying to write an application that runs subprocesses and (among other things) displays their output in a GUI and allows the user to click a button to cancel them. I start the processes like this:</p>
<pre><code>queue = Queue.Queue(500)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE... | 2 | 2009-09-16T14:35:14Z | 5,942,234 | <p>I know this is an old post, but in case it still helps anyone, I think your problem could be solved by passing the subprocess.Popen instance to io_thread, rather than it's output stream.
If you do that, then you can replace your <code>while True:</code> line with <code>while process.poll() == None:</code>. </p>
<p>... | 2 | 2011-05-09T20:39:28Z | [
"python"
] |
How do I run a sub-process, display its output in a GUI and allow it to be terminated? | 1,433,367 | <p>I have been trying to write an application that runs subprocesses and (among other things) displays their output in a GUI and allows the user to click a button to cancel them. I start the processes like this:</p>
<pre><code>queue = Queue.Queue(500)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE... | 2 | 2009-09-16T14:35:14Z | 20,977,023 | <p>This is probably old enough, but still usefull to someone coming from search engine...</p>
<p>The reason that it shows that message is that after the subprocess has been completed it closes the file descriptors, therefore, the daemon thread (which is running concurrently) will try to use those closed descriptors ra... | 2 | 2014-01-07T16:39:25Z | [
"python"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 1,433,601 | <ul>
<li>Yes, you can</li>
<li>The only drawback is memory usage, and possible also speed if the file is big.</li>
<li>File size should be limited to how much space you have in memory.</li>
</ul>
<p>In general, there are better ways to do it, but for one-off scripts where you know memory is not an issue, sure.</p>
| 11 | 2009-09-16T15:08:40Z | [
"python",
"file",
"variables"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 1,433,619 | <p>The sole issue you can run into is memory consumption: Strings in Python are immutable. So when you need to change a byte, you need to copy the old string:</p>
<pre><code>new = old[0:pos] + newByte + old[pos+1:]
</code></pre>
<p>This needs up to three times the memory of <code>old</code>.</p>
<p>Instead of a stri... | 3 | 2009-09-16T15:10:40Z | [
"python",
"file",
"variables"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 1,433,621 | <pre><code>with open(filename) as f:
</code></pre>
<p>This only works on Python 2.x on Unix. It won't do what you expect on Python 3.x or on Windows, as these both draw a strong distinction between text and binary files. It's better to specify that the file is binary, like this:</p>
<pre><code>with open(filename, '... | 4 | 2009-09-16T15:10:58Z | [
"python",
"file",
"variables"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 1,433,650 | <p>Yes you can -provided the file is small enough-.</p>
<p>It is even very pythonic to further convert the return from read() to any container/iterable type as with say, string.split(), along with associated functional programming features to continue treating the file "at once".</p>
| 0 | 2009-09-16T15:17:30Z | [
"python",
"file",
"variables"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 1,436,408 | <p>While you've gotten good responses, it seems nobody has answered this part of your question (as often happens when you ask many questions in a question;-)...:</p>
<blockquote>
<p>Regarding file size concerns, to what
maximum size this solution should be
limited ?. And why ?</p>
</blockquote>
<p>The most impo... | 6 | 2009-09-17T02:14:58Z | [
"python",
"file",
"variables"
] |
Can we use a python variable to hold an entire file? | 1,433,577 | <p>Provided that we know that all the file will be loaded in memory and we can afford it,
what are the drawbacks (if any) or limitations (if any) of loading an entire file (possibly a binary file) in a python variable. If this is technically possible, should this be avoided, and why ?</p>
<p>Regarding file size concer... | 6 | 2009-09-16T15:03:20Z | 10,930,086 | <p>You can also use Python's v3 feature:</p>
<pre><code>>>> ''.join(open('htdocs/config.php', 'r').readlines())
"This is the first line of the file.\nSecond line of the file"
</code></pre>
<p>Read more here <a href="http://docs.python.org/py3k/tutorial/inputoutput.html" rel="nofollow">http://docs.python.org/... | 1 | 2012-06-07T10:33:53Z | [
"python",
"file",
"variables"
] |
Getting text values from XML in Python | 1,433,907 | <pre><code>from xml.dom.minidom import parseString
dom = parseString(data)
data = dom.getElementsByTagName('data')
</code></pre>
<p>the 'data' variable returns as an element object but I cant for the life of me see in the documentation to grab the text value of the element.</p>
<p>For example:</p>
<pre><code><som... | 4 | 2009-09-16T16:02:07Z | 1,433,946 | <p>This should do the trick:</p>
<pre><code>dom = parseString('<something><data>I WANT THIS</data></something>')
data = dom.getElementsByTagName('data')[0].childNodes[0].data
</code></pre>
<p>i.e. you need to wade deeper into the DOM structure to get at the text child node and then access its ... | 3 | 2009-09-16T16:09:13Z | [
"python",
"xml",
"parsing"
] |
Getting text values from XML in Python | 1,433,907 | <pre><code>from xml.dom.minidom import parseString
dom = parseString(data)
data = dom.getElementsByTagName('data')
</code></pre>
<p>the 'data' variable returns as an element object but I cant for the life of me see in the documentation to grab the text value of the element.</p>
<p>For example:</p>
<pre><code><som... | 4 | 2009-09-16T16:02:07Z | 1,433,958 | <p>So the way to look at it is that "I WANT THIS" is actually another node. It's a text child of "data".</p>
<pre><code>from xml.dom.minidom import parseString
dom = parseString(data)
nodes = dom.getElementsByTagName('data')
</code></pre>
<p>At this point, "nodes" is a NodeList and in your example, it has one item i... | 4 | 2009-09-16T16:10:41Z | [
"python",
"xml",
"parsing"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,155 | <p><code>python yourfile.py</code></p>
<p>You have to have python installed first. It will automatically compile your file into a .pyc binary, and then run it for you. It will automatically recompile any time your file changes.</p>
<p><a href="http://www.python.org/download/">http://www.python.org/download/</a></p>
| 25 | 2009-09-16T16:44:03Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,170 | <p>Python compiles its files to bytecode before executing them. That means you have to have a Python interpreter installed on the target machine.</p>
<p>If you don't want to install Python on the target machine use <a href="http://www.py2exe.org/">py2exe</a>, <a href="http://svn.pythonmac.org/py2app/py2app/trunk/doc/i... | 15 | 2009-09-16T16:48:05Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,199 | <p>To add to <a href="http://stackoverflow.com/questions/1434144/pyhton-programming/1434155#1434155">Paul McMillan's answer</a>, if you are on Windows and you have Python installed, then any files ending with the extension ".py" should be associated with the <code>python</code> executable, allowing you to run it like s... | 5 | 2009-09-16T16:54:17Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,239 | <p>On most Unix-like systems, you can use the shebang to tell the operating system which interpreter should be called. You simply put</p>
<pre><code>#!/path/to/python
</code></pre>
<p>in the first line of your file, where of course you have to replace "/path/to/" with the path you have on your system. In most cases t... | 1 | 2009-09-16T17:01:44Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,277 | <p>If you want to transform a python source file into a double-clickable <code>.exe</code> on windows, you can use <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a>, which can help you build an easy to distribute package.</p>
| 3 | 2009-09-16T17:09:32Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,434,486 | <p>Python is an interpreted language, so you don't need to compile it; just to run it. As it happens, the standard version of python will compile this to "bytecode", just like Java etc. does, and will save that (in .pyc files) and run it next time around, saving time, if you haven't updated the file since. If you've ... | 8 | 2009-09-16T17:44:59Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 1,529,049 | <p>If you just want to compile sources, without running them, you can do this</p>
<pre><code>compileall.py <directory>
</code></pre>
<p>this command will compile python code in that directory recursively</p>
<p><strong>compileall</strong> script is usually located in directory like</p>
<pre><code>/usr/local/l... | 9 | 2009-10-07T01:27:11Z | [
"python",
"compiler-construction"
] |
Compiling Python | 1,434,144 | <p>How can I compile and run a python file (*.py extension)?</p>
| 41 | 2009-09-16T16:42:51Z | 19,947,830 | <p><strong>Answer for Windows</strong></p>
<ol>
<li>first you must install python</li>
<li>then set path variable</li>
<li>after that write your python program and save</li>
<li>think there is a python program that name "<strong>hello.py</strong>"</li>
<li>open cmd.exe</li>
<li>then goto the path that you saved your "... | 0 | 2013-11-13T07:26:36Z | [
"python",
"compiler-construction"
] |
python help needed | 1,434,276 | <pre><code>import os
import sys, urllib2, urllib
import re
import time
from threading import Thread
class testit(Thread):
def __init__ (self):
Thread.__init__(self)
def run(self):
url = 'http://games.espnstar.asia/the-greatest-odi/post_brackets.php'
data = urllib.urlencode([('id',"btn_13_9_... | -1 | 2009-09-16T17:09:12Z | 1,434,292 | <p>Unfortunately, it looks like the code formatter here on Stack Overflow turns everything into spaces. But the error is quite self-explanatory. Python, unlike the curly-brace languages (like C, C++, and Java) uses indentation to mark blocks of code. The error means that a block is improperly indented.</p>
| 4 | 2009-09-16T17:12:13Z | [
"python",
"indentation"
] |
python help needed | 1,434,276 | <pre><code>import os
import sys, urllib2, urllib
import re
import time
from threading import Thread
class testit(Thread):
def __init__ (self):
Thread.__init__(self)
def run(self):
url = 'http://games.espnstar.asia/the-greatest-odi/post_brackets.php'
data = urllib.urlencode([('id',"btn_13_9_... | -1 | 2009-09-16T17:09:12Z | 1,434,329 | <p>I edited your post to replace all the tabs with <code><TAB></code>. You need to delete the indentation on those lines and line it back up with spaces. Some editors can do that for you, but I don't know which editor you are using.</p>
<p>If you get serious about Python, you should reconfigure your editor to al... | 5 | 2009-09-16T17:17:43Z | [
"python",
"indentation"
] |
PyQt4 - Image Watermark | 1,434,582 | <p>I'm trying to open a PNG image and write some text to it (a watermark) via QImage and QPainter. The code works 100% on Linux but when I run it on Windows XP (haven't tested with any other versions of Windows) the text is never written to the image. I have the code in a try/except block, but no errors are returned.</... | 2 | 2009-09-16T18:04:35Z | 1,434,605 | <p>First thing that comes to my mind is maybe it isn't finding the specified font on Windows.</p>
| 0 | 2009-09-16T18:10:09Z | [
"python",
"pyqt4",
"qimage",
"qpainter"
] |
PyQt4 - Image Watermark | 1,434,582 | <p>I'm trying to open a PNG image and write some text to it (a watermark) via QImage and QPainter. The code works 100% on Linux but when I run it on Windows XP (haven't tested with any other versions of Windows) the text is never written to the image. I have the code in a try/except block, but no errors are returned.</... | 2 | 2009-09-16T18:04:35Z | 1,434,615 | <p>My guess would be that whatever png lib you are using on Windows doesn't do tranparency (properly)</p>
| 0 | 2009-09-16T18:12:22Z | [
"python",
"pyqt4",
"qimage",
"qpainter"
] |
Invalid syntax error for "print expr"? | 1,434,751 | <pre><code>import os
import sys, urllib2, urllib
import re
import time
from threading import Thread
class testit(Thread):
def _init_ (self):
Thread.__init__(self)
def run(self):
url = 'http://games.espnstar.asia/the-greatest-odi/post_brackets.php'
data = urllib.urlencode([('id'... | -1 | 2009-09-16T18:39:45Z | 1,434,860 | <p>From <a href="http://pydoc.org/1.6/time.html#-ctime" rel="nofollow">this page</a>:</p>
<blockquote>
<p>ctime(...)</p>
<p>ctime(seconds) -> string</p>
<p>Convert a time in seconds since the Epoch to a string in local time.</p>
<p>This is equivalent to asctime(localtime(seconds)).</p>
</blockquote>
... | -2 | 2009-09-16T18:59:35Z | [
"python",
"syntax-error"
] |
Invalid syntax error for "print expr"? | 1,434,751 | <pre><code>import os
import sys, urllib2, urllib
import re
import time
from threading import Thread
class testit(Thread):
def _init_ (self):
Thread.__init__(self)
def run(self):
url = 'http://games.espnstar.asia/the-greatest-odi/post_brackets.php'
data = urllib.urlencode([('id'... | -1 | 2009-09-16T18:39:45Z | 1,434,881 | <p>This is because (in Python 3.0 onwards at least), print is a function.</p>
<p>Use:</p>
<pre><code>print (time.ctime())
</code></pre>
<p>and it should be fine.</p>
| 4 | 2009-09-16T19:02:59Z | [
"python",
"syntax-error"
] |
Thread error in Python & PyQt | 1,434,831 | <p>I noticed that when the function <a href="http://doc.trolltech.com/4.0/qabstractitemview.html#setModel" rel="nofollow">setModel</a> is executed in parallel thread (I tried threading.Timer or threading.thread), I get this:</p>
<pre><code>QObject: Cannot create children for a parent that is in a different thread.
(Pa... | 2 | 2009-09-16T18:54:35Z | 1,434,850 | <p>Looks like you've stumped on a Qt limitation there. Try using signals or events if you need objects to communicate across threads.</p>
<p>Or ask the Qt folk about this. It doesn't seem specific to PyQt.</p>
| 0 | 2009-09-16T18:58:17Z | [
"python",
"multithreading",
"pyqt"
] |
Thread error in Python & PyQt | 1,434,831 | <p>I noticed that when the function <a href="http://doc.trolltech.com/4.0/qabstractitemview.html#setModel" rel="nofollow">setModel</a> is executed in parallel thread (I tried threading.Timer or threading.thread), I get this:</p>
<pre><code>QObject: Cannot create children for a parent that is in a different thread.
(Pa... | 2 | 2009-09-16T18:54:35Z | 1,436,366 | <p>It is indeed a fact of life that multithreaded use of Qt (and other rich frameworks) is a delicate and difficult job, requiring explicit attention and care -- see <a href="http://doc.trolltech.com/4.5/threads.html">Qt's docs</a> for an excellent coverage of the subject (for readers experienced in threading in genera... | 5 | 2009-09-17T01:52:29Z | [
"python",
"multithreading",
"pyqt"
] |
Socket in use error when reusing sockets | 1,434,914 | <p>I am writing an XMLRPC client in c++ that is intended to talk to a python XMLRPC server. </p>
<p>Unfortunately, at this time, the python XMLRPC server is only capable of fielding one request on a connection, then it shuts down, I discovered this thanks to mhawke's response to my previous query about a <a href="h... | 3 | 2009-09-16T19:10:31Z | 1,434,962 | <p>Do you close the sockets after using it?</p>
| 0 | 2009-09-16T19:21:14Z | [
"c++",
"python",
"sockets",
"xml-rpc"
] |
Socket in use error when reusing sockets | 1,434,914 | <p>I am writing an XMLRPC client in c++ that is intended to talk to a python XMLRPC server. </p>
<p>Unfortunately, at this time, the python XMLRPC server is only capable of fielding one request on a connection, then it shuts down, I discovered this thanks to mhawke's response to my previous query about a <a href="h... | 3 | 2009-09-16T19:10:31Z | 1,435,353 | <p>Update:</p>
<p>I tossed this into the code and it seems to be working now.</p>
<pre><code>if(::connect(s_, (sockaddr *) &addr, sizeof(sockaddr)))
{
int err = WSAGetLastError();
if(err == 10048) //if socket in user error, force kill and reopen socket
{
closesocket(s_);
WSACleanup... | 1 | 2009-09-16T20:42:54Z | [
"c++",
"python",
"sockets",
"xml-rpc"
] |
Socket in use error when reusing sockets | 1,434,914 | <p>I am writing an XMLRPC client in c++ that is intended to talk to a python XMLRPC server. </p>
<p>Unfortunately, at this time, the python XMLRPC server is only capable of fielding one request on a connection, then it shuts down, I discovered this thanks to mhawke's response to my previous query about a <a href="h... | 3 | 2009-09-16T19:10:31Z | 1,436,261 | <p>The problem is being caused by sockets hanging around in the TIME_WAIT state which is entered once you close the client's socket. By default the socket will remain in this state for 4 minutes before it is available for reuse. Your client (possibly helped by other processes) is consuming them all within a 4 minute pe... | 11 | 2009-09-17T01:09:07Z | [
"c++",
"python",
"sockets",
"xml-rpc"
] |
Python: bind child class at run time | 1,434,949 | <p>Can anyone tell me how to bind to specific child class at run time in the following code? I want mCar instance in the following example redirect to class Truck or Compact according to command line options? </p>
<pre><code>class Car(object):
pass
class Truck(Car):
pass
class Compact(Car):
pass
</code><... | 0 | 2009-09-16T19:17:44Z | 1,434,974 | <p>You mean like this?</p>
<pre><code>car_classes = {
'car' : Car,
'truck' : Truck,
'compact' : Compact
}
if __name__ == '__main__':
option = sys.argv[1]
mCar = car_classes[option]()
print 'I am a', mCar.__class__.__name__
</code></pre>
| 4 | 2009-09-16T19:24:09Z | [
"python",
"class"
] |
Python: bind child class at run time | 1,434,949 | <p>Can anyone tell me how to bind to specific child class at run time in the following code? I want mCar instance in the following example redirect to class Truck or Compact according to command line options? </p>
<pre><code>class Car(object):
pass
class Truck(Car):
pass
class Compact(Car):
pass
</code><... | 0 | 2009-09-16T19:17:44Z | 1,436,341 | <p>As a side note, while not particularly recommended, it IS possible to assign a different value to <code>self.__class__</code> -- be that in <code>__init__</code> or anywhere else. Do notice that this will change the lookups for class-level names (such as methods), but per se it will not alter the instance's state (n... | 1 | 2009-09-17T01:40:22Z | [
"python",
"class"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 1,435,426 | <p>Have a look at this article: <a href="http://www.lshift.net/blog/2008/11/14/tracing-python-memory-leaks" rel="nofollow">Tracing python memory leaks</a></p>
<p>Also, note that the <a href="http://docs.python.org/library/gc.html" rel="nofollow">garbage collection module</a> actually can have debug flags set. Look at ... | 52 | 2009-09-16T20:58:17Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 1,435,434 | <p>Not sure about "Best Practices" for memory leaks in python, but python should clear it's own memory by it's garbage collector. So mainly I would start by checking for circular list of some short, since they won't be picked up by the garbage collector.</p>
| 3 | 2009-09-16T20:59:56Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 1,435,485 | <p>You should specially have a look on your global or static data (long living data).</p>
<p>When this data grows without restriction, you can also get troubles in Python.</p>
<p>The garbage collector can only collect data, that is not referenced any more. But your static data can hookup data elements that should be ... | 6 | 2009-09-16T21:08:54Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 9,352,959 | <p>This is by no means exhaustive advice. But number one thing to keep in mind when writing with the thought of avoiding future memory leaks (loops) is to make sure that anything which accepts a reference to a call-back, should store that call-back as a weak reference.</p>
| 2 | 2012-02-19T20:32:21Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 23,883,524 | <p>Let me recommend <a href="https://pypi.python.org/pypi/mem_top">mem_top</a> tool,<br>
that helped me to solve a similar issue.</p>
<p>It just instantly shows top suspects for memory leaks in a Python program.</p>
| 9 | 2014-05-27T07:32:36Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Python memory leaks | 1,435,415 | <p>I have a long-running script which, if let to run long enough, will consume all the memory on my system.</p>
<p>Without going into details about the script, I have two questions:</p>
<ol>
<li>Are there any "Best Practices" to follow, which will help prevent leaks from occurring?</li>
<li>What techniques are there ... | 72 | 2009-09-16T20:56:04Z | 29,923,561 | <p>I tried out most options mentioned previously but found this small and intuitive package to be the best: <a href="https://github.com/pympler/pympler" rel="nofollow">pympler</a></p>
<p>It's quite straight forward to trace objects that were not garbage-collected, check this small example:</p>
<p>install package via ... | 33 | 2015-04-28T15:27:15Z | [
"python",
"debugging",
"memory-management",
"memory-leaks"
] |
Retrieving values from 2 different tables with Django's QuerySet | 1,435,438 | <p>For the following models:</p>
<pre><code>class Topping(models.Model):
name = models.CharField(max_length=100)
class Pizza(models.Model):
name = models.CharField(max_length=100)
toppings = models.ManyToManyField(Toppping)
</code></pre>
<p>My data looks like the following:</p>
<p>Pizza and Topping tabl... | 3 | 2009-09-16T21:00:47Z | 1,435,566 | <p>There is no direct way to get a pizza when you have a topping from the model above. You could do</p>
<pre><code>pizzas = topping.pizza_set.all()
</code></pre>
<p>for all pizzas with a topping or probably (in case the topping exists on only one pizza with the name "deluxe")</p>
<pre><code>pizza = topping.pizza_set... | 0 | 2009-09-16T21:25:57Z | [
"python",
"django",
"django-models"
] |
Retrieving values from 2 different tables with Django's QuerySet | 1,435,438 | <p>For the following models:</p>
<pre><code>class Topping(models.Model):
name = models.CharField(max_length=100)
class Pizza(models.Model):
name = models.CharField(max_length=100)
toppings = models.ManyToManyField(Toppping)
</code></pre>
<p>My data looks like the following:</p>
<p>Pizza and Topping tabl... | 3 | 2009-09-16T21:00:47Z | 1,438,650 | <p>I don't think there is a clean solution to this, since you want data from two different models. Depending on your data structure you might want to use select_related to avoid hitting the database for all the toppings. Going for your desired result, I would do:</p>
<pre><code>result = []
pizzas = Pizza.objects.selec... | 2 | 2009-09-17T12:45:50Z | [
"python",
"django",
"django-models"
] |
manage.py syncdb doesn't add tables for some models | 1,435,523 | <p>My second not-so-adept question of the day: I have a django project with four installed apps. When I run manage.py syndb, it only creates tables for two of them. To my knowledge, there are no problems in any of my models files, and all the apps are specified in INSTALLED_APPS in my settings file. Manage.py syndb jus... | 4 | 2009-09-16T21:17:42Z | 1,435,758 | <p>I think I ran across something similar.</p>
<p>I had an issue where a model wasn't being reset.
In this case it turned out that there was an error in my models that wasn't being spit out.</p>
<p>Although I think syncdb, when run, spit out some kind of error.</p>
<p>In any case try to import your models file from ... | 8 | 2009-09-16T22:06:55Z | [
"python",
"django",
"django-models",
"django-syncdb"
] |
manage.py syncdb doesn't add tables for some models | 1,435,523 | <p>My second not-so-adept question of the day: I have a django project with four installed apps. When I run manage.py syndb, it only creates tables for two of them. To my knowledge, there are no problems in any of my models files, and all the apps are specified in INSTALLED_APPS in my settings file. Manage.py syndb jus... | 4 | 2009-09-16T21:17:42Z | 1,436,024 | <p>Unfortunately, manage.py silently fails to load an app where there's an import error in its models.py (<a href="http://code.djangoproject.com/ticket/10706">ticket #10706</a>). Chances are that there's a typo in one of your models.py files... check all of the import statements closely (or use pylint). </p>
<p>Recent... | 6 | 2009-09-16T23:26:28Z | [
"python",
"django",
"django-models",
"django-syncdb"
] |
Matplotlib Legend for Scatter with custom colours | 1,435,535 | <p>I'm a bit of newbie at this and am trying to create a scatter chart with custom bubble sizes and colours. The chart displays fine but how do I get a legend saying what the colours refer to. This is as far as I've got:</p>
<pre><code>inc = []
out = []
bal = []
col = []
fig=Figure()
ax=fig.add_subplot(111)
inc = ... | 10 | 2009-09-16T21:20:11Z | 1,435,635 | <p>Have a look into this:</p>
<p><a href="http://matplotlib.sourceforge.net/api/pyplot%5Fapi.html#matplotlib.pyplot.legend" rel="nofollow">http://matplotlib.sourceforge.net/api/pyplot%5Fapi.html#matplotlib.pyplot.legend</a></p>
<p>Hope that helps. If not just ask for more :)</p>
| -3 | 2009-09-16T21:38:28Z | [
"python",
"charts",
"matplotlib"
] |
Matplotlib Legend for Scatter with custom colours | 1,435,535 | <p>I'm a bit of newbie at this and am trying to create a scatter chart with custom bubble sizes and colours. The chart displays fine but how do I get a legend saying what the colours refer to. This is as far as I've got:</p>
<pre><code>inc = []
out = []
bal = []
col = []
fig=Figure()
ax=fig.add_subplot(111)
inc = ... | 10 | 2009-09-16T21:20:11Z | 1,435,977 | <p>Maybe this <a href="http://en.wikipedia.org/wiki/File%3aWeight_Growth_of_RN_First_Rate_Line-of-Battle_Ships_1630-1875.svg" rel="nofollow">example</a> is helpful.</p>
<p>In general, the items in the legend are related with some kind of <em>plotted object</em>. The <code>scatter</code> function/method treats all circ... | 9 | 2009-09-16T23:06:52Z | [
"python",
"charts",
"matplotlib"
] |
Getting Started with Tornado | 1,435,896 | <p>After installing the necessary packages through apt (python 2.5, simplejson etc) I get an error when I try to run the demos.</p>
<pre>
<code>
: Request instance has no attribute 'responseHeaders'
/usr/lib/python2.5/site-packages/tornado/web.py, line 404 in flush
402 for k,v in self._generate_headers():
403 ... | 3 | 2009-09-16T22:43:30Z | 1,436,012 | <p>I was under the impression tornado didn't depend on twisted. Have you tried the "official" version? line 404 is completely different.</p>
<p><a href="http://github.com/facebook/tornado/blob/master/tornado/web.py" rel="nofollow">http://github.com/facebook/tornado/blob/master/tornado/web.py</a></p>
<pre><code>def f... | 2 | 2009-09-16T23:21:32Z | [
"python",
"twisted",
"tornado"
] |
Primer on writing documentation with Sphinx? | 1,436,092 | <p>I followed a tutorial that showed only how to build it, I have it up and running on my web host but I'm a bit confused on how I should be creating and writing documentation.</p>
<p>So far I've made a small little edit to the index.rst file and made a new reference to a page, then it obviously complains that it's an... | 5 | 2009-09-16T23:57:52Z | 1,436,131 | <p>From your question I can't tell if the <a href="http://matplotlib.sourceforge.net/sampledoc/index.html" rel="nofollow">sampledoc</a> tutorial is the tutorial that you've already tried. If you haven't tried it I suggest you do.</p>
| 6 | 2009-09-17T00:12:12Z | [
"python",
"documentation",
"python-sphinx"
] |
Problem with building Boost Graph Library Python bindings under Leopard | 1,436,182 | <p>I've inherited some Python code which is importing boost.graph and I'm having an issue setting up the following under Mac OS X Leopard (I believe this is what I need to install to get it working):</p>
<p><a href="http://osl.iu.edu/~dgregor/bgl-python/" rel="nofollow">http://osl.iu.edu/~dgregor/bgl-python/</a></p>
... | 0 | 2009-09-17T00:38:22Z | 1,443,154 | <p>This error suggests the project is not a standalone, and is meant to be put inside the Boost source tree.</p>
| 0 | 2009-09-18T08:07:41Z | [
"python",
"boost",
"graph",
"osx-leopard",
"binding"
] |
state of HTML after onload javascript | 1,436,211 | <p>many webpages use onload JavaScript to manipulate their DOM. Is there a way I can automate accessing the state of the HTML after these JavaScript operations?</p>
<p>A took like wget is not useful here because it just downloads the original source.
Is there perhaps a way to use a web browser rendering engine?</p>
... | 1 | 2009-09-17T00:51:50Z | 1,436,306 | <p>The only good way I know to do such things is to automate a browser, for example via <a href="http://seleniumhq.org/projects/remote-control/" rel="nofollow">Selenium RC</a>. If you have no idea of how to deduce that the page has finished running the relevant javascript, then, just a real live user visiting that page... | 2 | 2009-09-17T01:29:14Z | [
"javascript",
"python",
"html",
"screen-scraping"
] |
state of HTML after onload javascript | 1,436,211 | <p>many webpages use onload JavaScript to manipulate their DOM. Is there a way I can automate accessing the state of the HTML after these JavaScript operations?</p>
<p>A took like wget is not useful here because it just downloads the original source.
Is there perhaps a way to use a web browser rendering engine?</p>
... | 1 | 2009-09-17T00:51:50Z | 1,436,325 | <p>Please see related info at stackoverflow:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/tagged/screen-scraping">screen-scraping</a> </li>
<li><a href="http://stackoverflow.com/questions/857515/screen-scraping-from-a-web-page-with-a-lot-of-javascript">Screen Scraping from a web page with a lot of Javasc... | 1 | 2009-09-17T01:34:30Z | [
"javascript",
"python",
"html",
"screen-scraping"
] |
Fixing broken urls | 1,436,382 | <p>Does anyone know of a library for fixing "broken" urls. When I try to open a url such as </p>
<pre><code>http://www.domain.com/../page.html
http://www.domain.com//page.html
http://www.domain.com/page.html#stuff
</code></pre>
<p>urllib2.urlopen chokes and gives me an HTTPError traceback. Does anyone know of a lib... | 0 | 2009-09-17T02:02:06Z | 1,436,541 | <p>What about something like...:</p>
<pre><code>import re
import urlparse
urls = '''
http://www.domain.com/../page.html
http://www.domain.com//page.html
http://www.domain.com/page.html#stuff
'''.split()
def main():
for u in urls:
pieces = list(urlparse.urlparse(u))
pieces[2] = re.sub(r'^[./]*', '/', pieces... | 2 | 2009-09-17T03:10:31Z | [
"python",
"url",
"urllib2"
] |
Fixing broken urls | 1,436,382 | <p>Does anyone know of a library for fixing "broken" urls. When I try to open a url such as </p>
<pre><code>http://www.domain.com/../page.html
http://www.domain.com//page.html
http://www.domain.com/page.html#stuff
</code></pre>
<p>urllib2.urlopen chokes and gives me an HTTPError traceback. Does anyone know of a lib... | 0 | 2009-09-17T02:02:06Z | 1,440,536 | <p>See also <a href="http://stackoverflow.com/questions/120951/how-can-i-normalize-a-url-in-python">this question</a>.</p>
| 0 | 2009-09-17T18:25:35Z | [
"python",
"url",
"urllib2"
] |
Python - Iterate over all classes | 1,436,384 | <p>How can I iterate over a list of all classes loaded in memory?
I'm thinking of doing it for a backup, looking for all classes inheriting from db.Model (Google App Engine). </p>
<p>Thanks,
Neal Walters </p>
| 2 | 2009-09-17T02:03:31Z | 1,436,401 | <p>Classes are defined in modules. Modules are created by an <code>import</code> statement.</p>
<p>Modules are simply dictionaries. If you want, you can use the <code>dir(x)</code> function on a module named <code>x</code></p>
<p>Or you can use <code>x.__dict__</code> on a module named <code>x</code>.</p>
| 2 | 2009-09-17T02:09:30Z | [
"python",
"google-app-engine",
"loops"
] |
Python - Iterate over all classes | 1,436,384 | <p>How can I iterate over a list of all classes loaded in memory?
I'm thinking of doing it for a backup, looking for all classes inheriting from db.Model (Google App Engine). </p>
<p>Thanks,
Neal Walters </p>
| 2 | 2009-09-17T02:03:31Z | 1,436,451 | <p>In "normal" Python, you can reach all objects via the <code>gc.getobjects()</code> function of the <code>gc</code> standard library module; it's then very easy to loop on them, checking which one are classes (rather than instances or anything else -- I do believe you mean <em>instances</em> of classes, but you can v... | 9 | 2009-09-17T02:34:35Z | [
"python",
"google-app-engine",
"loops"
] |
Python - Iterate over all classes | 1,436,384 | <p>How can I iterate over a list of all classes loaded in memory?
I'm thinking of doing it for a backup, looking for all classes inheriting from db.Model (Google App Engine). </p>
<p>Thanks,
Neal Walters </p>
| 2 | 2009-09-17T02:03:31Z | 1,436,472 | <p>Based on S.Lott's response:
This works if I omit the "if issubclass" except then I get classes I don't want.</p>
<pre><code> import dbModels
self.response.out.write("<br/><br/>Class Names:</br/>")
for item in dbModels.__dict__:
if issubclass(item, db.Model):
self.response.out... | 0 | 2009-09-17T02:40:47Z | [
"python",
"google-app-engine",
"loops"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 1,436,450 | <p>From googling around some , e.g. <a href="http://www.python.org/doc/2.5/whatsnew/pep-353.html" rel="nofollow">PEP 353</a> (assuming I'm understanding it) and <a href="http://mail.python.org/pipermail/python-list/2006-January/532801.html" rel="nofollow">this exchange</a> it looks like the real issue is probably platf... | 0 | 2009-09-17T02:34:33Z | [
"python"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 1,436,482 | <blockquote>
<p>10B booleans (1.25MB of memory, assuming Python is sane)</p>
</blockquote>
<p>I think you have your arithmetic wrong -- stored supercompactly, 10B booleans would be 1.25 <strong>GIGA</strong>, _not__ <strong>MEGA</strong>, bytes.</p>
<p>A list takes at least 4 bytes per item, so you'd need 40GB to d... | 2 | 2009-09-17T02:43:32Z | [
"python"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 1,436,491 | <p>With a 32-bit address space, any language is going to be struggling to be able to address such an array. Then there's the problem of how much real memory you have on your computer.</p>
<p>If you want 10B array elements, each element representing either true or false, use an array.array('I', ...) ...</p>
<pre><code... | 5 | 2009-09-17T02:46:39Z | [
"python"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 1,436,547 | <p>A dense bit vector is plausible but it won't be optimal unless you know you won't have more than about <code>10**10</code> elements, all clustered near each other, with a reasonably randomized distribution. If you have a different distribution, then a different structure will be better. </p>
<p>For instance, if y... | 3 | 2009-09-17T03:13:53Z | [
"python"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 1,436,606 | <p>The <a href="http://pypi.python.org/pypi/bitarray/" rel="nofollow">bitarray</a> package looks like it might be another useful option.</p>
| 4 | 2009-09-17T03:39:22Z | [
"python"
] |
long-index arrays in python | 1,436,411 | <p>I'm attempting to shorten the memory footprint of 10B sequential integers by referencing them as indexes in a boolean array. In other words, I need to create an array of 10,000,000,000 elements, but that's well into the "Long" range. When I try to reference an array index greater than sys.maxint the array blows up:<... | 2 | 2009-09-17T02:18:22Z | 2,555,287 | <p>Another option for very large bit arrays would be to use <a href="http://python-bitstring.googlecode.com" rel="nofollow">bitstring</a>. It uses a <code>bytearray</code> (or <code>array.array</code> on older Python versions) to store the data but its interface is just an array of bits. For your case you could use:</p... | 1 | 2010-03-31T18:04:31Z | [
"python"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.