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
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
21,708,126
<p>Make sure you are not naming your source file random.py, and that there is not a file in your working directory called random.pyc.. either could cause your program to try and import your local random.py file instead of pythons random module. </p>
0
2014-02-11T16:59:01Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
23,711,022
<p>You can go for this:</p> <pre><code>&gt;&gt;&gt; A = ['r','a','n','d','o','m'] &gt;&gt;&gt; B = [1,2,3,4,5,6] &gt;&gt;&gt; import random &gt;&gt;&gt; random.sample(A+B, len(A+B)) [3, 'r', 4, 'n', 6, 5, 'm', 2, 1, 'a', 'o', 'd'] </code></pre> <p>if you want to go back to two lists, you then split this long list int...
0
2014-05-17T11:53:48Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
24,464,173
<p>It works fine. I am trying it here with functions as list objects:</p> <pre><code> from random import shuffle def foo1(): print "foo1", def foo2(): print "foo2", def foo3(): print "foo3", A=[foo1,foo2,foo3] for x in A: x() print "\r" shuffle(A) ...
-3
2014-06-28T06:11:44Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
27,064,019
<p>it took me some time to get that too, but the documentation for shuffle is very clear:</p> <blockquote> <p>shuffle list x <strong>in place</strong>; return None.</p> </blockquote> <p>so you shouldn't <code>print random.shuffle(b)</code> but <code>random.shuffle(b)</code> and than <code>print b</code>.</p>
11
2014-11-21T14:49:30Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
28,777,473
<p>If you happen to be using numpy already (very popular for scientific and financial applications) you can save yourself an import.</p> <pre><code>import numpy as np np.random.shuffle(b) print(b) </code></pre> <p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.shuffle.html">http://docs....
6
2015-02-28T02:35:34Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
36,850,264
<p><strong>The shuffling process is "with replacement"</strong>, so the occurrence of each item may change! At least when when items in your list is also list. </p> <p>E.g.,</p> <pre><code>ml = [[0], [1]] * 10 </code></pre> <p>After, </p> <pre><code>random.shuffle(ml) </code></pre> <p>The number of [0] may be 9 or...
0
2016-04-25T20:18:27Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
37,062,184
<pre><code>from random import random my_list = range(10) shuffled_list = sorted(my_list, key=lambda x: random()) </code></pre> <p>This alternative may be useful for some applications where you want to swap the ordering function.</p>
0
2016-05-06T00:04:46Z
[ "python", "list", "random", "shuffle" ]
Shuffling a list of objects in python
976,882
<p>I have a list of objects in Python and I want to shuffle them. I thought I could use the <code>random.shuffle</code> method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this?</p> <pre><code>import random class a: foo = "bar" a1 = a() a2 = a(...
290
2009-06-10T16:56:59Z
37,062,527
<p>One can define a function called <code>shuffled</code> (in the same sense of <code>sort</code> vs <code>sorted</code>)</p> <pre><code>def shuffled(x): import random y = x[:] random.shuffle(y) return y x = shuffled([1, 2, 3, 4]) print x </code></pre>
1
2016-05-06T00:48:25Z
[ "python", "list", "random", "shuffle" ]
static stylesheets gets reloaded with each post request
976,898
<p>Every time there is a post from page the entire bunch of css gets reloaded. Is it possible to tell them not to come in again and again. There is a series of GET that get fired. Can we optimize in some way or is it normal behavior?</p> <p>The environment is google apps in python.</p>
2
2009-06-10T17:01:08Z
976,986
<p>Check out Using <a href="http://code.google.com/appengine/docs/python/gettingstarted/staticfiles.html" rel="nofollow">Static Files</a> and <a href="http://code.google.com/appengine/docs/python/config/appconfig.html#Static%5FFile%5FHandlers" rel="nofollow">Handlers for Static Files</a>. Since the latter link refer to...
2
2009-06-10T17:18:09Z
[ "python", "css", "google-app-engine", "post", "get" ]
static stylesheets gets reloaded with each post request
976,898
<p>Every time there is a post from page the entire bunch of css gets reloaded. Is it possible to tell them not to come in again and again. There is a series of GET that get fired. Can we optimize in some way or is it normal behavior?</p> <p>The environment is google apps in python.</p>
2
2009-06-10T17:01:08Z
977,488
<p>You just have to put all your css in a "static directory" and specify an expiration into the app.yaml file.</p> <p>Here is the app.yaml of one of my project:</p> <pre><code>application: &lt;my_app_id&gt; version: 1 runtime: python api_version: 1 skip_files: | ^(.*/)?( (app\.yaml)| (index\.yaml)| (\...
0
2009-06-10T18:50:39Z
[ "python", "css", "google-app-engine", "post", "get" ]
static stylesheets gets reloaded with each post request
976,898
<p>Every time there is a post from page the entire bunch of css gets reloaded. Is it possible to tell them not to come in again and again. There is a series of GET that get fired. Can we optimize in some way or is it normal behavior?</p> <p>The environment is google apps in python.</p>
2
2009-06-10T17:01:08Z
978,724
<p>If your CSS comes from a static file, then as Steve mentioned you want to put it in a static directory and specify it in your app.yaml file. For example, if your CSS files are in a directory called stylesheets:</p> <pre><code>handlers: - url: /stylesheets static_dir: stylesheets expiration: "180d" </code></pre>...
1
2009-06-11T00:07:55Z
[ "python", "css", "google-app-engine", "post", "get" ]
During a subprocess call, catch critical windows errors in Python instead of letting the OS handle it by showing nasty error pop-ups
977,275
<p>"The application failed to initialize properly ... Click on OK,to terminate the application." is the message from the error pop-up. What is the way to catch these errors in Python code?</p>
-1
2009-06-10T18:08:49Z
985,166
<p>Sounds familiar? This will block your <code>subprocess.Popen</code> forever.. until you click the 'OK' button. Add the following code to your module initialization to workaround this issue:</p> <pre><code>import win32api, win32con win32api.SetErrorMode(win32con.SEM_FAILCRITICALERRORS | win32co...
3
2009-06-12T05:35:13Z
[ "python", "windows", "subprocess" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,476
<pre><code>1) Loop through every line in the log a)If line matches appropriate Regex: Display/Store Next Line as the item title. Look for the next line containing "Result XXXX." and parse out that result for including in the result set. </code></pre> <p>EDIT: added a bit more now that I...
5
2009-06-10T18:48:39Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,530
<p>Parsing is not done using regex. If you have a reasonably well structured text (which it looks as you do), you can use faster testing (e.g. line.startswith() or such). A list of dictionaries seems to be a suitable data type for such key-value pairs. Not sure what else to tell you. This seems pretty trivial.</p> <p>...
0
2009-06-10T18:58:22Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,531
<p>Maybe something like (<code>log.log</code> is your file):</p> <pre><code>def doOutput(s): # process or store data print s s='' for line in open('log.log').readlines(): if line.startswith('====='): if len(s): doOutput(s) s='' else: s+=line if len(s): doOutput(...
1
2009-06-10T18:58:38Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,541
<p>I would recommend starting a loop that looks for the "===" in the line. Let that key you off to the Title which is the next line. Set a flag that looks for the results, and if you don't find the results before you hit the next "===", say no results. Else, log the results with the title. Reset your flag and repeat...
1
2009-06-10T19:01:31Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,550
<p>You could try something like this (in c-like pseudocode, since i don't know python):</p> <pre><code>string line=getline(); regex boundary="^==== [^=]+ ====$"; regex info="^Info: (.*)$"; regex test_data="Test ([^.]*)\. Result ([^.]*)\. Time ([^.]*)\.$"; regex stats="Stats: (.*)$"; while(!eof()) { // sanity check ...
0
2009-06-10T19:03:26Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,790
<p>Here's some not so good looking perl code that does the job. Perhaps you can find it useful in some way. Quick hack, there are other ways of doing it (I feel that this code needs defending).</p> <pre><code>#!/usr/bin/perl -w # # $Id$ # use strict; use warnings; my @ITEMS; my $item; my $state = 0; open(FD, "&lt; ...
-1
2009-06-10T19:50:11Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
977,892
<p>I know you didn't ask for real code but this is too great an opportunity for a generator-based text muncher to pass up:</p> <pre><code># data is a multiline string containing your log, but this # function could be easily rewritten to accept a file handle. def get_stats(data): title = "" grab_title = False ...
5
2009-06-10T20:14:31Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
978,416
<p>Regular expression with group matching seems to do the job in python:</p> <pre><code>import re data = """===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the ite...
1
2009-06-10T22:13:10Z
[ "python", "parsing" ]
How would I go about parsing the following log?
977,458
<p>I need to parse a log in the following format:</p> <pre><code>===== Item 5483/14800 ===== This is the item title Info: some note ===== Item 5483/14800 (Update 1/3) ===== This is the item title Info: some other note ===== Item 5483/14800 (Update 2/3) ===== This is the item title Info: some more notes ===== Item 548...
2
2009-06-10T18:44:15Z
984,569
<p>This is sort of a continuation of maciejka's solution (see the comments there). If the data is in the file daniels.log, then we could go through it item by item with itertools.groupby, and apply a multi-line regexp to each item. This should scale fine.</p> <pre><code>import itertools, re p = re.compile("Result ([^...
1
2009-06-12T01:04:02Z
[ "python", "parsing" ]
Comparing 2 .txt files using difflib in Python
977,491
<p>I am trying to compare 2 text files and output the first string in the comparison file that does not match but am having difficulty since I am very new to python. Can anybody please give me a sample way to use this module. </p> <p>When I try something like:</p> <pre><code>result = difflib.SequenceMatcher(None, t...
15
2009-06-10T18:51:20Z
977,552
<p>Are you sure both files exist ?</p> <p>Just tested it and i get a perfect result.</p> <p>To get the results i use something like:</p> <pre><code>import difflib diff=difflib.ndiff(open(testFile).readlines(), open(comparisonFile).readlines()) try: while 1: print diff.next(), except: pass </code></...
5
2009-06-10T19:03:47Z
[ "python", "difflib" ]
Comparing 2 .txt files using difflib in Python
977,491
<p>I am trying to compare 2 text files and output the first string in the comparison file that does not match but am having difficulty since I am very new to python. Can anybody please give me a sample way to use this module. </p> <p>When I try something like:</p> <pre><code>result = difflib.SequenceMatcher(None, t...
15
2009-06-10T18:51:20Z
977,563
<p>For starters, you need to pass strings to difflib.SequenceMatcher, not files:</p> <pre><code># Like so difflib.SequenceMatcher(None, str1, str2) # Or just read the files in difflib.SequenceMatcher(None, file1.read(), file2.read()) </code></pre> <p>That'll fix your error anyway. To get the first non-matching strin...
23
2009-06-10T19:06:00Z
[ "python", "difflib" ]
Comparing 2 .txt files using difflib in Python
977,491
<p>I am trying to compare 2 text files and output the first string in the comparison file that does not match but am having difficulty since I am very new to python. Can anybody please give me a sample way to use this module. </p> <p>When I try something like:</p> <pre><code>result = difflib.SequenceMatcher(None, t...
15
2009-06-10T18:51:20Z
977,737
<p>It sounds like you may not need difflib at all. If you're comparing line by line, try something like this:</p> <pre><code>test_lines = open("test.txt").readlines() correct_lines = open("correct.txt").readlines() for test, correct in zip(test_lines, correct_lines): if test != correct: print "Oh no! Expe...
3
2009-06-10T19:39:59Z
[ "python", "difflib" ]
Comparing 2 .txt files using difflib in Python
977,491
<p>I am trying to compare 2 text files and output the first string in the comparison file that does not match but am having difficulty since I am very new to python. Can anybody please give me a sample way to use this module. </p> <p>When I try something like:</p> <pre><code>result = difflib.SequenceMatcher(None, t...
15
2009-06-10T18:51:20Z
21,790,513
<p>Here is a quick example of comparing the contents of two files using Python difflib...</p> <pre><code>import difflib file1 = "myFile1.txt" file2 = "myFile2.txt" diff = difflib.ndiff(open(file1).readlines(),open(file2).readlines()) print ''.join(diff), </code></pre>
5
2014-02-14T22:18:55Z
[ "python", "difflib" ]
Redirecting FORTRAN (called via F2PY) output in Python
977,840
<p>I'm trying to figure out how to redirect output from some FORTRAN code for which I've generated a Python interface by using F2PY. I've tried:</p> <pre><code>from fortran_code import fortran_function stdout_holder = sys.stdout stderr_holder = sys.stderr sys.stdout = file("/dev/null","w") fortran_function() sys.stdou...
9
2009-06-10T19:59:15Z
978,264
<p>The stdin and stdout fds are being inherited by the C shared library.</p> <pre><code>from fortran_code import fortran_function import os print "will run fortran function!" # open 2 fds null_fds = [os.open(os.devnull, os.O_RDWR) for x in xrange(2)] # save the current file descriptors to a tuple save = os.dup(1), o...
14
2009-06-10T21:33:26Z
[ "python", "unix", "fortran", "stdout" ]
Redirecting FORTRAN (called via F2PY) output in Python
977,840
<p>I'm trying to figure out how to redirect output from some FORTRAN code for which I've generated a Python interface by using F2PY. I've tried:</p> <pre><code>from fortran_code import fortran_function stdout_holder = sys.stdout stderr_holder = sys.stderr sys.stdout = file("/dev/null","w") fortran_function() sys.stdou...
9
2009-06-10T19:59:15Z
17,753,573
<p>Here's a <a href="http://docs.python.org/2/library/contextlib.html" rel="nofollow">context manager</a> that I recently wrote and found useful, because I was having a similar problem with <a href="http://docs.python.org/2/distutils/apiref.html#distutils.ccompiler.CCompiler.has_function" rel="nofollow"><code>distutils...
2
2013-07-19T18:59:26Z
[ "python", "unix", "fortran", "stdout" ]
Is there any way to add two bytes with overflow in python?
978,149
<p>I am using pySerial to read in data from an attached device. I want to calculate the checksum of each received packet. The packet is read in as a char array, with the actual checksum being the very last byte, at the end of the packet. To calculate the checksum, I would normally sum over the packet payload, and then ...
3
2009-06-10T21:03:09Z
978,154
<p>Sure, just take the modulus of your result to fit it back in the size you want. You can do the modulus at the end or at every step. For example:</p> <pre><code>&gt;&gt;&gt; payload = [100, 101, 102, 103, 104] # arbitrary sequence of bytes &gt;&gt;&gt; sum(payload) % 256 # modulo 256 to make the answer fit in a sing...
6
2009-06-10T21:04:45Z
[ "python", "overflow", "checksum" ]
Is there any way to add two bytes with overflow in python?
978,149
<p>I am using pySerial to read in data from an attached device. I want to calculate the checksum of each received packet. The packet is read in as a char array, with the actual checksum being the very last byte, at the end of the packet. To calculate the checksum, I would normally sum over the packet payload, and then ...
3
2009-06-10T21:03:09Z
978,261
<p>to improve upon the earlier example, just bitwise-and it with 0xFF. not sure if python does the optimization by default or not.</p> <pre><code>sum(bytes) &amp; 0xFF </code></pre>
2
2009-06-10T21:32:32Z
[ "python", "overflow", "checksum" ]
Is there any way to add two bytes with overflow in python?
978,149
<p>I am using pySerial to read in data from an attached device. I want to calculate the checksum of each received packet. The packet is read in as a char array, with the actual checksum being the very last byte, at the end of the packet. To calculate the checksum, I would normally sum over the packet payload, and then ...
3
2009-06-10T21:03:09Z
32,319,852
<p>Summing the bytes and then taking the modulus, as in <code>sum(bytes) % 256</code> (or <code>sum(bytes) &amp; 0xFF</code>), is (in many programming languages) vulnerable to integer overflow, since there is a finite maximum value that integer types can represent.</p> <p>But, since we are talking about Python, this i...
0
2015-08-31T20:56:57Z
[ "python", "overflow", "checksum" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,590
<p>Consuming the webservices is more efficient because there are a lot more things you can do to scale your webservices and webserver (via caching, etc.). By consuming the middle layer, you also have the options to change the returned data format (e.g. you can decide to use JSON rather than XML). Scaling database is ...
3
2009-06-10T23:13:52Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,593
<p>First off -- measure. Don't just assume that one is better or worse than the other.</p> <p>Second, if you really don't want to measure, I'd guess the database is a bit faster (assuming the database is relatively local compared to the web service). Network latency usually is more than parse time unless we're talki...
6
2009-06-10T23:14:27Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,603
<p>There is not enough information to be able to say for sure in the general case. Why don't you do some tests and find out? Since it sounds like you are using python you will probably want to use the timeit module.</p> <p>Some things that could effect the result:</p> <ul> <li>Performance of the web service you are u...
1
2009-06-10T23:18:38Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,607
<p>It depends - who is calling the web service? Is the web service called every time the user hits the page? If that's the case I'd recommend introducing a caching layer of some sort - many web service API's throttle the amount of hits you can make per hour.</p> <p>Whether you choose to parse the cached XML on the fly...
1
2009-06-10T23:19:51Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,717
<p>It depends from case to case, you'll have to measure (or at least make an educated guess).</p> <p>You'll have to consider several things.</p> <p>Web service</p> <ul> <li>it might hit database itself</li> <li>it can be cached</li> <li>it will introduce network latency and might be unreliable</li> <li>or it could b...
0
2009-06-11T00:06:35Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,773
<p>As a few people have said, it depends, and you should test it.</p> <p>Often external services are slow, and caching them locally (in a database in memory, e.g., with memcached) is faster. But perhaps not.</p> <p>Fortunately, it's cheap and easy to test.</p>
0
2009-06-11T00:29:58Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,857
<p>Everyone is being very polite in answering this question: "it depends"... "you should test"... and so forth.</p> <p>True, the question does not go into great detail about the application and network topographies involved, but if the question is even being asked, then it's likely a) the DB is "local" to the applicat...
4
2009-06-11T01:08:21Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
978,864
<p>Test definitely. As a rule of thumb, XML is good for communicating between apps, but once you have the data inside of your app, everything should go into a database table. This may not apply in all cases, but 95% of the time it has for me. Anytime I ever tried to store data any other way (ex. XML in a content man...
0
2009-06-11T01:12:47Z
[ "python", "mysql", "xml", "django", "parsing" ]
Is it more efficient to parse external XML or to hit the database?
978,581
<p>I was wondering when dealing with a web service API that returns XML, whether it's better (faster) to just call the external service each time and parse the XML (using ElementTree) for display on your site or to save the records into the database (after parsing it once or however many times you need to each day) and...
1
2009-06-10T23:09:40Z
979,028
<p>It sounds like you essentially want to <em>cache</em> results, and are wondering if it's worth it. But if so, I would NOT use a database (I assume you are thinking of a relational DB): RDBMSs are not good for caching; even though many use them. You don't need persistence nor ACID. If choice was between Oracle/MySQL ...
0
2009-06-11T02:26:38Z
[ "python", "mysql", "xml", "django", "parsing" ]
Vim, Python, and Django autocompletion (pysmell?)
978,643
<p>Does anyone know how to set up auto completion to work nicely with python, django, and vim?</p> <p>I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to ...
46
2009-06-10T23:35:16Z
978,766
<p>I've had good luck with exuberant-ctags for this.</p> <p>I use this macro in my vimrc:</p> <pre> execute 'map :!/usr/bin/exuberant-ctags -f '.&tags.' --recurse '.$_P4ROOT.' ' </pre> <p>You'll want to modify that slightly, so that it includes your python /site-packages/django/ directory as well as your own code.<...
3
2009-06-11T00:26:40Z
[ "python", "django", "vim" ]
Vim, Python, and Django autocompletion (pysmell?)
978,643
<p>Does anyone know how to set up auto completion to work nicely with python, django, and vim?</p> <p>I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to ...
46
2009-06-10T23:35:16Z
978,778
<p>First off, thank you for asking this question, as it forced me to figure this out myself and it's great!</p> <p>Here is the page I used as a reference: <a href="http://github.com/orestis/pysmell/tree/v0.6">PySmell v0.6 released : orestis.gr</a></p> <ol> <li>Install PySmell using the <code>setup.py install</code> c...
30
2009-06-11T00:32:43Z
[ "python", "django", "vim" ]
Vim, Python, and Django autocompletion (pysmell?)
978,643
<p>Does anyone know how to set up auto completion to work nicely with python, django, and vim?</p> <p>I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to ...
46
2009-06-10T23:35:16Z
979,347
<p><img src="http://blog.dispatched.ch/wp-content/uploads/2009/05/vim-as-python-ide.png" alt="alt text" /></p> <p>You can set up VIM with buffers, buffer display, auto complete, even Py Doc display.</p> <p><a href="http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/">Here you go</a></p>
12
2009-06-11T04:43:30Z
[ "python", "django", "vim" ]
Vim, Python, and Django autocompletion (pysmell?)
978,643
<p>Does anyone know how to set up auto completion to work nicely with python, django, and vim?</p> <p>I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to ...
46
2009-06-10T23:35:16Z
10,666,285
<p>Today, you not need special extentions for django autocomplete in vim. Make sure that you have vim with python support. To check it, type in xterm:</p> <p>vim --version|grep python</p> <h1>output:</h1> <p><strong>+python</strong> -python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs ... ...</p> <p>To m...
2
2012-05-19T15:01:29Z
[ "python", "django", "vim" ]
Vim, Python, and Django autocompletion (pysmell?)
978,643
<p>Does anyone know how to set up auto completion to work nicely with python, django, and vim?</p> <p>I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to ...
46
2009-06-10T23:35:16Z
12,885,498
<p>As I wrote in other places, I developed Jedi. I really think it is far better than all the existing solutions (even PyCharm).</p> <p><a href="https://github.com/davidhalter/jedi-vim">https://github.com/davidhalter/jedi-vim</a></p> <p>It is built upon pythoncomplete and much much more powerful!</p> <p>It works for...
12
2012-10-14T19:00:53Z
[ "python", "django", "vim" ]
How to expose client_address to all methods, using xmlrpclib?
978,784
<p>I create little SimpleXMLRPCServer for check ip of client.</p> <p>I try this:</p> <h1>Server</h1> <p>import xmlrpclib</p> <p>from SimpleXMLRPCServer import SimpleXMLRPCServer</p> <p>server = SimpleXMLRPCServer(("localhost", 8000))</p> <p>def MyIp(): return "Your ip is: %s" % server.socket.getpeername()</p>...
2
2009-06-11T00:34:23Z
978,868
<p>If you want for example to pass <code>client_address</code> as the first argument to every function, you could subclass SimpleXMLRPCRequestHandler (pass your subclass as the handler when you instantiate SimpleXMLRPCServer) and override <code>_dispatch</code> (to prepend <code>self.client_address</code> to the params...
3
2009-06-11T01:15:31Z
[ "python", "xmlrpclib" ]
Error message "MemoryError" in Python
978,841
<p>Here's my problem: I'm trying to parse a big text file (about 15,000&nbsp;KB) and write it to a MySQL database. I'm using Python 2.6, and the script parses about half the file and adds it to the database before freezing up. Sometimes it displays the text:</p> <blockquote> <p>MemoryError.</p> </blockquote> <p>Oth...
1
2009-06-11T00:59:50Z
978,917
<p>Generators are a good idea, but you seem to miss the biggest problem:</p> <p>(str for str in splitter.split(<strong>open(fn).read()</strong>) if str and str &lt;> 'A' and str &lt;> 'S')</p> <p>You're reading the whole file in at once even if you only need to work with bits at a time. You're code is too complicated...
7
2009-06-11T01:39:01Z
[ "python", "memory-management" ]
Error message "MemoryError" in Python
978,841
<p>Here's my problem: I'm trying to parse a big text file (about 15,000&nbsp;KB) and write it to a MySQL database. I'm using Python 2.6, and the script parses about half the file and adds it to the database before freezing up. Sometimes it displays the text:</p> <blockquote> <p>MemoryError.</p> </blockquote> <p>Oth...
1
2009-06-11T00:59:50Z
978,961
<p>Try to comment out <code>add(record)</code> to see if the problem is in your code or on the database side. All the records are added in one transaction (if supported) and maybe this leads to a problem if it get too many records. If commenting out <code>add(record)</code> helps, you could try to call <code>commit()</...
1
2009-06-11T02:00:43Z
[ "python", "memory-management" ]
Error message "MemoryError" in Python
978,841
<p>Here's my problem: I'm trying to parse a big text file (about 15,000&nbsp;KB) and write it to a MySQL database. I'm using Python 2.6, and the script parses about half the file and adds it to the database before freezing up. Sometimes it displays the text:</p> <blockquote> <p>MemoryError.</p> </blockquote> <p>Oth...
1
2009-06-11T00:59:50Z
978,981
<p>This isn't a Python memory issue, but perhaps it's worth thinking about. The previous answers make me think you'll sort that issue out quickly.</p> <p>I wonder about the rollback logs in MySQL. If a single transaction is too large, perhaps you can checkpoint chunks. Commit each chunk separately instead of trying...
1
2009-06-11T02:08:53Z
[ "python", "memory-management" ]
Error message "MemoryError" in Python
978,841
<p>Here's my problem: I'm trying to parse a big text file (about 15,000&nbsp;KB) and write it to a MySQL database. I'm using Python 2.6, and the script parses about half the file and adds it to the database before freezing up. Sometimes it displays the text:</p> <blockquote> <p>MemoryError.</p> </blockquote> <p>Oth...
1
2009-06-11T00:59:50Z
5,736,532
<p>I noticed that you use a lot of slit() calls. This is memory consuming, according to <a href="http://mail.python.org/pipermail/python-bugs-list/2006-January/031571.html" rel="nofollow">http://mail.python.org/pipermail/python-bugs-list/2006-January/031571.html</a> . You can start investigating this.</p>
2
2011-04-20T20:55:24Z
[ "python", "memory-management" ]
django import search path
979,371
<p>I'm configuring autocompletion for python and django in vim. One of the problems is that I need to set an environment variable DJANGO_SETTINGS_MODULE=myapp.settings. The django tutorial states that </p> <blockquote> <p>The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings...
2
2009-06-11T04:51:37Z
979,393
<p>Try appending the path to sys.path at runtime.</p> <pre><code>import sys sys.path.append('/path/to/myapp') </code></pre>
3
2009-06-11T05:01:47Z
[ "python", "django" ]
django import search path
979,371
<p>I'm configuring autocompletion for python and django in vim. One of the problems is that I need to set an environment variable DJANGO_SETTINGS_MODULE=myapp.settings. The django tutorial states that </p> <blockquote> <p>The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings...
2
2009-06-11T04:51:37Z
979,403
<p>Add this in your <code>.bashrc</code> or <code>.bash_profile</code></p> <p><code>export PATH=$PATH:/path/to/django/bin</code></p> <p><code>export PYTHONPATH=$PYTHONPATH:/path/to/myapp</code></p>
3
2009-06-11T05:06:16Z
[ "python", "django" ]
django import search path
979,371
<p>I'm configuring autocompletion for python and django in vim. One of the problems is that I need to set an environment variable DJANGO_SETTINGS_MODULE=myapp.settings. The django tutorial states that </p> <blockquote> <p>The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings...
2
2009-06-11T04:51:37Z
980,446
<p>Three choices.</p> <ol> <li><p>Set <code>PYTHONPATH</code> environment variable to include your application's directory. Be sure it has an <code>__init__.py</code> file.</p></li> <li><p>Create a <code>.pth</code> file in site-packages to point to your application's directory. </p></li> <li><p>Install your applica...
3
2009-06-11T10:46:06Z
[ "python", "django" ]
django import search path
979,371
<p>I'm configuring autocompletion for python and django in vim. One of the problems is that I need to set an environment variable DJANGO_SETTINGS_MODULE=myapp.settings. The django tutorial states that </p> <blockquote> <p>The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings...
2
2009-06-11T04:51:37Z
27,721,624
<p>import from path containing dir with module (with <code>__init__.py</code> file)</p> <p>example:</p> <pre><code>dm@batman:~/.local/15/lib/python2.7/site-packages $ pwd /home/d/dm/.local/15/lib/python2.7/site-packages dm@batman:~/.local/15/lib/python2.7/site-packages $ ls django Django-1.5.11-py2.7.egg-info dm@b...
0
2014-12-31T13:10:03Z
[ "python", "django" ]
Getting all items less than a month old
979,533
<p>Is there a way to get all objects with a date less than a month ago in django.</p> <p>Something like:</p> <pre><code>items = Item.objects.filter(less than a month old).order_by(...) </code></pre>
14
2009-06-11T05:46:51Z
979,538
<p>What is your definition of a "month"? 30 days? 31 days? Past that, this should do it:</p> <pre><code>from datetime import datetime, timedelta last_month = datetime.today() - timedelta(days=30) items = Item.objects.filter(my_date__gte=last_month).order_by(...) </code></pre> <p>Takes advantange of the <a href="http:...
25
2009-06-11T05:50:09Z
[ "python", "django", "django-views" ]
Getting all items less than a month old
979,533
<p>Is there a way to get all objects with a date less than a month ago in django.</p> <p>Something like:</p> <pre><code>items = Item.objects.filter(less than a month old).order_by(...) </code></pre>
14
2009-06-11T05:46:51Z
979,543
<pre><code>items = Item.objects.filter(created_date__gte=aMonthAgo) </code></pre> <p>Where aMonthAgo would be calculated by datetime and timedelta.</p>
4
2009-06-11T05:51:41Z
[ "python", "django", "django-views" ]
Adding SSL support to Python 2.6
979,551
<p>I tried using the <code>ssl</code> module in Python 2.6 but I was told that it wasn't available. After installing OpenSSL, I recompiled 2.6 but the problem persists.</p> <p>Any suggestions?</p>
2
2009-06-11T05:56:20Z
979,598
<p>Did you install the OpenSSL development libraries? I had to install <code>openssl-devel</code> on CentOS, for example. On Ubuntu, <code>sudo apt-get build-dep python2.5</code> did the trick (even for Python 2.6).</p>
4
2009-06-11T06:16:06Z
[ "python", "ssl", "openssl" ]
Adding SSL support to Python 2.6
979,551
<p>I tried using the <code>ssl</code> module in Python 2.6 but I was told that it wasn't available. After installing OpenSSL, I recompiled 2.6 but the problem persists.</p> <p>Any suggestions?</p>
2
2009-06-11T05:56:20Z
979,604
<p>Use <a href="http://pexpect.sourceforge.net/pexpect.html" rel="nofollow">pexpect</a> with the openssl binary.</p>
-4
2009-06-11T06:17:45Z
[ "python", "ssl", "openssl" ]
Adding SSL support to Python 2.6
979,551
<p>I tried using the <code>ssl</code> module in Python 2.6 but I was told that it wasn't available. After installing OpenSSL, I recompiled 2.6 but the problem persists.</p> <p>Any suggestions?</p>
2
2009-06-11T05:56:20Z
996,622
<p>Use the binaries provided by python.org or by your OS distributor. It's a lot easier than building it yourself, and all the features are usually compiled in.</p> <p>If you really need to build it yourself, you'll need to provide more information here about what build options you provided, what your environment is ...
-1
2009-06-15T15:03:43Z
[ "python", "ssl", "openssl" ]
Extracting IP from request in Python
979,599
<p>I have a Pythonic HTTP server that is supposed to determine client's IP. How do I do that in Python? Is there any way to get the request headers and extract it from there?</p> <p>PS: I'm using WebPy.</p>
3
2009-06-11T06:16:20Z
979,637
<p>web.env.get('REMOTE_ADDR')</p>
3
2009-06-11T06:30:25Z
[ "python", "http", "header", "request", "ip" ]
Extracting IP from request in Python
979,599
<p>I have a Pythonic HTTP server that is supposed to determine client's IP. How do I do that in Python? Is there any way to get the request headers and extract it from there?</p> <p>PS: I'm using WebPy.</p>
3
2009-06-11T06:16:20Z
980,339
<p>Use web.ctx:</p> <pre><code>class example: def GET(self): print web.ctx.ip </code></pre> <p>More info <a href="http://webpy.org/cookbook/ctx" rel="nofollow">here</a></p>
4
2009-06-11T10:15:13Z
[ "python", "http", "header", "request", "ip" ]
Format an array of tuples in a nice "table"
980,000
<p>Say I have an array of tuples which look like that:</p> <pre><code>[('url#id1', 'url#predicate1', 'value1'), ('url#id1', 'url#predicate2', 'value2'), ('url#id1', 'url#predicate3', 'value3'), ('url#id2', 'url#predicate1', 'value4'), ('url#id2', 'url#predicate2', 'value5')] </code></pre> <p>I would like be able to r...
0
2009-06-11T08:39:08Z
980,016
<p>Your dict of dict is probably on the right track. While you create that dict of dict, you could also maintain a list of ids and a list of predicates. That way, you can remember the ordering and build the table by looping through those lists.</p> <p>using the <code>zip</code> function on your initial array wil give ...
0
2009-06-11T08:45:50Z
[ "python", "django", "algorithm" ]
Format an array of tuples in a nice "table"
980,000
<p>Say I have an array of tuples which look like that:</p> <pre><code>[('url#id1', 'url#predicate1', 'value1'), ('url#id1', 'url#predicate2', 'value2'), ('url#id1', 'url#predicate3', 'value3'), ('url#id2', 'url#predicate1', 'value4'), ('url#id2', 'url#predicate2', 'value5')] </code></pre> <p>I would like be able to r...
0
2009-06-11T08:39:08Z
985,296
<p>Ok,</p> <p>At last I came up with that code:</p> <pre><code>columns = dict() columnsTitles = [] rows = dict() colIdxCounter = 1 # Start with 1 because the first col are ids rowIdxCounter = 1 # Start with 1 because the columns titles for i in dataset: if not rows.has_key(i[0]): rows[i[0]] = rowIdxCoun...
0
2009-06-12T06:42:18Z
[ "python", "django", "algorithm" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
980,098
<p>lxml isn't going to help you at all. It isn't concerned about front-end rendering at all.</p> <p>To accurately work out how something renders, you need to render it. For that you need to hook into a browser, spawn the page and run some JS on the page to find the DOM element and get its attributes.</p> <p>It's tota...
3
2009-06-11T09:08:12Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
980,157
<p>The problem is that current browsers don't render things quite the same. If you're looking for the standards compliant way of doing things, you could probably write something in Python to render the page, but that's going to be a hell of a lot of work.</p> <p>You could use the <a href="http://docs.wxwidgets.org/2.6...
0
2009-06-11T09:25:48Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
980,619
<p>I agree with <a href="http://stackoverflow.com/users/12870/oli">Oli</a>, rendering the page in question and inspecting DOM via JavaScript is the most practical way IMHO.</p> <p>You might find <a href="http://jquery.com" rel="nofollow">jQuery</a> very useful here:</p> <pre><code>$(document).ready(function() { v...
1
2009-06-11T11:37:58Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
980,671
<p>Yes, Javascript is the way to go:</p> <p>var allElements=document.getElementsByTagName("*"); will select all the elements in the page.</p> <p>Then you can loop through this a extract the information you need from each element. Good documentation about getting the dimensions and positions of an element <a href="htt...
1
2009-06-11T11:51:05Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
1,029,109
<p>You might consider looking at <a href="http://search.cpan.org/dist/Test-WWW-Selenium/lib/WWW/Selenium.pm" rel="nofollow">WWW::Selenium</a>. With it (and <a href="http://seleniumhq.org/projects/remote-control/" rel="nofollow">selenium rc</a>) you can puppet string IE, Firefox, or Safari from inside of Perl.</p>
0
2009-06-22T19:56:38Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
1,038,408
<p>I was not able to find any easy solution (ie. Java/Perl/Python :) to hook onto Webkit/Gecko to solve the above rendering problem. The best I could find was the <a href="http://lobobrowser.org/" rel="nofollow">Lobo rendering engine</a> written in Java which has a very clear API that does exactly what I want - access...
1
2009-06-24T13:38:28Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Finding rendered HTML element positions using WebKit (or Gecko)
980,058
<p>I would like to get the dimensions (coordinates) for all the HTML elements of a webpage as they are rendered by a browser, that is the positions they are rendered at. For example, <code>(top-left,top-right,bottom-left,bottom-right)</code></p> <p>Could not find this in lxml. So, is there any library in Python that d...
2
2009-06-11T08:57:28Z
3,897,453
<p>you have three main options:</p> <p>1) <a href="http://www.gnu.org/software/pythonwebkit" rel="nofollow">http://www.gnu.org/software/pythonwebkit</a> is webkit-based;</p> <p>2) python-comtypes for accessing MSHTML (windows only)</p> <p>3) hulahop (python-xpcom) which is xulrunner-based</p> <p>you should get the ...
1
2010-10-09T19:02:50Z
[ "python", "html", "perl", "rendering", "rendering-engine" ]
Difference between dir(…) and vars(…).keys() in Python?
980,249
<p>Is there a difference between <code>dir(…)</code> and <code>vars(…).keys()</code> in Python?</p> <p>(I hope there is a difference, because otherwise this would break the "one way to do it" principle... :)</p>
30
2009-06-11T09:46:51Z
980,276
<p>The documentation has this to say about <a href="http://docs.python.org/library/functions.html"><code>dir</code></a>:</p> <blockquote> <p>Without arguments, return the list of names in the current local scope. <em>With an argument, attempt to return a list of valid attributes for that object.</em></p> </blockquot...
18
2009-06-11T09:53:11Z
[ "python" ]
Difference between dir(…) and vars(…).keys() in Python?
980,249
<p>Is there a difference between <code>dir(…)</code> and <code>vars(…).keys()</code> in Python?</p> <p>(I hope there is a difference, because otherwise this would break the "one way to do it" principle... :)</p>
30
2009-06-11T09:46:51Z
981,624
<p>Python objects store their instance variables in a dictionary that belongs to the object. <code>vars(x)</code> returns this dictionary (as does <code>x.__dict__</code>). <code>dir(x)</code>, on the other hand, returns a dictionary of <code>x</code>'s "attributes, its class's attributes, and recursively the attribute...
45
2009-06-11T14:57:52Z
[ "python" ]
Difference between dir(…) and vars(…).keys() in Python?
980,249
<p>Is there a difference between <code>dir(…)</code> and <code>vars(…).keys()</code> in Python?</p> <p>(I hope there is a difference, because otherwise this would break the "one way to do it" principle... :)</p>
30
2009-06-11T09:46:51Z
39,463,891
<p>Apart from Answers given, I would like to add that, using vars() with instances built-in types will give error, as instances builtin types do not have <code>__dict__</code> attribute.</p> <p>eg. </p> <pre><code>In [96]: vars([]) --------------------------------------------------------------------------- TypeError...
0
2016-09-13T06:54:42Z
[ "python" ]
Python QuickBase API Help
980,932
<p>Hey, I'm having some trouble using the QuickBase API from Python. From the QuickBase guide, there are two methods of hitting the API: POST and GET. I can handle the GET calls, but some API methods require XML to be sent over POST. The link to the documentation is here: <a href="http://member.developer.intuit.com...
1
2009-06-11T13:02:07Z
981,468
<p>Got it. For some reason The data I was sending in my POST needed to have a .strip() placed after it.</p>
0
2009-06-11T14:33:47Z
[ "python", "xml", "api", "post", "quickbase" ]
Trying to create a Python Server
981,033
<p>I am looking at trying to create a python server, which allows me to run root commands on a Centos Server remotely, I would also like the server to be able to respond with the result's of the command.</p> <p>I have found another question on here which has a basic python server, however it throws a error, the code i...
0
2009-06-11T13:21:18Z
981,043
<p>you need to keep indentation at the same level for each nested statement throughout your code.</p>
2
2009-06-11T13:23:37Z
[ "python", "syntax-error" ]
Trying to create a Python Server
981,033
<p>I am looking at trying to create a python server, which allows me to run root commands on a Centos Server remotely, I would also like the server to be able to respond with the result's of the command.</p> <p>I have found another question on here which has a basic python server, however it throws a error, the code i...
0
2009-06-11T13:21:18Z
981,095
<p>On a different note: why not using <a href="http://www.twistedmatrix.com" rel="nofollow">TwistedMatrix</a>?</p>
2
2009-06-11T13:32:07Z
[ "python", "syntax-error" ]
Executing shell commands as given UID in Python
981,052
<p>I need a way to execute the os.system() module as different UID's. It would need to behave similar to the following BASH code (note these are not the exact commands I am executing):</p> <pre><code>su user1 ls ~ mv file1 su user2 ls ~ mv file1 </code></pre> <p>The target platform is GNU Linux Generic. </p> <p>Of c...
0
2009-06-11T13:24:49Z
981,319
<p>I think that's not trivial: you can do that with a shell because each command is launched into its own process, which has its own id. But with python, everything will have the uid of the python interpreted process (of course, assuming you don't launch subprocesses using the subprocess module and co). I don't know a ...
3
2009-06-11T14:08:00Z
[ "python", "linux", "permissions", "os.system" ]
Executing shell commands as given UID in Python
981,052
<p>I need a way to execute the os.system() module as different UID's. It would need to behave similar to the following BASH code (note these are not the exact commands I am executing):</p> <pre><code>su user1 ls ~ mv file1 su user2 ls ~ mv file1 </code></pre> <p>The target platform is GNU Linux Generic. </p> <p>Of c...
0
2009-06-11T13:24:49Z
981,347
<p>The function you're looking for is called <code>os.seteuid</code>. I'm afraid you probably won't escape executing the script as root, in any case, but I think you can use the <code>capabilities(7)</code> framework to 'fence in' the execution a little, so that it can change users--but not do any of the other things ...
1
2009-06-11T14:12:20Z
[ "python", "linux", "permissions", "os.system" ]
Executing shell commands as given UID in Python
981,052
<p>I need a way to execute the os.system() module as different UID's. It would need to behave similar to the following BASH code (note these are not the exact commands I am executing):</p> <pre><code>su user1 ls ~ mv file1 su user2 ls ~ mv file1 </code></pre> <p>The target platform is GNU Linux Generic. </p> <p>Of c...
0
2009-06-11T13:24:49Z
981,467
<p>maybe sudo can help you here, otherwise you must be root to execute os.setuid</p> <p>alternatively if you want to have fun you can use pexpect to do things something like this, you can improve over this</p> <pre><code>p = pexpect.spawn("su guest") p.logfile = sys.stdout p.expect('Password:') p.sendline("guest") <...
2
2009-06-11T14:33:39Z
[ "python", "linux", "permissions", "os.system" ]
Executing shell commands as given UID in Python
981,052
<p>I need a way to execute the os.system() module as different UID's. It would need to behave similar to the following BASH code (note these are not the exact commands I am executing):</p> <pre><code>su user1 ls ~ mv file1 su user2 ls ~ mv file1 </code></pre> <p>The target platform is GNU Linux Generic. </p> <p>Of c...
0
2009-06-11T13:24:49Z
982,185
<p>Somewhere along the line, some process or other is going to need an effective UID of 0 (root), because only such a process can set the effective UID to an arbitrary other UID.</p> <p>At the shell, the <code>su</code> command is a SUID root program; it is appropriately privileged (POSIX jargon) and can set the real ...
1
2009-06-11T16:33:53Z
[ "python", "linux", "permissions", "os.system" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
981,218
<p>The <code>'%2.2f'</code> operator will only do the same number of decimals regardless of how many significant digits the number has. You will have to identify this in the number and manually frig the format. You might be able to short-cut this by printing to a string with a large number of decimal places and strip...
1
2009-06-11T13:53:22Z
[ "python", "floating-point" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
981,224
<p>I don't think there is a pre-defined format for this - format in python are inherited from C, and I am pretty sure you don't have the desired format in C.</p> <p>Now, in python 3, you have the <strong>format</strong> special function, where you can do your own formatting. Removing the last zeros in python is very e...
5
2009-06-11T13:54:35Z
[ "python", "floating-point" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
981,233
<p>How about using the decimal module?</p> <p>From the <a href="http://docs.python.org/library/decimal.html">documentation</a>:</p> <p>"The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is the customary presentation for...
9
2009-06-11T13:55:55Z
[ "python", "floating-point" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
5,612,090
<pre><code>def float_remove_zeros(input_val): """ Remove the last zeros after the decimal point: * 34.020 -&gt; 34.02 * 34.000 -&gt; 34 * 0 -&gt; 0 * 0.0 -&gt; 0 """ stripped = input_val if input_val != 0: stripped = str(input_val).rstrip('0').rstrip('.') else: st...
-2
2011-04-10T13:37:35Z
[ "python", "floating-point" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
12,497,279
<p>I think the simplest way would be "round" function. in your case:</p> <pre><code>&gt;&gt;&gt; round(104.06250000,4) 104.0625 </code></pre>
4
2012-09-19T14:52:43Z
[ "python", "floating-point" ]
Python: printing floats / modifying the output
981,189
<p>I have a problem which prints out a float variable. What I should get is for example: 104.0625, 119.0, 72.0. I know how to control the number of decimals but how do I control the number of zeros specifically, i.e. when the float is 104.06250000 the program should print 104.0625 but when the float is 119.00000 or 72....
6
2009-06-11T13:48:26Z
25,035,464
<p>You can use the string method format if you're using Python 2.6 and above.</p> <pre><code>&gt;&gt;&gt; print "{0}".format(1.0) 1.0 &gt;&gt;&gt; print "{0}".format(1.01) 1.01 &gt;&gt;&gt; print "{0}".format(float(1)) 1.0 &gt;&gt;&gt; print "{0}".format(1.010000) 1.01 </code></pre>
0
2014-07-30T11:18:27Z
[ "python", "floating-point" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,216
<p>Python runs in a virtual machine, so no.</p> <p>BUT:</p> <p>You could write a compiler that translates Python code to machine language. Once you've done that, you can do it.</p>
1
2009-06-11T13:53:12Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,227
<p>Never say never but eh.. no</p> <p>You might be able to hack something together to run user-mode parts of drivers in python. But kernel-mode stuff can only be done in C or assembly.</p>
0
2009-06-11T13:54:52Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,251
<p>No they cannot. Windows drivers must be written in a language that can </p> <ol> <li>Interface with the C based API</li> <li>Compile down to machine code </li> </ol> <p>Then again, there's nothing stopping you from writing a compiler that translates python to machine code ;)</p>
0
2009-06-11T13:59:38Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,268
<p>I don't know the restrictions on drivers on windows (memory allocation schemes, dynamic load of libraries and all), but you may be able to embed a python interpreter in your driver, at which point you can do whatever you want. Not that I think it is a good idea :)</p>
1
2009-06-11T14:01:27Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,320
<p>The definitive answer is not without embedding an interpreter in your otherwise C/assembly driver. Unless someone has a framework available, then the answer is no. Once you have the interpreter and bindings in place then the rest of the logic could be done in Python.</p> <p>However, writing drivers is one of the th...
3
2009-06-11T14:08:06Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,423
<p>A good way to gain insight why this is practically impossible is by reading <a href="http://www.microsoft.com/whdc/driver/kernel/KMcode.mspx" rel="nofollow">Microsoft's advice</a> on the use of C++ in drivers. As a derivative of C, the use of C++ appears to be straightforward. In practice, not so. </p> <p>For insta...
3
2009-06-11T14:26:22Z
[ "python", "windows", "drivers" ]
Can Windows drivers be written in Python?
981,200
<p>Can Windows drivers be written in Python?</p>
10
2009-06-11T13:50:06Z
981,462
<p>Yes. You cannot create the "classic" kernel-mode drivers. However, starting with XP, Windows offers a <a href="http://www.microsoft.com/whdc/driver/wdf/UMDF.mspx">User-Mode Driver Framework</a>. They can't do everything, obviously - any driver used in booting the OS obviously has to be kernel-mode. But with UMDF, yo...
16
2009-06-11T14:32:37Z
[ "python", "windows", "drivers" ]
Stomp Broadcast with Rabbitmq and Python
981,291
<p>Im trying to move a system from using morbid to rabbitmq, but I cannot seem to get the same broadcast behaviour morbid supplied by default. By broadcast I mean that when a message is added to the queue, every consumer recieves it. With rabbit, when a message is added they are distributed round robin style to every l...
2
2009-06-11T14:03:17Z
981,503
<p>Apparently you can't do with directly with STOMP; there is a <a href="http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2008-September/001775.html" rel="nofollow">mailing list thread</a> that shows all the hoops you have to jump through to get broadcast working with stomp (it involves some lower-level AMPQ stuff)...
3
2009-06-11T14:40:35Z
[ "python", "broadcast", "rabbitmq", "stomp" ]
Stomp Broadcast with Rabbitmq and Python
981,291
<p>Im trying to move a system from using morbid to rabbitmq, but I cannot seem to get the same broadcast behaviour morbid supplied by default. By broadcast I mean that when a message is added to the queue, every consumer recieves it. With rabbit, when a message is added they are distributed round robin style to every l...
2
2009-06-11T14:03:17Z
986,949
<p>I finally figured out how to do it by creating an exchange for each "recieving group", im not sure how well rabbit will do with thousands of exchanges, so you might want to figure test this heavily before trying it in production</p> <p>In the sending code:</p> <pre><code>conn.send(str(i), exchange=exchange, destin...
3
2009-06-12T14:32:06Z
[ "python", "broadcast", "rabbitmq", "stomp" ]
Send output from a python server back to client
981,348
<p>I now have a small java script server working correctly, called by:</p> <pre><code>&lt;?php $handle = fsockopen("udp://78.129.148.16",12345); fwrite($handle,"vzctlrestart110"); fclose($handle); ?&gt; </code></pre> <p>On a remote server the following python server is running and executing the comand's</p> <p...
0
2009-06-11T14:12:24Z
981,387
<p>Yes, you can read the output of the command. For this I would recommend the Python <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module. Then you can just <code>s.write()</code> it back.</p> <p>Naturally this has some implications, you would probably have to let your PHP scr...
1
2009-06-11T14:20:42Z
[ "python", "sockets" ]
Send output from a python server back to client
981,348
<p>I now have a small java script server working correctly, called by:</p> <pre><code>&lt;?php $handle = fsockopen("udp://78.129.148.16",12345); fwrite($handle,"vzctlrestart110"); fclose($handle); ?&gt; </code></pre> <p>On a remote server the following python server is running and executing the comand's</p> <p...
0
2009-06-11T14:12:24Z
981,396
<p>Here's an example of how to get the output of a command:</p> <pre><code>&gt;&gt;&gt; import commands &gt;&gt;&gt; s = commands.getoutput("ls *") &gt;&gt;&gt; s 'client.py' </code></pre>
0
2009-06-11T14:22:04Z
[ "python", "sockets" ]
Get original path from django filefield
981,371
<p>My django app accepts two files (in this case a jad and jar combo). Is there a way I can preserve the folders they came from?</p> <p>I need this so I can check later that they came from the same path.</p> <p>(And later on accept a whole load of files and be able to work out which came from the same folder).</p>
1
2009-06-11T14:17:02Z
981,666
<p>I think that is not possible, most browsers at least firefox3.0 do not allow fullpath to be seen, so even from JavaScript side you can not get full path If you could get full path you can send it to server, but I think you will have to be satisfied with file name</p>
1
2009-06-11T15:03:11Z
[ "python", "django", "file", "forms", "field" ]
Using a Django custom model method property in order_by()
981,375
<p>I'm currently learning Django and some of my models have custom methods to get values formatted in a specific way. Is it possible to use the value of one of these custom methods that I've defined as a property in a model with order_by()?</p> <p>Here is an example that demonstrates how the property is implemented.</...
34
2009-06-11T14:18:10Z
981,802
<p>No, you can't do that. <code>order_by</code> is applied at the database level, but the database can't know anything about your custom Python methods.</p> <p>You can either use the separate fields to order:</p> <pre><code>Author.objects.order_by('first_name', 'last_name') </code></pre> <p>or do the ordering in Pyt...
54
2009-06-11T15:27:12Z
[ "python", "django", "django-models" ]
Run a Python project in Eclipse as root
981,411
<p>I use Eclipse as my IDE, and when I run my application I wish the application itself to run as root. My program currently checks if it is root, and if not it restarts itself with <code>gksudo</code>. The output, however, isn't written to the console. I can't use <code>sudo</code>, since it doesn't give me a graphica...
8
2009-06-11T14:24:30Z
981,839
<p>Restrictive execution dominates. I believe that Eclipse does permit console interaction, but a quick and dirty, brute force approach would be to use suid if you're not concerned about security. Or throw up your own graphical prompt in Tkinter/other and use a bit of <a href="http://sourceforge.net/projects/pexpect/" ...
2
2009-06-11T15:32:24Z
[ "python", "eclipse", "root", "sudo", "gksudo" ]
Run a Python project in Eclipse as root
981,411
<p>I use Eclipse as my IDE, and when I run my application I wish the application itself to run as root. My program currently checks if it is root, and if not it restarts itself with <code>gksudo</code>. The output, however, isn't written to the console. I can't use <code>sudo</code>, since it doesn't give me a graphica...
8
2009-06-11T14:24:30Z
982,463
<p>It may not be an ideal solution, but the rare times that I need this same functionality I end up just running Eclipse as root.</p>
3
2009-06-11T17:23:51Z
[ "python", "eclipse", "root", "sudo", "gksudo" ]