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 |
|---|---|---|---|---|---|---|---|---|---|
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 1,408,476 | <p>This is a Python bug. When waiting for a condition in threading.Condition.wait(), KeyboardInterrupt is never sent. Repro:</p>
<pre><code>import threading
cond = threading.Condition(threading.Lock())
cond.acquire()
cond.wait(None)
print "done"
</code></pre>
<p>The KeyboardInterrupt exception won't be delivered un... | 90 | 2009-09-11T00:45:17Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 2,561,809 | <p>For some reasons, only exceptions inherited from the base <code>Exception</code> class are handled normally. As a workaround, you may re-raise your <code>KeyboardInterrupt</code> as an <code>Exception</code> instance:</p>
<pre><code>from multiprocessing import Pool
import time
class KeyboardInterruptError(Exceptio... | 13 | 2010-04-01T16:06:35Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 3,577,628 | <p>I found, for the time being, the best solution is to not use the multiprocessing.pool feature but rather roll your own pool functionality. I provided an example demonstrating the error with apply_async as well as an example showing how to avoid using the pool functionality altogether.</p>
<p><a href="http://www.bry... | 3 | 2010-08-26T17:16:12Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 6,191,991 | <p>From what I have recently found, the best solution is to set up the worker processes to ignore SIGINT altogether, and confine all the cleanup code to the parent process. This fixes the problem for both idle and busy worker processes, and requires no error handling code in your child processes.</p>
<pre><code>impo... | 30 | 2011-05-31T18:39:47Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 13,159,399 | <p>Usually this simple structure works for <kbd>Ctrl</kbd>-<kbd>C</kbd> on Pool :</p>
<pre><code>def signal_handle(_signal, frame):
print "Stopping the Jobs."
signal.signal(signal.SIGINT, signal_handle)
</code></pre>
<p>As was stated in few similar posts:</p>
<p><a href="http://stackoverflow.com/questions/42053... | 6 | 2012-10-31T13:44:37Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
Keyboard Interrupts with python's multiprocessing Pool | 1,408,356 | <p>How can I handle KeyboardInterrupt events with python's multiprocessing Pools? Here is a simple example:</p>
<pre><code>from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
sleep(1)
return i*i
def go():
pool = Pool(8)
try:
results = pool.map(sl... | 78 | 2009-09-10T23:59:35Z | 23,682,499 | <p>It seems there are two issues that make exceptions while multiprocessing annoying. The first (noted by Glenn) is that you need to use <code>map_async</code> with a timeout instead of <code>map</code> in order to get an immediate response (i.e., don't finish processing the entire list). The second (noted by Andrey)... | 3 | 2014-05-15T15:23:24Z | [
"python",
"multiprocessing",
"pool",
"keyboardinterrupt"
] |
How can you profile a parallelized Python script? | 1,408,393 | <p>Suppose I have a python script called <code>my_parallel_script.py</code> that involves using <code>multiprocessing</code> to parallelize several things and I run it with the following command:</p>
<pre><code>python -m cProfile my_parallel_script.py
</code></pre>
<p>This generates profiling output for the <strong>p... | 9 | 2009-09-11T00:15:21Z | 1,408,533 | <p>cProfile only works within a single process, so you will not automatically get the child process profiled.</p>
<p>I would recommend that you tweak the child process code so that you can invoke it separately as a single process. Then run it under the profiler. You probably don't need to run your system multi-proce... | 8 | 2009-09-11T01:11:09Z | [
"python",
"profile",
"parallel-processing",
"multiprocessing"
] |
Improving a Python Script that Updates Apache DocumentRoot | 1,408,424 | <p>I'm tired of going through all the steps it takes (for me) to change the DocumentRoot in Apache. I'm trying to facilitate the process with the following Python script...</p>
<pre><code>#!/usr/bin/python
import sys, re
if len(sys.argv) == 2:
f = open('/tmp/apachecdr', 'w')
f.write(open('/etc/apache2/httpd.conf')... | 0 | 2009-09-11T00:26:09Z | 1,408,547 | <p>You're doing a lot of file work for not much benefit. Why do you write /tmp/apachecdr and then immediately read it again? If you are copying httpd.conf to the tmp directory as a backup, the shutil module provides functions to copy files:</p>
<pre><code>#!/usr/bin/python
import shutil, sys, re
HTTPD_CONF = '/etc... | 0 | 2009-09-11T01:16:56Z | [
"python",
"apache",
"document",
"root"
] |
Improving a Python Script that Updates Apache DocumentRoot | 1,408,424 | <p>I'm tired of going through all the steps it takes (for me) to change the DocumentRoot in Apache. I'm trying to facilitate the process with the following Python script...</p>
<pre><code>#!/usr/bin/python
import sys, re
if len(sys.argv) == 2:
f = open('/tmp/apachecdr', 'w')
f.write(open('/etc/apache2/httpd.conf')... | 0 | 2009-09-11T00:26:09Z | 1,408,570 | <p>In general Python programmers prefer explicit over implicit (see <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">http://www.python.org/dev/peps/pep-0020/</a>).
With that in mind you may want to rewrite this line</p>
<pre><code>f.write(open('/etc/apache2/httpd.conf').read())
</code></pre>
<p>To:<... | 0 | 2009-09-11T01:28:07Z | [
"python",
"apache",
"document",
"root"
] |
Improving a Python Script that Updates Apache DocumentRoot | 1,408,424 | <p>I'm tired of going through all the steps it takes (for me) to change the DocumentRoot in Apache. I'm trying to facilitate the process with the following Python script...</p>
<pre><code>#!/usr/bin/python
import sys, re
if len(sys.argv) == 2:
f = open('/tmp/apachecdr', 'w')
f.write(open('/etc/apache2/httpd.conf')... | 0 | 2009-09-11T00:26:09Z | 1,413,880 | <p>Thank you both for your answers. Ned, I was hoping you'd say something like that. The reason I was doing it like that is simply because I didn't know better. Thanks for providing your code, it was exactly what I was looking for. </p>
<p>Btw, the error is happening because "BACKUP" and "HTTPD_CONF" are reversed on l... | 0 | 2009-09-12T00:44:51Z | [
"python",
"apache",
"document",
"root"
] |
Track process status with Python | 1,408,627 | <p>I want to start a number of subprocesses in my Python script and then track when they complete or crash.</p>
<p>subprocess.Popen.poll() seems to return None when the process is still running, 0 on success, and non-zero on failure. Can that be expected on all OS's?
Unfortunately the standard library documentation is... | 3 | 2009-09-11T01:47:22Z | 1,408,646 | <p>Yes to all. </p>
| 1 | 2009-09-11T01:58:27Z | [
"python",
"process",
"crash",
"subprocess"
] |
Track process status with Python | 1,408,627 | <p>I want to start a number of subprocesses in my Python script and then track when they complete or crash.</p>
<p>subprocess.Popen.poll() seems to return None when the process is still running, 0 on success, and non-zero on failure. Can that be expected on all OS's?
Unfortunately the standard library documentation is... | 3 | 2009-09-11T01:47:22Z | 1,408,668 | <p>This may not be a very good answer to your question, but just in case you are at risk of reinventing a wheel, take a look at <a href="http://supervisord.org/" rel="nofollow">Supervisor</a> </p>
<blockquote>
<p><em>Supervisor is a client/server system that allows its users to monitor and
control a number of proc... | 4 | 2009-09-11T02:08:19Z | [
"python",
"process",
"crash",
"subprocess"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,408,710 | <p>All the Unix shells (that I know of) implement shell pipelines via something else than a pty
(typically, they use Unix pipes!-); therefore, the C/C++ runtime library in <code>cpp_program</code> will KNOW its output is NOT a terminal, and therefore it WILL buffer the output (in chunks of a few KB at a time). Unless y... | 1 | 2009-09-11T02:35:11Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,408,717 | <p>You may want to try <code>flush</code>ing the stdout stream in the cpp program.</p>
| 1 | 2009-09-11T02:40:18Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,408,730 | <p>What you need is for your C program to call fflush(stdout) after every line. For example, with the GNU grep tool, you can invoke the option '--line-buffered', which causes this behavior. See <strong><a href="http://www.thinkage.ca/english/gcos/expl/nsc/lib/fflush.html">fflush</a></strong>.</p>
| 8 | 2009-09-11T02:43:34Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,408,768 | <p>ok this maybe sound stupid but it might work:</p>
<p>output your pgm to a file</p>
<pre><code>$ c_program >> ./out.log
</code></pre>
<p>develop a python program that read from tail command</p>
<pre><code>import os
tailoutput = os.popen("tail -n 0 -f ./out.log")
try:
while 1:
line = tailoutput... | -1 | 2009-09-11T02:58:54Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,408,829 | <p>Just set stdout to be line buffered at the beginning of your C program (before performing any output), like this:</p>
<pre><code>#include <stdio.h>
setvbuf(stdout, NULL, _IOLBF, 0);
</code></pre>
<p>or</p>
<pre><code>#include <stdio.h>
setlinebuf(stdout);
</code></pre>
<p>Either one will work on Linu... | 17 | 2009-09-11T03:27:43Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Getting another program's output as input on the fly | 1,408,678 | <p>I've two programs I'm using in this way:</p>
<pre><code>$ c_program | python_program.py
</code></pre>
<p>c_program prints something using <code>printf()</code> and python_program.py reads using <code>sys.stdin.readline()</code> </p>
<p>I'd like to make the python_program.py process c_program's output as it print... | 9 | 2009-09-11T02:12:28Z | 1,409,028 | <p>If you can modify your C program, you've already received your <a href="http://stackoverflow.com/questions/1408678/getting-another-programs-output-as-input-on-the-fly/1408730#1408730">answer</a> but i thought i'd include a solution for those that can't/won't modify code.</p>
<p><a href="http://expect.sourceforge.ne... | 6 | 2009-09-11T04:51:18Z | [
"python",
"c",
"linux",
"bash",
"stdio"
] |
Trimming Python Runtime | 1,408,726 | <p>We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge.</p>
<p>Obviously, we don't use every... | 6 | 2009-09-11T02:42:49Z | 1,408,735 | <p>One trick I've learned while trimming down .py files to ship: Delete all the .pyc files in the standard library, then run your application throughly (that is, enough to be sure all the Python modules it needs will be loaded). If you examine the standard library directories, there will be .pyc files for all the modu... | 6 | 2009-09-11T02:46:39Z | [
"python"
] |
Trimming Python Runtime | 1,408,726 | <p>We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge.</p>
<p>Obviously, we don't use every... | 6 | 2009-09-11T02:42:49Z | 1,408,742 | <p>Both <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a> and <a href="http://www.pyinstaller.org/" rel="nofollow">pyinstaller</a> (<strong>NOTE</strong>: for the latter use the SVN version, the released one is VERY long in the tooth;-) do their "trimming" via <a href="http://www.python.org/doc/2.6.2/library/m... | 5 | 2009-09-11T02:48:54Z | [
"python"
] |
Trimming Python Runtime | 1,408,726 | <p>We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge.</p>
<p>Obviously, we don't use every... | 6 | 2009-09-11T02:42:49Z | 1,408,815 | <p>This <a href="http://www.py2exe.org/index.cgi/BetterCompression" rel="nofollow">py2exe page on compression</a> suggests using <a href="http://upx.sourceforge.net/" rel="nofollow">UPX</a> to compress any DLLs or .pyd files (which are actually just DLLs, still). Obviously this doesn't help in trimming out unneeded <e... | 1 | 2009-09-11T03:19:42Z | [
"python"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,408,842 | <p>One possibility:</p>
<pre><code>def f(**kw):
acceptable_names = set('a', 'b', 'c')
if not (set(kw) <= acceptable_names):
raise WhateverYouWantException(whatever)
...proceed...
</code></pre>
<p>IOW, it's very easy to check that the passed-in names are within the acceptable set and otherwise raise whate... | 5 | 2009-09-11T03:31:08Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,408,851 | <p>How about using a decorator to validate the incoming kwargs?</p>
<pre><code>def validate_kwargs(*keys):
def entangle(f):
def inner(*args, **kwargs):
for key in kwargs:
if not key in keys:
raise ValueError("Received bad kwarg: '%s', expected: %s" % (key, ke... | 2 | 2009-09-11T03:34:33Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,408,854 | <p>There's probably better ways to do this, but here's my take:</p>
<pre><code>def CompareArgs(argdict, **kwargs):
if not set(argdict.keys()) <= set(kwargs.keys()):
# not <= may seem weird, but comparing sets sometimes gives weird results.
# set1 <= set2 means that all items in set 1 are p... | 0 | 2009-09-11T03:36:13Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,408,856 | <p>Perhaps raise an error if they pass any *args?</p>
<pre><code>def func(*args, **kwargs):
if args:
raise TypeError("no positional args allowed")
arg1 = kwargs.pop("arg1", "default")
if kwargs:
raise TypeError("unknown args " + str(kwargs.keys()))
</code></pre>
<p>It'd be simple to factor it into takin... | 0 | 2009-09-11T03:37:09Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,408,860 | <p>Here is the easiest and simplest way:</p>
<pre><code>def func(a=None, b=None, c=None):
args = locals().copy()
print args
func(2, "egg")
</code></pre>
<p>This give the output: <code>{'a': 2, 'c': None, 'b': 'egg'}</code>.
The reason <code>args</code> should be a copy of the <code>locals</code> dictionary i... | 9 | 2009-09-11T03:37:44Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,409,284 | <p>I was inspired by lost-theory's decorator goodness, and after playing about with it for a bit came up with this:</p>
<pre><code>def actual_kwargs():
"""
Decorator that provides the wrapped function with an attribute 'actual_kwargs'
containing just those keyword arguments actually passed in to the functi... | 22 | 2009-09-11T06:25:43Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 1,409,376 | <p>Magic is not the answer:</p>
<pre><code>def funky(a=None, b=None, c=None):
for name, value in [('a', a), ('b', b), ('c', c)]:
print name, value
</code></pre>
| 0 | 2009-09-11T06:53:28Z | [
"python",
"arguments",
"keyword"
] |
Getting the the keyword arguments actually passed to a Python method | 1,408,818 | <p>I'm dreaming of a Python method with explicit keyword args:</p>
<pre><code>def func(a=None, b=None, c=None):
for arg, val in magic_arg_dict.items(): # Where do I get the magic?
print '%s: %s' % (arg, val)
</code></pre>
<p>I want to get a dictionary of only those arguments the caller actually passed i... | 16 | 2009-09-11T03:21:12Z | 10,504,589 | <p>This is easiest accomplished with a single instance of a sentry object:</p>
<pre><code># Top of module, does not need to be exposed in __all__
missing = {}
# Function prototype
def myFunc(a = missing, b = missing, c = missing):
if a is not missing:
# User specified argument a
if b is missing:
... | 1 | 2012-05-08T18:46:38Z | [
"python",
"arguments",
"keyword"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 1,408,937 | <p>When you use the extends template tag, you're saying that the current template extends another -- that it is a child template, dependent on a parent template. Django will look at your child template and use its content to populate the parent.</p>
<p>Everything that you want to use in a child template should be wit... | 69 | 2009-09-11T04:13:46Z | [
"python",
"django",
"django-templates"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 1,409,113 | <p>More info about why it wasn't working for me in case it helps future people:</p>
<p>The reason why it wasn't working is that {% include %} in django doesn't like special characters like fancy apostrophe. The template data I was trying to include was pasted from word. I had to manually remove all of these special ... | 7 | 2009-09-11T05:21:24Z | [
"python",
"django",
"django-templates"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 13,298,495 | <p>From Django docs:</p>
<blockquote>
<p>The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each ... | 53 | 2012-11-08T21:55:20Z | [
"python",
"django",
"django-templates"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 13,896,420 | <p>Added for reference to future people who find this via google: You might want to look at the {% overextend %} tag provided by the mezzanine library for cases like this. </p>
| 1 | 2012-12-15T21:34:41Z | [
"python",
"django",
"django-templates"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 16,111,696 | <p>You can't pull in blocks from an included file into a child template to override the parent template's blocks. However, you can specify a parent in a variable and have the base template specified in the context.</p>
<p>From the <a href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#extends" rel="no... | 2 | 2013-04-19T18:57:19Z | [
"python",
"django",
"django-templates"
] |
django templates: include and extends | 1,408,925 | <p>I would like to provide the same content inside 2 different base files. </p>
<p>So I'm trying to do this:</p>
<p>page1.html:</p>
<pre><code>{% extends "base1.html" %}
{% include "commondata.html" %}
</code></pre>
<p>page2.html:</p>
<pre><code>{% extends "base2.html" %}
{% include "commondata.html" %}
</code></... | 54 | 2009-09-11T04:08:38Z | 23,626,151 | <p>In my opinion, the right (best) answer to this question is the one from <strong>podshumok</strong>, as it explains why the behaviour of include when used along with inheritance.</p>
<p>However, I was somewhat surprised that nobody mentioned the <strong>ssi</strong> tag provided by the Django templating system, whic... | 0 | 2014-05-13T08:24:55Z | [
"python",
"django",
"django-templates"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 1,410,442 | <p>If you're using JQuery this should do the trick:</p>
<p><strong>Your form</strong></p>
<pre><code>TO_HIDE_ATTRS = {'class': 'hidden'}
class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.TextInput(attrs=TO_HIDE_ATTRS))
</code></pre>
<p><strong>Javascript code to add ... | -16 | 2009-09-11T11:55:51Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 1,627,875 | <p>I think it's simpler to achieve the ":" label omission for HiddenInput widget by modifying <code>class AdminField(object)</code> in <code>contrib/admin/helpers.py</code> from :</p>
<pre><code> if self.is_checkbox:
classes.append(u'vCheckboxLabel')
contents = force_unicode(escape(self.field.label)... | 3 | 2009-10-26T23:16:37Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 4,246,248 | <p>I can't believe several people have suggested using jQuery for this...</p>
<p>Is it a case of: when the only tool you know is a hammer everything looks like a nail?</p>
<p>Come on, if you're going to do it from the client-side (instead of fixing the source of the problem in the back-end code) surely the right plac... | 33 | 2010-11-22T14:12:59Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 6,498,935 | <p>Check the answer at <a href="http://stackoverflow.com/questions/4999005/create-a-hidden-field-in-django-admin/6498907#6498907">Create a hidden field in django-admin</a>, it can be done without JavaScript by overriding <code>admin/includes/fieldset.html</code> From there, you can inject a CSS class, and do the rest.<... | 3 | 2011-06-27T21:03:42Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 12,171,430 | <p>Try</p>
<p>{% for field in form.visible_fields %}</p>
| 13 | 2012-08-29T05:29:49Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 12,588,805 | <p>Oraculum has got it right. You shouldn't be cleaning this up on the client side. If it is clutter, then you shouldn't be sending it to client at all. Building on Oraculum's answer, you should use a custom form template because you you probably still want the hidden values in the form.</p>
<pre><code>{% for field in... | 30 | 2012-09-25T18:17:05Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 18,235,005 | <p>In theory, you should be able to pass <code>label_suffix</code> into the form constructor. However, the Django admin ignores this.</p>
<p>You've been bitten by two bugs in Django: <a href="https://code.djangoproject.com/ticket/18134" rel="nofollow">#18134 'BoundField.label_tag should include form.label_suffix'</a> ... | 2 | 2013-08-14T14:50:16Z | [
"python",
"django",
"django-admin"
] |
How do I hide the field label for a HiddenInput widget in Django Admin? | 1,408,940 | <p>I've got a bit of Django form code that looks like this:</p>
<pre><code>class GalleryAdminForm(forms.ModelForm):
auto_id=False
order = forms.CharField(widget=forms.HiddenInput())
</code></pre>
<p>And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:</p>
... | 16 | 2009-09-11T04:15:15Z | 23,968,360 | <p>Based upon the solution by Wilfried Hughes I 've changed the fieldset.html with little improvements.</p>
<p>The code snippet below not only hides the input element instead if the fieldset contains only one single element which input-type is set to hidden it also hides the surrounding div-elements wasting no space i... | 2 | 2014-05-31T09:16:48Z | [
"python",
"django",
"django-admin"
] |
Auto-generate form fields for a Form in django | 1,409,192 | <p>I have some models and I want to generate a multi-selection form from this data.
So the form would contain an entry for each category and the choices would be the skills in that category.</p>
<p><strong>models.py</strong></p>
<pre><code>class SkillCategory(models.Model):
name = models.CharField(max_length=50)
... | 7 | 2009-09-11T05:51:26Z | 1,409,325 | <p>What you want is a Formset. This will give you a set of rows, each of which maps to a specific Skill.</p>
<p>See the <a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/" rel="nofollow">Formset documentation</a> and the page specifically on generating <a href="http://docs.djangoproject.com/en/dev/t... | 1 | 2009-09-11T06:39:16Z | [
"python",
"django",
"django-forms"
] |
Auto-generate form fields for a Form in django | 1,409,192 | <p>I have some models and I want to generate a multi-selection form from this data.
So the form would contain an entry for each category and the choices would be the skills in that category.</p>
<p><strong>models.py</strong></p>
<pre><code>class SkillCategory(models.Model):
name = models.CharField(max_length=50)
... | 7 | 2009-09-11T05:51:26Z | 1,409,609 | <p>Okay so you can't set fields like that on forms.Form, for reasons which will become apparent when you see <a href="http://code.djangoproject.com/browser/django/trunk/django/forms/forms.py#L53" rel="nofollow">DeclarativeFieldsMetaclass</a>, the metaclass of forms.Form (but not of forms.BaseForm). A solution which may... | 2 | 2009-09-11T08:04:34Z | [
"python",
"django",
"django-forms"
] |
Auto-generate form fields for a Form in django | 1,409,192 | <p>I have some models and I want to generate a multi-selection form from this data.
So the form would contain an entry for each category and the choices would be the skills in that category.</p>
<p><strong>models.py</strong></p>
<pre><code>class SkillCategory(models.Model):
name = models.CharField(max_length=50)
... | 7 | 2009-09-11T05:51:26Z | 1,410,800 | <p>Take a look at creating dynamic forms in Django, from <a href="http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/" rel="nofollow">b-list.org</a> and <a href="http://uswaretech.com/blog/2008/10/dynamic-forms-with-django/" rel="nofollow">uswaretech.com</a>. I've had success using these examples to dynamically cr... | 1 | 2009-09-11T13:15:57Z | [
"python",
"django",
"django-forms"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 1,409,336 | <p>Edit 1: Answering new question:</p>
<p>You ask how you can create a function with this signature:</p>
<pre><code>def fun(a, b, opt=None):
pass
</code></pre>
<p>The correct way to do that in Python is thus:</p>
<pre><code>def fun(a, b, opt=None):
pass
</code></pre>
<p>Edit 2: Answering explanation:</p>
... | -4 | 2009-09-11T06:42:08Z | [
"python",
"function"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 1,409,496 | <p>For your usecase, having a docstring in the class/function should work -- that will show up in help() okay, and can be set programmatically (func.__doc__ = "stuff").</p>
<p>I can't see any way of setting the actual signature. I would have thought the <a href="http://docs.python.org/library/functools.html">functools... | 10 | 2009-09-11T07:35:49Z | [
"python",
"function"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 1,409,895 | <p>You can't do this with live code.</p>
<p>That is, you seem to be wanting to take an actual, live function that looks like this:</p>
<pre><code>def f(*args, **kwargs):
print args[0]
</code></pre>
<p>and change it to one like this:</p>
<pre><code>def f(a):
print a
</code></pre>
<p>The reason this can't be... | 0 | 2009-09-11T09:16:59Z | [
"python",
"function"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 1,410,151 | <p>"We want create_initiation_function to change the signature"</p>
<p>Please don't do this.</p>
<p>"We want to use this function in a number of classes"</p>
<p>Please use ordinary inheritance. </p>
<p>There's no value in having the signature "changed" at run time.</p>
<p>You're creating a maintenance nightmare. ... | 2 | 2009-09-11T10:23:29Z | [
"python",
"function"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 1,410,320 | <p>Maybe I didn't understand the problem well, but if it's about keeping the same behavior while changing the function signature, then you can do something like :</p>
<pre><code># define a function
def my_func(name, age) :
print "I am %s and I am %s" % (name, age)
# label the function with a backup name
save_func... | 0 | 2009-09-11T11:18:34Z | [
"python",
"function"
] |
Set function signature in Python | 1,409,295 | <p>Suppose I have a generic function f. I want to <strong>programmatically</strong> create a function f2 that behaves the same as f, but has a customised signature.</p>
<p><strong>More detail</strong></p>
<p>Given a list l and and dictionary d I want to be able to:</p>
<ul>
<li>Set the non-keyword arguments of f2 to... | 15 | 2009-09-11T06:29:05Z | 33,112,180 | <p>From <a href="https://www.python.org/dev/peps/pep-0362/#visualizing-callable-objects-signature" rel="nofollow">PEP-0362</a>, there actually does appear to be a way to set the signature in py3.3+, using the <code>fn.__signature__</code> attribute:</p>
<pre><code>def shared_vars(*shared_args):
"""Decorator factor... | 3 | 2015-10-13T20:38:16Z | [
"python",
"function"
] |
Python App Engine uploaded image content-type | 1,409,377 | <p>I know that I can accept image uploads by having a form that POSTs to App Engine like so:</p>
<pre><code><form action="/upload_received" enctype="multipart/form-data" method="post">
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Upload Image"></div... | 4 | 2009-09-11T06:53:30Z | 1,409,416 | <p>Based on my research, browsers, except for Internet Explorer (version 6 at least), determine the file mime type by using the file's extension. Given that you want image mime types, you could use a simple Python dictionary in order to achieve this.</p>
<p>Unfortunately I don't know of any method in Python that tries... | 0 | 2009-09-11T07:09:31Z | [
"python",
"image",
"google-app-engine",
"upload",
"multipart"
] |
Python App Engine uploaded image content-type | 1,409,377 | <p>I know that I can accept image uploads by having a form that POSTs to App Engine like so:</p>
<pre><code><form action="/upload_received" enctype="multipart/form-data" method="post">
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Upload Image"></div... | 4 | 2009-09-11T06:53:30Z | 1,409,468 | <p>Instead of using self.request.get(fieldname), use self.request.POST[fieldname]. This returns a cgi.FieldStorage object (see the Python library docs for details), which has 'filename', 'type' and 'value' attributes.</p>
| 6 | 2009-09-11T07:27:44Z | [
"python",
"image",
"google-app-engine",
"upload",
"multipart"
] |
Python App Engine uploaded image content-type | 1,409,377 | <p>I know that I can accept image uploads by having a form that POSTs to App Engine like so:</p>
<pre><code><form action="/upload_received" enctype="multipart/form-data" method="post">
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Upload Image"></div... | 4 | 2009-09-11T06:53:30Z | 1,495,810 | <p>Try the python mimetypes module, it will guess the content type and encoding for you,</p>
<p>e.g.</p>
<p>>>import mimetypes</p>
<p>>>mimetypes.guess_type("/home/sean/desktop/comedy/30seconds.mp4")</p>
<p>('video/mp4', None)</p>
| 2 | 2009-09-30T02:14:28Z | [
"python",
"image",
"google-app-engine",
"upload",
"multipart"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,409,872 | <p>The docstring conventions are at <a href="http://ftp.python.org/dev/peps/pep-0257/" rel="nofollow">PEP 257</a>. </p>
<p>The example there follows this format for specifying arguments, you can add the types if they matter:</p>
<pre><code>def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword ar... | 10 | 2009-09-11T09:12:21Z | [
"python"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,409,885 | <p>The most pythonic solution is to document with examples. If possible, state what operations an object must support to be acceptable, rather than a specific type.</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface)
"""Initialise host with given name and network_interface.
networ... | 8 | 2009-09-11T09:14:46Z | [
"python"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,409,916 | <p>Personally I found very usefull to use <a href="http://www.logilab.org/857" rel="nofollow">pylint</a> to validate my code.</p>
<p>If you follow pylint suggestion almost automatically your code become more readable,
you will improve your python writing skills, respect naming conventions. You can also define your own... | 4 | 2009-09-11T09:21:32Z | [
"python"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,410,204 | <p>Python, though not as overtly typed as C or Java, is still typed and will throw exceptions if you're doing things with types that simply do not play nice together.</p>
<p>To that end, if you're concerned about your code being used correctly, maintained correctly, etc. simply use docstrings, comments, or even more e... | 2 | 2009-09-11T10:41:55Z | [
"python"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,410,208 | <p>Using dynamic languages will teach you something about static languages: all the help you got from the static language that you now miss in the dynamic language, it wasn't all that helpful.</p>
<p>To use your example, in a static language, you'd know that the parameter was a string, and in Python you don't. So in ... | 18 | 2009-09-11T10:43:07Z | [
"python"
] |
Python code readability | 1,409,821 | <p>I have a programming experience with statically typed languages. Now writing code in Python I feel difficulties with its readability. Lets say I have a class <em>Host</em>:</p>
<pre><code>class Host(object):
def __init__(self, name, network_interface):
self.name = name
self.network_interface = network_int... | 11 | 2009-09-11T08:58:26Z | 1,414,678 | <p>One benefit of static typing is that types are a form of documentation. When programming in Python, you can document more flexibly and fluently. Of course in your example you want to say that network_interface should implement NetworkInterface, but in many cases the type is obvious from the context, variable name, o... | 1 | 2009-09-12T08:55:56Z | [
"python"
] |
Borg pattern problem when used in two different modules | 1,409,917 | <p>I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. </p>
<p>Is there a way to use the Borg in that cas... | 0 | 2009-09-11T09:21:49Z | 1,409,990 | <p>It's not the class names that is the problem. I'm not entirely sure why Python see the Borg class and the borg.Borg class as different, perhaps it's because you run this from <code>__main__</code>, I think python does not realize that <code>__main__</code> and borg is the same module.</p>
<p>The solution is easy. C... | 1 | 2009-09-11T09:40:17Z | [
"python"
] |
Borg pattern problem when used in two different modules | 1,409,917 | <p>I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. </p>
<p>Is there a way to use the Borg in that cas... | 0 | 2009-09-11T09:21:49Z | 1,409,995 | <p>The problem only occurs in your main-function. Move that code
to its own file and everything is as you'd expect. This code</p>
<pre><code>import borg
import borg2
if __name__ == "__main__":
borg.fct_ab()
borg2.fct_c()
</code></pre>
<p>delivers this output:</p>
<pre><code>borg.Borg 10438672
borg.Borg 1043... | 3 | 2009-09-11T09:41:02Z | [
"python"
] |
Borg pattern problem when used in two different modules | 1,409,917 | <p>I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. </p>
<p>Is there a way to use the Borg in that cas... | 0 | 2009-09-11T09:21:49Z | 1,410,391 | <p>I've fixed the issue in my real application by fixing an error in the import.</p>
<p>In fact, I have two different modules using the same 3rd module. </p>
<p>The 1st one was importing mypackage.mymodule while the 2nd one was importing mymodule. mypackage is installed as a python egg and the code I was working on i... | 0 | 2009-09-11T11:36:42Z | [
"python"
] |
Borg pattern problem when used in two different modules | 1,409,917 | <p>I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. </p>
<p>Is there a way to use the Borg in that cas... | 0 | 2009-09-11T09:21:49Z | 1,996,502 | <p>After a long day of struggling with Singletons and Borg, my conclusion is the following:</p>
<p>It seems that a Python module imported multiple times using different 'import paths' is actually imported multiple times. If that module contains a singleton, you get multiple instances.</p>
<p>Example:</p>
<pre><code>... | 3 | 2010-01-03T22:03:24Z | [
"python"
] |
Borg pattern problem when used in two different modules | 1,409,917 | <p>I am using the Borg pattern with mutual inclusion of modules. See the example code (not the real code but it shows the problem) below. In this case, I have two different Borgs because the class names (and I guess the class) are seen as different by the interpreter. </p>
<p>Is there a way to use the Borg in that cas... | 0 | 2009-09-11T09:21:49Z | 38,994,959 | <p>The solution --- as has already been mentioned --- is to avoid a recursive <code>import</code> of the main module, but <code>borg.py</code> is <em>not</em> being "imported twice". The problem is that importing it <em>at all</em> while it is already executing causes you define the <code>Borg</code> class twice, in t... | 0 | 2016-08-17T11:08:59Z | [
"python"
] |
## in python using notepad++ syntax coloring | 1,410,063 | <p>In my editor (notepad++) in Python script edit mode, a line</p>
<pre>
## is this a special comment or what?
</pre>
<p>Turns a different color (yellow) than a normal #comment.</p>
<p>What's special about a ##comment vs a #comment?</p>
| 2 | 2009-09-11T09:54:47Z | 1,410,088 | <p>From the Python point of view, there's no difference. However, Notepad++'s highlighter considers the ## sequence as a STRINGEOL, which is why it colours it this way. See this <a href="http://sourceforge.net/forum/message.php?msg%5Fid=4340178">thread</a>.</p>
| 5 | 2009-09-11T10:02:43Z | [
"python",
"comments"
] |
## in python using notepad++ syntax coloring | 1,410,063 | <p>In my editor (notepad++) in Python script edit mode, a line</p>
<pre>
## is this a special comment or what?
</pre>
<p>Turns a different color (yellow) than a normal #comment.</p>
<p>What's special about a ##comment vs a #comment?</p>
| 2 | 2009-09-11T09:54:47Z | 1,410,104 | <p>Also, in a <a href="http://mail.python.org/pipermail/doc-sig/2006-March/003558.html" rel="nofollow">different situations</a>:</p>
<blockquote>
<p>Comment whose first line is a double hash:</p>
<blockquote>
<p>This is used by doxygen and Fredrik Lundh's PythonDoc. In doxygen,
if there's text on the l... | 0 | 2009-09-11T10:08:02Z | [
"python",
"comments"
] |
## in python using notepad++ syntax coloring | 1,410,063 | <p>In my editor (notepad++) in Python script edit mode, a line</p>
<pre>
## is this a special comment or what?
</pre>
<p>Turns a different color (yellow) than a normal #comment.</p>
<p>What's special about a ##comment vs a #comment?</p>
| 2 | 2009-09-11T09:54:47Z | 7,221,958 | <p>I thought the difference had something to do with usage:</p>
<pre><code>#this is a code block header
</code></pre>
<p>vs.</p>
<pre><code>##this is a comment
</code></pre>
<p>I know Python doesn't care one way or the other, but I thought it was just convention to do it that way.</p>
| 2 | 2011-08-28T15:48:02Z | [
"python",
"comments"
] |
Inverse Dict in Python | 1,410,087 | <p>I am trying to create a new dict using a list of values of an existing dict as individual keys.</p>
<p>So for example:</p>
<pre><code>dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]})
</code></pre>
<p>and I would like to obtain:</p>
<pre><code>dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','b'], 4:['... | 2 | 2009-09-11T10:02:32Z | 1,410,118 | <p>If you are using Python 2.5 or above, use the <a href="http://docs.python.org/library/collections.html#collections.defaultdict" rel="nofollow"><code>defaultdict</code> class from the <code>collections</code></a> module; a <code>defaultdict</code> automatically creates values on the first access to a missing key, so ... | 8 | 2009-09-11T10:12:23Z | [
"python",
"data-structures",
"dictionary"
] |
Inverse Dict in Python | 1,410,087 | <p>I am trying to create a new dict using a list of values of an existing dict as individual keys.</p>
<p>So for example:</p>
<pre><code>dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]})
</code></pre>
<p>and I would like to obtain:</p>
<pre><code>dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','b'], 4:['... | 2 | 2009-09-11T10:02:32Z | 1,410,186 | <p>Notice that you don't need the <code>dict</code> in your examples: the <code>{}</code> syntax gives you a dict:</p>
<pre><code>dict1 = {'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]}
</code></pre>
| 4 | 2009-09-11T10:35:46Z | [
"python",
"data-structures",
"dictionary"
] |
Inverse Dict in Python | 1,410,087 | <p>I am trying to create a new dict using a list of values of an existing dict as individual keys.</p>
<p>So for example:</p>
<pre><code>dict1 = dict({'a':[1,2,3], 'b':[1,2,3,4], 'c':[1,2]})
</code></pre>
<p>and I would like to obtain:</p>
<pre><code>dict2 = dict({1:['a','b','c'], 2:['a','b','c'], 3:['a','b'], 4:['... | 2 | 2009-09-11T10:02:32Z | 1,410,194 | <p>Other way:</p>
<pre><code>dict2={}
[[ (dict2.setdefault(i,[]) or 1) and (dict2[i].append(x)) for i in y ] for (x,y) in dict1.items()]
</code></pre>
| -3 | 2009-09-11T10:39:25Z | [
"python",
"data-structures",
"dictionary"
] |
Returning a c++ array (pointer) from boost python | 1,410,272 | <p>I'm currently writing python bindings for a c++ library I'm working on. The library reads some binary file format and reading speed is very important. While optimizing the library for speed, I noticed that std::vector (used in the instances I'm reading) was eating up a lot of processing time, so I replaced those wit... | 1 | 2009-09-11T11:03:36Z | 1,887,396 | <p>I will recomend a wrap data and dataLength with proxy class and returns from Instance this proxy. In our project we use this way to export data from our app to python.</p>
<p>If you want I can give you few links to our implementation and explain how it works.</p>
| 1 | 2009-12-11T11:18:26Z | [
"python",
"boost",
"binding",
"boost-python"
] |
Returning a c++ array (pointer) from boost python | 1,410,272 | <p>I'm currently writing python bindings for a c++ library I'm working on. The library reads some binary file format and reading speed is very important. While optimizing the library for speed, I noticed that std::vector (used in the instances I'm reading) was eating up a lot of processing time, so I replaced those wit... | 1 | 2009-09-11T11:03:36Z | 2,119,573 | <p>If you change your class to work with <code>std::vector</code> instances, take a look at the vector indexing suite (<a href="http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/indexing.html" rel="nofollow">http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/indexing.html</a>), which allows you to expose vec... | 4 | 2010-01-22T18:37:57Z | [
"python",
"boost",
"binding",
"boost-python"
] |
How to implement Unicode string matching by folding in python | 1,410,308 | <p>I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string.</p>
<p>... | 9 | 2009-09-11T11:15:48Z | 1,410,356 | <p>Have a look at this: <a href="ftp://alan.smcvt.edu/hefferon/unicode2ascii.py" rel="nofollow">ftp://alan.smcvt.edu/hefferon/unicode2ascii.py</a></p>
<p>Probably not complete, but might get you started.</p>
| 1 | 2009-09-11T11:28:33Z | [
"python",
"unicode"
] |
How to implement Unicode string matching by folding in python | 1,410,308 | <p>I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string.</p>
<p>... | 9 | 2009-09-11T11:15:48Z | 1,410,365 | <p>You can use <a href="http://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string/518232#518232">this</a> <code>strip_accents</code> function to remove the accents:</p>
<pre><code>def strip_accents(s):
return ''.join((c for c in unicodedata.normalize('NFD', unicode(... | 5 | 2009-09-11T11:31:30Z | [
"python",
"unicode"
] |
How to implement Unicode string matching by folding in python | 1,410,308 | <p>I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string.</p>
<p>... | 9 | 2009-09-11T11:15:48Z | 2,327,977 | <p>A general purpose solution (especially for search normalization and generating slugs) is the unidecode module:</p>
<p><a href="http://pypi.python.org/pypi/Unidecode" rel="nofollow">http://pypi.python.org/pypi/Unidecode</a></p>
<p>It's a port of the Text::Unidecode module for Perl. It's not complete, but it transla... | 1 | 2010-02-24T17:18:10Z | [
"python",
"unicode"
] |
How to implement Unicode string matching by folding in python | 1,410,308 | <p>I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string.</p>
<p>... | 9 | 2009-09-11T11:15:48Z | 2,759,500 | <p>What about this one:</p>
<pre><code>normalize('NFKD', unicode_string).encode('ASCII', 'ignore').lower()
</code></pre>
<p>Taken from here (Spanish) <a href="http://python.org.ar/pyar/Recetario/NormalizarCaracteresUnicode" rel="nofollow">http://python.org.ar/pyar/Recetario/NormalizarCaracteresUnicode</a></p>
| 1 | 2010-05-03T16:20:21Z | [
"python",
"unicode"
] |
How to implement Unicode string matching by folding in python | 1,410,308 | <p>I have an application implementing incremental search. I have a catalog of unicode strings to be matched and match them to a given "key" string; a catalog string is a "hit" if it contains all of the characters in the key, in order, and it is ranked better if the key characters cluster in the catalog string.</p>
<p>... | 9 | 2009-09-11T11:15:48Z | 5,203,502 | <blockquote>
<p>For my application, I already addressed this in a different comment: I want to have a <em>unicode</em> result and <em>leave unhandled characters</em> untouched.</p>
</blockquote>
<p>In that case, the correct way to do this is to create a UCA collator object with its strength set to compare at primar... | 4 | 2011-03-05T11:24:27Z | [
"python",
"unicode"
] |
What are the pros and cons of PyRo and RPyC python libs? | 1,410,328 | <p>I am looking for a remote procedure call engine for Python and I've found that <a href="http://pythonhosted.org/Pyro4/" rel="nofollow">PyRo (Python Remote Object)</a> and <a href="http://rpyc.wikidot.com/" rel="nofollow">RPyC (Remote Python Call) </a> are both the kind of thing I am searching for.</p>
<p>However, I... | 13 | 2009-09-11T11:20:11Z | 1,411,448 | <p>I personally find them roughly equivalent, but RPyC's author (<a href="http://rpyc.wikispaces.com/about">here</a>) claims more simplicity (and maybe for somebody not all that used to distributed computing he's got a point; I may be too used to it to make a good judge;-). Quoting him...:</p>
<blockquote>
<p>altho... | 17 | 2009-09-11T15:02:58Z | [
"python",
"distributed",
"pyro",
"rpyc"
] |
What are the pros and cons of PyRo and RPyC python libs? | 1,410,328 | <p>I am looking for a remote procedure call engine for Python and I've found that <a href="http://pythonhosted.org/Pyro4/" rel="nofollow">PyRo (Python Remote Object)</a> and <a href="http://rpyc.wikidot.com/" rel="nofollow">RPyC (Remote Python Call) </a> are both the kind of thing I am searching for.</p>
<p>However, I... | 13 | 2009-09-11T11:20:11Z | 13,998,998 | <p>YMMV, but here are my results from evaluating RPyC, Pyro4 and ZeroRPC for use on an upcoming project. Note that there are not in-depth tests, nor is this intended to be an in-depth review, just my notes on how well each works for the needs of my upcoming project.</p>
<p>ZeroRPC:</p>
<ul>
<li>quite a few dependenc... | 5 | 2012-12-22T00:41:12Z | [
"python",
"distributed",
"pyro",
"rpyc"
] |
Python image mirroring | 1,410,406 | <p>I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.</p>
<p>I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel a... | 0 | 2009-09-11T11:42:31Z | 1,410,438 | <p>It's not really an Python question, is it?</p>
<p>The easiest solution would be to first mirror horizontal and then vertical.
Another one would be to switch pixel rows with columns.</p>
<p>Or to do your algorithm but switch the pixels from left-top to bottom-right...</p>
| 1 | 2009-09-11T11:55:17Z | [
"python",
"image-manipulation"
] |
Python image mirroring | 1,410,406 | <p>I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.</p>
<p>I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel a... | 0 | 2009-09-11T11:42:31Z | 1,410,439 | <p>If I understood correctly what you need is to "flip" the image by a diagonal. Since there are two of them I'll presume that you mean the one that goes from left bottom to right top.</p>
<p>In order to flip by this diagonal you need to transform each row from the source in columns in the destination. The left part o... | 1 | 2009-09-11T11:55:29Z | [
"python",
"image-manipulation"
] |
Python image mirroring | 1,410,406 | <p>I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.</p>
<p>I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel a... | 0 | 2009-09-11T11:42:31Z | 1,410,787 | <p>Using PIL (the Python Imaging Library) this is a relatively straightforward task. Notice however, that the output image is square -- thus not the same size as the original image.</p>
<p>Here is the code:</p>
<pre><code>from PIL import Image, ImageDraw
# load the image, create the mirrored image, and the result pl... | 2 | 2009-09-11T13:12:44Z | [
"python",
"image-manipulation"
] |
Python image mirroring | 1,410,406 | <p>I've been making a picture mirroring in horizontal and vertical axes. Now I'm going to make the diagonal.</p>
<p>I had done the hori and verti width two for loops which in the hori scenario loops through all the pixels in the height and only the half of the pixels in the width. Then it gets the color of the pixel a... | 0 | 2009-09-11T11:42:31Z | 12,682,853 | <p>Here's how to mirror diagonally in JES; It only works for a square image though:</p>
<pre><code>def mirrorDiagonal(picture):
for sourceX in range(0,getWidth(picture)):
for sourceY in range (0,getHeight(picture)):
pex=getPixel(picture,sourceY,sourceX)
pix=getPixel(picture, sourceX,sourceY)
c... | 0 | 2012-10-02T00:13:16Z | [
"python",
"image-manipulation"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,452 | <ol>
<li><p>use editor / IDE that supports code highlighting. E.g., Notepad++ has word-highlighting feature that I find very useful.</p></li>
<li><p>use unit tests</p></li>
</ol>
<p>stupid errors will be weeded out first, so I wouldn't worry to much about this type of errors. it's "smart" error you should be afraid of... | 3 | 2009-09-11T11:58:52Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,455 | <p><a href="http://eclipse.org/" rel="nofollow">Eclipse</a> has a good <a href="http://pydev.org/" rel="nofollow">python plugin</a> for doing the syntax highlighting and debugging.</p>
| 0 | 2009-09-11T12:00:09Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,456 | <p>Looks like <a href="http://pychecker.sourceforge.net/" rel="nofollow">PyChecker</a> or <a href="http://www.logilab.org/857" rel="nofollow">pylint</a> are what you're looking for</p>
| 7 | 2009-09-11T12:00:12Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,460 | <ol>
<li><p>Use tools such as <a href="http://www.logilab.org/857" rel="nofollow">pylint</a> or <a href="http://pychecker.sourceforge.net/" rel="nofollow">PyChecker</a>.</p></li>
<li><p>Write unit tests.</p></li>
</ol>
| 3 | 2009-09-11T12:00:38Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,696 | <p>Pylint is almost doing what you are looking for. </p>
<p>You can also force the compilation of your python files. That will show some basic syntax error (it doesn't have all the capability of a c++ compiler)</p>
<p>I've read <a href="http://www.ibm.com/developerworks/opensource/library/os-ecant/" rel="nofollow">th... | 0 | 2009-09-11T12:55:36Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,410,933 | <p>Unit test. <a href="http://docs.python.org/library/unittest.html" rel="nofollow">http://docs.python.org/library/unittest.html</a></p>
<p>If your tests are written at a reasonable level of granularity, it can be as fast to unit test as it is to run lint or a compiler.</p>
| 3 | 2009-09-11T13:40:06Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 1,411,292 | <ul>
<li><strong>Static analysis</strong> (as from the IDE, or from tools like pyLint and pyChecker) is a very quick and effective way to check simple errors, and enforce a common style. </li>
<li><strong>Unit tests</strong> are a great way to ensure the code stands for its contract. </li>
<li><strong>Code reviews</s... | 2 | 2009-09-11T14:39:24Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 9,675,780 | <p>As with other languages, you should use assertions liberally throughout your code. Use assertions when you must rely on the predicate to be true for the program to run, not as exception/error handling. An assertion should be used to check for irrecoverable errors and force the program to crash. <a href="http://stack... | 0 | 2012-03-12T22:36:41Z | [
"python",
"compiler-construction",
"correctness"
] |
Checking Python code correctness | 1,410,444 | <p>In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.</p>
<p>Thanks</p>
| 5 | 2009-09-11T11:56:30Z | 29,248,370 | <p>You may need this:</p>
<pre><code>python -m py_compile script.py
</code></pre>
| 0 | 2015-03-25T05:51:12Z | [
"python",
"compiler-construction",
"correctness"
] |
copy.deepcopy vs pickle | 1,410,615 | <p>I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, <code>copy.deepcopy</code> is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so</p>
<ol>
<li>Why shouldn't I(we) always be u... | 24 | 2009-09-11T12:39:43Z | 1,410,813 | <p>You should be using deepcopy because it makes your code more readable. Using a serialization mechanism to copy objects in memory is at the very least confusing to another developer reading your code. Using deepcopy also means you get to reap the benefits of future optimizations in deepcopy.</p>
<p>First rule of opt... | 6 | 2009-09-11T13:18:11Z | [
"python",
"pickle",
"deep-copy"
] |
copy.deepcopy vs pickle | 1,410,615 | <p>I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, <code>copy.deepcopy</code> is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so</p>
<ol>
<li>Why shouldn't I(we) always be u... | 24 | 2009-09-11T12:39:43Z | 1,411,229 | <p>Problem is, pickle+unpickle can be faster (in the C implementation) because it's <em>less general</em> than deepcopy: many objects can be deepcopied but not pickled. Suppose for example that your class <code>A</code> were changed to...:</p>
<pre><code>class A(object):
class B(object): pass
def __init__(self): ... | 27 | 2009-09-11T14:27:53Z | [
"python",
"pickle",
"deep-copy"
] |
copy.deepcopy vs pickle | 1,410,615 | <p>I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, <code>copy.deepcopy</code> is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so</p>
<ol>
<li>Why shouldn't I(we) always be u... | 24 | 2009-09-11T12:39:43Z | 1,411,305 | <p>Even faster would be to avoid the copy in the first place. You mention that you are doing rendering. Why does it need to copy objects?</p>
| 1 | 2009-09-11T14:41:01Z | [
"python",
"pickle",
"deep-copy"
] |
copy.deepcopy vs pickle | 1,410,615 | <p>I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, <code>copy.deepcopy</code> is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so</p>
<ol>
<li>Why shouldn't I(we) always be u... | 24 | 2009-09-11T12:39:43Z | 19,065,623 | <p>Short and somewhat late:</p>
<ul>
<li>If you have to cPickle an object anyway, you might as well use the cPickle method to deepcopy (but document)</li>
</ul>
<p>e.g. You might consider:</p>
<pre><code>def mydeepcopy(obj):
try:
return cPickle.loads(cPickle.dumps(obj, -1))
except PicklingError:
... | 0 | 2013-09-28T09:54:24Z | [
"python",
"pickle",
"deep-copy"
] |
copy.deepcopy vs pickle | 1,410,615 | <p>I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, <code>copy.deepcopy</code> is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much faster, so</p>
<ol>
<li>Why shouldn't I(we) always be u... | 24 | 2009-09-11T12:39:43Z | 22,619,902 | <p>It is <em>not</em> always the case that cPickle is faster than deepcopy(). While cPickle is probably always faster than pickle, whether it is faster than deepcopy depends on</p>
<ul>
<li>the size and nesting level of the structures to be copied,</li>
<li>the type of contained objects, and</li>
<li>the size of the ... | 0 | 2014-03-24T20:27:53Z | [
"python",
"pickle",
"deep-copy"
] |
Is there a way to reopen a socket? | 1,410,723 | <p>I create many "short-term" sockets in some code that look like that :</p>
<pre><code>nb=1000
for i in range(nb):
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((adr, prt)
sck.send('question %i'%i)
sck.shutdown(SHUT_WR)
answer=sck.recv(4096)
print 'answer %i : %s' % (%i, ... | 6 | 2009-09-11T13:00:14Z | 1,410,764 | <p>No, this is a limitation of the underlying C sockets (and the TCP/IP protocol, for that matter). My question to you is: why are you shutting them down when you can architect your application to use them?</p>
<p>The problem with many short-term sockets is that shutting them down puts them in a state where they canno... | 14 | 2009-09-11T13:08:37Z | [
"python",
"sockets"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.