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
Python: simple CLI GUI
2,239,476
<p>A simple question on a python module. Let's say I have the following code:</p> <pre><code>for i in range(1000): print i </code></pre> <p>It'll output something along the lines of:</p> <blockquote> <p>1</p> <p>2</p> <p>'Snip'</p> <p>999</p> </blockquote> <p>Is it possible to have the program ou...
0
2010-02-10T18:51:38Z
2,239,505
<p>If you want the character to be overwritten/replaced each time, you may need to use a terminal control library like 'curses'. <a href="http://docs.python.org/dev/howto/curses.html" rel="nofollow">Here's a Python how-to article</a> to get you started.</p>
3
2010-02-10T18:58:09Z
[ "python" ]
Python: simple CLI GUI
2,239,476
<p>A simple question on a python module. Let's say I have the following code:</p> <pre><code>for i in range(1000): print i </code></pre> <p>It'll output something along the lines of:</p> <blockquote> <p>1</p> <p>2</p> <p>'Snip'</p> <p>999</p> </blockquote> <p>Is it possible to have the program ou...
0
2010-02-10T18:51:38Z
2,239,508
<p>For a simple case the following code works just fine:</p> <pre><code>sys.stdout.write(str(i)+'\r') sys.stdout.flush() </code></pre>
2
2010-02-10T18:58:37Z
[ "python" ]
Python: simple CLI GUI
2,239,476
<p>A simple question on a python module. Let's say I have the following code:</p> <pre><code>for i in range(1000): print i </code></pre> <p>It'll output something along the lines of:</p> <blockquote> <p>1</p> <p>2</p> <p>'Snip'</p> <p>999</p> </blockquote> <p>Is it possible to have the program ou...
0
2010-02-10T18:51:38Z
2,239,511
<pre><code>import os for i in range(1000): print i os.system("clear") </code></pre> <p>edit: as mentioned in the comment below, change "clear" to "cls" if using windows.</p>
2
2010-02-10T18:59:01Z
[ "python" ]
Python: simple CLI GUI
2,239,476
<p>A simple question on a python module. Let's say I have the following code:</p> <pre><code>for i in range(1000): print i </code></pre> <p>It'll output something along the lines of:</p> <blockquote> <p>1</p> <p>2</p> <p>'Snip'</p> <p>999</p> </blockquote> <p>Is it possible to have the program ou...
0
2010-02-10T18:51:38Z
2,239,531
<p>Text printed to stdout can't be dynamically changed. You need to use a panel.</p> <p><a href="http://docs.python.org/library/curses.panel.html" rel="nofollow">http://docs.python.org/library/curses.panel.html</a></p> <p>Initialize a new panel, and then use Panel.set_userptr(obj) in your loop.</p>
1
2010-02-10T19:03:19Z
[ "python" ]
How can files be added to a tarfile with Python, without adding the directory hierarchy?
2,239,655
<p>When I invoke <code>add()</code> on a <code>tarfile</code> object with a file path, the file is added to the tarball with directory hierarchy associated. In other words, if I unzip the tarfile the directories in the original directories hierarchy are reproduced.</p> <p>Is there a way to simply adding a plain file w...
24
2010-02-10T19:21:54Z
2,239,679
<p>You can use <a href="http://www.python.org/doc/2.6/library/tarfile.html#tarfile.TarFile.addfile"><code>tarfile.addfile()</code></a>, in the <a href="http://www.python.org/doc/2.6/library/tarfile.html#tarfile.TarInfo"><code>TarInfo</code></a> object, which is the first parameter, you can specify a <code>name</code> t...
27
2010-02-10T19:25:02Z
[ "python", "tar" ]
How can files be added to a tarfile with Python, without adding the directory hierarchy?
2,239,655
<p>When I invoke <code>add()</code> on a <code>tarfile</code> object with a file path, the file is added to the tarball with directory hierarchy associated. In other words, if I unzip the tarfile the directories in the original directories hierarchy are reproduced.</p> <p>Is there a way to simply adding a plain file w...
24
2010-02-10T19:21:54Z
2,993,751
<p>Maybe you can use the "arcname" argument to TarFile.add(name, arcname). It takes an alternate name that the file will have inside the archive.</p>
2
2010-06-07T22:56:29Z
[ "python", "tar" ]
How can files be added to a tarfile with Python, without adding the directory hierarchy?
2,239,655
<p>When I invoke <code>add()</code> on a <code>tarfile</code> object with a file path, the file is added to the tarball with directory hierarchy associated. In other words, if I unzip the tarfile the directories in the original directories hierarchy are reproduced.</p> <p>Is there a way to simply adding a plain file w...
24
2010-02-10T19:21:54Z
10,962,896
<p>The <strong>arch</strong> argument of TarFile.add() method is an alternate and convenient way to match your destination.</p> <p>Example: you want to archive a dir <em>repo/a.git/</em> to a <em>tar.gz</em> file, but you rather want the tree root in the archive begins by <em>a.git/</em> but not <em>repo/a.git/</em>, ...
31
2012-06-09T17:06:56Z
[ "python", "tar" ]
blocking channels vs async message passing
2,239,731
<p>I've noticed two methods to "message passing". One I've seen Erlang use and the other is from Stackless Python. From what I understand here's the difference</p> <p>Erlang Style - Messages are sent and queued into the mailbox of the receiving process. From there they are removed in a FIFO basis. Once the first proce...
8
2010-02-10T19:32:53Z
2,240,157
<p>Broadly speaking, this is unbounded queues vs bounded queues. A stackless channel can be considered a special case of a queue with 0 size.</p> <p>Bounded queues have a tendency to deadlock. Two threads/processes trying to send a message to each other, both with a full queue.</p> <p>Unbounded queues have more sub...
3
2010-02-10T20:34:40Z
[ "python", "erlang", "actor", "stackless", "python-stackless" ]
blocking channels vs async message passing
2,239,731
<p>I've noticed two methods to "message passing". One I've seen Erlang use and the other is from Stackless Python. From what I understand here's the difference</p> <p>Erlang Style - Messages are sent and queued into the mailbox of the receiving process. From there they are removed in a FIFO basis. Once the first proce...
8
2010-02-10T19:32:53Z
2,240,486
<p>My experience in Erlang programming is that when you expect a high messaging rate (that is, a faster producer than consumer) then you add your own flow control. A simple scenario</p> <ul> <li>The consumer will: send message, wait for ack, then repeat. </li> <li>The producer will: wait for message, send ack when mes...
6
2010-02-10T21:29:16Z
[ "python", "erlang", "actor", "stackless", "python-stackless" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,750
<pre><code>&gt;&gt;&gt; a = 255556 &gt;&gt;&gt; a == 255556 True &gt;&gt;&gt; a is 255556 False </code></pre> <p>I think that should answer it ;-)</p> <p>The reason is that some often-used objects, such as the booleans True and False, all 1-letter strings and short numbers are allocated once by the interpreter, and e...
5
2010-02-10T19:36:09Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,751
<p>That will only work for small numbers and I'm guessing it's also implementation-dependent. Python uses the same object instance for small numbers (iirc &lt;256), but this changes for bigger numbers.</p> <pre><code>&gt;&gt;&gt; a = 2104214124 &gt;&gt;&gt; b = 2104214124 &gt;&gt;&gt; a == b True &gt;&gt;&gt; a is b F...
5
2010-02-10T19:36:25Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,753
<p>Use <code>==</code>. Only integers from -1 to 256 will work with <code>is</code>.</p>
31
2010-02-10T19:36:39Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,764
<p>== is what you want, "is" just happens to work on your examples.</p>
0
2010-02-10T19:37:54Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,769
<pre><code>&gt;&gt;&gt; 2 == 2.0 True &gt;&gt;&gt; 2 is 2.0 False </code></pre> <p>Use ==</p>
0
2010-02-10T19:39:12Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
2,239,788
<p>Others have answered your question, but I'll go into a little bit more detail:</p> <p>Python's <code>is</code> compares identity - it asks the question "is this one thing exactly identical to this other thing" (similar to <code>==</code> in Java). So, there are some times when using <code>is</code> makes sense - th...
15
2010-02-10T19:41:59Z
[ "python" ]
Is it better to use "is" or "==" for number comparison in Python?
2,239,737
<p>Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?</p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; a = 1 &gt;&gt;&gt; a is 1 True &gt;&gt;&gt; a == 1 True &gt;&gt;&gt; a is 0 False &gt;&gt;&gt; a == 0 False </code></pre>
16
2010-02-10T19:34:10Z
35,038,071
<pre><code>import math def Compare(x,y): if x==y: print x," is equal to ",y elif x&gt;y: print x," is grater than ",y else: print x," is less than ",y def App(): print"Enter a number " a = raw_input() int(a) print"Enter second value" b = raw_input() int(b) ...
0
2016-01-27T12:58:28Z
[ "python" ]
How to set file/directory ownership/permissions in a Samba share on Windows using Python/.NET?
2,239,762
<p>I need to create directories and files in a Samba share on Windows, from a Python script. I can (and do) also use .NET 3.5 from Python. I would like to create these directories and files with certain owners and permissions. Can I achieve this somehow?</p>
1
2010-02-10T19:37:37Z
2,239,830
<p>You can use CACLS.exe to display or modify Access Control Lists (ACLs) for files and folders. You can call this from Python with something like:</p> <p><code>result = os.popen("cacls " + '"' + theDirPath + '"')</code></p> <p>There is a good example of a Python script which sets up permissions <a href="http://timg...
0
2010-02-10T19:46:55Z
[ ".net", "python", "windows", "samba" ]
Python equiv. of PHP foreach []?
2,239,771
<p>I am fetching rows from the database and wish to populate a multi-dimensional dictionary.</p> <p>The php version would be roughly this:</p> <pre><code>foreach($query as $rows): $values[$rows-&gt;id][] = $rows-&gt;name; endforeach; return $values; </code></pre> <p>I can't seem to find out the following issues...
1
2010-02-10T19:39:19Z
2,239,784
<pre><code> all_rows=[] for row in query: all_rows.append(row['col']) print(all_rows) </code></pre>
2
2010-02-10T19:41:37Z
[ "python", "dictionary", "key" ]
Python equiv. of PHP foreach []?
2,239,771
<p>I am fetching rows from the database and wish to populate a multi-dimensional dictionary.</p> <p>The php version would be roughly this:</p> <pre><code>foreach($query as $rows): $values[$rows-&gt;id][] = $rows-&gt;name; endforeach; return $values; </code></pre> <p>I can't seem to find out the following issues...
1
2010-02-10T19:39:19Z
2,239,786
<pre><code>values = collections.defaultdict(list) for rows in query: values[rows.id].append(rows.name) return values </code></pre>
8
2010-02-10T19:41:55Z
[ "python", "dictionary", "key" ]
Python equiv. of PHP foreach []?
2,239,771
<p>I am fetching rows from the database and wish to populate a multi-dimensional dictionary.</p> <p>The php version would be roughly this:</p> <pre><code>foreach($query as $rows): $values[$rows-&gt;id][] = $rows-&gt;name; endforeach; return $values; </code></pre> <p>I can't seem to find out the following issues...
1
2010-02-10T19:39:19Z
2,239,816
<p>You can do:</p> <pre><code>from collections import defaultdict values = defaultdict(list) for row in query: values[row.id].append(row.name) return values </code></pre> <p>Edit: forgot to return the values.</p>
1
2010-02-10T19:45:17Z
[ "python", "dictionary", "key" ]
Python equiv. of PHP foreach []?
2,239,771
<p>I am fetching rows from the database and wish to populate a multi-dimensional dictionary.</p> <p>The php version would be roughly this:</p> <pre><code>foreach($query as $rows): $values[$rows-&gt;id][] = $rows-&gt;name; endforeach; return $values; </code></pre> <p>I can't seem to find out the following issues...
1
2010-02-10T19:39:19Z
2,239,826
<p>Just a general note:</p> <ol> <li>Python's dictionaries are mappings without order, while adding numerical keys would allow "sequential" access, in case of iteration there's no guarantee that order will coincide with the natural order of keys.</li> <li>It's better not to translate from PHP to Python (or any other l...
4
2010-02-10T19:46:22Z
[ "python", "dictionary", "key" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,239,905
<p>It's stored in the operating system's disk cache in memory until it is flushed to disk, either implicitly due to timing or space issues, or explicitly via <a href="http://docs.python.org/library/stdtypes.html#file.flush"><code>fp.flush()</code></a>.</p>
5
2010-02-10T19:56:56Z
[ "python" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,239,910
<p>There will be write buffering in the Linux kernel, but at (ir)regular intervals they will be flushed to disk. Running out of such buffer space should never cause an application-level memory error; the buffers should empty before that happens, pausing the application while doing so.</p>
3
2010-02-10T19:57:37Z
[ "python" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,239,955
<p>If you a writing out a large file for which the writes might fail you a better off flushing the file to disk yourself at regular intervals using <code>fp.flush()</code>. This way the file will be in a location of your choosing that you can easily get to rather than being at the mercy of the OS:</p> <pre><code>fp = ...
1
2010-02-10T20:04:22Z
[ "python" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,239,960
<p>If you write line by line, it should not be a problem. You should show the code of what you are doing before the write. For a start you can try to delete objects where not necessary, use <code>fp.flush()</code> etc..</p>
0
2010-02-10T20:05:16Z
[ "python" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,240,133
<p>File writing should never give a memory error; with all probability, you have some bug in another place.</p> <p>If you have a loop, and a memory error, then I would look if you are "leaking" references to objects.<br> Something like:</p> <pre><code>def do_something(a, b = []): b.append(a) return b fp = op...
0
2010-02-10T20:31:04Z
[ "python" ]
Does the Python "open" function save its content in memory or in a temp file?
2,239,888
<p>For the following Python code:</p> <pre><code>fp = open('output.txt', 'wb') # Very big file, writes a lot of lines, n is a very large number for i in range(1, n): fp.write('something' * n) fp.close() </code></pre> <p>The writing process above can last more than 30 min. Sometimes I get the error <code>MemoryErr...
6
2010-02-10T19:54:53Z
2,240,174
<p>Building on ataylor's comment to the question: </p> <p>You might want to nest your loop. Something like </p> <pre><code>for i in range(1,n): for each in range n: fp.write('something') fp.close() </code></pre> <p>That way, the only thing that gets put into memory is the string <code>"something"</code>,...
2
2010-02-10T20:38:32Z
[ "python" ]
Why does Nose not see any of my environmental variables?
2,240,067
<p>I'm just getting started using Nose and Nosetests and my tests are failing because Nose can't see the environmental variables.</p> <p>So far, the errors: AttributeError: 'Settings' object has no attribute 'DJANGO_SETTINGS_MODULE'</p> <p>I fixed this by exporting DJANGO_SETTINGS_MODULE from .bash_profile</p> <pre>...
0
2010-02-10T20:22:24Z
2,240,125
<p>Apparently nose <a href="http://code.djangoproject.com/attachment/ticket/11777/11777.diff" rel="nofollow">doesn't call</a> <code>create_test_db()</code> in <code>django/db/backends/creation.py</code>, so you are seeing this error. Just set it to <code>None</code>, or call the method yourself. Not sure if this is f...
1
2010-02-10T20:30:04Z
[ "python", "django", "nose" ]
Why does Nose not see any of my environmental variables?
2,240,067
<p>I'm just getting started using Nose and Nosetests and my tests are failing because Nose can't see the environmental variables.</p> <p>So far, the errors: AttributeError: 'Settings' object has no attribute 'DJANGO_SETTINGS_MODULE'</p> <p>I fixed this by exporting DJANGO_SETTINGS_MODULE from .bash_profile</p> <pre>...
0
2010-02-10T20:22:24Z
2,240,456
<p>As Alok said, Nose doesn't call BaseDatabaseCreation.create_test_db('None') from django.db.backends.creation so you will need to set this setting manually. </p> <p>I was not able to get that to work.</p> <p>However, I found NoseDjango. </p> <p>Install NoseDjango with: </p> <pre><code>easy_install django-nose ...
1
2010-02-10T21:24:11Z
[ "python", "django", "nose" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,318
<p>You could use a regular expression to divide the string into groups:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('(\d+)\s*(\w+)') &gt;&gt;&gt; p.match('2GB').groups() ('2', 'GB') &gt;&gt;&gt; p.match('17 ft').groups() ('17', 'ft') </code></pre>
5
2010-02-10T21:03:21Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,322
<p>How about using a regular expression</p> <p><a href="http://python.org/doc/1.6/lib/module-regsub.html" rel="nofollow">http://python.org/doc/1.6/lib/module-regsub.html</a></p>
0
2010-02-10T21:03:48Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,323
<p>You should use regular expressions, grouping together what you want to find out:</p> <pre><code>import re s = "17GB" match = re.match(r"^([1-9][0-9]*)\s*(GB|MB|KB|B)$", s) if match: print "Number: %d, unit: %s" % (int(match.group(1)), match.group(2)) </code></pre> <p>Change the regex according to what you want t...
1
2010-02-10T21:03:52Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,328
<p><a href="http://docs.python.org/library/tokenize.html" rel="nofollow"><code>tokenize</code></a> can help:</p> <pre><code>&gt;&gt;&gt; import StringIO &gt;&gt;&gt; s = StringIO.StringIO('27GB') &gt;&gt;&gt; for token in tokenize.generate_tokens(s.readline): ... print token ... (2, '27', (1, 0), (1, 2), '27GB') (1...
3
2010-02-10T21:04:24Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,350
<p>For this task, I would definitely use a regular expression:</p> <pre><code>import re there = re.compile(r'\s*(\d+)\s*(\S+)') thematch = there.match(s) if thematch: number, unit = thematch.groups() else: raise ValueError('String %r not in the expected format' % s) </code></pre> <p>In the RE pattern, <code>\s</c...
0
2010-02-10T21:07:45Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,358
<p>A regular expression.</p> <pre><code>import re m = re.match(r'\s*(?P&lt;n&gt;[-+]?[.0-9])\s*(?P&lt;u&gt;.*)', s) if m is None: raise ValueError("not a number with units") number = m.group("n") unit = m.group("u") </code></pre> <p>This will give you a number (integer or fixed point; too hard to disambiguate scie...
0
2010-02-10T21:08:55Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,372
<pre><code>s='17GB' for i,c in enumerate(s): if not c.isdigit(): break number=int(s[:i]) unit=s[i:] </code></pre>
2
2010-02-10T21:10:43Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,240,469
<p>You can break out of the loop when you find the first non-digit character</p> <pre><code>for i,c in enumerate(s): if not c.isdigit(): break number = s[:i] unit = s[i:].lstrip() </code></pre> <p>If you have negative and decimals:</p> <pre><code>numeric = '0123456789-.' for i,c in enumerate(s): if c...
7
2010-02-10T21:26:47Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
2,241,663
<pre><code>&gt;&gt;&gt; s="17GB" &gt;&gt;&gt; ind=map(str.isalpha,s).index(True) &gt;&gt;&gt; num,suffix=s[:ind],s[ind:] &gt;&gt;&gt; print num+":"+suffix 17:GB </code></pre>
1
2010-02-11T01:39:02Z
[ "python", "string", "units-of-measurement" ]
Separate number from unit in a string in Python
2,240,303
<p>I have strings containing numbers with their units, e.g. 2GB, 17ft, etc. I would like to separate the number from the unit and create 2 different strings. Sometimes, there is a whitespace between them (e.g. 2 GB) and it's easy to do it using split(' ').</p> <p>When they are together (e.g. 2GB), I would test every c...
3
2010-02-10T20:59:55Z
30,087,094
<p>This uses an approach which should be a bit more forgiving than regexes. Note: this is not as performant as the other solutions posted.</p> <pre><code>def split_units(value): """ &gt;&gt;&gt; split_units("2GB") (2.0, 'GB') &gt;&gt;&gt; split_units("17 ft") (17.0, 'ft') &gt;&gt;&gt; split_uni...
0
2015-05-06T20:39:32Z
[ "python", "string", "units-of-measurement" ]
Python 2.6.4 property decorators not working
2,240,351
<p>I've seen many examples <a href="http://blog.ianbicking.org/property-decorator.html">online</a> and <a href="http://stackoverflow.com/questions/2123585/python-multiple-properties-one-setter-getter">in this forum</a> of how to create properties in Python with special <strong>getters</strong> and <strong>setters</stro...
6
2010-02-10T21:07:49Z
2,240,368
<p><strong>PathInfo</strong> must subclass <strong>object</strong>.</p> <p>Like this:</p> <pre><code>class PathInfo(object): </code></pre> <p>Properties work only on new style classes.</p>
20
2010-02-10T21:09:59Z
[ "python", "properties", "decorator" ]
Will shell scripts called from python persist after the python script ends?
2,240,494
<p>As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the pyth...
4
2010-02-10T21:30:51Z
2,240,522
<p><code>os.system()</code> does not return until the process it launches has ended. Use <a href="http://www.jython.org/docs/library/subprocess.html" rel="nofollow"><code>subprocess</code></a> or <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html" rel="nofollow"><code>Runtime.exec()</code></a> if y...
1
2010-02-10T21:34:31Z
[ "python", "automation", "jython", "sh" ]
Will shell scripts called from python persist after the python script ends?
2,240,494
<p>As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the pyth...
4
2010-02-10T21:30:51Z
2,240,526
<p>I wonder if using subprocess.Popen would work better for you.</p> <p>maybe doing something like shell=True </p>
0
2010-02-10T21:35:20Z
[ "python", "automation", "jython", "sh" ]
Will shell scripts called from python persist after the python script ends?
2,240,494
<p>As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the pyth...
4
2010-02-10T21:30:51Z
2,240,528
<p>Python threads will all die with Python. Also, os.system is blocking. But that's okay -- if the command that os.system() runs launches a new process (but not a child process), all will be fine. On Windows, for instance, if the command begins with "start" the "start"'d process will remain after Python dies. </p> ...
2
2010-02-10T21:35:56Z
[ "python", "automation", "jython", "sh" ]
Will shell scripts called from python persist after the python script ends?
2,240,494
<p>As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the pyth...
4
2010-02-10T21:30:51Z
2,240,544
<p>Threads won't work because they are part of the process. The <code>system</code> call won't work because it blocks as your new process executes.</p> <p>You will need to use something like <code>os.fork()</code> to spawn a new process and execute it in the new process. Take a look at <a href="http://docs.python.org/...
0
2010-02-10T21:38:44Z
[ "python", "automation", "jython", "sh" ]
Will shell scripts called from python persist after the python script ends?
2,240,494
<p>As part of an automated test, I have a python script that needs to call two shell scripts that start two different servers that need to interact after the calling script ends. (It's actually a jython script, but I'm not sure that matters at this point.) What can I do to ensure that the servers stay up after the pyth...
4
2010-02-10T21:30:51Z
2,240,547
<p>Generally, to launch a long-running server that's independent of its parent, you need to daemonize it. Depending on your environment, there are various wrappers that can assist in this process. </p>
0
2010-02-10T21:39:23Z
[ "python", "automation", "jython", "sh" ]
Declare which signals are subscribed to on DBus?
2,240,562
<p>Is there a way to declare which signals are subscribed by a Python application over DBus? </p> <p>In other words, is there a way to advertise through the "Introspectable" interface which signals are subscribed to. I use "D-Feet D-Bus debugger".</p> <p>E.g. Application subscribes to signal X (using the <code>add_si...
3
2010-02-10T21:42:07Z
2,364,175
<p>This is probably not possible since a signal is emitted on the bus and the application just picks out what is interesting. Subscribing is not happening inside dbus.</p>
1
2010-03-02T15:17:51Z
[ "python", "dbus" ]
Declare which signals are subscribed to on DBus?
2,240,562
<p>Is there a way to declare which signals are subscribed by a Python application over DBus? </p> <p>In other words, is there a way to advertise through the "Introspectable" interface which signals are subscribed to. I use "D-Feet D-Bus debugger".</p> <p>E.g. Application subscribes to signal X (using the <code>add_si...
3
2010-02-10T21:42:07Z
2,389,202
<p>D-Bus clients call <code>AddMatch</code> on the bus daemon to register their interest in messages matching a particular pattern; most bindings add a match rule either for all signals on a particular service and object path, or for signals on a particular interface on that service and object path, when you create a p...
3
2010-03-05T19:02:02Z
[ "python", "dbus" ]
PyQt beginremoverows
2,240,582
<p><br> In the example below:</p> <pre><code>from PyQt4 import QtCore, QtGui class Ui_Dialog(QtGui.QDialog): def __init__(self,parent=None): QtGui.QDialog.__init__(self,parent) self.setObjectName("Dialog") self.resize(600, 500) self.model = QtGui.QDirModel() self.tree = Q...
3
2010-02-10T21:45:04Z
2,240,859
<blockquote> <p>I know I'm making mistake on this line:</p> <pre><code>self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index)) </code></pre> </blockquote> <p>Yes, you're right. Let's look at what you're passing in:</p> <pre><code>index.parent() - the parent of index index.row() - the r...
3
2010-02-10T22:38:00Z
[ "python", "pyqt" ]
PyQt beginremoverows
2,240,582
<p><br> In the example below:</p> <pre><code>from PyQt4 import QtCore, QtGui class Ui_Dialog(QtGui.QDialog): def __init__(self,parent=None): QtGui.QDialog.__init__(self,parent) self.setObjectName("Dialog") self.resize(600, 500) self.model = QtGui.QDirModel() self.tree = Q...
3
2010-02-10T21:45:04Z
2,244,657
<p>Jebagnanadas - I recommend changing your design slightly; instead of using the UI as your model <em>and</em> view, create separate obects to represent what is in your TreeView, and then update those objects and rebuild/refresh your TreeView.</p> <p>Your <code>test()</code> method should just remove the selected obj...
0
2010-02-11T13:23:14Z
[ "python", "pyqt" ]
PyQt beginremoverows
2,240,582
<p><br> In the example below:</p> <pre><code>from PyQt4 import QtCore, QtGui class Ui_Dialog(QtGui.QDialog): def __init__(self,parent=None): QtGui.QDialog.__init__(self,parent) self.setObjectName("Dialog") self.resize(600, 500) self.model = QtGui.QDirModel() self.tree = Q...
3
2010-02-10T21:45:04Z
2,319,831
<p>Thanks jcoon and kaleb.. I've hidden the row using setRowHidden() function from the tree view..</p>
0
2010-02-23T16:26:46Z
[ "python", "pyqt" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
2,242,256
<p>For good cross-platform support, I would look at <a href="http://www.riverbankcomputing.co.uk/software/pyqt/download" rel="nofollow">PyQt</a>. It will add some heft to your library, but they've done a good job working out most of the kinks.</p>
0
2010-02-11T04:24:30Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
2,242,691
<p>On win you can use <a href="http://www.fullphat.net/index.php" rel="nofollow">snarl</a>.</p> <p>Using it with python: <a href="http://www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972" rel="nofollow">www.k23productions.com/e107_plugins/forum/forum_viewtopic.php?2972</a></p>
1
2010-02-11T06:30:52Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
2,242,918
<ul> <li><p>How to go about writing it</p> <p>Check how <code>keyring</code> handles the cross-platform issues (it's a python library which plugs into various autodetected keychain backends for storage)</p></li> <li><p>Growl isn't bundled with OSX you have to install it separately, OSX doesn't come with any built-in n...
6
2010-02-11T07:37:58Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
2,244,315
<p><a href="http://code.google.com/p/readertray/source/browse/trunk/readergui.py">Here's a desktop notifier I wrote a few years ago using wxPython</a> - it behaves identically across Windows and Linux and should also run on OSX. It contains a threaded event loop that can be used to animate a notification window contain...
17
2010-02-11T12:21:36Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
2,249,672
<p>try <a href="http://www.riverbankcomputing.co.uk/software/pyqt/intro" rel="nofollow">PyQt4</a>, if you don't care about the size.</p> <p>here is the class for this job: <a href="http://doc.trolltech.com/4.5/qsystemtrayicon.html" rel="nofollow">http://doc.trolltech.com/4.5/qsystemtrayicon.html</a></p>
3
2010-02-12T04:28:41Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
3,240,350
<p>Sounds like you need <a href="http://www.growlforwindows.com/gfw/default.aspx" rel="nofollow">Growl for Windows</a></p>
1
2010-07-13T18:44:36Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
7,076,231
<p>At Pycon 2010 there was a <a href="http://python.mirocommunity.org/video/1595/pycon-2010-cross-platform-appl">presentation on cross-platform Python development</a>. There was a html page about it as well, containing some advice for cross-platform notification. However, I don't find it online anymore, but I saved a ...
15
2011-08-16T09:37:38Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Cross-platform desktop notifier in Python
2,240,674
<p>I am looking for <a href="http://growl.info/">Growl</a>-like, Windows balloon-tip-like notifications library in Python. Imagine writing code like:</p> <pre><code>&gt;&gt;&gt; import desktopnotifier as dn &gt;&gt;&gt; dn.notify('Title', 'Long description goes here') </code></pre> <p>.. and that would notify with co...
43
2010-02-10T22:02:36Z
21,660,406
<p>Here is something simple that works for me. The Toast stays up for 2 secs and disappears. Yes, OP didn't want 'gigantic' PyQt4, but this may be useful to others.</p> <pre><code>import sys, time from PyQt4 import QtCore, QtGui import uiToast window = None # global # Usage: Toast('Message') class Toast(QtGui.QM...
1
2014-02-09T14:38:11Z
[ "python", "cross-platform", "desktop", "notifications", "growl" ]
Is CherryPy a robust webserver (ie, is it reliable under a huge load like Apache)?
2,240,700
<p>I'm wondering because <a href="http://www.cherrypy.org/">CherryPy</a> is, from my knowledge, built purely in Python, which is obviously slower than C et al. Does this mean that it's only good for dev / testing environments, or could I use it behind <a href="http://www.nginx.org/">NGINX</a> like I use Apache with <a ...
12
2010-02-10T22:07:06Z
2,240,753
<p>CherryPy's WSGI server is about as fast as a pure-Python WSGI server is going to get. I personally use it behind Nginx in production, but even standalone on my dev machine I can load each instance with several hundred requests / sec. without problems.</p> <p>Can you find a faster server? Yes. Is CherryPy a robust ...
17
2010-02-10T22:14:18Z
[ "python", "webserver", "cherrypy" ]
Is CherryPy a robust webserver (ie, is it reliable under a huge load like Apache)?
2,240,700
<p>I'm wondering because <a href="http://www.cherrypy.org/">CherryPy</a> is, from my knowledge, built purely in Python, which is obviously slower than C et al. Does this mean that it's only good for dev / testing environments, or could I use it behind <a href="http://www.nginx.org/">NGINX</a> like I use Apache with <a ...
12
2010-02-10T22:07:06Z
2,240,883
<p>You should probably consider Apache + mod_wsgi as the standard front-end for any Python-based web application.</p> <p>You do not want to serve any static content (.CSS, .JPEG, etc.) from any Python-based application; you want static files served by Apache. </p> <p>You want the dynamic HTML page handle separately b...
4
2010-02-10T22:41:45Z
[ "python", "webserver", "cherrypy" ]
In Python, How Do I Set an Expiration Date on a User Object in Active Directory?
2,240,963
<p><strong>Setup:</strong> I have the user object in hand, via <code>win32com.client.Dispatch('ADsNameSpaces')</code>, in a standard Windows environment, using ActiveState Python of the 2.6 flavor. Apparently, <code>Get()</code> and <code>Put()</code>/<code>SetInfo()</code> methods are the appropriate ways to read fro...
2
2010-02-10T22:56:47Z
9,647,005
<p>Well I never used ADsNameSpaces COM objects before. But looking at the examples <a href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.advancedfilters.accountexpirationdate.aspx" rel="nofollow">Here</a>, I think you may want to try something like:</p> <pre><code>objUser.AccountE...
2
2012-03-10T13:59:14Z
[ "python", "active-directory" ]
Is there a significant overhead by using different versions of sha hashing (hashlib module)
2,241,013
<p>The <a href="http://docs.python.org/library/hashlib.html"><code>hashlib</code></a> Python module provides the following hash algorithms constructors: <code>md5()</code>, <code>sha1()</code>, <code>sha224()</code>, <code>sha256()</code>, <code>sha384()</code>, and <code>sha512()</code>.</p> <p>Assuming I don't want ...
12
2010-02-10T23:03:57Z
2,241,072
<p>Why not just benchmark it?</p> <pre><code>&gt;&gt;&gt; def sha1(s): ... return hashlib.sha1(s).hexdigest() ... &gt;&gt;&gt; def sha512(s): ... return hashlib.sha512(s).hexdigest() ... &gt;&gt;&gt; t1 = timeit.Timer("sha1('asdf' * 100)", "from __main__ import sha1") &gt;&gt;&gt; t512 = timeit.Timer("sha512('...
18
2010-02-10T23:18:13Z
[ "python", "hash", "hashlib" ]
Is there a significant overhead by using different versions of sha hashing (hashlib module)
2,241,013
<p>The <a href="http://docs.python.org/library/hashlib.html"><code>hashlib</code></a> Python module provides the following hash algorithms constructors: <code>md5()</code>, <code>sha1()</code>, <code>sha224()</code>, <code>sha256()</code>, <code>sha384()</code>, and <code>sha512()</code>.</p> <p>Assuming I don't want ...
12
2010-02-10T23:03:57Z
2,241,109
<p>Perhaps a naive test... but it looks like it depends on how much you're hashing. 2 blocks of sha512 is faster than 4 blocks of sha256?</p> <pre><code>&gt;&gt;&gt; import timeit &gt;&gt;&gt; import hashlib &gt;&gt;&gt; for sha in [ x for x in dir(hashlib) if x.startswith('sha') ]: ... t = timeit.Timer("hashlib.%s(...
3
2010-02-10T23:24:17Z
[ "python", "hash", "hashlib" ]
Django and VirtualEnv Development/Deployment Best Practices
2,241,055
<p>Just curious how people are deploying their Django projects in combination with virtualenv</p> <ul> <li>More specifically, how do you keep your production virtualenv's synched correctly with your development machine?</li> </ul> <p>I use git for scm but I don't have my virtualenv inside the git repo - should I, or ...
38
2010-02-10T23:14:20Z
2,241,905
<p>I use this bootstrap.py: <a href="http://github.com/ccnmtl/ccnmtldjango/blob/master/ccnmtldjango/template/bootstrap.py" rel="nofollow">http://github.com/ccnmtl/ccnmtldjango/blob/master/ccnmtldjango/template/bootstrap.py</a></p> <p>which expects are directory called 'requirements' that looks something like this: <a ...
4
2010-02-11T02:46:41Z
[ "python", "django", "git", "virtualenv" ]
Django and VirtualEnv Development/Deployment Best Practices
2,241,055
<p>Just curious how people are deploying their Django projects in combination with virtualenv</p> <ul> <li>More specifically, how do you keep your production virtualenv's synched correctly with your development machine?</li> </ul> <p>I use git for scm but I don't have my virtualenv inside the git repo - should I, or ...
38
2010-02-10T23:14:20Z
2,243,060
<p>I just set something like this up at work using pip, Fabric and git. The flow is basically like this, and borrows heavily from <a href="http://www.morethanseven.net/2009/07/27/fabric-django-git-apache-mod-wsgi-virtualenv-and-p/" rel="nofollow">this script</a>:</p> <ol> <li>In our source tree, we maintain a require...
21
2010-02-11T08:13:02Z
[ "python", "django", "git", "virtualenv" ]
Is there a Python ebXML client?
2,241,149
<p>I am trying to use a remote web service with an ebXML/SOAP interface from my python application and am hitting a wall about how to best accomplish it.</p> <p>So far, what I can find are lots of Java interface bindings but none for Python.</p> <p>Do I have to start my project over in Java?</p>
1
2010-02-10T23:32:22Z
2,241,390
<p>Doesn't look too hopeful at <a href="http://pypi.python.org/pypi?%3Aaction=search&amp;term=ebXML&amp;submit=search" rel="nofollow">http://pypi.python.org/pypi?%3Aaction=search&amp;term=ebXML&amp;submit=search</a></p> <p>Do you know if there is a C library?</p>
0
2010-02-11T00:19:47Z
[ "python", "ebxml" ]
Is it possible to declare a function without arguments but then pass some arguments to that function without raising exception?
2,241,200
<p>In python is it possible to have the above code without raising an exception ?</p> <pre><code>def myfunc(): pass # TypeError myfunc() takes no arguments (1 given) myfunc('param') </code></pre> <p>Usually in php in some circumstances I launch a function without parameters and then retrieve the parameters insid...
6
2010-02-10T23:43:20Z
2,241,220
<p>Sure can!</p> <p>You can define variable length parameter lists like so:</p> <pre><code>def foo(*args): print len(args) </code></pre> <p>args is a tuple of your parameters so calling:</p> <pre><code>foo(1,2) </code></pre> <p>gives you the tuple (1, 2) inside your function.</p>
4
2010-02-10T23:47:44Z
[ "python", "exception", "arguments" ]
Is it possible to declare a function without arguments but then pass some arguments to that function without raising exception?
2,241,200
<p>In python is it possible to have the above code without raising an exception ?</p> <pre><code>def myfunc(): pass # TypeError myfunc() takes no arguments (1 given) myfunc('param') </code></pre> <p>Usually in php in some circumstances I launch a function without parameters and then retrieve the parameters insid...
6
2010-02-10T23:43:20Z
2,241,224
<p>There are two ways to pass args in</p> <p><strong>By Position</strong></p> <pre><code>&gt;&gt;&gt; def myfunc(*args): ... print "args", args ... &gt;&gt;&gt; myfunc("param") args ('param',) </code></pre> <p><strong>By Keyword</strong></p> <pre><code>&gt;&gt;&gt; def myfunc(**kw): ... print "kw", kw ... &gt;&g...
9
2010-02-10T23:48:31Z
[ "python", "exception", "arguments" ]
Is it possible to declare a function without arguments but then pass some arguments to that function without raising exception?
2,241,200
<p>In python is it possible to have the above code without raising an exception ?</p> <pre><code>def myfunc(): pass # TypeError myfunc() takes no arguments (1 given) myfunc('param') </code></pre> <p>Usually in php in some circumstances I launch a function without parameters and then retrieve the parameters insid...
6
2010-02-10T23:43:20Z
2,241,232
<pre><code>&gt;&gt;&gt; def myFunc(*args, **kwargs): ... # This function accepts arbitary arguments: ... # Keywords arguments are available in the kwargs dict; ... # Regular arguments are in the args tuple. ... # (This behaviour is dictated by the stars, not by ... # the name of the formal parameters.) ... ...
8
2010-02-10T23:50:41Z
[ "python", "exception", "arguments" ]
Is it possible to declare a function without arguments but then pass some arguments to that function without raising exception?
2,241,200
<p>In python is it possible to have the above code without raising an exception ?</p> <pre><code>def myfunc(): pass # TypeError myfunc() takes no arguments (1 given) myfunc('param') </code></pre> <p>Usually in php in some circumstances I launch a function without parameters and then retrieve the parameters insid...
6
2010-02-10T23:43:20Z
23,711,694
<p>Here is a function decorator I wrote to do just that, along with an example of usage:</p> <pre><code>def IgnoreExtraArguments(f): import types c = f.func_code if c.co_flags &amp; 0x04 or c.co_flags&amp;0x08: raise ValueError('function already accepts optional arguments') newc = types.CodeTyp...
1
2014-05-17T13:05:16Z
[ "python", "exception", "arguments" ]
running clock and triggering
2,241,234
<p>constantly running a clock and trigger an other function for every 5 seconds.</p> <p>Please give me idea how to do this.</p> <p>Thanks a bunch </p>
3
2010-02-10T23:51:35Z
2,241,266
<pre><code>import time while True: time.sleep(5) someFunction() </code></pre> <p>As gnibbler says, the interval will actually be (5 seconds + the time it takes to run someFunction). If you need it to be exactly 5 seconds:</p> <pre><code>targetTime = time.time() while True: someFunction() targetTime +...
1
2010-02-10T23:58:53Z
[ "python" ]
running clock and triggering
2,241,234
<p>constantly running a clock and trigger an other function for every 5 seconds.</p> <p>Please give me idea how to do this.</p> <p>Thanks a bunch </p>
3
2010-02-10T23:51:35Z
2,241,283
<p>You can use the scheduler from the <a href="http://docs.python.org/library/sched.html" rel="nofollow">sched</a> module.</p> <p>Just make the scheduled function reschedule itself every time it completes.</p>
1
2010-02-11T00:01:35Z
[ "python" ]
running clock and triggering
2,241,234
<p>constantly running a clock and trigger an other function for every 5 seconds.</p> <p>Please give me idea how to do this.</p> <p>Thanks a bunch </p>
3
2010-02-10T23:51:35Z
2,241,321
<pre><code>&gt;&gt;&gt; import sched, time &gt;&gt;&gt; s = sched.scheduler(time.time, time.sleep) &gt;&gt;&gt; def print_time(): ... s.enter(5, 1, print_time, ()) ... print "From print_time", time.time() ... &gt;&gt;&gt; s.enter(0, 1, print_time, ()) Event(time=1265846894.4069381, priority=1, action=&lt;funct...
4
2010-02-11T00:08:44Z
[ "python" ]
Python regex - r prefix
2,241,600
<p>Can anyone explain why example 1 below works, when the r prefix is not used? I thought the r prefix must be used whenever escape sequences are used? Example 2 and example 3 demonstrates this..</p> <pre><code># example 1 import re print (re.sub('\s+', ' ', 'hello there there')) # prints 'hello there there' ...
27
2010-02-11T01:18:56Z
2,241,618
<p>Because <code>\</code> begin escape sequences only when they are valid escape sequences.</p> <pre><code>&gt;&gt;&gt; '\n' '\n' &gt;&gt;&gt; r'\n' '\\n' &gt;&gt;&gt; print '\n' &gt;&gt;&gt; print r'\n' \n &gt;&gt;&gt; '\s' '\s' &gt;&gt;&gt; r'\s' '\\s' &gt;&gt;&gt; print '\s' \s &gt;&gt;&gt; print r'\s' \s </code>...
41
2010-02-11T01:24:16Z
[ "python", "regex", "string", "literals", "prefix" ]
Python regex - r prefix
2,241,600
<p>Can anyone explain why example 1 below works, when the r prefix is not used? I thought the r prefix must be used whenever escape sequences are used? Example 2 and example 3 demonstrates this..</p> <pre><code># example 1 import re print (re.sub('\s+', ' ', 'hello there there')) # prints 'hello there there' ...
27
2010-02-11T01:18:56Z
2,241,621
<p>Not all sequences involving backslashes are escape sequences. <code>\t</code> and <code>\f</code> are, for example, but <code>\s</code> is not. In a non-raw string literal, any <code>\</code> that is not part of an escape sequence is seen as just another <code>\</code>:</p> <pre><code>&gt;&gt;&gt; "\s" '\\s' &gt;&g...
4
2010-02-11T01:24:55Z
[ "python", "regex", "string", "literals", "prefix" ]
Python regex - r prefix
2,241,600
<p>Can anyone explain why example 1 below works, when the r prefix is not used? I thought the r prefix must be used whenever escape sequences are used? Example 2 and example 3 demonstrates this..</p> <pre><code># example 1 import re print (re.sub('\s+', ' ', 'hello there there')) # prints 'hello there there' ...
27
2010-02-11T01:18:56Z
2,241,641
<p>the 'r' means the the following is a "raw string", ie. backslash characters are treated literally instead of signifying special treatment of the following character.</p> <p><a href="http://docs.python.org/reference/lexical_analysis.html#literals">http://docs.python.org/reference/lexical_analysis.html#literals</a></...
10
2010-02-11T01:30:11Z
[ "python", "regex", "string", "literals", "prefix" ]
Facebook calling Google App Engine code using GET instead of POST
2,241,689
<p>I've been developing a Facebook app using Google App Engine in Python and the <a href="http://github.com/sciyoshi/pyfacebook" rel="nofollow">pyfacebook bindings</a>. For weeks everything worked fine but suddenly it stopped.</p> <p>At first I thought it was a code change so I rolled back the entire dev directory to ...
2
2010-02-11T01:49:49Z
2,443,470
<p>Although I'm not completely sure this was the cause, it appears I changed from an FBML app to an IFrame app. FBML mode relies on POST calls but IFrame appears to use GET. I'm inferring this answer from <a href="http://dollarmani-facebook.blogspot.com/2008/09/using-facebook-api-in-python.html" rel="nofollow">what I ...
0
2010-03-14T19:39:14Z
[ "python", "google-app-engine", "facebook" ]
Facebook calling Google App Engine code using GET instead of POST
2,241,689
<p>I've been developing a Facebook app using Google App Engine in Python and the <a href="http://github.com/sciyoshi/pyfacebook" rel="nofollow">pyfacebook bindings</a>. For weeks everything worked fine but suddenly it stopped.</p> <p>At first I thought it was a code change so I rolled back the entire dev directory to ...
2
2010-02-11T01:49:49Z
5,794,185
<p>The typical flow for a user when using the application begins with the user landing at some Canvas URL, like <a href="http://apps.facebook.com/runwithfriends/" rel="nofollow">http://apps.facebook.com/runwithfriends/</a>. At this point, Facebook will load up it's chrome, and render a tag to your application. You'll ...
1
2011-04-26T17:33:24Z
[ "python", "google-app-engine", "facebook" ]
PySVN error: URL doesn't exist
2,241,754
<p>I got a SVN repository copied onto my computer using svnsync. Now when I try to replay it using PySVN it fails at a specific revision (29762) with the message:</p> <blockquote> <p>pysvn._pysvn_2_6.ClientError: URL 'svn://svn.zope.org/repos/main/ZODB/trunk/src/Persistence' doesn't exist</p> </blockquote> <p>I can...
0
2010-02-11T02:04:44Z
2,422,933
<p>Although the error message doesn't hint to that, I believe it was caused by running out of disk space. After deleting other files on the drive and re-running the script it worked fine.</p>
0
2010-03-11T06:06:20Z
[ "python", "svn", "pysvn" ]
PySVN error: URL doesn't exist
2,241,754
<p>I got a SVN repository copied onto my computer using svnsync. Now when I try to replay it using PySVN it fails at a specific revision (29762) with the message:</p> <blockquote> <p>pysvn._pysvn_2_6.ClientError: URL 'svn://svn.zope.org/repos/main/ZODB/trunk/src/Persistence' doesn't exist</p> </blockquote> <p>I can...
0
2010-02-11T02:04:44Z
7,363,302
<pre><code>try: client.update(path,revision=pysvn.Revision(pysvn.opt_revision_kind.number,RevNumber),ignore_externals=False) except: print "Revision skipped at", RevNumber continue </code></pre> <p>This does not solve the problem, but you can use try/ except for your code to go on, if you are ok with omit...
0
2011-09-09T14:36:12Z
[ "python", "svn", "pysvn" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
2,241,904
<p><code>dict.fromkeys([1, 2, 3, 4])</code></p> <p>This is actually a classmethod, so it works for dict-subclasses (like <code>collections.defaultdict</code>) as well. The optional second argument specifies the value to use for the keys (defaults to <code>None</code>.)</p>
156
2010-02-11T02:45:35Z
[ "dictionary", "python" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
2,241,911
<pre><code>dict.fromkeys(keys, None) </code></pre>
27
2010-02-11T02:48:21Z
[ "dictionary", "python" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
2,242,375
<pre><code>d = {} for i in keys: d[i] = None </code></pre>
3
2010-02-11T04:59:55Z
[ "dictionary", "python" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
2,244,026
<p>nobody cared to give a dict-comprehension solution ?</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3,5,6,7] &gt;&gt;&gt; {key: None for key in keys} {1: None, 2: None, 3: None, 5: None, 6: None, 7: None} </code></pre>
82
2010-02-11T11:18:18Z
[ "dictionary", "python" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
32,182,408
<pre><code>&gt;&gt;&gt; keyDict = {"a","b","c","d"} &gt;&gt;&gt; dict([(key, []) for key in keyDict]) </code></pre> <p>Output:</p> <pre><code>{'a': [], 'c': [], 'b': [], 'd': []} </code></pre>
4
2015-08-24T12:31:27Z
[ "dictionary", "python" ]
How to initialize a dict with keys from a list and empty value in Python?
2,241,891
<p>I'd like to get from this:</p> <pre><code>keys = [1,2,3] </code></pre> <p>to this:</p> <pre><code>{1: None, 2: None, 3: None} </code></pre> <p>Is there a pythonic way of doing it?</p> <p>This is an ugly way to do it:</p> <pre><code>&gt;&gt;&gt; keys = [1,2,3] &gt;&gt;&gt; dict([(1,2)]) {1: 2} &gt;&gt;&gt; dict...
84
2010-02-11T02:43:19Z
34,070,102
<pre><code>d = {} for i in range(len(keys)): d.update({i+1:[]}) </code></pre>
-2
2015-12-03T15:40:49Z
[ "dictionary", "python" ]
How to send a xml-rpc request in python?
2,242,174
<p>I was just wondering, how would I be able to send a <code>xml-rpc</code> request in python? I know you can use <code>xmlrpclib</code>, but how do I send out a request in <code>xml</code> to access a function?</p> <p>I would like to see the <code>xml</code> response.</p> <p>So basically I would like to send the fol...
5
2010-02-11T03:59:09Z
2,242,202
<p>Here's a simple XML-RPC client in Python:</p> <pre><code>import xmlrpclib s = xmlrpclib.ServerProxy('http://localhost:8000') print s.myfunction(2, 4) </code></pre> <p>Works with this server:</p> <pre><code>from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler...
10
2010-02-11T04:06:34Z
[ "python", "xml-rpc" ]
How to send a xml-rpc request in python?
2,242,174
<p>I was just wondering, how would I be able to send a <code>xml-rpc</code> request in python? I know you can use <code>xmlrpclib</code>, but how do I send out a request in <code>xml</code> to access a function?</p> <p>I would like to see the <code>xml</code> response.</p> <p>So basically I would like to send the fol...
5
2010-02-11T03:59:09Z
2,242,204
<p>What do you mean by "get around"? <a href="http://docs.python.org/library/xmlrpclib.html?highlight=xmlrpclib#module-xmlrpclib" rel="nofollow">xmlrpclib</a> is <em>the</em> normal way to write an XML-RPC client in python. Just look at <a href="http://svn.python.org/view/python/trunk/Lib/xmlrpclib.py?revision=74543&...
1
2010-02-11T04:08:34Z
[ "python", "xml-rpc" ]
How to send a xml-rpc request in python?
2,242,174
<p>I was just wondering, how would I be able to send a <code>xml-rpc</code> request in python? I know you can use <code>xmlrpclib</code>, but how do I send out a request in <code>xml</code> to access a function?</p> <p>I would like to see the <code>xml</code> response.</p> <p>So basically I would like to send the fol...
5
2010-02-11T03:59:09Z
37,847,779
<p>I have pared down the source code in <a href="https://docs.python.org/3/library/xmlrpc.client.html" rel="nofollow">xmlrpc.client</a> to a minimum required to send a xml rpc request (as I was interested in trying to port the functionality). It returns the response XML.</p> <p>Server:</p> <pre><code>from xmlrpc.serv...
1
2016-06-16T00:08:20Z
[ "python", "xml-rpc" ]
Python Shell, Logging Commands for Easy Re-Execution
2,242,288
<p>Say I do something like this in a python shell for my Django app:</p> <pre><code>&gt;&gt;&gt;from myapp.models import User &gt;&gt;&gt;user = User.objects.get(pk=5) &gt;&gt;&gt;groups = user.groups.all() </code></pre> <p>What I'd like to do is stash these 3 commands somehow without leaving the shell. The goal bein...
2
2010-02-11T04:34:35Z
2,242,311
<p>The Django shell will use <a href="http://ipython.scipy.org/" rel="nofollow">IPython</a> if available, which supports a persistent history.</p> <p>Also, writing throwaway scripts <a href="http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/" rel="nofollow">is not difficult</a>.</p>
2
2010-02-11T04:39:55Z
[ "python", "django", "shell" ]
Python Shell, Logging Commands for Easy Re-Execution
2,242,288
<p>Say I do something like this in a python shell for my Django app:</p> <pre><code>&gt;&gt;&gt;from myapp.models import User &gt;&gt;&gt;user = User.objects.get(pk=5) &gt;&gt;&gt;groups = user.groups.all() </code></pre> <p>What I'd like to do is stash these 3 commands somehow without leaving the shell. The goal bein...
2
2010-02-11T04:34:35Z
2,242,425
<p>So thanks to Ignacio, with IPython installed:</p> <pre><code>&gt;&gt;&gt;from myapp.models import User &gt;&gt;&gt;user = User.objects.get(pk=5) &gt;&gt;&gt;groups = user.groups.all() &gt;&gt;&gt;#Ipython Tricks Follow &gt;&gt;&gt;hist #shows you lines in your history &gt;&gt;&gt;edit 1:3 # Edit n:m lines above in ...
1
2010-02-11T05:12:06Z
[ "python", "django", "shell" ]
Python Shell, Logging Commands for Easy Re-Execution
2,242,288
<p>Say I do something like this in a python shell for my Django app:</p> <pre><code>&gt;&gt;&gt;from myapp.models import User &gt;&gt;&gt;user = User.objects.get(pk=5) &gt;&gt;&gt;groups = user.groups.all() </code></pre> <p>What I'd like to do is stash these 3 commands somehow without leaving the shell. The goal bein...
2
2010-02-11T04:34:35Z
2,257,739
<p>Koobz, since you've just become a recent convert to ipython, there's a cool <a href="http://proteus-tech.com/blog/code-garden/bpython-django/" rel="nofollow">hack</a> I use for automatically importing all my application models in interactive mode:</p> <pre><code>#!/bin/env python # based on http://proteus-tech.com/...
1
2010-02-13T13:48:28Z
[ "python", "django", "shell" ]
more pythonic way of finding element in list that maximizes a function
2,242,489
<p>OK, I have this simple function that finds the element of the list that maximizes the value of another positive function.</p> <pre><code>def get_max(f, s): # f is a function and s is an iterable best = None best_value = -1 for element in s: this_value = f(element) if this_value &gt...
3
2010-02-11T05:33:31Z
2,242,496
<pre><code>def get_max(f, s): return max(s, key=f) </code></pre>
14
2010-02-11T05:35:52Z
[ "list", "coding-style", "python", "maximize" ]
methods of metaclasses on class instances
2,242,715
<p>I was wondering what happens to methods declared on a metaclass. I expected that if you declare a method on a metaclass, it will end up being a classmethod, however, the behavior is different. Example</p> <pre><code>&gt;&gt;&gt; class A(object): ... @classmethod ... def foo(cls): ... print "foo" ......
9
2010-02-11T06:37:39Z
2,242,740
<p>The rule is like this: when searching for an attribute on an object, the object's class and its parent classes are considered as well. An object's class's metaclass, however, is <em>not</em> considered. When you access an attribute of a class, the class's class is the metaclass, so it <em>is</em> considered. The fal...
7
2010-02-11T06:43:02Z
[ "python", "metaclass" ]
methods of metaclasses on class instances
2,242,715
<p>I was wondering what happens to methods declared on a metaclass. I expected that if you declare a method on a metaclass, it will end up being a classmethod, however, the behavior is different. Example</p> <pre><code>&gt;&gt;&gt; class A(object): ... @classmethod ... def foo(cls): ... print "foo" ......
9
2010-02-11T06:37:39Z
2,245,993
<p>You raise a good point. </p> <p>Here is a <a href="http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html" rel="nofollow">good reference</a>to get a better understanding of the relations between objects, classes and metaclasses:</p> <p>I also find <a href="http://users.rcn.com/python/...
12
2010-02-11T16:43:33Z
[ "python", "metaclass" ]
methods of metaclasses on class instances
2,242,715
<p>I was wondering what happens to methods declared on a metaclass. I expected that if you declare a method on a metaclass, it will end up being a classmethod, however, the behavior is different. Example</p> <pre><code>&gt;&gt;&gt; class A(object): ... @classmethod ... def foo(cls): ... print "foo" ......
9
2010-02-11T06:37:39Z
2,260,146
<p>The way I understand it is that Meta is a class, and A is an instance of it. Thus, when you call A.foo(), it checks through the object and its class. So, when trying A.foo, it first looks through the methods A itself holds, and then the methods of its class, Meta. As A holds no method foo itself, it uses the one of ...
0
2010-02-14T04:34:29Z
[ "python", "metaclass" ]
Django user impersonation by admin
2,242,909
<p>I have a Django app. When logged in as an admin user, I want to be able to pass a secret parameter in the URL and have the whole site behave as if I were another user.</p> <p>Let's say I have the URL <code>/my-profile/</code> which shows the currently logged in user's profile. I want to be able to do something like...
17
2010-02-11T07:35:59Z
2,242,953
<p>Set up so you have two different host names to the same server. If you are doing it locally, you can connect with 127.0.0.1, or localhost, for example. Your browser will see this as three different sites, and you can be logged in with different users. The same works for your site.</p> <p>So in addition to www.mysi...
-1
2010-02-11T07:45:00Z
[ "python", "django", "impersonation" ]
Django user impersonation by admin
2,242,909
<p>I have a Django app. When logged in as an admin user, I want to be able to pass a secret parameter in the URL and have the whole site behave as if I were another user.</p> <p>Let's say I have the URL <code>/my-profile/</code> which shows the currently logged in user's profile. I want to be able to do something like...
17
2010-02-11T07:35:59Z
2,243,865
<p>I solved this with a simple middleware. It also handles redirects (that is, the GET parameter is preserved during a redirect). Here it is:</p> <pre><code>class ImpersonateMiddleware(object): def process_request(self, request): if request.user.is_superuser and "__impersonate" in request.GET: ...
9
2010-02-11T10:42:12Z
[ "python", "django", "impersonation" ]
Django user impersonation by admin
2,242,909
<p>I have a Django app. When logged in as an admin user, I want to be able to pass a secret parameter in the URL and have the whole site behave as if I were another user.</p> <p>Let's say I have the URL <code>/my-profile/</code> which shows the currently logged in user's profile. I want to be able to do something like...
17
2010-02-11T07:35:59Z
2,249,857
<p>i don't see how that is a security hole any more than using su - someuser as root on a a unix machine. root or an django-admin with root/admin access to the database can fake anything if he/she wants to. the risk is only in the django-admin account being cracked at which point the cracker could hide tracks by becom...
2
2010-02-12T05:29:54Z
[ "python", "django", "impersonation" ]
Django user impersonation by admin
2,242,909
<p>I have a Django app. When logged in as an admin user, I want to be able to pass a secret parameter in the URL and have the whole site behave as if I were another user.</p> <p>Let's say I have the URL <code>/my-profile/</code> which shows the currently logged in user's profile. I want to be able to do something like...
17
2010-02-11T07:35:59Z
4,672,864
<p>I don't have enough reputation to edit or reply yet (I think), but I found that although ionaut's solution worked in simple cases, a more robust solution for me was to use a session variable. That way, even AJAX requests are served correctly without modifying the request URL to include a GET impersonation parameter....
30
2011-01-12T19:02:19Z
[ "python", "django", "impersonation" ]