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 |
|---|---|---|---|---|---|---|---|---|---|
Change directory to the directory of a Python script | 509,742 | <p>How do i change directory to the directory with my python script in? So far I figured out I should use <code>os.chdir</code> and <code>sys.argv[0]</code>. I'm sure there is a better way then to write my own function to parse argv[0].</p>
| 4 | 2009-02-04T01:24:43Z | 23,595,382 | <p><code>os.chdir(os.path.dirname(os.path.abspath(__file__)))</code> should do it. </p>
<p><code>os.chdir(os.path.dirname(__file__))</code> would not work if the script is run from the directory in which it is present.</p>
| 3 | 2014-05-11T16:56:16Z | [
"python",
"scripting",
"directory"
] |
Change directory to the directory of a Python script | 509,742 | <p>How do i change directory to the directory with my python script in? So far I figured out I should use <code>os.chdir</code> and <code>sys.argv[0]</code>. I'm sure there is a better way then to write my own function to parse argv[0].</p>
| 4 | 2009-02-04T01:24:43Z | 33,669,495 | <p>on windows OS, if you call something like <strong><em>python somefile.py</em></strong> this <strong><em>os.chdir(os.path.dirname(__file__))</em></strong> will throw a WindowsError. But this should work for all cases: </p>
<pre><code>import os
absFilePath = os.path.abspath(__file__)
os.chdir( os.path.dirname(absFile... | 0 | 2015-11-12T10:37:18Z | [
"python",
"scripting",
"directory"
] |
Best way to write a Python function that integrates a gaussian? | 509,994 | <p>In attempting to use scipy's quad method to integrate a gaussian (lets say there's a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional functi... | 6 | 2009-02-04T03:49:20Z | 510,014 | <p>Why not just always do your integration from -infinity to +infinity, so that you always know the answer? (joking!)</p>
<p>My guess is that the only reason that there's not already a canned Gaussian function in SciPy is that it's a trivial function to write. Your suggestion about writing your own function and passin... | 2 | 2009-02-04T03:59:32Z | [
"python",
"scipy",
"gaussian",
"integral"
] |
Best way to write a Python function that integrates a gaussian? | 509,994 | <p>In attempting to use scipy's quad method to integrate a gaussian (lets say there's a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional functi... | 6 | 2009-02-04T03:49:20Z | 510,037 | <p>I assume you're handling multivariate Gaussians; if so, SciPy already has the function you're looking for: it's called MVNDIST ("MultiVariate Normal DISTribution). The SciPy documentation is, as ever, terrible, so I can't even find where the function is buried, but <a href="http://www.google.com/search?q=site%3Ascip... | 3 | 2009-02-04T04:10:36Z | [
"python",
"scipy",
"gaussian",
"integral"
] |
Best way to write a Python function that integrates a gaussian? | 509,994 | <p>In attempting to use scipy's quad method to integrate a gaussian (lets say there's a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional functi... | 6 | 2009-02-04T03:49:20Z | 510,040 | <p>scipy ships with the "error function", aka Gaussian integral:</p>
<pre><code>import scipy.special
help(scipy.special.erf)
</code></pre>
| 12 | 2009-02-04T04:11:03Z | [
"python",
"scipy",
"gaussian",
"integral"
] |
Best way to write a Python function that integrates a gaussian? | 509,994 | <p>In attempting to use scipy's quad method to integrate a gaussian (lets say there's a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional functi... | 6 | 2009-02-04T03:49:20Z | 511,155 | <p>Okay, you appear to be pretty confused about several things. Let's start at the beginning: you mentioned a "multidimensional function", but then go on to discuss the usual one-variable Gaussian curve. This is <em>not</em> a multidimensional function: when you integrate it, you only integrate one variable (x). The di... | 27 | 2009-02-04T12:32:51Z | [
"python",
"scipy",
"gaussian",
"integral"
] |
Best way to write a Python function that integrates a gaussian? | 509,994 | <p>In attempting to use scipy's quad method to integrate a gaussian (lets say there's a gaussian method named gauss), I was having problems passing needed parameters to gauss and leaving quad to do the integration over the correct variable. Does anyone have a good example of how to use quad w/ a multidimensional functi... | 6 | 2009-02-04T03:49:20Z | 4,724,815 | <p>The gaussian distribution is also called a normal distribution. The cdf function in the scipy norm module does what you want.</p>
<pre><code>from scipy.stats import norm
print norm.cdf(0.0)
>>>0.5
</code></pre>
<p><a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.sta... | 3 | 2011-01-18T13:49:59Z | [
"python",
"scipy",
"gaussian",
"integral"
] |
How to call a python function from a foreign language thread (C++) | 510,085 | <p>I am developing a program that use DirectShow to grab audio data from
media files. DirectShow use thread to pass audio data to the callback
function in my program, and I let that callback function call another
function in Python.</p>
<p>I use Boost.Python to wrapper my library, the callback function :</p>
<pre><co... | 4 | 2009-02-04T04:39:02Z | 510,094 | <p>Have the c++ callback place the data in a queue. Have the python code poll the queue to extract the data.</p>
| 1 | 2009-02-04T04:42:12Z | [
"c++",
"python",
"multithreading",
"boost",
"locking"
] |
How to call a python function from a foreign language thread (C++) | 510,085 | <p>I am developing a program that use DirectShow to grab audio data from
media files. DirectShow use thread to pass audio data to the callback
function in my program, and I let that callback function call another
function in Python.</p>
<p>I use Boost.Python to wrapper my library, the callback function :</p>
<pre><co... | 4 | 2009-02-04T04:39:02Z | 511,740 | <p>Take a look at PyGILState_Ensure()/PyGILState_Release(), from PEP 311
<a href="http://www.python.org/dev/peps/pep-0311/">http://www.python.org/dev/peps/pep-0311/</a></p>
<p>Here is an example taken from the PEP itself:</p>
<pre><code>void SomeCFunction(void)
{
/* ensure we hold the lock */
PyGILState_STATE... | 6 | 2009-02-04T15:06:30Z | [
"c++",
"python",
"multithreading",
"boost",
"locking"
] |
creating blank field and receving the INTEGER PRIMARY KEY with sqlite, python | 510,135 | <p>I am using sqlite with python. When i insert into table A i need to feed it an ID from table B. So what i wanted to do is insert default data into B, grab the id (which is auto increment) and use it in table A. Whats the best way receive the key from the table i just inserted into?</p>
| 3 | 2009-02-04T05:06:34Z | 510,167 | <p>Check out <a href="http://www.sqlite.org/c3ref/last_insert_rowid.html" rel="nofollow">sqlite3_last_insert_rowid()</a> -- it's probably what you're looking for:</p>
<blockquote>
<p>Each entry in an SQLite table has a
unique 64-bit signed integer key
called the "rowid". The rowid is
always available as an und... | 1 | 2009-02-04T05:28:16Z | [
"python",
"sqlite"
] |
creating blank field and receving the INTEGER PRIMARY KEY with sqlite, python | 510,135 | <p>I am using sqlite with python. When i insert into table A i need to feed it an ID from table B. So what i wanted to do is insert default data into B, grab the id (which is auto increment) and use it in table A. Whats the best way receive the key from the table i just inserted into?</p>
| 3 | 2009-02-04T05:06:34Z | 510,182 | <p>As Christian said, <code>sqlite3_last_insert_rowid()</code> is what you want... but that's the C level API, and you're using the Python DB-API bindings for SQLite.</p>
<p>It looks like the cursor method <code>lastrowid</code> will do what you want <a href="http://docs.python.org/library/sqlite3.html" rel="nofollow"... | 7 | 2009-02-04T05:34:27Z | [
"python",
"sqlite"
] |
creating blank field and receving the INTEGER PRIMARY KEY with sqlite, python | 510,135 | <p>I am using sqlite with python. When i insert into table A i need to feed it an ID from table B. So what i wanted to do is insert default data into B, grab the id (which is auto increment) and use it in table A. Whats the best way receive the key from the table i just inserted into?</p>
| 3 | 2009-02-04T05:06:34Z | 511,610 | <p>Simply use:</p>
<pre><code>SELECT last_insert_rowid();
</code></pre>
<p>However, if you have multiple connections writing to the database, you might not get back the key that you expect.</p>
| 1 | 2009-02-04T14:36:36Z | [
"python",
"sqlite"
] |
How do I use django mptt? | 510,339 | <p>I have a model:</p>
<pre><code>class Company(models.Model):
name = models.CharField( max_length=100)
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
mptt.register(Company, order_insertion_by=['name'])
</code></pre>
<p>and </p>
<pre><code>class Financials(models.Model):
... | 1 | 2009-02-04T07:01:32Z | 510,834 | <p>I don't quite follow your question. A tree stores one type of object, in your case Company. To link Financials to Company just add a foreign key from Financials to Company.</p>
<p>If this doesn't help please expand your question to give us some more detail about what you are trying to achieve.</p>
| 1 | 2009-02-04T10:45:07Z | [
"python",
"django",
"django-models",
"django-mptt"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 64,486 | <p>You can use the sleep() function in the time module. It can take a float argument for sub second resolution.</p>
<pre><code>from time import sleep
sleep(0.1) # Time in seconds.
</code></pre>
| 330 | 2008-09-15T16:34:29Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 510,351 | <pre><code>import time
time.sleep(5) # delays for 5 seconds
</code></pre>
<p>Here is another example where something is run once a minute:</p>
<pre><code>import time
while True:
print "This prints once a minute."
time.sleep(60) # Delay for 1 minute (60 seconds)
</code></pre>
| 1,176 | 2009-02-04T07:05:59Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 510,356 | <p>Please read <a href="http://www.faqts.com/knowledge_base/view.phtml/aid/2609/fid/378">http://www.faqts.com/knowledge_base/view.phtml/aid/2609/fid/378</a>, which can help you further:</p>
<blockquote>
<p>Try the sleep function in the time module.</p>
<pre><code>import time
time.sleep(60)
</code></pre>
<p>And... | 40 | 2009-02-04T07:07:32Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 6,234,820 | <p>What You Need Is </p>
<p><code>time.sleep(sec)</code></p>
<p>where sec is how many seconds delay you add there</p>
<p>you also need to </p>
<p><code>import time</code></p>
| 57 | 2011-06-04T05:03:16Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 22,583,596 | <p>There is a built-in Python module called <code>time</code>. Use it like this:</p>
<pre><code>import time
time.sleep(5)
</code></pre>
<p>This will make the script wait for 5 seconds.</p>
| 153 | 2014-03-22T21:06:10Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 23,131,522 | <p>There is a built in python module named time. </p>
<p>The 2 examples are identical but differ only in the way the method is imported from the module:</p>
<p>1 Use This:</p>
<pre><code> import time
time.sleep(Num of seconds to sleep)
</code></pre>
<p>2 Use this:</p>
<pre><code> from time import sleep
sleep(Num... | 19 | 2014-04-17T11:08:36Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 23,665,492 | <p>A bit of fun with sleepy generator.</p>
<p>The question is about time delay. It can be fixed time, but in some cases we might need a delay measured since last time. Here is one possible solutions:</p>
<h1>Delay measured since last time (waking up regularly)</h1>
<p>The situation can be, we want to do something as... | 18 | 2014-05-14T21:30:35Z | [
"python",
"sleep",
"timedelay"
] |
How can I make a time delay in Python? | 510,348 | <p>I would like to know how to put a time delay in a Python script.</p>
| 1,061 | 2009-02-04T07:04:09Z | 28,965,832 | <p>to halt further execution for defined time, sleep method is available in time package.
it easy all you have to do is at the top of your code type in<br>
<code>from time import sleep</code><br>
then use sleep in your desired code let say 5 seconds:</p>
<p><code>sleep(5)</code></p>
<p>alternatively you can impor... | 18 | 2015-03-10T14:04:40Z | [
"python",
"sleep",
"timedelay"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 510,364 | <p>Here's a link to a site that says how you can read a single character in Windows, Linux and OSX: <a href="http://code.activestate.com/recipes/134892/">http://code.activestate.com/recipes/134892/</a></p>
<pre><code>class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
... | 105 | 2009-02-04T07:11:04Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 510,404 | <pre><code>sys.stdin.read(1)
</code></pre>
<p>will basically read 1 byte from STDIN.</p>
<p>If you must use the method which does not wait for the <code>\n</code> you can use this code as suggested in previous answer:</p>
<pre><code>class _Getch:
"""Gets a single character from standard input. Does not echo to ... | 54 | 2009-02-04T07:30:51Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 510,897 | <p>I think it gets extremely clunky at this point, and debugging on the different platforms is a big mess.</p>
<p>You'd be better off using something like pyglet, pygame, cocos2d - if you are doing something more elaborate than this and will need visuals, OR <strong>curses</strong> if you are going to work with the te... | 12 | 2009-02-04T11:04:26Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 7,259,460 | <p>An alternative method:</p>
<pre><code>import os
import sys
import termios
import fcntl
def getch():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr... | 14 | 2011-08-31T15:30:40Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 20,865,751 | <p>This code, based off <a href="http://code.activestate.com/recipes/134892/">here</a>, will correctly raise KeyboardInterrupt and EOFError if <kbd>Ctrl</kbd>+<kbd>C</kbd> or <kbd>Ctrl</kbd>+<kbd>D</kbd> are pressed.</p>
<p>Should work on Windows and Linux. An OS X version is available from the original source.</p>
<... | 9 | 2014-01-01T05:13:33Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 21,659,588 | <p>The ActiveState <a href="http://code.activestate.com/recipes/134892/">recipe</a> quoted verbatim in two answers is over-engineered. It can be boiled down to this:</p>
<pre><code>def _find_getch():
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
... | 31 | 2014-02-09T13:27:42Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 24,355,550 | <p>This is NON-BLOCKING, reads a key and and stores it in keypress.key. </p>
<pre><code>import Tkinter as tk
class Keypress:
def __init__(self):
self.root = tk.Tk()
self.root.geometry('300x200')
self.root.bind('<KeyPress>', self.onKeyPress)
def onKeyPress(self, event):
... | 2 | 2014-06-22T20:40:36Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 25,342,814 | <p>Also worth trying is the <a href="https://github.com/magmax/python-readchar">readchar</a> library, which is in part based on the ActiveState recipe mentioned in other answers.</p>
<p>Installation:</p>
<pre><code>pip install readchar
</code></pre>
<p>Usage:</p>
<pre><code>import readchar
print("Reading a char:")
... | 21 | 2014-08-16T18:47:41Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 26,089,126 | <p>This might be a use case for a context manager. Leaving aside allowances for Windows OS, here's my suggestion:</p>
<pre><code>#!/usr/bin/env python3
# file: 'readchar.py'
"""
Implementation of a way to get a single character of input
without waiting for the user to hit <Enter>.
(OS is Linux, Ubuntu 14.04)
""... | 4 | 2014-09-28T20:07:19Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 27,693,470 | <p>The <code>curses</code> package in python can be used to enter "raw" mode for character input from the terminal with just a few statements. Curses' main use is to take over the screen for output, which may not be what you want. This code snippet uses <code>print()</code> statements instead, which are usable, but yo... | 0 | 2014-12-29T17:38:01Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 28,640,609 | <p>Try this with pygame:</p>
<pre><code>import pygame
pygame.init() // eliminate error, pygame.error: video system not initialized
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
d = "space key"
print "You pressed the", d, "."
</code></pre>
| 2 | 2015-02-21T00:36:21Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 31,550,142 | <p>Try using this: <a href="http://home.wlu.edu/~levys/software/kbhit.py" rel="nofollow">http://home.wlu.edu/~levys/software/kbhit.py</a>
It's non-blocking (that means that you can have a while loop and detect a key press without stopping it) and cross-platform.</p>
<pre><code>import os
# Windows
if os.name == 'nt':
... | 2 | 2015-07-21T21:42:36Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 31,749,681 | <p>The answers <a href="http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/" rel="nofollow">here</a> were informative, however I also wanted a way to get key presses asynchronously and fire off key presses in separate events, all in a thread-safe, cross-platform way. PyGame wa... | 4 | 2015-07-31T15:17:50Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 34,299,151 | <p>The build-in raw_input should help. </p>
<pre><code>for i in range(3):
print ("So much work to do!")
k = raw_input("Press any key to continue...")
print ("Ok, back to work.")
</code></pre>
| 1 | 2015-12-15T20:51:36Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 35,051,309 | <p>My solution for python3, not depending on any pip packages.</p>
<pre><code># precondition: import tty, sys
def query_yes_no(question, default=True):
"""
Ask the user a yes/no question.
Returns immediately upon reading one-char answer.
Accepts multiple language characters for yes/no.
"""
if n... | 0 | 2016-01-28T01:24:52Z | [
"python",
"input"
] |
Python read a single character from the user | 510,357 | <p>Is there a way of reading one single character from the user input? For instance, they press one key at the terminal and it is returned (sort of like <code>getch()</code>). I know there's a function in Windows for it, but I'd like something that is cross-platform.</p>
| 157 | 2009-02-04T07:08:03Z | 36,974,338 | <p>The (currently) top-ranked answer (with the ActiveState code) is overly complicated. I don't see a reason to use classes when a mere function should suffice. Below are two implementations that accomplish the same thing but with more readable code.</p>
<p><strong>Both of these implementations:</strong></p>
<ol>
<li... | 2 | 2016-05-02T02:49:47Z | [
"python",
"input"
] |
Is there a way to get the current ref count of an object in Python? | 510,406 | <p>Is there a way to get the current ref count of an object in Python?</p>
| 34 | 2009-02-04T07:32:51Z | 510,411 | <p>Using the <code>gc</code> module, the interface to the garbage collector guts, you can call <code>gc.get_referrers(foo)</code> to get a list of everything referring to <code>foo</code>.</p>
<p>Hence, <code>len(gc.get_referrers(foo))</code> will give you the length of that list: the number of referrers, which is wha... | 36 | 2009-02-04T07:36:25Z | [
"python",
"refcounting"
] |
Is there a way to get the current ref count of an object in Python? | 510,406 | <p>Is there a way to get the current ref count of an object in Python?</p>
| 34 | 2009-02-04T07:32:51Z | 510,417 | <p>According to some Python 2.0 reference (<a href="http://www.brunningonline.net/simon/python/quick-ref2_0.html">http://www.brunningonline.net/simon/python/quick-ref2_0.html</a>), the sys module contains a function:</p>
<pre><code>import sys
sys.getrefcount(object) #-- Returns the reference count of the object.
</cod... | 41 | 2009-02-04T07:39:03Z | [
"python",
"refcounting"
] |
Using Python Ctypes for ssdeep's fuzzy.dll but receive error | 510,443 | <p>I am trying to use Python and ctypes to use the <code>fuzzy.dll</code> from ssdeep. So far everything I have tried fails with an access violation error. Here is what I do after changing to the proper directory which contains the <code>fuzzy.dll</code> and <code>fuzzy.def</code> files:</p>
<pre><code>>>> im... | 2 | 2009-02-04T07:52:50Z | 511,749 | <p>There are two problems with your code:</p>
<ol>
<li><p>You should not use <code>windll.fuzzy</code>, but <code>cdll.fuzzy</code> -- from <a href="http://docs.python.org/library/ctypes.html#loading-dynamic-link-libraries" rel="nofollow">ctypes documentation</a>:</p>
<blockquote>
<p>cdll loads libraries which expo... | 4 | 2009-02-04T15:09:27Z | [
"python",
"ctypes"
] |
Spawn subprocess that expects console input without blocking? | 510,751 | <p>I am trying to do a CVS login from Python by calling the cvs.exe process.
When calling cvs.exe by hand, it prints a message to the console and then waits for the user to input the password. </p>
<p>When calling it with subprocess.Popen, I've noticed that the call blocks. The code is </p>
<pre><code>subprocess.Pope... | 4 | 2009-02-04T10:07:18Z | 510,889 | <ul>
<li>Remove the <code>shell=True</code> part. Your shell has nothing to do with it. Using <code>shell=True</code> is a common cause of trouble.</li>
<li>Use a list of parameters for cmd.</li>
</ul>
<p>Example:</p>
<pre><code>cmd = ['cvs',
'-d:pserver:anonymous@bayonne.cvs.sourceforge.net:/cvsroot/bayonne... | 2 | 2009-02-04T11:00:35Z | [
"python",
"windows",
"subprocess"
] |
Spawn subprocess that expects console input without blocking? | 510,751 | <p>I am trying to do a CVS login from Python by calling the cvs.exe process.
When calling cvs.exe by hand, it prints a message to the console and then waits for the user to input the password. </p>
<p>When calling it with subprocess.Popen, I've noticed that the call blocks. The code is </p>
<pre><code>subprocess.Pope... | 4 | 2009-02-04T10:07:18Z | 513,521 | <p>If you are automating external programs that need input - like password - your best bet would probably be to use <a href="http://www.noah.org/wiki/Pexpect" rel="nofollow">pexpect</a>.</p>
| 0 | 2009-02-04T21:50:13Z | [
"python",
"windows",
"subprocess"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 510,858 | <p>Simple solution: don't unit test through dbus.</p>
<p>Instead write your unit tests to call your methods directly. That fits in more naturally with the nature of unit tests.</p>
<p>You might also want some automated integration tests, that check running through dbus, but they don't need to be so complete, nor run ... | 3 | 2009-02-04T10:52:45Z | [
"python",
"unit-testing",
"dbus"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 510,910 | <p>I might be a bit out of my league here, since I don't know python and only somewhat understand what this magical "dbus" is, but if I understand correctly, it requires you to create a rather unusual testing environment with runloops, extended setup/teardown, and so on.</p>
<p>The answer to your problem is to use <a ... | 2 | 2009-02-04T11:10:34Z | [
"python",
"unit-testing",
"dbus"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 511,168 | <p>You just need to make sure you are handling your main loop properly.</p>
<pre><code>def refresh_ui():
while gtk.events_pending():
gtk.main_iteration_do(False)
</code></pre>
<p>This will run the gtk main loop until it has finished processing everything, rather than just run it and block.</p>
<p>For a co... | 2 | 2009-02-04T12:36:02Z | [
"python",
"unit-testing",
"dbus"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 512,723 | <p>With some help from Ali A's post, I have managed to solve my problem. The blocking event loop needed to be launched into a separate process, so that it can listen for events without blocking the test.</p>
<p>Please be aware my question title contained some incorrect terminology, I was trying to write a functional t... | 6 | 2009-02-04T18:46:09Z | [
"python",
"unit-testing",
"dbus"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 762,079 | <p>You could also start the mainloop in a separate thread very simply inside your setUp method.</p>
<p>Something like this:</p>
<pre><code>import threading
class BaseTestCase(unittest.TestCase):
def setUp(self):
myservice = MyDBUSService()
self.loop = gobject.MainLoop()
threading.Thread(na... | 2 | 2009-04-17T20:27:17Z | [
"python",
"unit-testing",
"dbus"
] |
How to write a functional test for a DBUS service written in Python? | 510,821 | <p>(Title was: "How to write a unit test for a DBUS service written in Python?")</p>
<p>I've started to write a DBUS service using dbus-python, but I'm having trouble writing a test case for it.</p>
<p>Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this ... | 12 | 2009-02-04T10:29:14Z | 19,105,422 | <p>Check out <a href="https://pypi.python.org/pypi/python-dbusmock/0.7.2" rel="nofollow">python-dbusmock</a> library.</p>
<p>It hides the ugly subprocess logic behind your eyes, so you don't have to worry about it in your tests.</p>
| 0 | 2013-09-30T23:13:35Z | [
"python",
"unit-testing",
"dbus"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 510,988 | <p>type() ?</p>
<pre><code>>>> class A(object):
... def whoami(self):
... print type(self).__name__
...
>>>
>>> class B(A):
... pass
...
>>>
>>>
>>> o = B()
>>> o.whoami()
'B'
>>>
</code></pre>
| 52 | 2009-02-04T11:42:31Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 511,059 | <p>Have you tried the <code>__name__</code> attribute of the class? ie <code>type(x).__name__</code> will give you the name of the class, which I think is what you want.</p>
<pre><code>>>> import itertools
>>> x = itertools.count(0)
>>> type(x).__name__
'count'
</code></pre>
<p>This method ... | 948 | 2009-02-04T12:02:12Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 511,060 | <p>Do you want the name of the class as a string?</p>
<pre><code>instance.__class__.__name__
</code></pre>
| 177 | 2009-02-04T12:02:16Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 9,383,568 | <p>Good question.</p>
<p>Here's a simple example based on GHZ's which might help someone:</p>
<pre><code>>>> class person(object):
def init(self,name):
self.name=name
def info(self)
print "My name is {0}, I am a {1}".format(self.name,self.__class__.__name__)
>>&g... | 9 | 2012-02-21T19:07:40Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 16,293,038 | <pre><code>type(instance).__name__ != instance.__class__.__name #if class A is defined like
class A():
...
type(instance) == instance.__class__ #if class A is defined like
class A(object):
...
</code></pre>
<p>Example:</p>
<pre><code>>>> class aclass(object):
... pass
...
>>&g... | 16 | 2013-04-30T05:50:38Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
Getting the class name of an instance in Python | 510,972 | <p>How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?</p>
<p>Was thinking maybe <a href="https://docs.python.org/2/library/inspect.html">the inspect module</a> might have helped me... | 689 | 2009-02-04T11:37:48Z | 24,130,402 | <pre><code>class A:
pass
a = A()
str(a.__class__)
</code></pre>
<p>The sample code above (when input in the interactive interpreter) will produce <code>'__main__.A'</code> as opposed to <code>'A'</code> which is produced if the <code>__name__</code> attribute is invoked. By simply passing the result of <code>A.__cl... | 12 | 2014-06-09T23:04:17Z | [
"python",
"introspection",
"instanceof",
"python-datamodel"
] |
How can I tell python which version of libmysqlclient.so to use? | 511,011 | <p>I'm running a python script on a shared hosting server which until this morning had MySQL version 4. Now it has version 5. My python script can no longer connect to MySQL, as it can't find libmysqlclient_r.so.14:</p>
<pre><code>$ python my_script.py
Traceback (most recent call last):
File "my_script.py", line 6, in... | 2 | 2009-02-04T11:48:54Z | 511,045 | <p>One solution is to set your <code>PYTHONPATH</code> environment variable to have some local directory, and copy over (or link, I suppose) the version of the mysql lib you want.</p>
| 0 | 2009-02-04T11:56:05Z | [
"python"
] |
How can I tell python which version of libmysqlclient.so to use? | 511,011 | <p>I'm running a python script on a shared hosting server which until this morning had MySQL version 4. Now it has version 5. My python script can no longer connect to MySQL, as it can't find libmysqlclient_r.so.14:</p>
<pre><code>$ python my_script.py
Traceback (most recent call last):
File "my_script.py", line 6, in... | 2 | 2009-02-04T11:48:54Z | 511,062 | <p>You will have to recompile python-mysql (aka MySQLdb) to get it to link to the new version of libmysqlclient.</p>
<p>If your host originally set up the environment rather than you compiling it, you'll have to pester them.</p>
<blockquote>
<p>/usr/lib/libmysqlclient.so.14</p>
</blockquote>
<p>This looks like a r... | 1 | 2009-02-04T12:02:32Z | [
"python"
] |
How can I tell python which version of libmysqlclient.so to use? | 511,011 | <p>I'm running a python script on a shared hosting server which until this morning had MySQL version 4. Now it has version 5. My python script can no longer connect to MySQL, as it can't find libmysqlclient_r.so.14:</p>
<pre><code>$ python my_script.py
Traceback (most recent call last):
File "my_script.py", line 6, in... | 2 | 2009-02-04T11:48:54Z | 511,103 | <p>You can't tell the dynamic linker which version of a library to use, because the SONAME (full name of the library + interface) is part of the binary.</p>
<p>In your case, you can try to upload libmysqlclient_r.so.14 to the host and set <code>LD_LIBRARY_PATH</code> accordingly, so tell the dynamic linker which direc... | 5 | 2009-02-04T12:16:53Z | [
"python"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 511,208 | <p>You can use the <code>replace</code> method:</p>
<pre><code>>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
</code></pre>
| 19 | 2009-02-04T12:53:50Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 511,216 | <p>If you don't want the newline at the end of the print statement:</p>
<pre><code>import sys
sys.stdout.write("text")
</code></pre>
| 22 | 2009-02-04T12:55:43Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 511,272 | <pre><code>>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
</code></pre>
| 1 | 2009-02-04T13:10:24Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 513,411 | <p>Add a comma after "print":</p>
<pre><code>print "Hai Hello",
print "Good eve",
</code></pre>
<p>Altho "print" is gone in Python 3.0</p>
| 3 | 2009-02-04T21:21:20Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 814,402 | <p>Not sure if this is what you're asking for, but you can use the triple-quoted string:</p>
<pre><code>print """Hey man
And here's a new line
you can put multiple lines inside this kind of string
without using \\n"""
</code></pre>
<p>Will print:</p>
<pre>
Hey man
And here's a new line
you can put multiple lines i... | 0 | 2009-05-02T08:12:09Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 1,038,910 | <p>In Python 2.6:</p>
<pre><code>print "Hello.",
print "This is on the same line"
</code></pre>
<p>In Python 3.0</p>
<pre><code>print("Hello", end = " ")
print("This is on the same line")
</code></pre>
| 11 | 2009-06-24T15:00:05Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 6,063,403 | <p>Way old post but nobody seemed to successfully answer your question. Two possible answers:</p>
<p>First, either your string is actually <code>Hai Hello\\\\nGood eve\\\\n</code> printing as <code>Hai Hello\\nGood eve\\n</code> due to the escaped <code>\\\\</code>. Simple fix would be <code>mystring.replace("\\\\n","... | 2 | 2011-05-19T18:45:20Z | [
"python",
"string"
] |
How to print a string without including '\n' in Python | 511,204 | <p>Suppose my string is:</p>
<pre><code>' Hai Hello\nGood eve\n'
</code></pre>
<p>How do I eliminate the <code>'\n'</code> in between and make a string print like :</p>
<pre><code> Hai Hello Good eve
</code></pre>
<p>?</p>
| 15 | 2009-02-04T12:52:14Z | 8,593,347 | <p>I realize this is a terribly old post, but I just ran into this also and wanted to add to it for clarity. What the original user is asking, I believe, is how to get the OS to interpret the string "some text\nsome more text" as:</p>
<blockquote>
<p>some text</p>
<p>some more text</p>
</blockquote>
<p>Inste... | 1 | 2011-12-21T16:49:50Z | [
"python",
"string"
] |
Django Installed Apps Location | 511,291 | <p>I am an experienced PHP programmer using Django for the first time, and I think it is incredible!</p>
<p>I have a project that has a lot of apps, so I wanted to group them in an apps folder.</p>
<p>So the structure of the project is:</p>
<pre><code>/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
<... | 13 | 2009-02-04T13:15:59Z | 511,371 | <p>As long as your apps are in your PYTHONPATH, everything should work. Try setting that environment variable to the folder containing your apps.</p>
<pre><code>PYTHONPATH="/path/to/your/apps/dir/:$PYTHONPATH"
</code></pre>
| 1 | 2009-02-04T13:33:40Z | [
"python",
"django"
] |
Django Installed Apps Location | 511,291 | <p>I am an experienced PHP programmer using Django for the first time, and I think it is incredible!</p>
<p>I have a project that has a lot of apps, so I wanted to group them in an apps folder.</p>
<p>So the structure of the project is:</p>
<pre><code>/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
<... | 13 | 2009-02-04T13:15:59Z | 511,372 | <p>Your top-level <code>urls.py</code> (also named in your <code>settings.py</code>) must be able to use a simple "import" statement to get your applications.</p>
<p>Does <code>import project.apps.app1.urls</code> work? If not, then your <code>PYTHONPATH</code> isn't set up properly, or you didn't install your projec... | 0 | 2009-02-04T13:33:42Z | [
"python",
"django"
] |
Django Installed Apps Location | 511,291 | <p>I am an experienced PHP programmer using Django for the first time, and I think it is incredible!</p>
<p>I have a project that has a lot of apps, so I wanted to group them in an apps folder.</p>
<p>So the structure of the project is:</p>
<pre><code>/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
<... | 13 | 2009-02-04T13:15:59Z | 511,408 | <p>Make sure that the '__init__.py' file is in your apps directory, if it's not there it won't be recognized as part of the package.</p>
<p>So each of the folders here should have '__init__.py' file in it. (empty is fine).</p>
<pre><code>/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
</code></pre>
<... | 35 | 2009-02-04T13:41:41Z | [
"python",
"django"
] |
Django Installed Apps Location | 511,291 | <p>I am an experienced PHP programmer using Django for the first time, and I think it is incredible!</p>
<p>I have a project that has a lot of apps, so I wanted to group them in an apps folder.</p>
<p>So the structure of the project is:</p>
<pre><code>/project/
/project/apps/
/project/apps/app1/
/project/apps/app2
<... | 13 | 2009-02-04T13:15:59Z | 34,711,354 | <p>In settings.py in project folder </p>
| 0 | 2016-01-10T22:08:52Z | [
"python",
"django"
] |
Combined Python & Ruby extension module | 511,412 | <p>I have a C extension module for Python and I want to make it available to Rubyists.</p>
<p>The source has a number of C modules, with only one being Python-dependent. The rest depend only on each other and the standard library. I can build it with <code>python setup.py build</code> in the usual way.</p>
<p>I've be... | 7 | 2009-02-04T13:42:04Z | 511,871 | <p>One way to solve it is to create three different projects:</p>
<ul>
<li>The library itself, independent on python & ruby</li>
<li>Python bindings</li>
<li>Ruby bindings</li>
</ul>
<p>That's probably the cleanest solution, albeit it requires a bit more work when doing releases, but it has the advantage that you... | 5 | 2009-02-04T15:35:56Z | [
"python",
"ruby",
"setuptools",
"newgem"
] |
Combined Python & Ruby extension module | 511,412 | <p>I have a C extension module for Python and I want to make it available to Rubyists.</p>
<p>The source has a number of C modules, with only one being Python-dependent. The rest depend only on each other and the standard library. I can build it with <code>python setup.py build</code> in the usual way.</p>
<p>I've be... | 7 | 2009-02-04T13:42:04Z | 514,483 | <p>Complementing on what Johan said, I've used a couple c/c++ support libraries in Python thanks to swig. You write your code in c/c++ then make an intermediary template for each language that you want to support. Its rather painless for Python, but some considerations must be made for Ruby... namely I don't think pt... | 0 | 2009-02-05T03:54:55Z | [
"python",
"ruby",
"setuptools",
"newgem"
] |
Why doesn't PyRun_String evaluate bool literals? | 512,036 | <p>I need to evaluate a Python expression from C++. This code seems to work:</p>
<pre><code>PyObject * dict = PyDict_New();
PyObject * val = PyRun_String(expression, Py_eval_input, dict, 0);
Py_DECREF(dict);
</code></pre>
<p>Unfortunately, it fails horribly if expression is "True" of "False" (that is, val is 0 and Py... | 4 | 2009-02-04T16:10:14Z | 512,815 | <pre><code>PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals);
</code></pre>
<p>If you want True and False they will have to be in the <code>*globals</code> dict passed to the interpreter. You might be able to fix that by calling <code>PyEval_GetBuiltins</code>.</p>
<p>From the Py... | 4 | 2009-02-04T19:07:32Z | [
"python",
"boolean",
"cpython"
] |
How to copy a directory and its contents to an existing location using Python? | 512,173 | <p>I'm trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn't seem to be a way to do this. the <code>shutil.copytree()</code> function expects that the destination path not exist beforehand.</p>
<p>The exact result I'm... | 32 | 2009-02-04T16:41:12Z | 512,232 | <p>Why not implement it on your own using <code>os.walk</code>?</p>
| 0 | 2009-02-04T16:56:19Z | [
"python",
"operating-system",
"filesystems",
"copy"
] |
How to copy a directory and its contents to an existing location using Python? | 512,173 | <p>I'm trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn't seem to be a way to do this. the <code>shutil.copytree()</code> function expects that the destination path not exist beforehand.</p>
<p>The exact result I'm... | 32 | 2009-02-04T16:41:12Z | 512,273 | <p><a href="http://docs.python.org/distutils/apiref.html#distutils.dir_util.copy_tree"><code>distutils.dir_util.copy_tree</code></a> does what you want.</p>
<blockquote>
<p>Copy an entire directory tree src to a
new location dst. Both src and dst
must be directory names. If src is not
a directory, raise Distut... | 41 | 2009-02-04T17:03:21Z | [
"python",
"operating-system",
"filesystems",
"copy"
] |
How to copy a directory and its contents to an existing location using Python? | 512,173 | <p>I'm trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn't seem to be a way to do this. the <code>shutil.copytree()</code> function expects that the destination path not exist beforehand.</p>
<p>The exact result I'm... | 32 | 2009-02-04T16:41:12Z | 513,588 | <p>For highlevel file operations like that use the <a href="http://docs.python.org/library/shutil.html" rel="nofollow">shutil</a> module and in your case the copytree function. I think that is cleaner than "abusing" distutils.</p>
<p><strong>UPDATE:</strong>: Forget the answer, I overlooked that the OP did try shutil.... | 1 | 2009-02-04T22:06:33Z | [
"python",
"operating-system",
"filesystems",
"copy"
] |
How to copy a directory and its contents to an existing location using Python? | 512,173 | <p>I'm trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn't seem to be a way to do this. the <code>shutil.copytree()</code> function expects that the destination path not exist beforehand.</p>
<p>The exact result I'm... | 32 | 2009-02-04T16:41:12Z | 615,450 | <p>Are you gettting the error that says "Cannot create a directory when its already present"?
I am not sure how much silly is this, but all i did was to insert a single line into copytree module:
I changed :</p>
<pre><code>def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
</code>... | 0 | 2009-03-05T16:08:51Z | [
"python",
"operating-system",
"filesystems",
"copy"
] |
How to access templates in Python? | 512,499 | <p>Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.</p>
<p>In Java, the most recommended way to retrieve a resource of the program is to use <code>getClass().getClassLoader().... | 2 | 2009-02-04T17:45:28Z | 512,676 | <p>You can use <code>os.path.dirname(__file__)</code> to get the directory of the current module. Then use the <a href="http://docs.python.org/library/os.path.html" rel="nofollow">path manipulation</a> functions (specifically, os.path.join) and <a href="http://docs.python.org/tutorial/inputoutput.html#reading-and-writ... | 2 | 2009-02-04T18:34:38Z | [
"python",
"templates"
] |
How to access templates in Python? | 512,499 | <p>Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.</p>
<p>In Java, the most recommended way to retrieve a resource of the program is to use <code>getClass().getClassLoader().... | 2 | 2009-02-04T17:45:28Z | 512,868 | <p>What Daniel said. :-) Also, py2exe can be told to include external files (this is often used for images etc).</p>
| 1 | 2009-02-04T19:19:38Z | [
"python",
"templates"
] |
gedit plugin development in python | 512,600 | <p>Does anyone know where information about writing gedit plugins can be found ? I'm interested in writing them in Python. I know of <a href="http://live.gnome.org/Gedit/PythonPluginHowTo">Gedit/PythonPluginHowTo</a>
, but it isn't very good . Besides the code of writing a plugin that does nothing , I can't seem to fi... | 10 | 2009-02-04T18:15:16Z | 513,696 | <p>Have you checked out <a href="http://sp2hari.com/gedit-plugin/" rel="nofollow">http://sp2hari.com/gedit-plugin/</a> ? I think this is as thorough as you're going to get, without poring through other people's code. </p>
| 4 | 2009-02-04T22:31:32Z | [
"python",
"plugins",
"gedit"
] |
gedit plugin development in python | 512,600 | <p>Does anyone know where information about writing gedit plugins can be found ? I'm interested in writing them in Python. I know of <a href="http://live.gnome.org/Gedit/PythonPluginHowTo">Gedit/PythonPluginHowTo</a>
, but it isn't very good . Besides the code of writing a plugin that does nothing , I can't seem to fi... | 10 | 2009-02-04T18:15:16Z | 536,198 | <p>When I started working on my gedit plugin, I used the howto you gave a link to, also startign with <a href="http://www.russellbeattie.com/blog/my-first-gedit-plugin">this URL</a>. Then it was looking at other plugins code... I'm sorry to say that, but for me this topic is poorly documented and best and fastest way i... | 7 | 2009-02-11T10:08:36Z | [
"python",
"plugins",
"gedit"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 512,921 | <p>Before you start tearing your hair out over the garbage collector, you might be able to avoid that 100mb hit of loading the entire file into memory by using a memory-mapped file object. See the <a href="http://docs.python.org/library/mmap.html" rel="nofollow">mmap</a> module.</p>
| 3 | 2009-02-04T19:29:07Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 512,931 | <p>Don't read the entire 100 meg file in at a time. Use streams to process a little bit at a time. Check out this blog post that talks about handling large csv and xml files. <a href="http://lethain.com/entry/2009/jan/22/handling-very-large-csv-and-xml-files-in-python/" rel="nofollow">http://lethain.com/entry/2009/j... | 3 | 2009-02-04T19:32:48Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 513,000 | <p>So, from your comments I assume that your file looks something like this:</p>
<pre><code>item1,item2,item3,item4,item5,item6,item7,...,itemn
</code></pre>
<p>which you all reduce to a single value by repeated application of some combination function. As a solution, only read a single value at a time:</p>
<pre><co... | 2 | 2009-02-04T19:56:08Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 513,996 | <p>First of all, don't touch the garbage collector. That's not the problem, nor the solution.</p>
<p>It sounds like the real problem you're having is not with the file reading at all, but with the data structures that you're allocating as you process the files.
Condering using <i>del</i> to remove structures that you ... | 0 | 2009-02-05T00:22:10Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 515,820 | <p>I'd suggest looking at the <a href="http://www.dabeaz.com/generators/">presentation by David Beazley</a> on using generators in Python. This technique allows you to handle a lot of data, and do complex processing, quickly and without blowing up your memory use. IMO, the trick isn't holding a huge amount of data in m... | 7 | 2009-02-05T13:09:36Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
memory use in large data-structures manipulation/processing | 512,893 | <p>I have a number of large (~100 Mb) files which I'm regularly processing. While I'm trying to delete unneeded data structures during processing, memory consumption is a bit too high. I was wondering if there is a way to efficiently manipulate large data, e.g.:</p>
<pre><code>def read(self, filename):
fc = read_1... | 4 | 2009-02-04T19:25:03Z | 544,113 | <p>In your example code, data is being stored in the <code>fc</code> variable. If you don't keep a reference to <code>fc</code> around, your entire file contents will be removed from memory when the <code>read</code> method ends. </p>
<p>If they are not, then <strong>you are keeping a reference somewhere</strong>. May... | 0 | 2009-02-12T23:54:11Z | [
"python",
"data-structures",
"memory-leaks",
"garbage-collection"
] |
Invoking Wine From Apache | 513,023 | <p>I have Apache/2.2.11 using mod_python 3.3.1/Python 2.5 running under Gentoo linux. In my python script I invoke a win32 exe using wine (os.popen2 call). This works fine outside of Apache but under mod_python I get:</p>
<p>wine: cannot open /root/.wine : Permission denied</p>
<p>in /var/log/apache/error_log. My ... | 0 | 2009-02-04T20:01:09Z | 513,031 | <p>It's probably because <code>$HOME</code> isn't set correctly...</p>
<p>Btw. Are you really sure invoking wine from mod_python is a good idea?</p>
<p>If you are sure, something like that could work...</p>
<pre><code>from subprocess import Popen
HOME = '/the/home/of/www-data' #PLEASE edit
proc = Popen(cmd,... | 3 | 2009-02-04T20:03:02Z | [
"python",
"apache",
"mod-python",
"wine"
] |
Invoking Wine From Apache | 513,023 | <p>I have Apache/2.2.11 using mod_python 3.3.1/Python 2.5 running under Gentoo linux. In my python script I invoke a win32 exe using wine (os.popen2 call). This works fine outside of Apache but under mod_python I get:</p>
<p>wine: cannot open /root/.wine : Permission denied</p>
<p>in /var/log/apache/error_log. My ... | 0 | 2009-02-04T20:01:09Z | 6,263,440 | <p>I was also having very hard time and did lots of researched but failed. Finally found the simplest way by adding <code>'WINEPREFIX="/srv/www/.wine"'</code> in <code>/etc/init.d/httpd</code> file as:</p>
<pre><code>case $ARGV in
start|stop|restart|graceful|graceful-stop)
WINEPREFIX="/srv/www/.wine" $HTTPD -k $AR... | 1 | 2011-06-07T09:46:32Z | [
"python",
"apache",
"mod-python",
"wine"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,211 | <p>You could look into <a href="http://www.selenic.com/mercurial/wiki/" rel="nofollow">mercurial</a> or <a href="http://bazaar-vcs.org/" rel="nofollow">bazaar-ng</a> they are both written in python and support at least http downloads afaik, not really web based but written in one of the languages your hoster supports i... | 1 | 2009-02-04T20:41:30Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,231 | <p>Get a better hosting service. Seriously. Even if you found something that worked in PHP/Ruby/Perl/Whatever, it would still be a sub-par solution. It most likely wouldn't integrate with any IDE you have, and wouldn't have a good tool set available for working with it. It would be really clunky to do correctly.</p... | 7 | 2009-02-04T20:45:06Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,251 | <p><a href="http://www.selenic.com/mercurial/" rel="nofollow">Mercurial</a> has a web-interface and allows commits via http. It uses a couple of C extensions, but I would guess that all of them have pure-Python counterparts.</p>
<p>You can also just use WebDAV, when your hoster provides it.</p>
| 1 | 2009-02-04T20:47:51Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,253 | <p>you could try the reverse way</p>
<ul>
<li>use e.g. a free online svn/git Service to version control the sources on your dev machine</li>
<li>use usual ways to update the "production" machine aka site, like FTP</li>
</ul>
| 0 | 2009-02-04T20:48:38Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,306 | <p>for the time you've spent researching this you could have just gone out and got a much better hosting company. Is there some sort of special relationship or something? </p>
<p>I've never heard of any decent hosting company not offering cvs at a bare minimum. </p>
<p>(Rant Over). </p>
<p>You will save yourself a t... | 3 | 2009-02-04T20:56:12Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,322 | <p>Don't host your repository on your web server. Deploy from your server to the ftp/sftp - whatever.</p>
| 2 | 2009-02-04T20:59:22Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,366 | <p>I think it's actually a pretty good idea, but don't believe such a versioning system exists (yet) so hopefully you'll go ahead and make one.</p>
<p>I don't think adapting an existing solution is going to be easy, but it's probably worth looking into because if you use an existing solution you'll have all the client... | 1 | 2009-02-04T21:10:53Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 513,899 | <p>Why dont you want a client..? A simple client that you can run on your production machine which then syncs to your repository running on another server somewhere.</p>
<p>SVN is available over HTTP so writing a client that is able to sync your code is really easy in python or php.</p>
| 0 | 2009-02-04T23:34:14Z | [
"php",
"python",
"version-control",
"web-applications"
] |
pure web based versioning system | 513,173 | <p>My hosting service does not currently run/allow svn, git, cvs on their server. I would really like to be able to 'sync' my current source on my development machine with my production server. </p>
<p>I am looking for a <strong>pure php/python/ruby version control system</strong> (not just a <em>client</em> for a ver... | 3 | 2009-02-04T20:34:13Z | 515,956 | <p>Use Bazaar:</p>
<blockquote>
<p>Lightweight. No dedicated server with Bazaar installed is needed, just FTP access to a web server. A smart server is available for those requiring additional performance or security but it is not required in many cases - Bazaar 1.x over plain http performs well.</p>
</blockquote>
| 1 | 2009-02-05T13:52:10Z | [
"php",
"python",
"version-control",
"web-applications"
] |
Various Python datetime issues | 513,291 | <p>I have two methods that I'm using as custom tags in a template engine:</p>
<pre><code># Renders a <select> form field
def select_field(options, selected_item, field_name):
options = [(str(v),str(v)) for v in options]
html = ['<select name="%s">' % field_name]
for k,v in options:
tmp = '... | 2 | 2009-02-04T20:54:50Z | 513,318 | <p>Question 1:</p>
<pre><code>>>> '%02d' % 2
'02'
>>> '%02d' % 59
'59'
</code></pre>
| 1 | 2009-02-04T20:58:52Z | [
"python",
"datetime"
] |
Various Python datetime issues | 513,291 | <p>I have two methods that I'm using as custom tags in a template engine:</p>
<pre><code># Renders a <select> form field
def select_field(options, selected_item, field_name):
options = [(str(v),str(v)) for v in options]
html = ['<select name="%s">' % field_name]
for k,v in options:
tmp = '... | 2 | 2009-02-04T20:54:50Z | 513,339 | <p>To turn an integer range into two digit strings:</p>
<pre><code>>>> range(13)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> [ '%02d' % i for i in range(13) ]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
</code></pre>
<p>Then to get the AM/PM indicator:</p>
<pre... | 5 | 2009-02-04T21:03:45Z | [
"python",
"datetime"
] |
Various Python datetime issues | 513,291 | <p>I have two methods that I'm using as custom tags in a template engine:</p>
<pre><code># Renders a <select> form field
def select_field(options, selected_item, field_name):
options = [(str(v),str(v)) for v in options]
html = ['<select name="%s">' % field_name]
for k,v in options:
tmp = '... | 2 | 2009-02-04T20:54:50Z | 513,345 | <ol>
<li><p>Supply a format string to the <code>select_field</code> method to show leading zeros.</p>
<pre><code>>>> print "%02i" % (1, )
01
</code></pre></li>
<li><p>datetime.hour is 0-23, so you have to adapt it when setting the field. This is also the answer on how to detect AM/PM. Make sure to convert it ... | 0 | 2009-02-04T21:04:52Z | [
"python",
"datetime"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.