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
Speeding up the python "import" loader
2,010,255
<p>I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and generating 4 <code>stat()</code> calls - ["foo", "foo.py", "foo.pyc", "foo.so"] - for each check). For a complicated project ...
18
2010-01-06T00:47:29Z
2,010,354
<p>zipping up as many <code>pyc</code> files as feasible (with proper directory structure for packages), and putting that zipfile as the very first entry in sys.path (on the best available local disk, ideally) can speed up startup times a lot.</p>
9
2010-01-06T01:13:01Z
[ "python" ]
Speeding up the python "import" loader
2,010,255
<p>I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and generating 4 <code>stat()</code> calls - ["foo", "foo.py", "foo.pyc", "foo.so"] - for each check). For a complicated project ...
18
2010-01-06T00:47:29Z
2,010,707
<p>If you run out of options, you can create a <a href="http://en.wikipedia.org/wiki/RAM_disk" rel="nofollow">ramdisk</a> to store your python packages. A ramdisk appears as a directory in your file system, but will actually be mapped directly to your computer's RAM. Here are some <a href="http://www.vanemery.com/Linux...
2
2010-01-06T03:09:06Z
[ "python" ]
Speeding up the python "import" loader
2,010,255
<p>I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and generating 4 <code>stat()</code> calls - ["foo", "foo.py", "foo.pyc", "foo.so"] - for each check). For a complicated project ...
18
2010-01-06T00:47:29Z
2,010,759
<p>Something's missing from your premise--I've never seen some "more-or-less" basic modules take over a second to import, and I'm not running Python on what I would call cutting-edge hardware. Either you're running on some seriously old hardware, or you're running on an overloaded machine, or either your OS or Python ...
1
2010-01-06T03:24:58Z
[ "python" ]
Cannot decode/encode in UTF-8
2,010,323
<p>I have a text-box which allows users to enter a word.</p> <pre><code>The user enters: über </code></pre> <p>In the backend, I get the word like this:</p> <pre><code>def form_process(request): word = request.GET.get('the_word') word = word.encode('utf-8') #word = word.decode('utf-8') print word </...
1
2010-01-06T01:05:10Z
2,010,383
<p>Did you remember to put:</p> <pre><code>accept-charset="utf-8" </code></pre> <p>in the form tag?</p> <p>EDIT: Is the DEFAULT_CHARSET in settings.py set to 'utf-8' ?</p>
1
2010-01-06T01:20:22Z
[ "python", "django", "encoding", "utf-8", "decoding" ]
Cannot decode/encode in UTF-8
2,010,323
<p>I have a text-box which allows users to enter a word.</p> <pre><code>The user enters: über </code></pre> <p>In the backend, I get the word like this:</p> <pre><code>def form_process(request): word = request.GET.get('the_word') word = word.encode('utf-8') #word = word.decode('utf-8') print word </...
1
2010-01-06T01:05:10Z
2,010,461
<p>Solved!</p> <p>I had escape(word) ...in the javascript ...before I passed it to the server.</p>
1
2010-01-06T01:43:21Z
[ "python", "django", "encoding", "utf-8", "decoding" ]
Cannot decode/encode in UTF-8
2,010,323
<p>I have a text-box which allows users to enter a word.</p> <pre><code>The user enters: über </code></pre> <p>In the backend, I get the word like this:</p> <pre><code>def form_process(request): word = request.GET.get('the_word') word = word.encode('utf-8') #word = word.decode('utf-8') print word </...
1
2010-01-06T01:05:10Z
2,010,467
<p>Is there any reason to use print word? If not, its should work without those lines.</p> <pre><code>def form_process(request): word = request.GET.get('the_word') </code></pre>
0
2010-01-06T01:44:49Z
[ "python", "django", "encoding", "utf-8", "decoding" ]
where is the 'itertools' file
2,010,413
<pre><code>import itertools print itertools#ok </code></pre> <p>the code is ok</p> <p>but i can't find the itertools file.</p> <p>who can tell me where is the 'itertools file'</p> <hr> <p>my code is run python2.5</p> <pre><code>import itertools print itertools.__file__ Traceback (most recent call last): File ...
7
2010-01-06T01:29:14Z
2,010,429
<p>try this</p> <pre><code>&gt;&gt;&gt; import imp &gt;&gt;&gt; imp.find_module("itertools") </code></pre> <p><strong>update:</strong> since yours is None, another go through a manual way. Do a sys.path </p> <pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; sys.path ['', '/usr/lib/python2.6/lib-dynload' ] </code></pr...
1
2010-01-06T01:33:34Z
[ "python" ]
where is the 'itertools' file
2,010,413
<pre><code>import itertools print itertools#ok </code></pre> <p>the code is ok</p> <p>but i can't find the itertools file.</p> <p>who can tell me where is the 'itertools file'</p> <hr> <p>my code is run python2.5</p> <pre><code>import itertools print itertools.__file__ Traceback (most recent call last): File ...
7
2010-01-06T01:29:14Z
2,010,443
<pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; itertools.__file__ '/usr/lib64/python2.6/lib-dynload/itertools.so' </code></pre> <p>The fact that the filename ends with <code>.so</code> means it's an extension module written in C (rather than a normal Python module). If you want to take a look at the source code...
16
2010-01-06T01:37:50Z
[ "python" ]
where is the 'itertools' file
2,010,413
<pre><code>import itertools print itertools#ok </code></pre> <p>the code is ok</p> <p>but i can't find the itertools file.</p> <p>who can tell me where is the 'itertools file'</p> <hr> <p>my code is run python2.5</p> <pre><code>import itertools print itertools.__file__ Traceback (most recent call last): File ...
7
2010-01-06T01:29:14Z
2,010,754
<p>If you're looking for the <em>source</em> file (in C, of course), it's for example online <a href="http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?revision=76602&amp;view=markup" rel="nofollow">here</a>.</p>
4
2010-01-06T03:23:04Z
[ "python" ]
where is the 'itertools' file
2,010,413
<pre><code>import itertools print itertools#ok </code></pre> <p>the code is ok</p> <p>but i can't find the itertools file.</p> <p>who can tell me where is the 'itertools file'</p> <hr> <p>my code is run python2.5</p> <pre><code>import itertools print itertools.__file__ Traceback (most recent call last): File ...
7
2010-01-06T01:29:14Z
13,269,417
<p>For the windows users (I'm running Python 2.7.2, Win7x64, default installer package) the call to <code>__file__</code> will flame out as @zjm1126 has noted, I suspect the problem being that <code>itertools</code> is a builtin on the windows package. If you'd picked say, <code>exceptions</code>? You'd get the same be...
1
2012-11-07T12:03:43Z
[ "python" ]
Python halts while iteratively processing my 1GB csv file
2,010,451
<p>I have two files:</p> <ol> <li>metadata.csv: contains an ID, followed by vendor name, a filename, etc</li> <li>hashes.csv: contains an ID, followed by a hash The ID is essentially a foreign key of sorts, relating file metadata to its hash.</li> </ol> <p>I wrote this script to quickly extract out all hashes associa...
1
2010-01-06T01:39:33Z
2,010,491
<p>"Craps out" is not a particularly good description. What does it do? Does it swap? Fill all memory? Or just eats CPU without appearing to do anything?</p> <p>However, just for a start, use a dictionnary rather than a list for <code>stored_ids</code>. Searching in a dictionnary is usually done in O(1) time while sea...
3
2010-01-06T01:50:12Z
[ "python", "memory", "csv", "large-files" ]
Python halts while iteratively processing my 1GB csv file
2,010,451
<p>I have two files:</p> <ol> <li>metadata.csv: contains an ID, followed by vendor name, a filename, etc</li> <li>hashes.csv: contains an ID, followed by a hash The ID is essentially a foreign key of sorts, relating file metadata to its hash.</li> </ol> <p>I wrote this script to quickly extract out all hashes associa...
1
2010-01-06T01:39:33Z
2,010,650
<p>This code would die on any line that does not have at least 4 commas; for example, it would die on an empty line. If you are sure you dont want to use csv reader, then at least catch <code>IndexError</code> on <code>line.split(',')[4]</code></p>
0
2010-01-06T02:45:08Z
[ "python", "memory", "csv", "large-files" ]
Python halts while iteratively processing my 1GB csv file
2,010,451
<p>I have two files:</p> <ol> <li>metadata.csv: contains an ID, followed by vendor name, a filename, etc</li> <li>hashes.csv: contains an ID, followed by a hash The ID is essentially a foreign key of sorts, relating file metadata to its hash.</li> </ol> <p>I wrote this script to quickly extract out all hashes associa...
1
2010-01-06T01:39:33Z
2,010,918
<p>Please explain what do you mean by halt? it hangs or quits? are there any error traceback?</p> <p>a) It will fail on any line not having ","</p> <pre><code>&gt;&gt;&gt; 'hmmm'.split(",")[2] Traceback (most recent call last): File "&lt;string&gt;", line 1, in &lt;string&gt; IndexError: list index out of range </c...
0
2010-01-06T04:16:50Z
[ "python", "memory", "csv", "large-files" ]
Python halts while iteratively processing my 1GB csv file
2,010,451
<p>I have two files:</p> <ol> <li>metadata.csv: contains an ID, followed by vendor name, a filename, etc</li> <li>hashes.csv: contains an ID, followed by a hash The ID is essentially a foreign key of sorts, relating file metadata to its hash.</li> </ol> <p>I wrote this script to quickly extract out all hashes associa...
1
2010-01-06T01:39:33Z
2,011,007
<p>Searching in array took O(n), so use dict instead</p> <pre><code>stored_ids = dict((row[0],1) for row in entries if row[2] == options.vendor) </code></pre> <p>Or Use set</p> <pre><code>a=set(row[0] for row in entries if row[2] == options.vendor) b=set(line.split(",")[0] for line in hashes) c=a.intersection(b) </c...
0
2010-01-06T04:46:59Z
[ "python", "memory", "csv", "large-files" ]
SQLAlchemy filter query by related object
2,010,454
<p>Using <a href="http://en.wikipedia.org/wiki/SQLAlchemy" rel="nofollow">SQLAlchemy</a>, I have a one to many relation with two tables - users and scores. I am trying to query the top 10 users sorted by their aggregate score over the past X amount of days.</p> <pre><code>users: id user_name score score...
6
2010-01-06T01:41:10Z
2,010,473
<p>You will need to use a subquery in order to compute the aggregate score for each user. Subqueries are described here: <a href="http://www.sqlalchemy.org/docs/05/ormtutorial.html?highlight=subquery#using-subqueries" rel="nofollow">http://www.sqlalchemy.org/docs/05/ormtutorial.html?highlight=subquery#using-subqueries<...
1
2010-01-06T01:46:15Z
[ "python", "sqlalchemy" ]
SQLAlchemy filter query by related object
2,010,454
<p>Using <a href="http://en.wikipedia.org/wiki/SQLAlchemy" rel="nofollow">SQLAlchemy</a>, I have a one to many relation with two tables - users and scores. I am trying to query the top 10 users sorted by their aggregate score over the past X amount of days.</p> <pre><code>users: id user_name score score...
6
2010-01-06T01:41:10Z
2,011,465
<p>I am assuming the column (not the relation) you're using for the join is called Score.user_id, so change it if this is not the case.</p> <p>You will need to do something like this:</p> <pre><code>DBSession.query(Score.user_id, func.sum(Score.score_amount).label('total_score')).group_by(Score.user_id).filter(Score....
0
2010-01-06T07:22:57Z
[ "python", "sqlalchemy" ]
SQLAlchemy filter query by related object
2,010,454
<p>Using <a href="http://en.wikipedia.org/wiki/SQLAlchemy" rel="nofollow">SQLAlchemy</a>, I have a one to many relation with two tables - users and scores. I am trying to query the top 10 users sorted by their aggregate score over the past X amount of days.</p> <pre><code>users: id user_name score score...
6
2010-01-06T01:41:10Z
2,015,755
<p>The single-joined-row way, with a <code>group_by</code> added in for all user columns although MySQL will let you group on just the "id" column if you choose:</p> <pre><code> sess.query(User, func.sum(Score.amount).label('score_increase')).\ join(User.scores).\ filter(Score.created_...
9
2010-01-06T19:55:54Z
[ "python", "sqlalchemy" ]
How do you get all the rows from a particular table using BeautifulSoup?
2,010,481
<p>I am learning Python and BeautifulSoup to scrape data from the web, and read a HTML table. I can read it into Open Office and it says that it is Table #11.</p> <p>It seems like BeautifulSoup is the preferred choice, but can anyone tell me how to grab a particular table and all the rows? I have looked at the module ...
9
2010-01-06T01:47:40Z
2,010,535
<p>This should be pretty straight forward if you have a chunk of HTML to parse with BeautifulSoup. The general idea is to navigate to your table using the <code>findChildren</code> method, then you can get the text value inside the cell with the <code>string</code> property.</p> <pre><code>&gt;&gt;&gt; from BeautifulS...
19
2010-01-06T02:03:25Z
[ "python", "beautifulsoup" ]
Python: why aren't variables updating?
2,010,566
<p>A python program that I'm debugging has the following code (including <code>print</code> statements for debugging):</p> <pre><code>print "BEFORE..." print "oup[\"0\"] = " + str(oup["0"]) print "oup[\"2008\"] = " + str(oup["2008"]) print "oup[\"2009\"] = " + str(oup["2009"]) oup0 = oup["0"] oup2008 = oup["2008"] ou...
0
2010-01-06T02:11:46Z
2,010,580
<p>If the values are integers, then <code>(oup2008 / ouptotal)</code> will be zero, so they will be updated to their own value + 0, hence no change.</p> <p>Convert them to floats for the calculation, then back if required, and it should work as expected.</p> <p>Example:</p> <pre><code>oup["2008"] = oup2008 + int(oup...
6
2010-01-06T02:18:32Z
[ "python", "variables", "integer" ]
Python: why aren't variables updating?
2,010,566
<p>A python program that I'm debugging has the following code (including <code>print</code> statements for debugging):</p> <pre><code>print "BEFORE..." print "oup[\"0\"] = " + str(oup["0"]) print "oup[\"2008\"] = " + str(oup["2008"]) print "oup[\"2009\"] = " + str(oup["2009"]) oup0 = oup["0"] oup2008 = oup["2008"] ou...
0
2010-01-06T02:11:46Z
2,010,585
<p>Integer division results in integer results. either use <code>float()</code> on one of the division terms or add <code>from __future__ import division</code> at the top.</p>
3
2010-01-06T02:19:34Z
[ "python", "variables", "integer" ]
Python: why aren't variables updating?
2,010,566
<p>A python program that I'm debugging has the following code (including <code>print</code> statements for debugging):</p> <pre><code>print "BEFORE..." print "oup[\"0\"] = " + str(oup["0"]) print "oup[\"2008\"] = " + str(oup["2008"]) print "oup[\"2009\"] = " + str(oup["2009"]) oup0 = oup["0"] oup2008 = oup["2008"] ou...
0
2010-01-06T02:11:46Z
2,010,598
<p>They are, you're just updating them with the same value. In Python 2.6, <code>/</code> is still an integer division operator(<a href="http://docs.python.org/reference/expressions.html#binary-arithmetic-operations" rel="nofollow">reference</a>), so dividing <code>(oup2008 / ouptotal)</code> is dividing a number by a ...
1
2010-01-06T02:23:34Z
[ "python", "variables", "integer" ]
python crypto high level wrapper
2,010,575
<p>I'm using PyCrypto (on google app engine) for AES encryption.</p> <p>PyCrypto gives I guess a raw interface to AES--i need to pad my keys and my inputs to 16 byte multiples.</p> <p>Is there a higher level library which takes care of this stuff for me?</p>
1
2010-01-06T02:17:19Z
2,010,737
<p>An easy, friendly wrapper on top of PyCrypto is <a href="http://www.freenet.org.nz/ezPyCrypto/" rel="nofollow">ezPyCrypto</a>.</p>
5
2010-01-06T03:19:38Z
[ "python", "google-app-engine", "aes", "pycrypto" ]
What does "mro()" do in Python?
2,010,692
<p>In <code>django.utils.functional.py</code>:</p> <pre><code>for t in type(res).mro():#&lt;-----this if t in self.__dispatch: return self.__dispatch[t][funcname](res, *args, **kw) </code></pre> <p>I don't understand mro.<br> What does "mro()" do?<br> What does mro mean?</p>
77
2010-01-06T03:04:16Z
2,010,706
<p><code>mro()</code> stands for Method Resolution Order. It returns a list of types the class is derived from, in the order they are searched for methods.</p>
60
2010-01-06T03:08:58Z
[ "python" ]
What does "mro()" do in Python?
2,010,692
<p>In <code>django.utils.functional.py</code>:</p> <pre><code>for t in type(res).mro():#&lt;-----this if t in self.__dispatch: return self.__dispatch[t][funcname](res, *args, **kw) </code></pre> <p>I don't understand mro.<br> What does "mro()" do?<br> What does mro mean?</p>
77
2010-01-06T03:04:16Z
2,010,732
<p>Follow along...:</p> <pre><code>&gt;&gt;&gt; class A(object): pass ... &gt;&gt;&gt; A.__mro__ (&lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) &gt;&gt;&gt; class B(A): pass ... &gt;&gt;&gt; B.__mro__ (&lt;class '__main__.B'&gt;, &lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) &gt;&gt;&gt; class C(A): pass ....
138
2010-01-06T03:17:19Z
[ "python" ]
What does "mro()" do in Python?
2,010,692
<p>In <code>django.utils.functional.py</code>:</p> <pre><code>for t in type(res).mro():#&lt;-----this if t in self.__dispatch: return self.__dispatch[t][funcname](res, *args, **kw) </code></pre> <p>I don't understand mro.<br> What does "mro()" do?<br> What does mro mean?</p>
77
2010-01-06T03:04:16Z
36,142,245
<p>This would perhaps show the order of resolution.</p> <pre><code>class A(object): def dothis(self): print('I am from A class') class B(A): pass class C(object): def dothis(self): print('I am from C class') class D(B, C): pass d_instance= D() d_instance.dothis() print(D.mro()) </co...
1
2016-03-21T21:47:41Z
[ "python" ]
What's the suggested way of importing modules within a django project
2,010,746
<p>This has always bothered me, and I've never really come up with my own preferred way of doing this.</p> <p>When importing something from one of your own applications in a django project, do you import with:</p> <pre><code>from myproject.mymodule.model import SomeModel from myproject.anotherone.model import Another...
0
2010-01-06T03:21:12Z
2,010,765
<p>I would recommend using the second alternative:</p> <pre><code>from mymodule.model import SomeModel from anotherone.model import AnotherModel </code></pre> <p>In Django, it's recommended to write reusable applications, that you may deploy in multiple projects. Specifying the name of the project would hinder this ...
5
2010-01-06T03:26:06Z
[ "python", "django" ]
What's the suggested way of importing modules within a django project
2,010,746
<p>This has always bothered me, and I've never really come up with my own preferred way of doing this.</p> <p>When importing something from one of your own applications in a django project, do you import with:</p> <pre><code>from myproject.mymodule.model import SomeModel from myproject.anotherone.model import Another...
0
2010-01-06T03:21:12Z
2,010,773
<p>I prefer to use absolute imports whenever possible. The first reason is that relative imports are on their way out in Python 3, so it's best not to get into that habit. (Based on the way you phrased the question, I'm assuming that you put the application packages inside your project package.) The second reason is th...
1
2010-01-06T03:28:44Z
[ "python", "django" ]
What's the suggested way of importing modules within a django project
2,010,746
<p>This has always bothered me, and I've never really come up with my own preferred way of doing this.</p> <p>When importing something from one of your own applications in a django project, do you import with:</p> <pre><code>from myproject.mymodule.model import SomeModel from myproject.anotherone.model import Another...
0
2010-01-06T03:21:12Z
2,010,783
<p>How are you packaging your modules normally? If you're packaging them all under the "myproject" module, you should just keep doing that. If you're not, there's no reason to start. I don't see that being in a Django project has anything to do with the question--if you're not reusing code, why bother segregating yo...
0
2010-01-06T03:32:08Z
[ "python", "django" ]
Cherrypy hanging on form post
2,010,766
<p>I'm currently trying to remove a legacy python framework (webware 0.8.1) and layer cherrypy 3.1.2 on top of it. Instead of converting all the webware pages to cherrypy pages, I'm merely processing it through webware and passing it to cherrypy like so.</p> <pre><code>def default(self, url, *suburl, **kwarg): ....
0
2010-01-06T03:26:07Z
2,010,815
<p>I don't really have experience with Webware, but based on the appearance of your code, Webware is trying to use cgi.FieldStorage to retrieve your field variables, but FieldStorage can't get the length, or gets the length incorrectly (probably because whatever Webware does to get the Content-Length header doesn't wor...
2
2010-01-06T03:40:23Z
[ "python", "cherrypy" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
2,010,828
<p>You can try running command <code>arp -a</code></p> <p>Here is few links about Mac Address grabbing (not tested)</p> <p>In Linux/Unix, arping, </p> <p><a href="http://www.ibm.com/developerworks/aix/library/au-pythocli/" rel="nofollow">http://www.ibm.com/developerworks/aix/library/au-pythocli/</a></p> <p>In Windo...
4
2010-01-06T03:44:17Z
[ "python", "linux", "networking", "mac-address" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
2,010,975
<p>Many years ago, I was tasked with gathering various machine info from all machines on a corporate campus. One desired piece of info was the MAC address, which is difficult to get on a network that spanned multiple subnets. At the time, I used the Windows built-in "nbtstat" command.</p> <p>Today there is a Unix ut...
0
2010-01-06T04:36:29Z
[ "python", "linux", "networking", "mac-address" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
2,011,003
<p>you can use this on either win32 or linux</p> <pre><code>import subprocess import sys remotehost="192.168.0.122" cmd="arp -a" p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) output, errors = p.communicate() if output is not None : if sys.platform in ['linux','linux2']: for i in output.split(...
0
2010-01-06T04:45:20Z
[ "python", "linux", "networking", "mac-address" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
2,165,803
<p>arp entries might never be right, I tried to ping a host several times but arp -a would not give me it's mac/ethernet address. (No worry with the windows code from active state BTW)</p> <p>The reliable way on Linux (and *nix) is to use arping or scappy (see <a href="http://en.wikipedia.org/wiki/Arping" rel="nofollo...
2
2010-01-29T22:56:37Z
[ "python", "linux", "networking", "mac-address" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
18,181,853
<p>Use these commands:</p> <pre><code>arp -n &lt;IP Address&gt;|awk '/&lt;ip address&gt;/ {print $3}' </code></pre> <p>for example, if you want mac address of 192.168.10.1:</p> <pre><code>#arp -n 192.168.10.1|awk '/192.168.10.1/ {print $3}' #00:0c:29:68:8f:a4 </code></pre>
3
2013-08-12T07:47:51Z
[ "python", "linux", "networking", "mac-address" ]
Get remote MAC address using Python and Linux
2,010,816
<p>How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.</p>
5
2010-01-06T03:40:28Z
21,761,811
<p>If you just want to query the OS' arp cache, reasonably recent linux kernels support this:</p> <pre><code>import os, sys host = sys.argv[1] # ping is optional (sends a WHO_HAS request) os.popen('ping -c 1 %s' % host) # grep with a space at the end of IP address to make sure you get a single line fields = os.pope...
1
2014-02-13T17:52:03Z
[ "python", "linux", "networking", "mac-address" ]
How to implement a voice chat functionality using Python?
2,011,042
<p>My goal is a cross-platform voice chat application. The part I am a bit confused about it's the voice transferring one : )</p> <p>What can you suggest? Maybe a binding to some low-level library or even a framework?</p> <p>BTW, I don't have to use Python, so if you think that Python is not a good idea for this purp...
0
2010-01-06T04:58:01Z
2,011,074
<p>You should look into Telepathy:</p> <p><a href="http://telepathy.freedesktop.org/wiki/" rel="nofollow">http://telepathy.freedesktop.org/wiki/</a></p> <p>There are Python bindings available:</p> <p><a href="http://telepathy.freedesktop.org/wiki/Telepathy%20Python" rel="nofollow">http://telepathy.freedesktop.org/wi...
3
2010-01-06T05:10:06Z
[ "python", "chat", "voice" ]
how to check the character count of a file in python
2,011,048
<p>I have a python code which reads many files. but some files are extremely large due to which i have errors coming in other codes. i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files. Thanks.</p>
5
2010-01-06T05:00:43Z
2,011,054
<pre><code>os.path.getsize(path) </code></pre> <blockquote> <p>Return the size, in bytes, of path. Raise os.error if the file does not exist or is inaccessible.</p> </blockquote>
4
2010-01-06T05:03:18Z
[ "python", "size", "character" ]
how to check the character count of a file in python
2,011,048
<p>I have a python code which reads many files. but some files are extremely large due to which i have errors coming in other codes. i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files. Thanks.</p>
5
2010-01-06T05:00:43Z
2,011,055
<pre><code>os.stat(filepath).st_size </code></pre> <p>Assuming by ‘characters’ you mean bytes. ETA:</p> <blockquote> <p>i need total character count just like what the command 'wc filename' gives me unix</p> </blockquote> <p>In which mode? <code>wc</code> on it own will give you a line, word and byte count (sa...
8
2010-01-06T05:03:19Z
[ "python", "size", "character" ]
how to check the character count of a file in python
2,011,048
<p>I have a python code which reads many files. but some files are extremely large due to which i have errors coming in other codes. i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files. Thanks.</p>
5
2010-01-06T05:00:43Z
2,011,061
<p>Try</p> <pre><code>import os os.path.getsize(filePath) </code></pre> <p>to get the size of your file, in bytes.</p>
5
2010-01-06T05:05:04Z
[ "python", "size", "character" ]
how to check the character count of a file in python
2,011,048
<p>I have a python code which reads many files. but some files are extremely large due to which i have errors coming in other codes. i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files. Thanks.</p>
5
2010-01-06T05:00:43Z
2,011,063
<p>If you want the unicode <strong>character</strong> count for a text file given a specific encoding, you will have to read in the entire file to do that.</p> <p>However, if you want the <strong>byte</strong> count for a given file, you want <code>os.path.getsize()</code>, which should only need to do a <code>stat</c...
7
2010-01-06T05:05:18Z
[ "python", "size", "character" ]
how to check the character count of a file in python
2,011,048
<p>I have a python code which reads many files. but some files are extremely large due to which i have errors coming in other codes. i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files. Thanks.</p>
5
2010-01-06T05:00:43Z
2,011,137
<p>alternative way</p> <pre><code>f=open("file") os.fstat( f.fileno() ).st_size f.close() </code></pre>
2
2010-01-06T05:33:00Z
[ "python", "size", "character" ]
Creating and deploying a python chat application using Twisted
2,011,136
<p>I have created a <strong>chat server application</strong> using the Twisted framework. I am running it on my local machine and now I want to go global. The application is similar to omegle.com.</p> <p>How can I develop on a third party commercial server so that it runs continuously? Do I need to get a dedicated ser...
2
2010-01-06T05:32:57Z
2,011,181
<p>As per <a href="http://stackoverflow.com/questions/956869/if-i-develop-a-chat-server-using-twisted-where-can-i-deploy-it">this SO answer</a>,</p> <blockquote> <p>You can deploy Twisted on any hosting provider who gives you a shell prompt and doesn't limit your long-running processes.</p> <p>Some exampl...
3
2010-01-06T05:50:03Z
[ "python", "hosting", "chat", "twisted" ]
Creating and deploying a python chat application using Twisted
2,011,136
<p>I have created a <strong>chat server application</strong> using the Twisted framework. I am running it on my local machine and now I want to go global. The application is similar to omegle.com.</p> <p>How can I develop on a third party commercial server so that it runs continuously? Do I need to get a dedicated ser...
2
2010-01-06T05:32:57Z
2,011,190
<p>Take a look at <a href="http://wiki.python.org/moin/PythonHosting" rel="nofollow">Python friendly hosts</a> to get an idea of what is available and what it will cost you. Typically, you could get away with a shared hosting package as long as you have a shell. However, if your program begins serving tons of clients, ...
0
2010-01-06T05:52:15Z
[ "python", "hosting", "chat", "twisted" ]
Compiling Python 3.1.1 32-bit
2,011,295
<p>We're using this configure script with an option we spotted in the configure help menu:</p> <pre><code>./configure --with-universal-archs=32-bit --prefix="$HOME/python" make make install </code></pre> <p>But when all the dust is settled and we check out what it gives us:</p> <pre><code>✔python-3.1.1⤿ file ~/p...
1
2010-01-06T06:30:30Z
2,011,358
<p>I'd try <code>CFLAGS=-m32 LDFLAGS=-m32</code> although I don't have the requisite hardware to test that it works.</p> <p>I'm not familiar with the <code>--with-universal-archs</code> option -- must be specific to OS X?</p>
0
2010-01-06T06:46:56Z
[ "python" ]
Compiling Python 3.1.1 32-bit
2,011,295
<p>We're using this configure script with an option we spotted in the configure help menu:</p> <pre><code>./configure --with-universal-archs=32-bit --prefix="$HOME/python" make make install </code></pre> <p>But when all the dust is settled and we check out what it gives us:</p> <pre><code>✔python-3.1.1⤿ file ~/p...
1
2010-01-06T06:30:30Z
2,011,868
<p>There's no need to build your own. Use the <a href="http://www.python.org/download/releases/3.1.1/" rel="nofollow">Python 3.1.1 OS X installer from python.org</a>. It's 32-bit only and will work just fine on OS X 10.4 through 10.6. If you need to install any packages with C extension modules on 10.6, you'll need ...
2
2010-01-06T09:09:57Z
[ "python" ]
how can i get the value '111'
2,011,568
<pre><code>def a(): w='www' a.a='aaa' print a.__dict__ a.__dict__={'1':'111','2':'222'} print a.1#error print a['1']#error </code></pre> <p>how can i get the value '111' thanks</p>
-1
2010-01-06T07:52:37Z
2,011,576
<p>You can access it thanks to the <code>__dict__</code> member. See following code </p> <pre><code>def a(): w='www' a.a='aaa' print a.__dict__ a.__dict__={'1':'111','2':'222'} print a.__dict__['1'] </code></pre>
1
2010-01-06T07:57:02Z
[ "python" ]
how can i get the value '111'
2,011,568
<pre><code>def a(): w='www' a.a='aaa' print a.__dict__ a.__dict__={'1':'111','2':'222'} print a.1#error print a['1']#error </code></pre> <p>how can i get the value '111' thanks</p>
-1
2010-01-06T07:52:37Z
2,011,579
<p>You'll have to do </p> <pre><code>print a.__dict__['1'] </code></pre> <p>or</p> <pre><code>print getattr(a, '1') </code></pre> <p>"1" is not a valid variable name in Python. If you did:</p> <pre><code>a.__dict__ = {'a1' : '111'} print a.a1 </code></pre> <p>it would work.</p>
9
2010-01-06T07:57:44Z
[ "python" ]
how can i get the value '111'
2,011,568
<pre><code>def a(): w='www' a.a='aaa' print a.__dict__ a.__dict__={'1':'111','2':'222'} print a.1#error print a['1']#error </code></pre> <p>how can i get the value '111' thanks</p>
-1
2010-01-06T07:52:37Z
2,012,114
<p>Since you say are just a beginner, perhaps you're just looking for the even easier: </p> <pre><code> a = {'1':'111','2':'222'} </code></pre> <p>so <code>a['1']</code> returns the desired <code>'111'</code></p>
3
2010-01-06T10:04:45Z
[ "python" ]
Can I find out where a Python application crashed using the data dump?
2,011,578
<p>My application crashed recently on a client's computer. I suspect it's because of PyQt's own memory management, which can lead to a invalid memory accesses when not properly handled.</p> <p>When Python crashes like this, no backtrace is printed, only a data dump is written to the disk.</p> <p>Is there a possibilit...
5
2010-01-06T07:57:06Z
2,011,807
<p>Is this a linux core dump? If so you can examine it with gdb. You will need to run in on a system with an identical OS and version of Python, including 3rd party libraries. Run <code>gdb -c /path/to/core/file</code>. Once gdb has loaded then the command <code>bt</code> will list the stack trace for the main thre...
6
2010-01-06T08:55:49Z
[ "python", "debugging" ]
Can I find out where a Python application crashed using the data dump?
2,011,578
<p>My application crashed recently on a client's computer. I suspect it's because of PyQt's own memory management, which can lead to a invalid memory accesses when not properly handled.</p> <p>When Python crashes like this, no backtrace is printed, only a data dump is written to the disk.</p> <p>Is there a possibilit...
5
2010-01-06T07:57:06Z
2,012,455
<p>Does your application produce a log? If so, you can have logging produce an in-memory log which you might be able to find within the core dump. Also, you can have them send you the log file itself <em>instead</em> of the core dump.</p>
0
2010-01-06T11:12:42Z
[ "python", "debugging" ]
Can I find out where a Python application crashed using the data dump?
2,011,578
<p>My application crashed recently on a client's computer. I suspect it's because of PyQt's own memory management, which can lead to a invalid memory accesses when not properly handled.</p> <p>When Python crashes like this, no backtrace is printed, only a data dump is written to the disk.</p> <p>Is there a possibilit...
5
2010-01-06T07:57:06Z
2,012,595
<p>There's a file named <code>gdbinit</code> in the Python source tree (in <code>Misc/gdbinit</code>) which provides a set of macros for gdb so as to display the current interpreter context. Just type <code>source gdbinit</code> in gdb and then you can execute macros such as <code>pystack</code>. The list of available ...
1
2010-01-06T11:36:58Z
[ "python", "debugging" ]
Can I find out where a Python application crashed using the data dump?
2,011,578
<p>My application crashed recently on a client's computer. I suspect it's because of PyQt's own memory management, which can lead to a invalid memory accesses when not properly handled.</p> <p>When Python crashes like this, no backtrace is printed, only a data dump is written to the disk.</p> <p>Is there a possibilit...
5
2010-01-06T07:57:06Z
14,039,036
<p>Not sure if it helps, but if you can catch the exception, you could use <a href="http://github.com/gooli/pydump" rel="nofollow">http://github.com/gooli/pydump</a> to store a dump and load it later in a Python debugger.</p>
1
2012-12-26T10:10:26Z
[ "python", "debugging" ]
Display random choice (Python)
2,011,583
<p>I have a list[] of items from which I'd like to display one randomly, but the displayed item must not repeat more than once in last x requests. </p> <ol> <li>list1 = item1, item2, item3, item4, item5, item6, item7, item8, item9, item 10 </li> <li>Display a random selection from the list above </li> <li>list2 = stor...
1
2010-01-06T07:59:31Z
2,011,605
<p>Is this what you want?</p> <pre><code>list1 = range(10) import random random.shuffle(list1) list2 = list1[:7] for item in list2: print item print list1[7] </code></pre> <p>In other words, look at <code>random.shuffle()</code>. If you want to keep the original list intact, you can copy it: <code>list_copy = li...
4
2010-01-06T08:04:55Z
[ "python", "list", "random", "limit" ]
Display random choice (Python)
2,011,583
<p>I have a list[] of items from which I'd like to display one randomly, but the displayed item must not repeat more than once in last x requests. </p> <ol> <li>list1 = item1, item2, item3, item4, item5, item6, item7, item8, item9, item 10 </li> <li>Display a random selection from the list above </li> <li>list2 = stor...
1
2010-01-06T07:59:31Z
2,011,617
<p>collections.deque is the only sequence type in python that naturally supports being bounded (and only in Python 2.6 and up.) If using python 2.6 or newer:</p> <pre><code># Setup from collections import deque from random import choice used = deque(maxlen=7) # Now your sampling bit item = random.choice([x for x in ...
7
2010-01-06T08:09:23Z
[ "python", "list", "random", "limit" ]
Display random choice (Python)
2,011,583
<p>I have a list[] of items from which I'd like to display one randomly, but the displayed item must not repeat more than once in last x requests. </p> <ol> <li>list1 = item1, item2, item3, item4, item5, item6, item7, item8, item9, item 10 </li> <li>Display a random selection from the list above </li> <li>list2 = stor...
1
2010-01-06T07:59:31Z
2,011,622
<p>Something like:</p> <pre><code># Setup import random list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = [] # Loop for as long as you want to display items while loopCondition: index = random.randint(0, len(list1)-1) item = list1.pop(index) print item list2.append(item) if(len(list2) &gt; 7): ...
1
2010-01-06T08:11:01Z
[ "python", "list", "random", "limit" ]
Display random choice (Python)
2,011,583
<p>I have a list[] of items from which I'd like to display one randomly, but the displayed item must not repeat more than once in last x requests. </p> <ol> <li>list1 = item1, item2, item3, item4, item5, item6, item7, item8, item9, item 10 </li> <li>Display a random selection from the list above </li> <li>list2 = stor...
1
2010-01-06T07:59:31Z
2,011,747
<p>You could try using a generator function and call <code>.next()</code> whenever you need a new item.</p> <pre><code>import random def randomizer(l, x): penalty_box = [] random.shuffle(l) while True: element = l.pop(0) # for show print penalty_box, l yield element ...
2
2010-01-06T08:45:23Z
[ "python", "list", "random", "limit" ]
Display random choice (Python)
2,011,583
<p>I have a list[] of items from which I'd like to display one randomly, but the displayed item must not repeat more than once in last x requests. </p> <ol> <li>list1 = item1, item2, item3, item4, item5, item6, item7, item8, item9, item 10 </li> <li>Display a random selection from the list above </li> <li>list2 = stor...
1
2010-01-06T07:59:31Z
2,011,794
<p>I'd use set objects to get a list of items in list1 but not in list2:</p> <pre><code>import random list1 = set(["item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10"]) list2 = [] while True: # Or something selection = random.choice(tuple(list1.difference(set(li...
1
2010-01-06T08:54:00Z
[ "python", "list", "random", "limit" ]
Vim Python indentation not working?
2,011,589
<p>I have <a href="http://en.wikipedia.org/wiki/Vim_%28text_editor%29" rel="nofollow">Vim</a> 7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go.</p> <pre><code>$ find /usr/share/vim/vim70/ -name \*python\* /usr/share/vim/vim70/syntax/python.vim /usr/share/vim/vim70/ftplugin/pyth...
9
2010-01-06T08:01:39Z
2,011,800
<p>Setting tabstop, shiftwidth, etc... in your vimrc is correct. These set your global settings, as well as serve as parameters to the filetype-specific indentation support.</p> <p>The language indentation plugins use these settings, but typically also set an indent expression (<code>:he inde</code>) appropriate for ...
2
2010-01-06T08:54:16Z
[ "python", "vim", "vim-plugin" ]
Vim Python indentation not working?
2,011,589
<p>I have <a href="http://en.wikipedia.org/wiki/Vim_%28text_editor%29" rel="nofollow">Vim</a> 7 (enhanced) on CentOS 5, and it comes with all the usual Vim plugins/scripts ready to go.</p> <pre><code>$ find /usr/share/vim/vim70/ -name \*python\* /usr/share/vim/vim70/syntax/python.vim /usr/share/vim/vim70/ftplugin/pyth...
9
2010-01-06T08:01:39Z
2,014,248
<p>My understanding is that the python.vim file is just a syntax-highlighting file possibly, because Python files can be indented multiple ways. <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29#Development" rel="nofollow">PEP8</a> prescribes four spaces, but legacy files could be different includ...
3
2010-01-06T16:04:47Z
[ "python", "vim", "vim-plugin" ]
unicode error when saving an object in django admin
2,011,629
<p>In my django app, I have some objects that cause the corresponding URL in the django admin to be non ascii. (for example: <code>http://mysite/admin/myapp/myclass/Présentation/</code>)</p> <p>I can edit the object without any problem but when I save it I have the following error:</p> <p><code>UnicodeEncodeError: '...
3
2010-01-06T08:12:51Z
2,012,162
<p>I'm pretty sure your database is probably using the latin1 encoding. Django supposes you have everything set as unicode (utf8).</p> <p>To check this out, get into MySQL Shell and type:</p> <pre><code>mysql&gt; show variables like 'char%'; </code></pre> <p>If you see a bunch of latin1 (or any other encoding that i...
6
2010-01-06T10:17:37Z
[ "python", "django", "unicode", "django-admin" ]
unicode error when saving an object in django admin
2,011,629
<p>In my django app, I have some objects that cause the corresponding URL in the django admin to be non ascii. (for example: <code>http://mysite/admin/myapp/myclass/Présentation/</code>)</p> <p>I can edit the object without any problem but when I save it I have the following error:</p> <p><code>UnicodeEncodeError: '...
3
2010-01-06T08:12:51Z
2,013,318
<p>in your model, have you tried putting</p> <pre><code>class Model(models.Model): def __unicode__(self): return self.name ## having a model object named "name" </code></pre> <p>Not sure if this is the answer you are searhing for, but have you tried?</p>
1
2010-01-06T13:46:49Z
[ "python", "django", "unicode", "django-admin" ]
unicode error when saving an object in django admin
2,011,629
<p>In my django app, I have some objects that cause the corresponding URL in the django admin to be non ascii. (for example: <code>http://mysite/admin/myapp/myclass/Présentation/</code>)</p> <p>I can edit the object without any problem but when I save it I have the following error:</p> <p><code>UnicodeEncodeError: '...
3
2010-01-06T08:12:51Z
2,109,571
<p>I am now using the default id as primary key for every class of my model. As a consequence, I don't have forbidden characters in the url when accessing objects from the admin site.</p> <p>I do recommend to keep the default id as primary key in most cases</p>
0
2010-01-21T13:41:43Z
[ "python", "django", "unicode", "django-admin" ]
unicode error when saving an object in django admin
2,011,629
<p>In my django app, I have some objects that cause the corresponding URL in the django admin to be non ascii. (for example: <code>http://mysite/admin/myapp/myclass/Présentation/</code>)</p> <p>I can edit the object without any problem but when I save it I have the following error:</p> <p><code>UnicodeEncodeError: '...
3
2010-01-06T08:12:51Z
34,095,398
<p><a href="http://itekblog.com/ascii-codec-cant-encode-characters-in-position/" rel="nofollow">This link</a> saved my day<br> you have to add unicode support for your admin in your <strong>models.py</strong>:</p> <pre><code>class MyModel (models.Model): some_field = models.CharField(max_length=1000) def __uni...
1
2015-12-04T18:48:22Z
[ "python", "django", "unicode", "django-admin" ]
pycurl module not available after installation on Snow Leopard
2,011,640
<p>I'm running Python 2.6.4 on Mac Snow Leopard. I installed pycurl using:</p> <pre><code>sudo env ARCHFLAGS="-arch x86_64" easy_install setuptools pycurl==7.16.2.1 </code></pre> <p>The installation completes with no issues and says pycurl is installed in subsequent installation attempts. However, when I try to "impo...
0
2010-01-06T08:15:47Z
2,011,661
<p>I guess you have 2 different versions of python, you can find out by running command <code>which python</code></p>
1
2010-01-06T08:21:18Z
[ "python", "osx-snow-leopard", "pycurl" ]
pycurl module not available after installation on Snow Leopard
2,011,640
<p>I'm running Python 2.6.4 on Mac Snow Leopard. I installed pycurl using:</p> <pre><code>sudo env ARCHFLAGS="-arch x86_64" easy_install setuptools pycurl==7.16.2.1 </code></pre> <p>The installation completes with no issues and says pycurl is installed in subsequent installation attempts. However, when I try to "impo...
0
2010-01-06T08:15:47Z
2,011,665
<p>I would suspect you have 2 versions of python on your system. How about removing easy install and reinstalling it. </p> <p>Remove the current easy install script by typing <code>which easy_install</code> and then <code>rm [easy install full path]</code>.</p> <p>To install easy install</p> <pre><code>wget http://p...
2
2010-01-06T08:23:08Z
[ "python", "osx-snow-leopard", "pycurl" ]
pycurl module not available after installation on Snow Leopard
2,011,640
<p>I'm running Python 2.6.4 on Mac Snow Leopard. I installed pycurl using:</p> <pre><code>sudo env ARCHFLAGS="-arch x86_64" easy_install setuptools pycurl==7.16.2.1 </code></pre> <p>The installation completes with no issues and says pycurl is installed in subsequent installation attempts. However, when I try to "impo...
0
2010-01-06T08:15:47Z
2,011,830
<p>If you have installed Python 2.6.4 using the python.org OS X installer, you need to install the optional <code>10.4u SDK</code> from the 10.6 <code>Xcode</code> mpkg to install any python packages that include C extension modules.</p>
0
2010-01-06T09:02:08Z
[ "python", "osx-snow-leopard", "pycurl" ]
Django dynamic number of filter for a objects request
2,011,752
<p>How can I do something like this :</p> <pre><code>products_list = Product.objects.all() for key in keywords: products_list = products_list.filter(name__icontains=q) </code></pre> <p>This don't work.</p>
3
2010-01-06T08:45:53Z
2,011,781
<p>You are filtering the list with several AND statements, and you want OR statements. Try something like this:</p> <pre><code>from django.db.models import Q products_list = Product.objects.all() orq = None for key in keywords: thisq = Q(name__icontains=q) if orq: orq = thisq | orq else: ...
2
2010-01-06T08:50:21Z
[ "python", "django", "django-models" ]
why my code error? I copied the 'memoize' function in django.utils.functional
2,011,823
<p>my code:</p> <pre><code>a=[1,2,3,4] b=a[:2] c=[] c[b]='sss'#error </code></pre> <p>memoize function:</p> <pre><code>def memoize(func, cache, num_args): def wrapper(*args): mem_args = args[:num_args]#&lt;------this if mem_args in cache: return cache[mem_args] result = func(*...
-2
2010-01-06T09:00:18Z
2,011,831
<p>In the <code>memoize</code> function, I'm assuming <code>cache</code> is a <code>dict</code>. Also, since a is a <code>list</code>, b will also be a <code>list</code>, and lists are not hashable. Use a <code>tuple</code>.</p> <p>Try</p> <pre><code>a = (1, 2, 3, 4) # Parens, not brackets b = a[:2] c = {} # Curly br...
2
2010-01-06T09:02:21Z
[ "python", "django" ]
why my code error? I copied the 'memoize' function in django.utils.functional
2,011,823
<p>my code:</p> <pre><code>a=[1,2,3,4] b=a[:2] c=[] c[b]='sss'#error </code></pre> <p>memoize function:</p> <pre><code>def memoize(func, cache, num_args): def wrapper(*args): mem_args = args[:num_args]#&lt;------this if mem_args in cache: return cache[mem_args] result = func(*...
-2
2010-01-06T09:00:18Z
2,012,784
<p>What has your question got to do with the memoize function you post? </p> <p>Presumably (although we have to guess, since you didn't post the actual error) you're getting a TypeError. This is due to two mistakes.</p> <p>Firstly, <code>c</code> is a list. So you can't use arbitrary keys, you can only use integer in...
2
2010-01-06T12:12:21Z
[ "python", "django" ]
Changing schema using cx_Oracle
2,012,035
<p>Well, I hope this is not a duplicate, the search did not yield anything useful.</p> <p>I have been toying with <code>cx_Oracle</code> for the past few days, installing and using it. Everything went fine until I reached my current problem: I'd like to change my schema. If I were using sqlplus a simple 'alter session...
5
2010-01-06T09:45:37Z
2,012,709
<p>Try reinstalling cx_Oracle. Your cx_Oracle is probably messed up. What's your OS and python version?</p>
2
2010-01-06T11:54:09Z
[ "python", "oracle", "cx-oracle" ]
Changing schema using cx_Oracle
2,012,035
<p>Well, I hope this is not a duplicate, the search did not yield anything useful.</p> <p>I have been toying with <code>cx_Oracle</code> for the past few days, installing and using it. Everything went fine until I reached my current problem: I'd like to change my schema. If I were using sqlplus a simple 'alter session...
5
2010-01-06T09:45:37Z
2,013,239
<p>Okay, I finally, after much trying and error, followed <code>fn</code> suggestion and investigated inside <code>cx_Oracle</code> to find what was wrong.</p> <p>It turns out that a number of arguments and methods are only available through some flags:</p> <ul> <li><code>WITH_UNICODE</code> activates <code>encoding<...
6
2010-01-06T13:34:50Z
[ "python", "oracle", "cx-oracle" ]
How to write last 50 lines from one file to another Python
2,012,118
<p>I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?</p> <pre><code>def email_success(): fp = open(results_file, 'r') sum_file = (fp.readlines()[-50:]) fp.clos...
0
2010-01-06T10:05:32Z
2,012,121
<pre><code>TypeError: coercing to Unicode: need string or buffer, tuple found </code></pre> <p>Error says its expect string or buffer but you are passing <code>tuple</code>, so just join it with <code>""</code> to make it to string</p> <p>So, Try</p> <pre><code>sum_file = "".join(fp.readlines()[-50:]) </code></pre> ...
5
2010-01-06T10:06:52Z
[ "python" ]
How to write last 50 lines from one file to another Python
2,012,118
<p>I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?</p> <pre><code>def email_success(): fp = open(results_file, 'r') sum_file = (fp.readlines()[-50:]) fp.clos...
0
2010-01-06T10:05:32Z
2,012,131
<p><code>fp.readlines()</code> method returns a list of lines. Therefore you can't apply <code>[-50:]</code> operator. </p>
0
2010-01-06T10:10:24Z
[ "python" ]
How to write last 50 lines from one file to another Python
2,012,118
<p>I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?</p> <pre><code>def email_success(): fp = open(results_file, 'r') sum_file = (fp.readlines()[-50:]) fp.clos...
0
2010-01-06T10:05:32Z
2,012,135
<p>It's open() raising the exception though... how did you define result_summary?</p>
2
2010-01-06T10:11:06Z
[ "python" ]
How to write last 50 lines from one file to another Python
2,012,118
<p>I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?</p> <pre><code>def email_success(): fp = open(results_file, 'r') sum_file = (fp.readlines()[-50:]) fp.clos...
0
2010-01-06T10:05:32Z
2,012,137
<p><code>result_summary</code> is a tuple, it needs to be either a <code>str</code> or buffer. Your explanation has nothing to do with the error you posted.</p>
2
2010-01-06T10:11:31Z
[ "python" ]
How to write last 50 lines from one file to another Python
2,012,118
<p>I am creating an email response to an overnight build, I want to get the last 50 lines from the results file and place it into a summary file. The code that I have done is below, can anyone help?</p> <pre><code>def email_success(): fp = open(results_file, 'r') sum_file = (fp.readlines()[-50:]) fp.clos...
0
2010-01-06T10:05:32Z
2,012,169
<pre><code>result_summary = (t, 'results_summary.txt') </code></pre> <p>and</p> <pre><code> myfile = open(result_summary,'w') </code></pre> <p>means</p> <pre><code> myfile = open((t, 'results_summary.txt'),'w') </code></pre> <p>which obviously won't work, try:</p> <pre><code> myfile = open(result_summary[1],'w') ...
1
2010-01-06T10:18:48Z
[ "python" ]
python games problem
2,012,341
<p>I have made a mine sweeper game which fully works except for one key point, which is:</p> <blockquote> <p>Allow the player to place flags on cells to mark mine locations and to remove flags. if a cell is marked with a flag the player can not activate it by clicking on it.</p> </blockquote> <p>Now I don...
-1
2010-01-06T10:53:21Z
2,012,917
<p>If you're asking for a hint on how to do that, a safe way to go is to make the playfield a 2D array and each item of the array an object holding certain information. (In your case that's probably a boolean telling whether the cell contains a mine and a boolean telling whether the cell is marked or not.)</p>
0
2010-01-06T12:38:55Z
[ "python" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
2,012,631
<p>How about:</p> <pre><code>&gt;&gt;&gt; any(isinstance(e, int) and e &gt; 0 for e in [1,2,'joe']) True </code></pre> <p>It also works with <code>all()</code> of course:</p> <pre><code>&gt;&gt;&gt; all(isinstance(e, int) and e &gt; 0 for e in [1,2,'joe']) False </code></pre>
77
2010-01-06T11:43:32Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
2,012,640
<p>Yo should use a "generator expression" - that is, a language construct that can consume iterators and apply filter and expressions on then on a single line:</p> <p>For example <code>(i ** 2 for i in xrange(10))</code> is a generator for the square of the first 10 natural numbers (0 to 9)</p> <p>They also allow a...
7
2010-01-06T11:45:16Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
2,012,710
<p><strong>any</strong> function returns True when any condition is True.</p> <pre><code>&gt;&gt;&gt; any(isinstance(e, int) and e &gt; 0 for e in [0 ,0, 1]) True # Returns True because 1 is greater than 0. &gt;&gt;&gt; any(isinstance(e, int) and e &gt; 0 for e in [0 ,0, 0]) False # Returns False because not a singl...
17
2010-01-06T11:54:44Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
2,014,566
<p>filter can work, plus it returns you the matching elements</p> <pre><code>&gt;&gt;&gt; filter(lambda e: isinstance(e, int) and e &gt; 0, [1,2,'joe']) [1, 2] </code></pre>
2
2010-01-06T16:48:38Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
19,868,175
<p>While the others gave good Pythonic answers (I'd just use the accepted answer in most cases), I just wanted to point out how easy it is to make your own utility function to do this yourself if you really prefer it:</p> <pre><code>def any_lambda(iterable, function): return any(function(i) for i in iterable) In [1...
4
2013-11-08T20:43:45Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
22,394,957
<p>Slight improvement to Antoine P's answer</p> <pre><code>&gt;&gt;&gt; any(type(e) is int for e in [1,2,'joe']) True </code></pre> <p>For <code>all()</code></p> <pre><code>&gt;&gt;&gt; all(type(e) is int for e in [1,2,'joe']) False </code></pre>
5
2014-03-14T02:20:43Z
[ "python", "functional-programming", "callback", "any" ]
any() function in Python with a callback
2,012,611
<p>The Python standard library defines an <a href="http://docs.python.org/library/functions.html#any"><code>any()</code></a> function that</p> <blockquote> <p>Return True if any element of the iterable is true. If the iterable is empty, return False.</p> </blockquote> <p>It checks only if the elements evaluate to <...
37
2010-01-06T11:39:35Z
28,413,945
<p>You can use a combination of <code>any</code> and <code>map</code> if you really want to keep your lambda notation like so :</p> <pre><code>any(map(lambda e: isinstance(e, int) and e &gt; 0, [1, 2, 'joe'])) </code></pre> <p>But it is better to use a generator expression because it will not build the whole list twi...
1
2015-02-09T16:02:51Z
[ "python", "functional-programming", "callback", "any" ]
Deleting files which start with a name Python
2,012,670
<p>I have a few files I want to delete, they have the same name at the start but have different version numbers. Does anyone know how to delete files using the start of their name? </p> <pre><code>Eg. version_1.1 version_1.2 </code></pre> <p>Is there a way of delting any file that starts with the name version?</p> <...
3
2010-01-06T11:48:52Z
2,012,706
<p>In which language?</p> <p>In bash (Linux / Unix) you could use:</p> <pre><code>rm version* </code></pre> <p>or in batch (Windows / DOS) you could use:</p> <pre><code>del version* </code></pre> <p>If you want to write something to do this in Python it would be fairly easy - just look at the documentation for reg...
3
2010-01-06T11:53:38Z
[ "python" ]
Deleting files which start with a name Python
2,012,670
<p>I have a few files I want to delete, they have the same name at the start but have different version numbers. Does anyone know how to delete files using the start of their name? </p> <pre><code>Eg. version_1.1 version_1.2 </code></pre> <p>Is there a way of delting any file that starts with the name version?</p> <...
3
2010-01-06T11:48:52Z
2,012,718
<pre><code>import os, glob for filename in glob.glob("mypath/version*"): os.remove(filename) </code></pre> <p>Substitute the correct path (or <code>.</code> (= current directory)) for <code>mypath</code>. And make sure you don't get the path wrong :)</p> <p>This will raise an Exception if a file is currently in ...
9
2010-01-06T11:56:47Z
[ "python" ]
Deleting files which start with a name Python
2,012,670
<p>I have a few files I want to delete, they have the same name at the start but have different version numbers. Does anyone know how to delete files using the start of their name? </p> <pre><code>Eg. version_1.1 version_1.2 </code></pre> <p>Is there a way of delting any file that starts with the name version?</p> <...
3
2010-01-06T11:48:52Z
2,012,744
<p>If you really want to use Python, you can just use a combination of <a href="http://docs.python.org/library/os.html#os.listdir" rel="nofollow">os.listdir()</a>, which returns a listing of all the files in a certain directory, and <a href="http://docs.python.org/library/os.html#os.remove" rel="nofollow">os.remove()</...
2
2010-01-06T12:03:31Z
[ "python" ]
Deleting files which start with a name Python
2,012,670
<p>I have a few files I want to delete, they have the same name at the start but have different version numbers. Does anyone know how to delete files using the start of their name? </p> <pre><code>Eg. version_1.1 version_1.2 </code></pre> <p>Is there a way of delting any file that starts with the name version?</p> <...
3
2010-01-06T11:48:52Z
2,012,792
<pre><code>import os os.chdir("/home/path") for file in os.listdir("."): if os.path.isfile(file) and file.startswith("version"): try: os.remove(file) except Exception,e: print e </code></pre>
0
2010-01-06T12:13:48Z
[ "python" ]
Polluting a class's environment
2,012,698
<p>I have an object that holds lots of ids that are accessed statically. I want to split that up into another object which holds only those ids without the need of making modifications to the already existen code base. Take for example:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGIN...
1
2010-01-06T11:52:18Z
2,012,726
<p>Something like:</p> <pre><code>class Car(object): for attr, value in _CarType.__dict__.items(): it not attr.startswith('_'): locals()[attr] = value del attr, value </code></pre> <p>Or you can do it out of the class declaration:</p> <pre><code>class Car(object): # snip for attr, va...
3
2010-01-06T11:57:55Z
[ "python", "oop" ]
Polluting a class's environment
2,012,698
<p>I have an object that holds lots of ids that are accessed statically. I want to split that up into another object which holds only those ids without the need of making modifications to the already existen code base. Take for example:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGIN...
1
2010-01-06T11:52:18Z
2,012,800
<p>Although this is not exactly what subclassing is made for, it accomplishes what you describe:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGINE = 1 # lots of these ids class Car(_CarType): types = _CarType </code></pre>
4
2010-01-06T12:15:54Z
[ "python", "oop" ]
Polluting a class's environment
2,012,698
<p>I have an object that holds lots of ids that are accessed statically. I want to split that up into another object which holds only those ids without the need of making modifications to the already existen code base. Take for example:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGIN...
1
2010-01-06T11:52:18Z
2,012,849
<p>This is how you could do this with a metaclass:</p> <pre><code>class _CarType(type): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGINE = 1 # lots of these ids def __init__(self,name,bases,dct): for key in dir(_CarType): if key.isupper(): setattr(self,key,getattr(_CarType,key)) cl...
2
2010-01-06T12:27:41Z
[ "python", "oop" ]
Polluting a class's environment
2,012,698
<p>I have an object that holds lots of ids that are accessed statically. I want to split that up into another object which holds only those ids without the need of making modifications to the already existen code base. Take for example:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGIN...
1
2010-01-06T11:52:18Z
2,012,902
<pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGINE = 1 # lots of these ids class Car: types = _CarType def __getattr__(self, name): return getattr(self.types, name) </code></pre> <p>If an attribute of an object is not found, and it defines that magic method <code>__getattr__</code...
0
2010-01-06T12:35:34Z
[ "python", "oop" ]
Polluting a class's environment
2,012,698
<p>I have an object that holds lots of ids that are accessed statically. I want to split that up into another object which holds only those ids without the need of making modifications to the already existen code base. Take for example:</p> <pre><code>class _CarType(object): DIESEL_CAR_ENGINE = 0 GAS_CAR_ENGIN...
1
2010-01-06T11:52:18Z
2,014,624
<p>Your options fall into two substantial categories: you either copy the attributes from <code>_CarType</code> into <code>Car</code>, or set <code>Car</code>'s metaclass to a custom one with a <code>__getattr__</code> method that delegates to <code>_CarType</code> (so it isn't exactly true that you can't use <code>__g...
2
2010-01-06T16:56:21Z
[ "python", "oop" ]
writing a custom anaylzer in pylucene/inheritance using jcc?
2,012,843
<p>I want to write a custom analyzer in pylucene. Usually in java lucene , when you write a analyzer class , your class inherits lucene's Analyzer class.</p> <p>but pylucene uses jcc , the java to c++/python compiler. </p> <p>So how do you let a python class inherit from a java class using jcc ,and especially how ...
2
2010-01-06T12:26:53Z
2,399,239
<p>You can inherit from any class in pylucene, but the ones with names that start with <strong>Python</strong> will also <a href="http://lucene.apache.org/pylucene/documentation/readme.html#extensions" rel="nofollow">extend the underlying Java class</a>, i.e., make the relevant methods "virtual" when called from java c...
0
2010-03-08T04:10:09Z
[ "python", "pylucene", "jcc" ]
writing a custom anaylzer in pylucene/inheritance using jcc?
2,012,843
<p>I want to write a custom analyzer in pylucene. Usually in java lucene , when you write a analyzer class , your class inherits lucene's Analyzer class.</p> <p>but pylucene uses jcc , the java to c++/python compiler. </p> <p>So how do you let a python class inherit from a java class using jcc ,and especially how ...
2
2010-01-06T12:26:53Z
9,557,574
<p>Here's an example of an Analyzer that wraps the EdgeNGram Filter.</p> <pre><code>import lucene class EdgeNGramAnalyzer(lucene.PythonAnalyzer): ''' This is an example of a custom Analyzer (in this case an edge-n-gram analyzer) EdgeNGram Analyzers are good for type-ahead ''' def __init__(self, si...
0
2012-03-04T18:19:53Z
[ "python", "pylucene", "jcc" ]
Trying to get a solid Python install working on my Mac?
2,012,959
<p>I have Mac OSX 10.5.8 with Xcode installed. I want to avoid MacPorts and want to just get a solid Python install foundation so I can then move on to mess with Django and other things. I want to use Buildout with my Python applications.</p> <p>I have installed binary Python 2.6.4 from the official site and installed...
3
2010-01-06T12:49:04Z
2,013,029
<p>Personally, what I do is leave my system python completely as is.</p> <p>I use the following to install 2.4, 2.5 and 2.6 versions of Python:</p> <p>Use a buildout from the plone collective to get python2.{4,5,6} installed with easy_install and PIL (including libjpeg support) </p> <p>Checkout the python buildout f...
0
2010-01-06T13:01:55Z
[ "python", "osx", "unix", "installation", "buildout" ]
Trying to get a solid Python install working on my Mac?
2,012,959
<p>I have Mac OSX 10.5.8 with Xcode installed. I want to avoid MacPorts and want to just get a solid Python install foundation so I can then move on to mess with Django and other things. I want to use Buildout with my Python applications.</p> <p>I have installed binary Python 2.6.4 from the official site and installed...
3
2010-01-06T12:49:04Z
2,013,030
<p>PirosBOX:~ piros$ python -V Python 2.5.1</p> <p>Have OSX 10.5.8 and i write django apps fine :D The important thing is to get the SVN release of Django, better!</p>
0
2010-01-06T13:01:57Z
[ "python", "osx", "unix", "installation", "buildout" ]
Trying to get a solid Python install working on my Mac?
2,012,959
<p>I have Mac OSX 10.5.8 with Xcode installed. I want to avoid MacPorts and want to just get a solid Python install foundation so I can then move on to mess with Django and other things. I want to use Buildout with my Python applications.</p> <p>I have installed binary Python 2.6.4 from the official site and installed...
3
2010-01-06T12:49:04Z
2,013,101
<p>Firstly, there's absolutely no need to install a new version of Python to work on Django in Leopard. The stock Python 2.5 works absolutely fine, and Django is 100% compatible with that version.</p> <p>Secondly, if you do want to use virtualenv with a different version of Python other than the system default, you si...
2
2010-01-06T13:14:28Z
[ "python", "osx", "unix", "installation", "buildout" ]
Trying to get a solid Python install working on my Mac?
2,012,959
<p>I have Mac OSX 10.5.8 with Xcode installed. I want to avoid MacPorts and want to just get a solid Python install foundation so I can then move on to mess with Django and other things. I want to use Buildout with my Python applications.</p> <p>I have installed binary Python 2.6.4 from the official site and installed...
3
2010-01-06T12:49:04Z
2,015,030
<p>When you install a new Python instance, you also need to install a new copy of <code>easy_install</code> for it. Follow the instructions for either the classic <a href="http://pypi.python.org/pypi/setuptools" rel="nofollow">setuptools</a> version or the newer <a href="http://pypi.python.org/pypi/distribute" rel="no...
1
2010-01-06T17:52:55Z
[ "python", "osx", "unix", "installation", "buildout" ]
Trying to get a solid Python install working on my Mac?
2,012,959
<p>I have Mac OSX 10.5.8 with Xcode installed. I want to avoid MacPorts and want to just get a solid Python install foundation so I can then move on to mess with Django and other things. I want to use Buildout with my Python applications.</p> <p>I have installed binary Python 2.6.4 from the official site and installed...
3
2010-01-06T12:49:04Z
2,015,573
<p>I found the following two links particularly useful when I had to do it:</p> <p><a href="http://jessenoller.com/2009/03/16/so-you-want-to-use-python-on-the-mac/" rel="nofollow">http://jessenoller.com/2009/03/16/so-you-want-to-use-python-on-the-mac/</a></p> <p><a href="http://blog.captnswing.net/2009/04/19/python-m...
0
2010-01-06T19:26:07Z
[ "python", "osx", "unix", "installation", "buildout" ]