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 |
|---|---|---|---|---|---|---|---|---|---|
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 1,024,852 | <p>Yeah, it's pretty easy. Just do the following:</p>
<pre><code>dict["key"] = "value"
</code></pre>
| 95 | 2009-06-21T22:09:09Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 1,165,836 | <pre><code>>>> x = {1:2}
>>> print x
{1: 2}
>>> x.update({3:4})
>>> print x
{1: 2, 3: 4}
</code></pre>
| 658 | 2009-07-22T14:48:17Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 8,381,589 | <p>I feel like consolidating info about Python dictionaries:</p>
<pre><code>### Making a dictionary ###
data = {}
# OR
data = dict()
### Initially adding values ###
data = {'a':1,'b':2,'c':3}
# OR
data = dict(a=1, b=2, c=3)
### Inserting/Updating value ###
data['a']=1 # updates if 'a' exists, else adds 'a'
# OR
... | 268 | 2011-12-05T06:07:36Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 8,381,704 | <pre><code>data = {}
data['a'] = 'A'
data['b'] = 'B'
for key, value in data.iteritems():
print "%s-%s" % (key, value)
</code></pre>
<p>results in </p>
<pre><code>a-A
b-B
</code></pre>
| 12 | 2011-12-05T06:26:24Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 10,339,748 | <p>If you want to add a dictionary within a dictionary you can do it this way. </p>
<p>Example: Add a new entry to your dictionary & sub dictionary</p>
<pre><code>dictionary = {}
dictionary["new key"] = "some new entry" # add new dictionary entry
dictionary["dictionary_within_a_dictionary"] = {} # this is require... | 26 | 2012-04-26T19:04:33Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 15,994,607 | <p>The orthodox syntax is <code>d[key] = value</code>, but if your keyboard is missing the square bracket keys you could do:</p>
<pre><code>d.__setitem__(key, value)
</code></pre>
<p>In fact, defining <code>__getitem__</code> and <code>__setitem__</code> methods is how you can make your own class support the square ... | 24 | 2013-04-14T00:58:27Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 16,750,088 | <p>you can create one</p>
<pre><code>class myDict(dict):
def __init__(self):
self = dict()
def add(self, key, value):
self[key] = value
## example
myd = myDict()
myd.add('apples',6)
myd.add('bananas',3)
print(myd)
</code></pre>
<p>gives</p>
<pre><code>>>>
{'apples': 6, 'bananas'... | 17 | 2013-05-25T13:33:35Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 18,294,122 | <p><a href="http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression">This popular question</a> addresses <em>functional</em> methods of merging dictionaries <code>a</code> and <code>b</code>.</p>
<p>Here are some of the more straightforward methods (tested in Pytho... | 14 | 2013-08-17T23:04:12Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 27,208,535 | <blockquote>
<h1>"Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an .add() method."</h1>
</blockquote>
<p>Yes it is possible, and it does have a method that implements this, but you don't want to use it directly.</p>
<p>To demonstrate how and how not to use it,... | 42 | 2014-11-29T23:57:59Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 30,541,405 | <p>This is exactly how I would do it:
# fixed data with sapce</p>
<pre><code>data = {}
data['f'] = 'F'
data['c'] = 'C'
for key, value in data.iteritems():
print "%s-%s" % (key, value)
</code></pre>
<p>This works for me. Enjoy!</p>
| 5 | 2015-05-30T01:52:47Z | [
"python",
"dictionary"
] |
Add key to a dictionary in Python? | 1,024,847 | <p>Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an <code>.add()</code> method.</p>
| 1,201 | 2009-06-21T22:07:39Z | 37,935,160 | <p>we can add new keys to dictionary by this way:</p>
<blockquote>
<p>Dictionary_Name[New_Key_Name] = New_Key_Value</p>
</blockquote>
<p>Here is the Example:</p>
<pre><code># This is my dictionary
my_dict = {'Key1': 'Value1', 'Key2': 'Value2'}
# Now add new key in my dictionary
my_dict['key3'] = 'Value3'
# Print u... | 1 | 2016-06-21T03:39:57Z | [
"python",
"dictionary"
] |
How do I install GASP for Python 2.6.2 on a Mac | 1,024,862 | <p>I'm currently trying to learn Python and am going through How to Think Like a Computer Scientist: Learning With Python. I have installed Python 2.6.2 on Mac OSX 10.4.11 and am using the IDLE.</p>
<p>At the end of chapter 4 Elkner et al. refer to GASP. However their instructions don't work as when I enter:</p>
<pre... | 1 | 2009-06-21T22:14:07Z | 1,024,868 | <p>This is actually somewhat of a coincidence; I'm one of the packagers of GASP. On our <a href="http://dev.laptop.org/pub/gasp/downloads/" rel="nofollow">download page</a>, which is linked by our main <a href="https://edge.launchpad.net/gasp-code" rel="nofollow">project page</a>, there are instructions on how to insta... | 1 | 2009-06-21T22:18:10Z | [
"python",
"pyobjc",
"gasp"
] |
How do I install GASP for Python 2.6.2 on a Mac | 1,024,862 | <p>I'm currently trying to learn Python and am going through How to Think Like a Computer Scientist: Learning With Python. I have installed Python 2.6.2 on Mac OSX 10.4.11 and am using the IDLE.</p>
<p>At the end of chapter 4 Elkner et al. refer to GASP. However their instructions don't work as when I enter:</p>
<pre... | 1 | 2009-06-21T22:14:07Z | 7,506,693 | <p>Well everybody, sorry for the incomplete sentences and overall poor English but I wanted to make this simple to read and understand for someone who is completely inexperienced in any sort of programming, as I am (very first day messing with this stuff, e.g., terminal). This is the result of hours of Googling that w... | 2 | 2011-09-21T21:10:48Z | [
"python",
"pyobjc",
"gasp"
] |
How do I install GASP for Python 2.6.2 on a Mac | 1,024,862 | <p>I'm currently trying to learn Python and am going through How to Think Like a Computer Scientist: Learning With Python. I have installed Python 2.6.2 on Mac OSX 10.4.11 and am using the IDLE.</p>
<p>At the end of chapter 4 Elkner et al. refer to GASP. However their instructions don't work as when I enter:</p>
<pre... | 1 | 2009-06-21T22:14:07Z | 11,143,564 | <p>This is an interesting problem faced by most of the readers using "How to Think Like a Computer Scientist : Learning with Python", when they reach 4th chapter.</p>
<p>Now to install GASP, you need to check whether you have python installed on your machine.</p>
<p>Assumption: I am going to assume that you are using... | 0 | 2012-06-21T17:31:22Z | [
"python",
"pyobjc",
"gasp"
] |
What's the best way to implement web service for ajax autocomplete | 1,025,018 | <p>I'm implementing a "Google Suggest" like autocomplete feature for tag searching using jQuery's autocomplete. </p>
<p>I need to provide a web service to jQuery giving it a list of suggestions based on what the user has typed. I see 2 ways of implementing the web service:</p>
<p>1) just store all the tags in a datab... | 2 | 2009-06-21T23:31:10Z | 1,025,027 | <p>I would use the first option. 'KISS' - (Keep It Simple Stupid).</p>
<p>For small amounts of data there shouldn't be much latency. We run the same kind of thing for a name search and results appear pretty quickly on a few thousand rows.</p>
<p>Hope that helps,</p>
<p>Josh</p>
| 1 | 2009-06-21T23:36:37Z | [
"python",
"ajax",
"autocomplete",
"trie"
] |
What's the best way to implement web service for ajax autocomplete | 1,025,018 | <p>I'm implementing a "Google Suggest" like autocomplete feature for tag searching using jQuery's autocomplete. </p>
<p>I need to provide a web service to jQuery giving it a list of suggestions based on what the user has typed. I see 2 ways of implementing the web service:</p>
<p>1) just store all the tags in a datab... | 2 | 2009-06-21T23:31:10Z | 1,025,355 | <p>Don't be concerned about latency before you <em>measure</em> things -- make up a bunch of pseudo-tags, stick them in the DB, and measure latencies for typical queries. Depending on your DB setup, your latency may be just fine and you're spared wasted worries.</p>
<p><em>Do</em> always worry about threading, though ... | 4 | 2009-06-22T03:16:08Z | [
"python",
"ajax",
"autocomplete",
"trie"
] |
Django Context Processor Trouble | 1,025,025 | <p>So I am just starting out on learning Django, and I'm attempting to complete one of the sample applications from the book. I'm getting stuck now on creating DRY URL's. More specifically, I cannot get my context processor to work. I create my context processor as so:</p>
<pre><code>from django.conf import settings
#... | 1 | 2009-06-21T23:35:27Z | 1,025,210 | <p><code>TEMPLATE_CONTEXT_PROCESSORS</code> should contain a list of callable objects, not modules. List the actual functions that will transform the template contexts. <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#id1" rel="nofollow">Link to docs</a>. </p>
| 4 | 2009-06-22T01:26:06Z | [
"python",
"django",
"django-urls"
] |
How to use win32 API's with python? | 1,025,029 | <p>How can I use win32 API in Python?<br>
What is the best and easiest way to do it?<br>
Can you please provide some examples?</p>
| 45 | 2009-06-21T23:37:04Z | 1,025,056 | <p>PyWin32, as mentioned by @chaos, is probably the most popular choice; the alternative is <a href="http://docs.python.org/library/ctypes.html">ctypes</a> which is part of Python's standard library. For example, <code>print ctypes.windll.kernel32.GetModuleHandleA(None)</code> will show the module-handle of the current... | 15 | 2009-06-21T23:51:31Z | [
"python",
"winapi",
"api"
] |
How to use win32 API's with python? | 1,025,029 | <p>How can I use win32 API in Python?<br>
What is the best and easiest way to do it?<br>
Can you please provide some examples?</p>
| 45 | 2009-06-21T23:37:04Z | 1,025,331 | <p>PyWin32 is the way to go - but how to use it? One approach is to begin with a concrete problem you're having and attempting to solve it. PyWin32 provides bindings for the Win32 API functions for which there are many, and you really have to pick a specific goal first.</p>
<p>In my Python 2.5 installation (ActiveStat... | 32 | 2009-06-22T03:04:13Z | [
"python",
"winapi",
"api"
] |
How to use win32 API's with python? | 1,025,029 | <p>How can I use win32 API in Python?<br>
What is the best and easiest way to do it?<br>
Can you please provide some examples?</p>
| 45 | 2009-06-21T23:37:04Z | 11,831,060 | <p>The important functions that you can to use in win32 Python are the message boxes, this is classical example of OK or Cancel.</p>
<pre><code>result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)
if result == 1:
print 'Ok'
elif result == 2:
print 'cancel'
</code></pre>
<p>The ... | 2 | 2012-08-06T15:16:13Z | [
"python",
"winapi",
"api"
] |
Finding the parent tag of a text string with ElementTree/lxml | 1,025,129 | <p>I'm trying to take a string of text, and "extract" the rest of the text in the paragraph/document from the html.</p>
<p>My current is approach is trying to find the "parent tag" of the string in the html that has been parsed with lxml. (if you know of a better way to tackle this problem, I'm all ears!)</p>
<p>For ... | 2 | 2009-06-22T00:29:02Z | 1,025,206 | <p>This is a simple way to do it with ElementTree. It does require that your HTML input is valid XML (so I have added the appropriate end tags to your HTML):</p>
<pre><code>import elementtree.ElementTree as ET
html = """<html>
<head>
</head>
<body>
<div>
<p>TEXT STRING HERE ......&... | 3 | 2009-06-22T01:19:18Z | [
"python",
"lxml",
"elementtree"
] |
Units conversion in Python | 1,025,145 | <p><a href="http://en.wikipedia.org/wiki/SymPy">SymPy</a> is a great tool for doing units conversions in Python:</p>
<pre><code>>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
</code></pre>
<p>You can easily roll your own:</p>
<pre><code>>>> units.BTU =... | 11 | 2009-06-22T00:41:10Z | 1,025,173 | <p>The Unum documentation has a pretty good writeup on why this is hard:</p>
<blockquote>
<p>Unum is unable to handle reliably conversions between °Celsius and Kelvin. The issue is referred as the 'false origin problem' : the 0°Celsius is defined as 273.15 K. This is really a special and annoying case, since in ge... | 8 | 2009-06-22T01:00:47Z | [
"python",
"math",
"symbolic-math",
"sympy"
] |
Units conversion in Python | 1,025,145 | <p><a href="http://en.wikipedia.org/wiki/SymPy">SymPy</a> is a great tool for doing units conversions in Python:</p>
<pre><code>>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
</code></pre>
<p>You can easily roll your own:</p>
<pre><code>>>> units.BTU =... | 11 | 2009-06-22T00:41:10Z | 1,025,184 | <p>I personally like <a href="http://packages.python.org/quantities/" rel="nofollow">Quantities</a> thanks to its <a href="http://en.wikipedia.org/wiki/NumPy" rel="nofollow">NumPy</a> integration, however it only does relative temperatures, not absolute.</p>
| 4 | 2009-06-22T01:07:29Z | [
"python",
"math",
"symbolic-math",
"sympy"
] |
Units conversion in Python | 1,025,145 | <p><a href="http://en.wikipedia.org/wiki/SymPy">SymPy</a> is a great tool for doing units conversions in Python:</p>
<pre><code>>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
</code></pre>
<p>You can easily roll your own:</p>
<pre><code>>>> units.BTU =... | 11 | 2009-06-22T00:41:10Z | 1,025,258 | <p>Example, how it could work:</p>
<pre><code>>>> T(0*F) + 10*C
T(265.37222222222221*K) # or T(47767/180*K)
>>> T(0*F + 10*C)
T(283.15*K)
>>> 0*F + T(10*C)
T(283.15*K)
>>> 0*F + 10*C
10*K
>>> T(0*F) + T(10*C)
Traceback (most recent call last):
File "<stdin>", line 1, ... | 0 | 2009-06-22T02:04:54Z | [
"python",
"math",
"symbolic-math",
"sympy"
] |
Units conversion in Python | 1,025,145 | <p><a href="http://en.wikipedia.org/wiki/SymPy">SymPy</a> is a great tool for doing units conversions in Python:</p>
<pre><code>>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
</code></pre>
<p>You can easily roll your own:</p>
<pre><code>>>> units.BTU =... | 11 | 2009-06-22T00:41:10Z | 25,211,381 | <p>The <a href="http://kdavies4.github.io/natu/" rel="nofollow">natu</a> package handles units of temperature. For instance, you can do this:</p>
<pre><code>>>> from natu.units import K, degC, degF
>>> T = 25*degC
>>> T/K
298.1500
>>> T/degF
77.0000
>>> 0*degC + 100*K
100.0... | 0 | 2014-08-08T20:01:45Z | [
"python",
"math",
"symbolic-math",
"sympy"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 1,025,230 | <p>My mistake: just found this <a href="http://docs.python.org/library/optparse.html#callback-example-6-variable-arguments">Callback Example 6</a>.</p>
| 9 | 2009-06-22T01:40:37Z | [
"python",
"optparse"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 1,025,232 | <p>I believe <code>optparse</code> does not support what you require (not directly -- as you noticed, you can do it if you're willing to do all the extra work of a callback!-). You could also do it most simply with the third-party extension <a href="http://code.google.com/p/argparse/">argparse</a>, which does support v... | 8 | 2009-06-22T01:41:19Z | [
"python",
"optparse"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 1,025,239 | <p>Wouldn't you be better off with this?</p>
<pre><code>foo.pl --files a.txt,b.txt,c.txt --verbose
</code></pre>
| 1 | 2009-06-22T01:48:31Z | [
"python",
"optparse"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 2,205,552 | <p>This took me a little while to figure out, but you can use the callback action to your options to get this done. Checkout how I grab an arbitrary number of args to the "--file" flag in this example.</p>
<pre><code>from optparse import OptionParser,
def cb(option, opt_str, value, parser):
args=[]
fo... | 20 | 2010-02-05T07:06:31Z | [
"python",
"optparse"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 15,162,881 | <p>Here's one way: Take the fileLst generating string in as a string and then use <a href="http://docs.python.org/2/library/glob.html" rel="nofollow">http://docs.python.org/2/library/glob.html</a> to do the expansion ugh this might not work without escaping the *</p>
<p>Actually, got a better way:
python myprog.py -V ... | 0 | 2013-03-01T16:57:35Z | [
"python",
"optparse"
] |
With Python's optparse module, how do you create an option that takes a variable number of arguments? | 1,025,214 | <p>With Perl's <code>Getopt::Long</code> you can easily define command-line options that take a variable number of arguments:</p>
<pre><code>foo.pl --files a.txt --verbose
foo.pl --files a.txt b.txt c.txt --verbose
</code></pre>
<p>Is there a way to do this directly with Python's <code>optparse</code> mod... | 10 | 2009-06-22T01:28:04Z | 30,767,994 | <p>I recently has this issue myself: I was on Python 2.6 and needed an option to take a variable number of arguments. I tried to use Dave's solution but found that it wouldn't work without also explicitly setting nargs to 0.</p>
<pre><code>def arg_list(option, opt_str, value, parser):
args = set()
for arg in p... | 0 | 2015-06-10T21:55:18Z | [
"python",
"optparse"
] |
How do I build a custom "list-type" entry to request.POST | 1,025,216 | <p>Basically I have a model with a ManyToMany field, and then a modelform derived from that model where that field is rendered as a "multiple choice" selectbox. In my template I'm having that field omitted, electing instead to prepare the values for that field in the view, then pass those prepared values into request.... | 0 | 2009-06-22T01:29:44Z | 1,026,038 | <p>It's really not clear what you're trying to do here, but I doubt that hacking the QueryDict is the right way to achieve it.</p>
<p>If you are trying to customise the display of the not_bases field, you can simply override the definition in your modelform declaration:</p>
<pre><code>class MyModelForm(forms.ModelFor... | 2 | 2009-06-22T08:15:46Z | [
"python",
"django"
] |
Disable GNOME's automount with Python | 1,025,244 | <p>I need to stop GNOME/Nautilus from automagically mounting new devices and partitions as they appear to the system. How can I accomplish this in python?</p>
| 1 | 2009-06-22T01:54:08Z | 1,025,369 | <p>Why would do it in Python? You can just use the commandline, as in:</p>
<pre><code>gconftool-2 --type bool --set /apps/nautilus/preferences/media_automount false
</code></pre>
<p>If you really need it to be in Python, then you can use the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subp... | 3 | 2009-06-22T03:27:13Z | [
"python",
"gnome",
"hal",
"automount"
] |
dual iterator in one python object | 1,025,348 | <p>In python, I am trying to write a class that support two different kind of iterator. Roughly speaking, this object contains a matrix of data and I want to have two different kind of iterator to support row iteration and column iteration. </p>
| 2 | 2009-06-22T03:12:19Z | 1,025,362 | <p>Okay, so make two separate methods where each is a generator.</p>
<pre><code>class Matrix(object):
def iter_rows(self):
for row in self.rows:
yield row
def iter_columns(self):
for column in self.columns:
yield column
</code></pre>
<p>Your <code>__iter__</code> could... | 2 | 2009-06-22T03:21:37Z | [
"python",
"iterator"
] |
dual iterator in one python object | 1,025,348 | <p>In python, I am trying to write a class that support two different kind of iterator. Roughly speaking, this object contains a matrix of data and I want to have two different kind of iterator to support row iteration and column iteration. </p>
| 2 | 2009-06-22T03:12:19Z | 1,025,366 | <p>Is this what you're looking for?</p>
<pre><code>class Matrix(object):
def __init__(self, rows):
self._rows = rows
def columns(self):
return zip(*self._rows)
def rows(self):
return self._rows
# Create a Matrix by providing rows.
m = Matrix([[1,2,3],
[4,5,6],
... | 4 | 2009-06-22T03:24:35Z | [
"python",
"iterator"
] |
dual iterator in one python object | 1,025,348 | <p>In python, I am trying to write a class that support two different kind of iterator. Roughly speaking, this object contains a matrix of data and I want to have two different kind of iterator to support row iteration and column iteration. </p>
| 2 | 2009-06-22T03:12:19Z | 1,025,384 | <p><code>dict</code> has several iterator-producing methods -- <code>iterkeys</code>, <code>itervalues</code>, <code>iteritems</code> -- and so should your class. If there's one "most natural" way of iterating, you should also alias it to <code>__iter__</code> for convenience and readability (that's probably going to b... | 5 | 2009-06-22T03:35:57Z | [
"python",
"iterator"
] |
Decimal alignment formatting in Python | 1,025,379 | <p>This <em>should</em> be easy.</p>
<p>Here's my array (rather, a method of generating representative test arrays):</p>
<pre><code>>>> ri = numpy.random.randint
>>> ri2 = lambda x: ''.join(ri(0,9,x).astype('S'))
>>> a = array([float(ri2(x)+ '.' + ri2(y)) for x,y in ri(1,10,(10,2))])
>&g... | 5 | 2009-06-22T03:34:16Z | 1,025,528 | <p>Sorry, but after thorough investigation I can't find any way to perform the task you require without a minimum of post-processing (to strip off the trailing zeros you don't want to see); something like:</p>
<pre><code>import re
ut0 = re.compile(r'(\d)0+$')
thelist = [ut0.sub(r'\1', "%12f" % x) for x in a]
print '... | 7 | 2009-06-22T05:07:44Z | [
"python",
"formatting",
"numpy",
"code-golf"
] |
Decimal alignment formatting in Python | 1,025,379 | <p>This <em>should</em> be easy.</p>
<p>Here's my array (rather, a method of generating representative test arrays):</p>
<pre><code>>>> ri = numpy.random.randint
>>> ri2 = lambda x: ''.join(ri(0,9,x).astype('S'))
>>> a = array([float(ri2(x)+ '.' + ri2(y)) for x,y in ri(1,10,(10,2))])
>&g... | 5 | 2009-06-22T03:34:16Z | 1,025,729 | <p>Pythons string formatting can both print out only the necessary decimals (with %g) or use a fixed set of decimals (with %f). However, you want to print out only the necessary decimals, except if the number is a whole number, then you want one decimal, and that makes it complex.</p>
<p>This means you would end up wi... | 1 | 2009-06-22T06:33:05Z | [
"python",
"formatting",
"numpy",
"code-golf"
] |
sqlite version for python26 | 1,025,493 | <p>which versions of sqlite may best suite for python 2.6.2?</p>
| 2 | 2009-06-22T04:49:30Z | 1,025,591 | <p>If your Python distribution already comes with a copy of sqlite (such as the Windows distribution, or Debian), this is the version you should use.</p>
<p>If you compile sqlite yourself, you should use the version that is recommended by the <a href="http://www.sqlite.org/" rel="nofollow">sqlite authors</a> (currentl... | 9 | 2009-06-22T05:36:23Z | [
"python",
"sqlite",
"python-2.6"
] |
sqlite version for python26 | 1,025,493 | <p>which versions of sqlite may best suite for python 2.6.2?</p>
| 2 | 2009-06-22T04:49:30Z | 1,028,006 | <p>I'm using 3.4.0 out of inertia (it's what came with the Python 2.* versions I'm using) but there's no real reason (save powerful inertia;-) to avoid upgrading to 3.4.2, which fixes a couple of bugs that could lead to DB corruption and introduces no incompatibilities that I know of. (If you stick with 3.4.0 I'm told ... | 0 | 2009-06-22T16:00:11Z | [
"python",
"sqlite",
"python-2.6"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,846 | <p>If you want an an HTML page to have some sort of server-side programming then you will need a webserver of some sort to do the processing.</p>
<p>My suggestion would be to get a web server running on your development box, or try to accomplish what you need to do with a local desktop application or script.</p>
| 1 | 2009-06-22T07:16:15Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,855 | <p>No you need some kind of server. Wh not try out the <a href="http://www.google.co.uk/search?q=usb%2Bweb%2Bserver&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a" rel="nofollow">portable webservers</a>? You can run them from your usb drive.</p>
| 0 | 2009-06-22T07:18:56Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,873 | <p>Python has a small built in Web server. If you already already got Python to run with the RS232 you might need to read <a href="http://fragments.turtlemeat.com/pythonwebserver.php" rel="nofollow">here</a> on how to set up a very simple and basic webserver.
An even easier one can look like <a href="http://effbot.org/... | 3 | 2009-06-22T07:24:29Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,901 | <p>Try also XML-RPC it gives you a simple way to pass remote procedure calls from YUI towards a simple XMLRPC server and from that towards your rs232 device</p>
| 0 | 2009-06-22T07:31:11Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,904 | <p>I see now that Daff mentioned the simple HTTP server, but I made an example on how you'd solve your problem (using <code>BaseHTTPServer</code>):</p>
<pre><code>import BaseHTTPServer
HOST_NAME = 'localhost'
PORT_NUMBER = 1337
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(s):
s.send... | 6 | 2009-06-22T07:31:26Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,025,929 | <p>another quick solution is https://addons.mozilla.org/en-US/firefox/addon/3002
POW, it's a firefox extension that adds a simple web server with Server Side JS built in. </p>
<p>You'd be able to access a command line and call a python script from there.</p>
| 1 | 2009-06-22T07:40:43Z | [
"python",
"html",
"browser"
] |
Call program from within a browser without using a webserver | 1,025,817 | <p>Is there a way to call a program (Python script) from a local HTML page?
I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)</p>
<p>Later, this will migrate to a server, but I... | 2 | 2009-06-22T07:03:31Z | 1,026,086 | <p>I see no reason why you can't setup a handler for .py/.bat/.vbs files in your browser. This should result in your chosen application running a script when you link to it. This won't work when you migrate to the server but as a testing platform it would work. Just remember to turn it off when you're done or you expos... | 0 | 2009-06-22T08:30:08Z | [
"python",
"html",
"browser"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,026,045 | <p>You can try starting with <a href="http://bitbucket.org/ubernostrum/django-registration/wiki/Home" rel="nofollow">django-registration</a>.</p>
<p>EDIT: You can probably hack something up on your own faster than learning Django. However, learning a framework will serve you better. You'll be able to easily ask a l... | 1 | 2009-06-22T08:18:39Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,026,046 | <p>django framework</p>
| 1 | 2009-06-22T08:18:41Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,026,169 | <p>Let me share my own experience with django. My prerequisits:</p>
<ul>
<li><p>average knowledge of python</p></li>
<li><p>very weak idea of how web works (no js skills, just a bit of css)</p></li>
<li><p>my day job is filled with coding in C and I just wanted to try something different,
so there certainly was a pas... | 4 | 2009-06-22T09:00:26Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,026,242 | <p>Django is the way to go. You can try it locally on your PC and see do you like it. It is very nice framework and allows you to quickly build your applications.</p>
<p>If you want to give Django quick go to see how it feels you can download <a href="http://www.portablepython.com" rel="nofollow">Portable Python</a> w... | 1 | 2009-06-22T09:20:34Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,026,260 | <p>For a more complete basic setup (with lots of preprogrammed features) I would point you at Pinax which is a web site on top of Django (which I praise of course, see the dedicated page on dreamhost Wiki at <a href="http://wiki.dreamhost.com/Django" rel="nofollow">http://wiki.dreamhost.com/Django</a>)</p>
<p>The intr... | 1 | 2009-06-22T09:25:21Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,027,704 | <p>Another voice to the choir.</p>
<p>Go for django. It's very good and easy to use.</p>
| 0 | 2009-06-22T15:02:30Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,027,850 | <p>There are several blog entries &c pointing out some problems with Python on Dreamhost and how to work around them to run several web frameworks that could suit you. (Most of the posts are over a year old so it may be that dreamhost has fixed some of the issues since then, of course, but the only way to really fi... | 2 | 2009-06-22T15:29:36Z | [
"python",
"dreamhost"
] |
How do I set up a basic website with registration in Python on Dreamhost? | 1,026,030 | <p>I need to write a basic website on Dreamhost. It needs to be done in Python.
I discovered Dreamhost permits me to write .py files, and read them.</p>
<h3>Example:</h3>
<pre><code>#!/usr/bin/python
print "Content-type: text/html\n\n"
print "hello world"
</code></pre>
<p>So now I am looking for a basic framework, o... | 2 | 2009-06-22T08:13:33Z | 1,092,696 | <p>I've noticed that a lot of people recommend Django. If you're running on a shared host on Dreamhost, the performance will not be satisfactory. </p>
<p>This is a known issue with Dreamhost shared hosting. I have installed web2py on my Dreamhost shared account and it seems to work okay; search the google groups for a... | 1 | 2009-07-07T14:33:39Z | [
"python",
"dreamhost"
] |
Django model query with custom select fields | 1,026,204 | <p>I'm using the row-level permission model known as django-granular-permissions (<a href="http://code.google.com/p/django-granular-permissions/" rel="nofollow">http://code.google.com/p/django-granular-permissions/</a>). The permission model simply has just two more fields which are content-type and object id.</p>
<p>... | 0 | 2009-06-22T09:12:19Z | 1,026,362 | <p>Normally you'd use select_related() for things like this, but unfortunately it doesn't work on reverse relationships. What you could do is turn the query around:</p>
<pre><code>users = [permission.user for permission in Permission.objects.select_related('user').filter(...)]
</code></pre>
| 0 | 2009-06-22T09:56:36Z | [
"python",
"django",
"django-models"
] |
Django model query with custom select fields | 1,026,204 | <p>I'm using the row-level permission model known as django-granular-permissions (<a href="http://code.google.com/p/django-granular-permissions/" rel="nofollow">http://code.google.com/p/django-granular-permissions/</a>). The permission model simply has just two more fields which are content-type and object id.</p>
<p>... | 0 | 2009-06-22T09:12:19Z | 1,026,477 | <pre><code>.extra(select={'is_staff': "%s.name='staff'" % Permission._meta.db_table, 'is_student': "%s.name='student'" % Permission._meta.db_table, })
</code></pre>
| 4 | 2009-06-22T10:36:12Z | [
"python",
"django",
"django-models"
] |
Cross-platform way to check admin rights in a Python script under Windows? | 1,026,431 | <p>Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, <code>os.getuid()</code> is UNIX-only and is not available under Windows.</p>
| 11 | 2009-06-22T10:20:32Z | 1,026,442 | <p>Try doing whatever you need admin rights for, and check for failure.</p>
<p>This will only work for some things though, what are you trying to do?</p>
| 4 | 2009-06-22T10:25:50Z | [
"python",
"privileges",
"admin-rights"
] |
Cross-platform way to check admin rights in a Python script under Windows? | 1,026,431 | <p>Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, <code>os.getuid()</code> is UNIX-only and is not available under Windows.</p>
| 11 | 2009-06-22T10:20:32Z | 1,026,516 | <p>It's better if you check which platform your script is running (using <code>sys.platform</code>) and do a test based on that, e.g. import some hasAdminRights function from another, platform-specific module.</p>
<p>On Windows you could check whether <code>Windows\System32</code> is writable using <code>os.access</co... | 3 | 2009-06-22T10:47:33Z | [
"python",
"privileges",
"admin-rights"
] |
Cross-platform way to check admin rights in a Python script under Windows? | 1,026,431 | <p>Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, <code>os.getuid()</code> is UNIX-only and is not available under Windows.</p>
| 11 | 2009-06-22T10:20:32Z | 1,026,626 | <pre><code>import ctypes, os
try:
is_admin = os.getuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
print is_admin
</code></pre>
| 26 | 2009-06-22T11:22:56Z | [
"python",
"privileges",
"admin-rights"
] |
Cross-platform way to check admin rights in a Python script under Windows? | 1,026,431 | <p>Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, <code>os.getuid()</code> is UNIX-only and is not available under Windows.</p>
| 11 | 2009-06-22T10:20:32Z | 1,038,617 | <p>Administrator group membership (Domain/Local/Enterprise) is one thing..</p>
<p>tailoring your application to not use blanket privilege and setting fine grained rights is a better option especially if the app is being used iinteractively.</p>
<p>testing for particular named privileges (se_shutdown se_restore etc), ... | 1 | 2009-06-24T14:07:57Z | [
"python",
"privileges",
"admin-rights"
] |
Algorithms for named entity recognition | 1,026,925 | <p>I would like to use named entity recognition (NER) to find adequate tags for texts in a database.</p>
<p>I know there is a Wikipedia article about this and lots of other pages describing NER, I would preferably hear something about this topic from you:</p>
<ul>
<li>What experiences did you make with the various al... | 19 | 2009-06-22T12:26:33Z | 1,026,976 | <p>I don't really know about NER, but judging from that example, you could make an algorithm that searched for capital letters in the words or something like that. For that I would recommend regex as the most easy to implement solution if you're thinking small.</p>
<p>Another option is to compare the texts with a data... | -9 | 2009-06-22T12:38:28Z | [
"php",
"python",
"extract",
"analysis",
"named-entity-recognition"
] |
Algorithms for named entity recognition | 1,026,925 | <p>I would like to use named entity recognition (NER) to find adequate tags for texts in a database.</p>
<p>I know there is a Wikipedia article about this and lots of other pages describing NER, I would preferably hear something about this topic from you:</p>
<ul>
<li>What experiences did you make with the various al... | 19 | 2009-06-22T12:26:33Z | 1,027,336 | <p>To start with check out <a href="http://www.nltk.org/">http://www.nltk.org/</a> if you plan working with python although as far as I know the code isn't "industrial strength" but it will get you started.</p>
<p>Check out section 7.5 from <a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch07.html">http://nltk.... | 13 | 2009-06-22T13:53:39Z | [
"php",
"python",
"extract",
"analysis",
"named-entity-recognition"
] |
Algorithms for named entity recognition | 1,026,925 | <p>I would like to use named entity recognition (NER) to find adequate tags for texts in a database.</p>
<p>I know there is a Wikipedia article about this and lots of other pages describing NER, I would preferably hear something about this topic from you:</p>
<ul>
<li>What experiences did you make with the various al... | 19 | 2009-06-22T12:26:33Z | 1,027,399 | <p>It depends on whether you want:</p>
<p><em>To learn about NER</em>: An excellent place to start is with <a href="http://www.nltk.org/" rel="nofollow">NLTK</a>, and the associated <a href="http://www.nltk.org/book" rel="nofollow">book</a>.</p>
<p><em>To implement the best solution</em>:
Here you're going to need t... | 3 | 2009-06-22T14:05:15Z | [
"php",
"python",
"extract",
"analysis",
"named-entity-recognition"
] |
Algorithms for named entity recognition | 1,026,925 | <p>I would like to use named entity recognition (NER) to find adequate tags for texts in a database.</p>
<p>I know there is a Wikipedia article about this and lots of other pages describing NER, I would preferably hear something about this topic from you:</p>
<ul>
<li>What experiences did you make with the various al... | 19 | 2009-06-22T12:26:33Z | 24,789,590 | <p>There's a few tools and API's out there.</p>
<p>There's a tool built on top of DBPedia called DBPedia Spotlight (<a href="https://github.com/dbpedia-spotlight/dbpedia-spotlight/wiki" rel="nofollow">https://github.com/dbpedia-spotlight/dbpedia-spotlight/wiki</a>). You can use their REST interface or download and in... | 1 | 2014-07-16T20:01:14Z | [
"php",
"python",
"extract",
"analysis",
"named-entity-recognition"
] |
How can I make sure all my Python code "compiles"? | 1,026,966 | <p>My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.</p>
<p>When I've written something in Python and come to the point where I can run it, there's still no guarantee that no l... | 14 | 2009-06-22T12:36:18Z | 1,026,984 | <p>I think what you are looking for is code test line coverage. You want to add tests to your script that will make sure all of your lines of code, or as many as you have time to, get tested. Testing is a great deal of work, but if you want the kind of assurance you are asking for, there is no free lunch, sorry :( .<... | 1 | 2009-06-22T12:39:25Z | [
"python",
"parsing",
"code-analysis"
] |
How can I make sure all my Python code "compiles"? | 1,026,966 | <p>My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.</p>
<p>When I've written something in Python and come to the point where I can run it, there's still no guarantee that no l... | 14 | 2009-06-22T12:36:18Z | 1,026,985 | <p>Look at <a href="http://pychecker.sourceforge.net/">PyChecker</a> and <a href="http://www.logilab.org/857">PyLint</a>.</p>
<p>Here's example output from pylint, resulting from the trivial program:</p>
<pre><code>print a
</code></pre>
<p>As you can see, it detects the undefined variable, which py_compile won't (de... | 21 | 2009-06-22T12:39:34Z | [
"python",
"parsing",
"code-analysis"
] |
How can I make sure all my Python code "compiles"? | 1,026,966 | <p>My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.</p>
<p>When I've written something in Python and come to the point where I can run it, there's still no guarantee that no l... | 14 | 2009-06-22T12:36:18Z | 1,027,032 | <p>Your code actually gets compiled when you run it, the Python runtime will complain if there is a syntax error in the code. Compared to statically compiled languages like C/C++ or Java, it does not check whether variable names and types are correct â for that you need to actually run the code (e.g. with automated t... | 0 | 2009-06-22T12:49:04Z | [
"python",
"parsing",
"code-analysis"
] |
How can I make sure all my Python code "compiles"? | 1,026,966 | <p>My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.</p>
<p>When I've written something in Python and come to the point where I can run it, there's still no guarantee that no l... | 14 | 2009-06-22T12:36:18Z | 1,027,127 | <p>If you are using Eclipse with <a href="http://pydev.sourceforge.net/" rel="nofollow">Pydev</a> as an IDE, it can flag many typos for you with red squigglies immediately, and has Pylint integration too. For example:</p>
<pre><code>foo = 5
print food
</code></pre>
<p>will be flagged as "Undefined variable: food". Of... | 1 | 2009-06-22T13:09:00Z | [
"python",
"parsing",
"code-analysis"
] |
How can I make sure all my Python code "compiles"? | 1,026,966 | <p>My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.</p>
<p>When I've written something in Python and come to the point where I can run it, there's still no guarantee that no l... | 14 | 2009-06-22T12:36:18Z | 1,061,573 | <p>Others have mentioned tools like PyLint which are pretty good, but the long and the short of it is that it's simply not possible to do 100%. In fact, you might not even want to do it. Part of the benefit to Python's dynamicity is that you can do crazy things like insert names into the local scope through a diction... | 2 | 2009-06-30T03:26:18Z | [
"python",
"parsing",
"code-analysis"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,596 | <p>Think of import being more like the linker.<br />
With "import mod2" you are simply telling python that it can find the function in the file mod2.py </p>
| 1 | 2009-06-22T14:43:13Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,604 | <p>In this case you're right: show_answer() is given an object, of which it calls the method "answer". As long as the object given to show_answer() has such a method, it doesn't matter where the object comes from.</p>
<p>If, however, you wanted to create an instance of Universe inside mod2, you'd have to import mod1, ... | 6 | 2009-06-22T14:44:40Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,637 | <p>import in Python loads the module into the given namespace. As such, is it as if the def show_answer actually existed in the mod1.py module. Because of this, mod2.py does not need to know of the Universe class and thus you do not need to import mod1 from mod2.py.</p>
| 1 | 2009-06-22T14:48:38Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,706 | <p>Actually, in this case, importing <code>mod1</code> in <code>mod2.py</code> should <b>not</b> work.<br />
Would it not create a circular reference? </p>
| 1 | 2009-06-22T15:03:07Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,708 | <p>In fact, according to <a href="http://effbot.org/zone/import-confusion.htm" rel="nofollow">this explanation</a> , the circular <code>import</code> will not work the way you want it to work: if you uncomment <code>import mod1</code>, the second module will still not know about the <code>Universe</code>. </p>
<p>I t... | 1 | 2009-06-22T15:03:16Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,027,729 | <p><code>import</code> is all about names -- mostly "bare names" that are bound at top level (AKA global level, AKA module-level names) in a certain module, say <code>mod2</code>. When you've done <code>import mod2</code>, you get the <code>mod2</code> namespace as an available name (top-level in your own module, if yo... | 4 | 2009-06-22T15:06:58Z | [
"python",
"import",
"module"
] |
Python - when is 'import' required? | 1,027,557 | <p>mod1.py</p>
<pre><code>import mod2
class Universe:
def __init__(self):
pass
def answer(self):
return 42
u = Universe()
mod2.show_answer(u)
</code></pre>
<p>mod2.py</p>
<pre><code>#import mod1 -- not necessary
def show_answer(thing):
print thing.answer()
</code></pre>
<p>Coming from ... | 3 | 2009-06-22T14:35:30Z | 1,028,100 | <p>I don't know much about C++, so can't directly compare it, but..</p>
<p><code>import</code> basically loads the other Python script (<code>mod2.py</code>) into the current script (the top level of <code>mod1.py</code>). It's not so much a link, it's closer to an <code>eval</code></p>
<p>For example, in Python'ish ... | 1 | 2009-06-22T16:19:12Z | [
"python",
"import",
"module"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 1,027,730 | <pre><code>>>> execfile('filename.py')
</code></pre>
<p>See <a href="http://docs.python.org/library/functions.html#execfile">the documentation</a>. If you are using Python 3.0, see <a href="http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0">this question</a>.</p>
<p>S... | 101 | 2009-06-22T15:07:10Z | [
"python"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 1,027,739 | <p>Several ways.</p>
<p>From the shell</p>
<pre><code>python someFile.py
</code></pre>
<p>From inside IDLE, hit <strong>F5</strong>.</p>
<p>If you're typing interactively, try this.</p>
<pre><code>>>> variables= {}
>>> execfile( "someFile.py", variables )
>>> print variables # globals fr... | 144 | 2009-06-22T15:08:38Z | [
"python"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 1,028,096 | <blockquote>
<p>I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
</blockquote>
<p>Well, simply importing the file with <code>import filename</code> (minus .py, needs to be in the same directory or on your <code>PYTHONPATH</code>) will run the file, making its variables, ... | 20 | 2009-06-22T16:18:11Z | [
"python"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 20,010,963 | <p>I am not an expert but this is what I noticed:</p>
<p>if your code is mycode.py for instance, and you type just 'import mycode', Python will execute it but it will not make all your variables available to the interpreter. I found that you should type actually 'from mycode import *' if you want to make all variable... | 7 | 2013-11-15T21:31:19Z | [
"python"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 31,566,843 | <h2>Python 2 + Python 3</h2>
<pre><code>exec(open("./path/to/script.py").read(), globals())
</code></pre>
<p>This will execute a script and put all it's global variables in the interpreter's global scope (the normal behavior in most other languages).</p>
<p><a href="https://docs.python.org/3/library/functions.html#e... | 21 | 2015-07-22T14:58:25Z | [
"python"
] |
How to execute a file within the python interpreter? | 1,027,714 | <p>I'm trying to execute a file with python commands from within the interpreter. </p>
<p>EDIT: I'm trying to use variables and settings from that file, not to invoke a separate process.</p>
| 132 | 2009-06-22T15:05:00Z | 36,311,203 | <p>For python3 use either with <code>xxxx = name</code> of <code>yourfile</code>. </p>
<pre><code>exec(open('./xxxx.py').read())
</code></pre>
| -1 | 2016-03-30T14:03:39Z | [
"python"
] |
MySQLdb through proxy | 1,027,751 | <p>I'm using the above mentioned Python lib to connect to a MySQL server. So far I've worked locally and all worked fine, until i realized I'll have to use my program in a network where all access goes through a proxy.</p>
<p>Does anyone now how I can set the connections managed by that lib to use a proxy?
Alternative... | 2 | 2009-06-22T15:11:05Z | 1,027,780 | <p>Do you have to do anything special to connect through a proxy?</p>
<p>I would guess you just supply the correct parameters to the <code>connect</code> function. From <a href="http://mysql-python.sourceforge.net/MySQLdb.html#id8" rel="nofollow">the documentation</a>, it looks as if you can specify the host name and ... | 0 | 2009-06-22T15:16:54Z | [
"python",
"mysql",
"proxy"
] |
MySQLdb through proxy | 1,027,751 | <p>I'm using the above mentioned Python lib to connect to a MySQL server. So far I've worked locally and all worked fine, until i realized I'll have to use my program in a network where all access goes through a proxy.</p>
<p>Does anyone now how I can set the connections managed by that lib to use a proxy?
Alternative... | 2 | 2009-06-22T15:11:05Z | 1,027,784 | <p>I use <a href="http://www.ssh.com/support/documentation/online/ssh/winhelp/32/Tunneling%5FExplained.html" rel="nofollow">ssh tunneling</a> for that kind of issues.
For example I am developing an application that connects to an oracle db.</p>
<p>In my code I write to connect to localhost and then from a shell I do:<... | 2 | 2009-06-22T15:17:01Z | [
"python",
"mysql",
"proxy"
] |
MySQLdb through proxy | 1,027,751 | <p>I'm using the above mentioned Python lib to connect to a MySQL server. So far I've worked locally and all worked fine, until i realized I'll have to use my program in a network where all access goes through a proxy.</p>
<p>Does anyone now how I can set the connections managed by that lib to use a proxy?
Alternative... | 2 | 2009-06-22T15:11:05Z | 1,027,817 | <p>there are a lot of different possibilities here. the only way you're going to get a definitive answer is to talk to the person that runs the proxy.</p>
<p>if this is a web app and the web server and the database serve are both on the other side of a proxy, then you won't need to connect to the mysql server at all s... | 1 | 2009-06-22T15:23:29Z | [
"python",
"mysql",
"proxy"
] |
Detect if X11 is available (python) | 1,027,894 | <p>Firstly, what is the best/simplest way to detect if X11 is running and available for a python script.</p>
<p>parent process?<br />
session leader?<br />
X environment variables?<br />
other? </p>
<p>Secondly, I would like to have a utility (python script) to present a gui if available, otherwise use a command lin... | 4 | 2009-06-22T15:38:45Z | 1,027,907 | <p>I'd check to see if DISPLAY is set ( this is what C API X11 applications do after all ). </p>
| 8 | 2009-06-22T15:41:25Z | [
"python",
"user-interface"
] |
Detect if X11 is available (python) | 1,027,894 | <p>Firstly, what is the best/simplest way to detect if X11 is running and available for a python script.</p>
<p>parent process?<br />
session leader?<br />
X environment variables?<br />
other? </p>
<p>Secondly, I would like to have a utility (python script) to present a gui if available, otherwise use a command lin... | 4 | 2009-06-22T15:38:45Z | 1,027,918 | <p>You could simply launch the gui part, and catch the exception it raises when X (or any other platform dependent graphics system is not available.</p>
<p>Make sure you really have an interactive terminal before running the text based part. Your process might have been started without a visible terminal, as is common... | 4 | 2009-06-22T15:42:38Z | [
"python",
"user-interface"
] |
Detect if X11 is available (python) | 1,027,894 | <p>Firstly, what is the best/simplest way to detect if X11 is running and available for a python script.</p>
<p>parent process?<br />
session leader?<br />
X environment variables?<br />
other? </p>
<p>Secondly, I would like to have a utility (python script) to present a gui if available, otherwise use a command lin... | 4 | 2009-06-22T15:38:45Z | 1,027,942 | <p>Check the return code of <code>xset -q</code>:</p>
<pre><code>def X_is_running():
from subprocess import Popen, PIPE
p = Popen(["xset", "-q"], stdout=PIPE, stderr=PIPE)
p.communicate()
return p.returncode == 0
</code></pre>
<p>As for the second part of your question, I suggest the following <code>m... | 5 | 2009-06-22T15:48:28Z | [
"python",
"user-interface"
] |
Is python a stable platform for facebook development? | 1,027,990 | <p>I'm trying to build my first facebook app, and it seems that the python facebook (<a href="http://code.google.com/p/pyfacebook/" rel="nofollow">pyfacebook</a>) wrapper is really out of date, and the most relevant functions, like stream functions, are not implemented.</p>
<p>Are there any mature python frontends for... | 7 | 2009-06-22T15:57:26Z | 1,028,027 | <p>The updated location of pyfacebook is <a href="http://github.com/sciyoshi/pyfacebook/tree/master" rel="nofollow">on github</a>. Plus, as <a href="http://arstechnica.com/open-source/news/2009/04/how-to-using-the-new-facebook-stream-api-in-a-desktop-app.ars" rel="nofollow">arstechnica</a> well explains:</p>
<blockquo... | 13 | 2009-06-22T16:04:39Z | [
"python",
"api",
"facebook"
] |
Is python a stable platform for facebook development? | 1,027,990 | <p>I'm trying to build my first facebook app, and it seems that the python facebook (<a href="http://code.google.com/p/pyfacebook/" rel="nofollow">pyfacebook</a>) wrapper is really out of date, and the most relevant functions, like stream functions, are not implemented.</p>
<p>Are there any mature python frontends for... | 7 | 2009-06-22T15:57:26Z | 1,028,032 | <p>Try <a href="http://github.com/sciyoshi/pyfacebook/tree/master" rel="nofollow">this site</a> instead.</p>
<p>It's pyfacebooks site on GitHub. The one you have is outdated.</p>
| 4 | 2009-06-22T16:05:01Z | [
"python",
"api",
"facebook"
] |
Is python a stable platform for facebook development? | 1,027,990 | <p>I'm trying to build my first facebook app, and it seems that the python facebook (<a href="http://code.google.com/p/pyfacebook/" rel="nofollow">pyfacebook</a>) wrapper is really out of date, and the most relevant functions, like stream functions, are not implemented.</p>
<p>Are there any mature python frontends for... | 7 | 2009-06-22T15:57:26Z | 1,028,325 | <p>If you're a facebook newbie, I'd suggest doing your first couple of apps in PHP. Facebook is written in PHP, the APIs are really designed around PHP (although they are language-neutral, theoretically.) The latest API support and most of the sample code is always in PHP.</p>
<p>Once you get the hang of it, you can d... | 3 | 2009-06-22T16:57:17Z | [
"python",
"api",
"facebook"
] |
Is python a stable platform for facebook development? | 1,027,990 | <p>I'm trying to build my first facebook app, and it seems that the python facebook (<a href="http://code.google.com/p/pyfacebook/" rel="nofollow">pyfacebook</a>) wrapper is really out of date, and the most relevant functions, like stream functions, are not implemented.</p>
<p>Are there any mature python frontends for... | 7 | 2009-06-22T15:57:26Z | 4,034,156 | <p>Facebook's own Python-SDK covers the newer Graph API now:</p>
<p><a href="http://github.com/facebook/python-sdk/" rel="nofollow">http://github.com/facebook/python-sdk/</a></p>
| 11 | 2010-10-27T14:08:22Z | [
"python",
"api",
"facebook"
] |
Conditional Django Middleware (or how to exclude the Admin System) | 1,028,019 | <p>I want to use some middleware I wrote across the whole of my site (large # of pages, so I chose not to use decorators as I wanted to use the code for all pages). Only issue is that I don't want to use the middleware for the admin code, and it seems to be active on them.</p>
<p>Is there any way I can configure the s... | 13 | 2009-06-22T16:03:21Z | 1,028,350 | <p>You could check the path in process_request (and any other process_*-methods in your middleware)</p>
<pre><code>def process_request(self, request):
if request.path.startswith('/admin/'):
return None
# rest of method
def process_response(self, request, response):
if request.path.startswith('/adm... | 6 | 2009-06-22T17:04:37Z | [
"python",
"django",
"django-admin",
"django-middleware"
] |
Conditional Django Middleware (or how to exclude the Admin System) | 1,028,019 | <p>I want to use some middleware I wrote across the whole of my site (large # of pages, so I chose not to use decorators as I wanted to use the code for all pages). Only issue is that I don't want to use the middleware for the admin code, and it seems to be active on them.</p>
<p>Is there any way I can configure the s... | 13 | 2009-06-22T16:03:21Z | 1,031,698 | <p>The main reason I wanted to do this was down to using an XML parser in the middleware which was messing up non-XML downloads. I have put some additional code for detecting if the code is XML and not trying to parse anything that it shouldn't.</p>
<p>For other middleware where this wouldn't be convenient, I'll proba... | 0 | 2009-06-23T10:13:38Z | [
"python",
"django",
"django-admin",
"django-middleware"
] |
Conditional Django Middleware (or how to exclude the Admin System) | 1,028,019 | <p>I want to use some middleware I wrote across the whole of my site (large # of pages, so I chose not to use decorators as I wanted to use the code for all pages). Only issue is that I don't want to use the middleware for the admin code, and it seems to be active on them.</p>
<p>Is there any way I can configure the s... | 13 | 2009-06-22T16:03:21Z | 3,204,374 | <p>A general way would be (based on piquadrat's answer)</p>
<pre><code>def process_request(self, request):
if request.path.startswith(reverse('admin:index')):
return None
# rest of method
</code></pre>
<p>This way if someone changes <code>/admin/</code> to <code>/django_admin/</code> you are still cov... | 24 | 2010-07-08T14:04:44Z | [
"python",
"django",
"django-admin",
"django-middleware"
] |
Can I avoid handling a file twice if I need the number of lines and I need to append to the file? | 1,028,122 | <p>I am writing a file to disk in stages. As I write it I need to know the line numbers that I am writing to use to build an index. The file now has 12 million lines so I need to build the index on the fly. I am doing this in four steps, with four groupings of the value that I am indexing on. Based on some examples... | 2 | 2009-06-22T16:22:32Z | 1,028,173 | <p>Open the file for updates ('u' or 'rw', I forget). Now you can read it until EOF and then start writing to append.</p>
| 0 | 2009-06-22T16:27:51Z | [
"python",
"file"
] |
Can I avoid handling a file twice if I need the number of lines and I need to append to the file? | 1,028,122 | <p>I am writing a file to disk in stages. As I write it I need to know the line numbers that I am writing to use to build an index. The file now has 12 million lines so I need to build the index on the fly. I am doing this in four steps, with four groupings of the value that I am indexing on. Based on some examples... | 2 | 2009-06-22T16:22:32Z | 1,028,189 | <p>You can open a file for reading AND writing:</p>
<pre><code>myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt','r+')
</code></pre>
<p>Try that.</p>
<p><strong>UPDATE: Ah, my mistake since the file might not exist. Use 'a+' instead of 'r+'.</strong></p>
| 4 | 2009-06-22T16:30:35Z | [
"python",
"file"
] |
Can I avoid handling a file twice if I need the number of lines and I need to append to the file? | 1,028,122 | <p>I am writing a file to disk in stages. As I write it I need to know the line numbers that I am writing to use to build an index. The file now has 12 million lines so I need to build the index on the fly. I am doing this in four steps, with four groupings of the value that I am indexing on. Based on some examples... | 2 | 2009-06-22T16:22:32Z | 1,028,339 | <p>I assume you are writing the file, in that case why don't you maintain a separate track of how many lines you have already written?
to me it looks very wasteful that you have to read whole file line by line just to get line number.</p>
| 0 | 2009-06-22T17:00:37Z | [
"python",
"file"
] |
Can I avoid handling a file twice if I need the number of lines and I need to append to the file? | 1,028,122 | <p>I am writing a file to disk in stages. As I write it I need to know the line numbers that I am writing to use to build an index. The file now has 12 million lines so I need to build the index on the fly. I am doing this in four steps, with four groupings of the value that I am indexing on. Based on some examples... | 2 | 2009-06-22T16:22:32Z | 1,029,764 | <p>A bit late to the party but for the file existing problem why not use (Psuedocode):</p>
<pre><code>If FileExists(C:\NEWMASTERLIST\FULLLIST.txt') then
begin
Open file etc
Calc numlines etc
end
else
Create new file etc
NumLines := 0;
end;
</code></pre>
| 0 | 2009-06-22T22:42:49Z | [
"python",
"file"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.