title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
python - os.makedirs don't understand ~ in my path?
2,057,045
<p>I have a little problem with ~ in my paths.</p> <p>This code example creates some dirs called "~/some_dir", and do not understand that I wanted to create some_dir in my home dir.</p> <pre><code>my_dir = "~/some_dir" if not os.path.exists(my_dir): os.makedirs(my_dir) </code></pre> <p>Note this is on a linux b...
37
2010-01-13T13:51:09Z
2,057,076
<p>That's probably because Python is not Bash and doesn't follow same conventions. You may use this:</p> <pre><code>homedir = os.path.expanduser('~') </code></pre>
8
2010-01-13T13:57:01Z
[ "python", "path" ]
python - os.makedirs don't understand ~ in my path?
2,057,045
<p>I have a little problem with ~ in my paths.</p> <p>This code example creates some dirs called "~/some_dir", and do not understand that I wanted to create some_dir in my home dir.</p> <pre><code>my_dir = "~/some_dir" if not os.path.exists(my_dir): os.makedirs(my_dir) </code></pre> <p>Note this is on a linux b...
37
2010-01-13T13:51:09Z
2,057,111
<p>The conversion of <code>~/some_dir</code> to <code>$HOME/some_dir</code> is called <a href="http://www.gnu.org/software/bash/manual/bashref.html#Tilde-Expansion">tilde expansion</a> and is a common user interface feature. The file system does not know anything about it.</p> <p>In Python, this feature is implemented...
25
2010-01-13T14:00:40Z
[ "python", "path" ]
Installing SUDS in python 2.6.4
2,057,380
<p>I am having real trouble installing SUDS in python 2.6.4. I have tried to install the setup file but it says the location of python cannot be found. This is because I have changed the location of python. I have tried to use easy_install but am having no luck. Does anyone know a simple way to do this or have a link t...
2
2010-01-13T14:40:04Z
2,057,405
<p>Have you tried setting PYTHONPATH to the location of python? Maybe this way it will know, where to install it.</p> <p>You are calling it with <code>python setup.py install</code>. Try <code>sudo python setup.py install</code>, if you are using some linux and you are sudoer.</p>
3
2010-01-13T14:43:52Z
[ "python", "installation", "easy-install", "suds", "setup.py" ]
Installing SUDS in python 2.6.4
2,057,380
<p>I am having real trouble installing SUDS in python 2.6.4. I have tried to install the setup file but it says the location of python cannot be found. This is because I have changed the location of python. I have tried to use easy_install but am having no luck. Does anyone know a simple way to do this or have a link t...
2
2010-01-13T14:40:04Z
8,060,623
<p>I got messages like this too when I installed suds and python-ntlm. Our site has a separate areafor installations so that we can maintain multiple versions, so my first installation step was </p> <pre><code>python setup.py install --prefix=/install/suds/suds-0.4 </code></pre> <p>and I got the same messages about ...
1
2011-11-09T05:12:20Z
[ "python", "installation", "easy-install", "suds", "setup.py" ]
Installing SUDS in python 2.6.4
2,057,380
<p>I am having real trouble installing SUDS in python 2.6.4. I have tried to install the setup file but it says the location of python cannot be found. This is because I have changed the location of python. I have tried to use easy_install but am having no luck. Does anyone know a simple way to do this or have a link t...
2
2010-01-13T14:40:04Z
29,905,916
<p>I would need more details of your OS to give a fully accurate response. From the sounds of your question, you changed your path of python. Normally you'll have a preinstalled version of python that is compatible with your OS. For example, CentOS 5.x comes with python 2.4, however you can do a <code>yum install</code...
0
2015-04-27T21:04:59Z
[ "python", "installation", "easy-install", "suds", "setup.py" ]
Delete item in a list using a for-loop
2,057,419
<p>I have an array with subjects and every subject has connected time. I want to compare every subjects in the list. If there are two of the same subjects, I want to add the times of both subjects, and also want to delete the second subject information (subject-name and time). </p> <p>But If I delete the item, the lis...
4
2010-01-13T14:46:04Z
2,057,471
<p>Iterate backwards, if you can:</p> <pre><code>for x in range(subjectlength - 1, -1, -1): </code></pre> <p>and similarly for <code>y</code>.</p>
9
2010-01-13T14:51:23Z
[ "python", "list", "for-loop", "collections" ]
Delete item in a list using a for-loop
2,057,419
<p>I have an array with subjects and every subject has connected time. I want to compare every subjects in the list. If there are two of the same subjects, I want to add the times of both subjects, and also want to delete the second subject information (subject-name and time). </p> <p>But If I delete the item, the lis...
4
2010-01-13T14:46:04Z
2,057,530
<p>The best practice is to make a new list of the entries to delete, and to delete them after walking the list:</p> <pre><code>to_del = [] subjectlength = 8 for x in range(subjectlength): for y in range(x): if subject[x] == subject[y]: #add time[x] = time[x] + time[y] to...
7
2010-01-13T14:57:29Z
[ "python", "list", "for-loop", "collections" ]
Delete item in a list using a for-loop
2,057,419
<p>I have an array with subjects and every subject has connected time. I want to compare every subjects in the list. If there are two of the same subjects, I want to add the times of both subjects, and also want to delete the second subject information (subject-name and time). </p> <p>But If I delete the item, the lis...
4
2010-01-13T14:46:04Z
2,057,554
<p>If the elements of <code>subject</code> are hashable:</p> <pre><code>finalinfo = {} for s, t in zip(subject, time): finalinfo[s] = finalinfo.get(s, 0) + t </code></pre> <p>This will result in a dict with <code>subject: time</code> key-value pairs.</p>
8
2010-01-13T14:59:47Z
[ "python", "list", "for-loop", "collections" ]
Delete item in a list using a for-loop
2,057,419
<p>I have an array with subjects and every subject has connected time. I want to compare every subjects in the list. If there are two of the same subjects, I want to add the times of both subjects, and also want to delete the second subject information (subject-name and time). </p> <p>But If I delete the item, the lis...
4
2010-01-13T14:46:04Z
2,057,600
<p>An alternate way would be to create the subject and time lists anew, using a dict to sum up the times of recurring subjects (I am assuming subjects are strings i.e. hashable).</p> <pre><code>subjects=['math','english','necromancy','philosophy','english','latin','physics','latin'] time=[1,2,3,4,5,6,7,8] tuples=zip(s...
2
2010-01-13T15:05:32Z
[ "python", "list", "for-loop", "collections" ]
Build, syntax-check, parse and evaluate a query
2,057,820
<p>I am building a query in a textarea with different conditions selected from the html controls. Also users are open to do modification to it.</p> <p>Client side: For the below list of condition:</p> <pre><code>a(1, 3) &gt; 20 b(4, 5) &lt; 90 c(3, 0) = 80 </code></pre> <p>I form a query:</p> <pre><code>a(1, 3) &gt...
1
2010-01-13T15:29:35Z
2,057,864
<p><a href="http://www.dabeaz.com/ply/" rel="nofollow">PLY</a> has a simple <a href="http://www.dabeaz.com/ply/example.html" rel="nofollow">expression example</a> that will get you most of the way there.</p>
4
2010-01-13T15:34:05Z
[ "javascript", "jquery", "python", "html" ]
Python GTK+: create custom signals?
2,057,921
<p>Is it possible to create new signals in Python GTK+ ? </p> <p>I'd like a skeleton code example, please.</p>
5
2010-01-13T15:40:53Z
2,057,942
<p>An excerpt:</p> <blockquote> <p><strong>Creating your own signals</strong></p> <p>The other thing you probably want to use when subclassing GObject is define custom signals. You can create your own signals that can be emitted so users of your class can connect to them.</p> <p>When a signal is ...
6
2010-01-13T15:43:04Z
[ "python", "pygtk", "gtk" ]
Python GTK+: create custom signals?
2,057,921
<p>Is it possible to create new signals in Python GTK+ ? </p> <p>I'd like a skeleton code example, please.</p>
5
2010-01-13T15:40:53Z
2,057,944
<p><a href="http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm#d0e570" rel="nofollow">There's a pretty lengthy article on pygtk's site about that.</a></p>
0
2010-01-13T15:43:09Z
[ "python", "pygtk", "gtk" ]
Python GTK+: create custom signals?
2,057,921
<p>Is it possible to create new signals in Python GTK+ ? </p> <p>I'd like a skeleton code example, please.</p>
5
2010-01-13T15:40:53Z
2,160,819
<p>The answers are correct of course, but using <a href="http://bitbucket.org/aafshar/pygtkhelpers-main/" rel="nofollow">libraries</a> is less prone to pains:</p> <pre><code>import gobject from pygtkhelpers.utils import gsignal class Car(gobject.GObject): gsignal('engine-started', float) # yeah baby </code></pr...
3
2010-01-29T08:51:38Z
[ "python", "pygtk", "gtk" ]
How to find out if I have installed a Python module in Linux?
2,058,172
<p>I tried to install a Python module by typing: sudo python setup.py install After I typed this command I got a lot of output to the screen. The lest few lines are bellow:</p> <pre><code>writing manifest file 'scikits.audiolab.egg-info/SOURCES.txt' removing '/usr/lib/python2.5/site-packages/scikits.audiolab-0.10.2-py...
1
2010-01-13T16:08:07Z
2,058,236
<p>Have you tried <code>import scikits.audiolab</code> or <code>import audiolab</code>?</p>
5
2010-01-13T16:13:14Z
[ "python", "import", "module", "install" ]
How to find out if I have installed a Python module in Linux?
2,058,172
<p>I tried to install a Python module by typing: sudo python setup.py install After I typed this command I got a lot of output to the screen. The lest few lines are bellow:</p> <pre><code>writing manifest file 'scikits.audiolab.egg-info/SOURCES.txt' removing '/usr/lib/python2.5/site-packages/scikits.audiolab-0.10.2-py...
1
2010-01-13T16:08:07Z
2,058,288
<ul> <li><p>check if you have the module somewhere inside: /usr/lib/python2.5/site-packages/ (search for a file named |modulename|.py so in your example - try: pyaudiolab.py or audiolab.py)</p></li> <li><p>if it exists - check if the directory in which it exists is found in the sys.path variable:</p> <p>import sys</p>...
0
2010-01-13T16:18:46Z
[ "python", "import", "module", "install" ]
How to find out if I have installed a Python module in Linux?
2,058,172
<p>I tried to install a Python module by typing: sudo python setup.py install After I typed this command I got a lot of output to the screen. The lest few lines are bellow:</p> <pre><code>writing manifest file 'scikits.audiolab.egg-info/SOURCES.txt' removing '/usr/lib/python2.5/site-packages/scikits.audiolab-0.10.2-py...
1
2010-01-13T16:08:07Z
2,058,329
<p>From the OP's comment to an answer, it's clear that <code>scikits.audiolab</code> is indeed where this module's been installed, but it also needs you to install <code>numpy</code>. Assuming the module's configuration files are correct, by using <code>easy_install</code> instead of the usual <code>python setup.py</c...
1
2010-01-13T16:23:37Z
[ "python", "import", "module", "install" ]
How to find out if I have installed a Python module in Linux?
2,058,172
<p>I tried to install a Python module by typing: sudo python setup.py install After I typed this command I got a lot of output to the screen. The lest few lines are bellow:</p> <pre><code>writing manifest file 'scikits.audiolab.egg-info/SOURCES.txt' removing '/usr/lib/python2.5/site-packages/scikits.audiolab-0.10.2-py...
1
2010-01-13T16:08:07Z
2,058,695
<p>Your library depends upon numoy. Try installing numpy:</p> <pre><code>sudo apt-get install python-numpy </code></pre>
1
2010-01-13T17:11:19Z
[ "python", "import", "module", "install" ]
How to find out if I have installed a Python module in Linux?
2,058,172
<p>I tried to install a Python module by typing: sudo python setup.py install After I typed this command I got a lot of output to the screen. The lest few lines are bellow:</p> <pre><code>writing manifest file 'scikits.audiolab.egg-info/SOURCES.txt' removing '/usr/lib/python2.5/site-packages/scikits.audiolab-0.10.2-py...
1
2010-01-13T16:08:07Z
3,318,316
<p>You need a more recent version of numpy (>= 1.2.0), as indicated on the audiolab installation informations.</p>
1
2010-07-23T12:58:12Z
[ "python", "import", "module", "install" ]
Some tables mixed together
2,058,197
<p>I have 2 different tables in my database. They have some variables common and some different. For example:</p> <h2>Table1:</h2> <ul> <li>ID </li> <li>Date </li> <li>Name </li> <li>Address </li> <li>Fax</li> </ul> <h2>Table2:</h2> <ul> <li>ID </li> <li>Date </li> <li>Name </li> <li>e-mail </li> <li>Telephone numb...
1
2010-01-13T16:09:19Z
2,058,311
<p>Select entries from both models, than put them into a single list and sort them. Like that:</p> <pre><code>result = (list(first_query) + list(second_query)) result.sort(cmp=foo) return result </code></pre> <p>where foo is a function, which is used to compare two elements:</p> <pre><code>def foo(a, b): if a.date...
0
2010-01-13T16:21:27Z
[ "python", "django" ]
Dynamic direct_to_template
2,058,261
<p>In my webapp, there are a lot of errors or other messages that just show a template that is very close to the URL. At the moment, I have half a dozen static mappers like this:</p> <pre><code>(r'^/message/foo/$', 'direct_to_template', {'template': 'message/foo.html'}), (r'^/message/bar/$', 'direct_to_template', {'te...
0
2010-01-13T16:16:35Z
2,058,395
<p>This is pretty easy. Do it like this:</p> <pre><code>(r'^/message/(?&lt;name&gt;\d+)/$', 'your_app.views.direct_to_template') </code></pre> <p>and:</p> <pre><code>def direct_to_template(name): return render_to_response('message/%s.html' % name) </code></pre>
4
2010-01-13T16:31:59Z
[ "python", "regex", "django", "django-templates", "django-urls" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,380
<p>It looks like legal Python. You might be able to just import it as a module and then write it back out after formatting it. </p>
1
2010-01-13T16:29:48Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,390
<p>Awk is typically line oriented, and bash is a shell, with limited numbrer of string manipulation functions. It really depends on where your strength as a programmer lies, but all other things being equal, I would choose <strong>python</strong>.</p> <p>Did you ever consider that by redirecting the time it took to p...
0
2010-01-13T16:31:47Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,399
<p>Tried evaluating it python? Looks like a list of tuples to me.</p> <pre><code>eval(your_string) </code></pre> <p>Note, it's massively unsafe! If there's code in there to delete your hard disk, evaluating it will run that code!</p>
2
2010-01-13T16:32:27Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,400
<p>Asking what's the best language for doing a given task is a very different question to say, asking: 'what's the best way of doing a given task in a particular language'. The first, what you're asking, is in most cases entirely subjective.</p> <p>Since this is a fairly simple task, I would suggest going with what yo...
0
2010-01-13T16:32:33Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,414
<p>I would like to use Python:</p> <pre><code>lines = open('filename.txt','r').readlines() n = len(lines) # n % 3 == 0 for i in range(0,n,3): name1 = lines[i].strip("',[]\n\r") name2 = lines[i+1].strip("',[]\n\r") value = lines[i+2].strip("',[]\n\r") print name1,name2,value </code></pre>
2
2010-01-13T16:34:05Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,420
<p>If the values are legal python values, you can take advantage of eval() since your data is a legal python data sucture. The following would work if values are integers, otherwise you might have to massage the print call a bit:</p> <pre><code>input = """[('name_1a', 'name_1b', 1), ('name...
0
2010-01-13T16:35:22Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,432
<p>Oh boy, here is a job for <code>ast.literal_eval</code>: (<code>literal_eval</code> is safer than <code>eval</code>, since it restricts the input string to literals such as strings, numbers, tuples, lists, dicts, booleans and None:</p> <pre><code>import ast filename='in' with open(filename,'r') as f: contents=f...
1
2010-01-13T16:36:50Z
[ "python", "bash", "awk" ]
Awk, bash or python for converting a regular file?
2,058,339
<p>I have a text file with lots of lines and with this structure:</p> <pre><code>[('name_1a', 'name_1b', value_1), ('name_2a', 'name_2b', value_2), ..... ..... ('name_XXXa', 'name_XXXb', value_XXX)] </code></pre> <p>I would like to convert it to:</p> <pre><code>name_1a, name_1b, value_1 name_2a, name_2b, value_2 ......
0
2010-01-13T16:24:54Z
2,058,556
<p>here's one way to do it with (g)awk</p> <pre><code>$ awk -vRS=")," ' { gsub(/\n|[\047\]\[)(]/,"") } 1' file name_1a,name_1b,value_1 name_2a,name_2b,value_2 name_XXXa,name_XXXb,value_XXX </code></pre>
1
2010-01-13T16:53:57Z
[ "python", "bash", "awk" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,058,560
<p>I tried to append items to my session without copying them out, appending the items, and then adding the list back to the session.</p> <p>This mistake is on a <a href="http://code.djangoproject.com/wiki/NewbieMistakes" rel="nofollow">NewbieMistakes</a> page, so hopefully I'm in good company.</p> <p>This is the <st...
11
2010-01-13T16:54:17Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,058,590
<p>Not splitting stuff up into multiple applications. It's not so much about reusability as it is about having a dozen models, and over 100 views in one app, it's damned unreadable. Plus I like to be able to scan my urls.py file easily to see where a URL points, when I have 100 URLs that gets harder.</p>
11
2010-01-13T16:57:35Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,058,929
<p>I think the biggest problem is that people try to code as if this were Java/C: They try to create overly generic applications that need never be changed when future requirements change (which is necessary for Java/C because those apps aren't so easy to change/redesign). What results is a hideously complicated applic...
9
2010-01-13T17:42:06Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,059,269
<p>I first and fore most mistake start writing python code without reading PEP. The worst thing i would quote are </p> <p>NOTE: Things I am quoting here are <strong>DONT'S</strong></p> <p>1</p> <pre><code>foo = bar foobar = bar foobarbuz = bar </code></pre> <p>2</p> <pre><code>foo = "foo" bar = "bar...
0
2010-01-13T18:40:22Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,060,303
<p>Too much logic in views. </p> <p>I used to write views that would struggle to fit in 40 lines. Now I consider more than 2-3 indentation levels, 10 or so LOC or a handful of inline comments in a view to be code smells.</p> <p>The temptation is to write minimal models, figure out your url routing, then do everything...
23
2010-01-13T21:10:35Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,060,365
<ol> <li><p>Monkeying around with pre-save and post-save events.</p> <p>If you can't simply do it in <code>save</code>, you should probably rethink what you're trying to do. After all, it's just a relational database under the hood. If what you're doing gets too complex, you'll have ORM mapping issues.</p></li> <li>...
4
2010-01-13T21:20:53Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
2,060,454
<p>Worst facepalm moment...returning an unlimited query, which happened to be several hundred thousand rows long. It was in a rarely used bit of code, so didn't happen often, but when it did it brought down the server. </p> <p>Always make sure your query results are limited, i.e.:</p> <pre><code>results = MyModel.que...
4
2010-01-13T21:36:38Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
3,299,378
<p>My worst mistake was using absolute imports like <code>&lt;project_name&gt;.&lt;app_name&gt;.models</code> rather than <code>&lt;app_name&gt;.models</code>. This way when I made a branch and wanted to check it out in different directory (like having and -stable of my project), it wouldn't run. I managed to revert i...
2
2010-07-21T12:50:24Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
3,299,807
<p>I've experienced a worst practice : use something else that the default id as primary key of model. </p> <p>It looked as a good idea but it caused some problems in the administration web site and it was difficult to restore the id as primary key on an existing database.</p> <p>I think that there is no specific cas...
2
2010-07-21T13:38:11Z
[ "python", "django" ]
Worst practices in Django you have ever seen
2,058,532
<p>What are the worst mistakes made using Django framework, that you have noticed? Have you seen some real misuses, that maybe should go as warnings to the Django docs?</p>
24
2010-01-13T16:50:31Z
3,496,620
<p>Not using raw_id fields for a key to 10000+ objects, then wondering why visiting the Admin brings a server to its knees</p>
5
2010-08-16T19:31:56Z
[ "python", "django" ]
Assigning one of multiple values based on what exists
2,058,544
<p>In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one <strong>evaluates to true</strong>, I would code the following:</p> <pre><code>$myVar = $var1 || $var2 || $var3; </code></pre> <p>I am working on separate projects in both Python and PHP, and I have not figured out how to cod...
0
2010-01-13T16:51:58Z
2,058,569
<p>In Python, I believe you can say</p> <pre><code>myVar = var1 or var2 or var3 </code></pre> <p>The Python documentation describes <code>x or y</code> as </p> <blockquote> <p>if x is false, then y, else x</p> </blockquote> <p><strong>Edit</strong>: theatrus brings up an interesting point - I hadn't even conside...
2
2010-01-13T16:55:58Z
[ "php", "python", "perl" ]
Assigning one of multiple values based on what exists
2,058,544
<p>In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one <strong>evaluates to true</strong>, I would code the following:</p> <pre><code>$myVar = $var1 || $var2 || $var3; </code></pre> <p>I am working on separate projects in both Python and PHP, and I have not figured out how to cod...
0
2010-01-13T16:51:58Z
2,058,577
<p>In PHP you can use <a href="http://php.net/manual/en/function.isset.php" rel="nofollow"><code>isset()</code></a> to test to see if the variable is set and not null. To achieve what you are looking for you can check whether <code>$var1</code>, <code>$var2</code> or <code>$var3</code> is set, and then set the value to...
1
2010-01-13T16:56:27Z
[ "php", "python", "perl" ]
Assigning one of multiple values based on what exists
2,058,544
<p>In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one <strong>evaluates to true</strong>, I would code the following:</p> <pre><code>$myVar = $var1 || $var2 || $var3; </code></pre> <p>I am working on separate projects in both Python and PHP, and I have not figured out how to cod...
0
2010-01-13T16:51:58Z
2,058,598
<p>Perl 'autovivifies' variables upon first reference (as far as I remember). Python will raise a <code>NameError</code> if a variable doesn't exist. However, you can do something like this. </p> <pre><code>var1 = var2 = var3 = None # code that might change the value of three variables mentioned above myvar = var1 or ...
3
2010-01-13T16:58:24Z
[ "php", "python", "perl" ]
Assigning one of multiple values based on what exists
2,058,544
<p>In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one <strong>evaluates to true</strong>, I would code the following:</p> <pre><code>$myVar = $var1 || $var2 || $var3; </code></pre> <p>I am working on separate projects in both Python and PHP, and I have not figured out how to cod...
0
2010-01-13T16:51:58Z
2,058,880
<p>There are also <a href="http://docs.python.org/whatsnew/2.5.html#pep-308-conditional-expressions" rel="nofollow">conditional expressions</a> in Python added in Python 2.5:</p> <pre><code>x = true_value if condition else false_value </code></pre> <p>I cannot speak to PHP.</p> <p>One thing to keep in mind with both...
0
2010-01-13T17:34:50Z
[ "php", "python", "perl" ]
Assigning one of multiple values based on what exists
2,058,544
<p>In Perl, if I want to assign $myVar a value of $var1, $var2, or $var3, based on which one <strong>evaluates to true</strong>, I would code the following:</p> <pre><code>$myVar = $var1 || $var2 || $var3; </code></pre> <p>I am working on separate projects in both Python and PHP, and I have not figured out how to cod...
0
2010-01-13T16:51:58Z
2,066,266
<p>In PHP you could do this (assumes the variables are set):</p> <pre><code>$vars = array($var1, $var2, $var3); foreach ($vars as $var) { if ($myvar = $var) { break; } } </code></pre> <p>Or, to make it slightly more perly :) :</p> <pre><code>foreach(array($var1,$var2,$var3)as$var)if($myvar=$var)break...
1
2010-01-14T17:51:16Z
[ "php", "python", "perl" ]
How to refine an initial query in Django?
2,058,573
<p>Following the reply to <a href="http://stackoverflow.com/questions/2030757/what-do-i-need-to-do-to-port-an-admin-application-to-frontend">this question</a> (Thanks again Ellie P!) I created a search page and a results page. </p> <p>For instance if you <a href="http://swimswith.com/search-form/" rel="nofollow">searc...
0
2010-01-13T16:56:13Z
2,060,037
<p>This view function answers the question:</p> <pre><code>def search(request): if 'q' in request.GET and request.GET['q']: q = request.GET['q'] q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True) q_year = Lawyer.objects.filter(last__icontains=q).values_list...
0
2010-01-13T20:34:45Z
[ "python", "django" ]
Python’s ftplib STOR reliable?
2,058,584
<p>I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp).</p> <pre><code>try: ftp = FTP(ftp.host.com) ftp.login(your_username, your_password) file = open(myfile.txt, "rb") ftp.st...
1
2010-01-13T16:57:05Z
2,058,624
<p>To get the exception earlier, use <a href="http://docs.python.org/library/socket.html?highlight=setdefaulttimeout#socket.setdefaulttimeout" rel="nofollow">socket.setdefaulttimeout</a>: e.g.,</p> <pre><code>import socket socket.setdefaulttimeout(20.0) </code></pre> <p>will give you an exception if a socket it block...
1
2010-01-13T17:01:43Z
[ "python", "exception", "ftp", "file-upload", "ftplib" ]
Python’s ftplib STOR reliable?
2,058,584
<p>I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp).</p> <pre><code>try: ftp = FTP(ftp.host.com) ftp.login(your_username, your_password) file = open(myfile.txt, "rb") ftp.st...
1
2010-01-13T16:57:05Z
2,058,636
<p>Don't use subprocess to shell out to delete a file - the os.unlink call will allow you to do this portably (the shutil library fills in the gaps when os fails)</p> <p>Right now, you are gobbling the error with your silly print statement - get a traceback from the exception which would give you a large number of clu...
2
2010-01-13T17:04:00Z
[ "python", "exception", "ftp", "file-upload", "ftplib" ]
Python’s ftplib STOR reliable?
2,058,584
<p>I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp).</p> <pre><code>try: ftp = FTP(ftp.host.com) ftp.login(your_username, your_password) file = open(myfile.txt, "rb") ftp.st...
1
2010-01-13T16:57:05Z
2,058,659
<p>how about this</p> <pre><code>def upload(ftp, filename): ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024) try: ftp = FTP("ftp.host.com") ftp.login(your_username, your_password) except Exception,e: print e else: file = open("myfile.txt", "rb") print 'STORing File now...' try...
1
2010-01-13T17:07:04Z
[ "python", "exception", "ftp", "file-upload", "ftplib" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
2,058,855
<p>Your question is a little vague, but I think what you are asking is how to specify it.</p> <p>You need to define <code>__version__</code> like so:</p> <pre><code>__version__ = '1.4.4' </code></pre> <p>And then you can confirm that setup.py knows about the version you just specified:</p> <pre><code>% ./setup.py -...
8
2010-01-13T17:30:37Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
2,058,872
<p>The best technique is to define <code>__version__</code> in your product code, then import it into setup.py from there. This gives you a value you can read in your running module, and have only one place to define it.</p> <p>The values in setup.py are not installed, and setup.py doesn't stick around after installa...
8
2010-01-13T17:33:14Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
2,073,599
<h2>Interrogate version string of already-installed distribution</h2> <p>To retrieve the version from inside your package at runtime (what your question appears to actually be asking), you can use:</p> <pre><code>import pkg_resources # part of setuptools version = pkg_resources.require("MyProject")[0].version </code...
127
2010-01-15T17:38:25Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
2,811,413
<p>To avoid importing a file (and thus executing its code) one could parse it and recover the <code>version</code> attribute from the syntax tree:</p> <pre><code># assuming 'path' holds the path to the file import ast with open(path, 'rU') as file: t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST) fo...
1
2010-05-11T14:13:08Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
3,619,714
<p>Create a file in your source tree, e.g. in yourbasedir/yourpackage/_version.py . Let that file contain only a single line of code, like this:</p> <p><code>__version__ = "1.1.0-r4704"</code></p> <p>Then in your setup.py, open that file and parse out the version number like this:</p> <pre> verstr = "unknown" try: ...
3
2010-09-01T15:30:37Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
5,872,024
<p>This should also work, using regular expressions and depending on the metadata fields to have a format like this:</p> <pre><code>__fieldname__ = 'value' </code></pre> <p>Use the following at the beginning of your setup.py:</p> <pre><code>import re main_py = open('yourmodule.py').read() metadata = dict(re.findall(...
1
2011-05-03T15:39:26Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
12,413,800
<p>I wasn't happy with these answers... didn't want to require setuptools, nor make a whole separate module for a single variable, so I came up with these.</p> <p>For when you are sure the main module is in pep8 style and will stay that way:</p> <pre><code>version = '0.30.unknown' with file('mypkg/mymod.py') as f: ...
5
2012-09-13T19:54:30Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
21,784,019
<p>There's a thousand ways to skin a cat -- here's mine:</p> <pre><code># Copied from (and hacked): # https://github.com/pypa/virtualenv/blob/develop/setup.py#L42 def get_version(filename): import os import re here = os.path.dirname(os.path.abspath(__file__)) f = open(os.path.join(here, filename)) ...
1
2014-02-14T16:06:53Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
24,517,154
<h1>example study: <code>mymodule</code></h1> <p>Imagine this configuration:</p> <pre><code>setup.py mymodule/ / __init__.py / version.py / myclasses.py </code></pre> <p>Then imagine some usual scenario where you have dependencies and <code>setup.py</code> looks like:</p> <pre><code>setup(.....
10
2014-07-01T18:39:27Z
[ "python", "setuptools" ]
How can I get the version defined in setup.py (setuptools) in my package?
2,058,802
<p>How could I get the version defined in setup.py from my package (for <code>--version</code>, or other purposes)?</p>
82
2010-01-13T17:24:49Z
30,471,662
<p>Cleaning up <a href="http://stackoverflow.com/a/12413800">http://stackoverflow.com/a/12413800</a> from @gringo-suave:</p> <pre><code>from itertools import ifilter from os import path from ast import parse with open(path.join('package_name', '__init__.py')) as f: __version__ = parse(next(ifilter(lambda line: li...
0
2015-05-27T01:30:02Z
[ "python", "setuptools" ]
Python-style classmethod for C#?
2,058,884
<p>Is there a way to do what <code>classmethod</code> does in Python in C#?</p> <p>That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from.</p> <p>An example of what I want, approximately, is</p> <pre><code>class Base: @classmethod def...
6
2010-01-13T17:35:02Z
2,058,896
<p>I think you want to take a look at generics.</p> <p>Guide from MSFT:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx</a></p>
3
2010-01-13T17:37:03Z
[ "c#", "python", "class-method" ]
Python-style classmethod for C#?
2,058,884
<p>Is there a way to do what <code>classmethod</code> does in Python in C#?</p> <p>That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from.</p> <p>An example of what I want, approximately, is</p> <pre><code>class Base: @classmethod def...
6
2010-01-13T17:35:02Z
2,061,820
<p>Depending on the context, you either want generics or virtual methods.</p>
2
2010-01-14T02:36:41Z
[ "c#", "python", "class-method" ]
Python-style classmethod for C#?
2,058,884
<p>Is there a way to do what <code>classmethod</code> does in Python in C#?</p> <p>That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from.</p> <p>An example of what I want, approximately, is</p> <pre><code>class Base: @classmethod def...
6
2010-01-13T17:35:02Z
2,070,448
<p>In principle, you <em>could</em> write something like this:</p> <pre><code>class Base { public static T Get&lt;T&gt;(int id) where T : Base, new() { return new T() { ID = id }; } public int ID { get; set; } } </code></pre> <p>Then you could write <code>var p = Base.Get&lt;Puppy&gt;...
1
2010-01-15T09:07:42Z
[ "c#", "python", "class-method" ]
Python-style classmethod for C#?
2,058,884
<p>Is there a way to do what <code>classmethod</code> does in Python in C#?</p> <p>That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from.</p> <p>An example of what I want, approximately, is</p> <pre><code>class Base: @classmethod def...
6
2010-01-13T17:35:02Z
2,070,486
<p>This is equivalent to class methods in Object Pascal. (a .Net implementation would be RemObject's oxygene).</p> <p>However, while class references and thus virtual class methods or seemingly static methods which can kinda access some class-level state are nice in there original platform, I don't think they make muc...
0
2010-01-15T09:17:06Z
[ "c#", "python", "class-method" ]
Python-style classmethod for C#?
2,058,884
<p>Is there a way to do what <code>classmethod</code> does in Python in C#?</p> <p>That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it's used from.</p> <p>An example of what I want, approximately, is</p> <pre><code>class Base: @classmethod def...
6
2010-01-13T17:35:02Z
24,428,089
<p>Because C# is statically compiled, a call to <code>Puppy.get()</code> is resolved at compile time, eg. it's known to point to <code>Base.get()</code> and that means that the generated CIL (Common Intermediate Language) will have a call instruction to <code>Base.get()</code>:</p> <pre><code>.method private hidebysig...
0
2014-06-26T10:26:32Z
[ "c#", "python", "class-method" ]
Python How to I check if last element has been reached in iterator tool chain?
2,058,894
<pre><code>for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement </code></pre> <p>How do I achieve this</p>
14
2010-01-13T17:36:52Z
2,058,910
<p>You can't per se. You'd need to store the current item, advance the iterator, and catch the <code>StopIteration</code> exception. And then you'd need to somehow signal that you have the last item.</p>
2
2010-01-13T17:39:10Z
[ "python" ]
Python How to I check if last element has been reached in iterator tool chain?
2,058,894
<pre><code>for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement </code></pre> <p>How do I achieve this</p>
14
2010-01-13T17:36:52Z
2,058,922
<p>When the loop ends, the <code>elt</code> variable doesn't go out of scope, and still holds the last value given to it by the loop. So you could just put the code at the end of the loop and operate on the <code>elt</code> variable. It's not terribly pretty, but Python's scoping rules aren't pretty either.</p> <p>The...
8
2010-01-13T17:40:35Z
[ "python" ]
Python How to I check if last element has been reached in iterator tool chain?
2,058,894
<pre><code>for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement </code></pre> <p>How do I achieve this</p>
14
2010-01-13T17:36:52Z
2,058,973
<p>You can do this by manually advancing the iterator in a while loop using <code>iter.next()</code>, then catching the <code>StopIteration</code> exception:</p> <pre><code>&gt;&gt;&gt; from itertools import chain &gt;&gt;&gt; it = chain([1,2,3],[4,5,6],[7,8,9]) &gt;&gt;&gt; while True: ... try: ... elem =...
16
2010-01-13T17:49:27Z
[ "python" ]
Python How to I check if last element has been reached in iterator tool chain?
2,058,894
<pre><code>for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement </code></pre> <p>How do I achieve this</p>
14
2010-01-13T17:36:52Z
13,655,329
<p>I do something like this:</p> <pre><code>rng = len(mlist) for i in range(rng): foo = mlist[i] foo.do_something_for_every_item_regardless() if i == rng - 1: #since we go from 0 to rng-1 foo.last_item_only_operation() </code></pre>
0
2012-12-01T01:02:49Z
[ "python" ]
How can I break up this long line in Python?
2,058,925
<p>How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide:</p> <pre><code>logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title)) </code></pre> <p>Is this my best option?</p> <pre><code>ur...
74
2010-01-13T17:41:07Z
2,058,937
<p>Consecutive string literals are joined by the compiler, and parenthesized expressions are considered to be a single line of code:</p> <pre><code>logger.info("Skipping {0} because it's thumbnail was " "already in our system as {1}.".format(line[indexes['url']], video.title)) </code></pre>
20
2010-01-13T17:43:47Z
[ "python", "formatting", "code-formatting" ]
How can I break up this long line in Python?
2,058,925
<p>How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide:</p> <pre><code>logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title)) </code></pre> <p>Is this my best option?</p> <pre><code>ur...
74
2010-01-13T17:41:07Z
2,059,018
<p>Personally I dislike hanging open blocks, so I'd format it as:</p> <pre><code>logger.info( 'Skipping {0} because its thumbnail was already in our system as {1}.' .format(line[indexes['url']], video.title) ) </code></pre> <p>In general I wouldn't bother struggle too hard to make code fit exactly within a 80...
4
2010-01-13T17:58:10Z
[ "python", "formatting", "code-formatting" ]
How can I break up this long line in Python?
2,058,925
<p>How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide:</p> <pre><code>logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title)) </code></pre> <p>Is this my best option?</p> <pre><code>ur...
74
2010-01-13T17:41:07Z
2,059,025
<p>That's a start. It's not a bad practice to define your longer strings outside of the code that uses them. It's a way to separate data and behavior. Your first option is to join string literals together implicitly by making them adjacent to one another:</p> <pre><code>("This is the first line of my text, " "which...
149
2010-01-13T17:59:25Z
[ "python", "formatting", "code-formatting" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
2,059,011
<p>as with any other list:</p> <pre><code>&gt;&gt;&gt; len(a) 3 </code></pre> <p>pythonic way to count sub-lists in a heterogeneous list would be:</p> <pre><code>&gt;&gt;&gt; sum(isinstance(i, list) for i in a) 3 </code></pre> <p>your second question is not clear. can you not read your code?</p>
3
2010-01-13T17:57:09Z
[ "python" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
2,059,012
<p>1) len(a) is 3.</p>
0
2010-01-13T17:57:15Z
[ "python" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
2,059,017
<p>The number of sub-lists is </p> <pre><code>len(a) </code></pre> <p>Each sublist is an element in the list, so you've got three elements in a, each of them is a list with three elements (which are numbers)</p>
0
2010-01-13T17:57:58Z
[ "python" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
2,059,023
<p>if different types are stored in a list you can could sublists this way:</p> <pre><code>n=0 for b in a: if type(b)==type([]): n+=1 </code></pre> <p>addition:</p> <p>yes, <code>sum(1 for x in a if isinstance(x, list))</code> is more pythonic</p>
0
2010-01-13T17:59:21Z
[ "python" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
2,059,028
<ol> <li><p><code>sum(1 for x in a if isinstance(x, list))</code></p> <p>This assumes that there could be things other than lists in a.</p></li> <li><p>No. Delegating to <code>itertools</code> generally means you give up knowing anything about the underlying values.</p></li> </ol>
3
2010-01-13T17:59:46Z
[ "python" ]
Python count sub lists in nested list
2,058,985
<p>I have created a list -></p> <pre><code>a = [[1,2,3],[4,5,6],[7,8,9]] </code></pre> <ol> <li><p>How do I get the count of number of sub-lists in a? Like in this case it is 3</p></li> <li><p>I am using iterator tool chain for traversing this list</p> <pre><code>for elt in itertools.chain.from_iterable(node): </co...
0
2010-01-13T17:51:50Z
22,362,723
<p>I found this page in an effort to learn how to get the sum all all the sublists. I figured out this could be done using:</p> <pre><code>sum(len(x) for x in a) </code></pre> <p>Maybe that would help someone else...</p>
3
2014-03-12T20:09:48Z
[ "python" ]
Utilizing objects in another class in Python
2,059,013
<p>In code(pseudo) like this</p> <pre><code>def path(): dirList = ['c:\\', 'y:\\', 'z:\\'] home_folder = 'peter.txt' complete = [s + home_folder for s in dirList] print complete def fileWrite(): filename = 'c:\peter.txt' text = 'Hello World' file = open(filename, 'w') file.write(text) ...
0
2010-01-13T17:57:20Z
2,059,043
<p>If I understand question correclty - you can add additional parameter to fileWrite like fileWrite(filename) and simply iterate over 'complete' sequence.</p>
1
2010-01-13T18:02:08Z
[ "python", "class", "iteration" ]
Utilizing objects in another class in Python
2,059,013
<p>In code(pseudo) like this</p> <pre><code>def path(): dirList = ['c:\\', 'y:\\', 'z:\\'] home_folder = 'peter.txt' complete = [s + home_folder for s in dirList] print complete def fileWrite(): filename = 'c:\peter.txt' text = 'Hello World' file = open(filename, 'w') file.write(text) ...
0
2010-01-13T17:57:20Z
2,059,064
<pre><code>import os def paths(filename): dirList = ['c:\\', 'y:\\', 'z:\\'] complete = [os.path.join(s, filename) for s in dirList] return complete def fileWrite(): for each_file in paths('c:\\peter.txt'): text = 'Hello World' file = open(each_file, 'w') file.write(text) ...
1
2010-01-13T18:06:56Z
[ "python", "class", "iteration" ]
Python If Statement with Multiple Conditions
2,059,111
<pre><code>if (message.value[0] == "/" or message.value[0] == "\"): do stuff. </code></pre> <p>I'm sure it's a simple syntax error, but something is wrong with this if statement.</p>
12
2010-01-13T18:14:58Z
2,059,120
<p>Escape the backslash:</p> <pre><code>if message.value[0] == "/" or message.value[0] == "\\": </code></pre> <p>From the <a href="http://docs.python.org/reference/lexical_analysis.html#string-literals">documentation</a>:</p> <blockquote> <p>The backslash (\) character is used to escape characters that otherwise...
42
2010-01-13T18:16:18Z
[ "python", "syntax" ]
Python If Statement with Multiple Conditions
2,059,111
<pre><code>if (message.value[0] == "/" or message.value[0] == "\"): do stuff. </code></pre> <p>I'm sure it's a simple syntax error, but something is wrong with this if statement.</p>
12
2010-01-13T18:14:58Z
2,059,128
<p>Try like this:</p> <pre><code>if message.value[0] == "/" or message.value[0] == "\\": do_stuff </code></pre>
3
2010-01-13T18:16:55Z
[ "python", "syntax" ]
Python If Statement with Multiple Conditions
2,059,111
<pre><code>if (message.value[0] == "/" or message.value[0] == "\"): do stuff. </code></pre> <p>I'm sure it's a simple syntax error, but something is wrong with this if statement.</p>
12
2010-01-13T18:14:58Z
2,059,135
<p>When you only need to check for equality, you can also simply use the <a href="http://docs.python.org/3/reference/expressions.html#in"><code>in</code></a> operator to do a membership test in a sequence of accepted elements:</p> <pre><code>if message.value[0] in ('/', '\\'): do_stuff() </code></pre>
48
2010-01-13T18:18:11Z
[ "python", "syntax" ]
Python If Statement with Multiple Conditions
2,059,111
<pre><code>if (message.value[0] == "/" or message.value[0] == "\"): do stuff. </code></pre> <p>I'm sure it's a simple syntax error, but something is wrong with this if statement.</p>
12
2010-01-13T18:14:58Z
36,402,401
<p>If message.value[] is string:</p> <pre><code>if message.value[0] in ('/', '\'): do_stuff() </code></pre> <p>If it not str</p>
0
2016-04-04T12:17:05Z
[ "python", "syntax" ]
After doing a SQLAlchemy update(), is there a way to get the changed values?
2,059,315
<p>After calling update(), I know that the return value has a .rowcount attribute that will reveal how many rows were changed. Is there a way to get the actual new <em>values</em> that they were changed to?</p> <p>For example, if I do the SQLAlchemy equivalent of:</p> <pre><code>UPDATE t SET x=x+1 WHERE y=z </code></...
0
2010-01-13T18:48:06Z
2,062,528
<p>The <code>ResultProxy</code> has two methods, <code>last_updated_params()</code> which returns a dictionary of every bind parameter value sent with the statement execution, as well as a collection <code>postfetch_cols()</code>, a list of columns for which an inline SQL expression was embedded in the UPDATE (for whic...
2
2010-01-14T06:19:45Z
[ "python", "sql", "sqlalchemy" ]
BeautifulSoup HTML table parsing
2,059,328
<p>I am trying to parse information (html tables) from this site: <a href="http://www.511virginia.org/RoadConditions.aspx?j=All&amp;r=1">http://www.511virginia.org/RoadConditions.aspx?j=All&amp;r=1</a></p> <p>Currently I am using BeautifulSoup and the code I have looks like this</p> <pre><code>from mechanize import B...
17
2010-01-13T18:50:14Z
2,059,366
<pre><code>start = cols[1].find('a').string </code></pre> <p>or simpler</p> <pre><code>start = cols[1].a.string </code></pre> <p>or better </p> <pre><code>start = str(cols[1].find(text=True)) </code></pre> <p>and</p> <pre><code>entry = [str(x) for x in cols.findAll(text=True)] </code></pre>
32
2010-01-13T18:56:45Z
[ "python", "table", "beautifulsoup", "mechanize", "html-parsing" ]
BeautifulSoup HTML table parsing
2,059,328
<p>I am trying to parse information (html tables) from this site: <a href="http://www.511virginia.org/RoadConditions.aspx?j=All&amp;r=1">http://www.511virginia.org/RoadConditions.aspx?j=All&amp;r=1</a></p> <p>Currently I am using BeautifulSoup and the code I have looks like this</p> <pre><code>from mechanize import B...
17
2010-01-13T18:50:14Z
21,205,174
<p>I was trying to reproduce your error, but the source html page was changed.</p> <p>About the error, I had a similar problem, trying to reproduce the example is <a href="http://palewi.re/posts/2008/04/20/python-recipe-grab-a-page-scrape-a-table-download-a-file/" rel="nofollow">here</a></p> <p>changing the proposed ...
2
2014-01-18T14:05:57Z
[ "python", "table", "beautifulsoup", "mechanize", "html-parsing" ]
Practices while releasing the python/ruby/script based web applications on production
2,059,337
<p>I am purely a windows programmer and spend all my time hacking VC++. </p> <p>Recently I have been heading several web based applications and myself built applications with python (/pylons framework) and doing projects on rails. All the web projects are hosted on ubuntu linux.</p> <p>The <strong>RELEASE</strong> pr...
1
2010-01-13T18:52:16Z
2,059,364
<p>I would create a branch in SVN for every release of web application and when the release is ready there, I would check it out on the server and set to be run or move it into the place of the old version.</p>
3
2010-01-13T18:56:12Z
[ "python", "ruby", "linux", "scripting", "release" ]
Practices while releasing the python/ruby/script based web applications on production
2,059,337
<p>I am purely a windows programmer and spend all my time hacking VC++. </p> <p>Recently I have been heading several web based applications and myself built applications with python (/pylons framework) and doing projects on rails. All the web projects are hosted on ubuntu linux.</p> <p>The <strong>RELEASE</strong> pr...
1
2010-01-13T18:52:16Z
2,059,406
<p>Is SVN checkout and svn update are the right methods to update the latest build files into the server? </p> <p>Very, very good methods. You know what you got. You can go backwards at any time.</p> <p>Are there any downside in using svn update? None.</p> <p>Any better method to release the script/web based scri...
2
2010-01-13T19:02:50Z
[ "python", "ruby", "linux", "scripting", "release" ]
Practices while releasing the python/ruby/script based web applications on production
2,059,337
<p>I am purely a windows programmer and spend all my time hacking VC++. </p> <p>Recently I have been heading several web based applications and myself built applications with python (/pylons framework) and doing projects on rails. All the web projects are hosted on ubuntu linux.</p> <p>The <strong>RELEASE</strong> pr...
1
2010-01-13T18:52:16Z
2,059,661
<p>The one downside of doing an svn update on your web root is that the .svn directories can potentially be made public, so be <a href="http://blog.beanstalkapp.com/2009/09/23/subversion-web-server-vulnerability/" rel="nofollow">careful about permissions on the web server</a>.</p> <p>That said, there are far better wa...
2
2010-01-13T19:40:36Z
[ "python", "ruby", "linux", "scripting", "release" ]
Practices while releasing the python/ruby/script based web applications on production
2,059,337
<p>I am purely a windows programmer and spend all my time hacking VC++. </p> <p>Recently I have been heading several web based applications and myself built applications with python (/pylons framework) and doing projects on rails. All the web projects are hosted on ubuntu linux.</p> <p>The <strong>RELEASE</strong> pr...
1
2010-01-13T18:52:16Z
4,454,448
<p>One downside of doing an svn update: though you can go back in time, to what revision do you go back to? You have to look it up. svn update pseudo-deployments work much cleaner if you use tags - in that case you'd be doing an svn switch to a different tag, not an svn update on the same branch or the trunk.</p> <...
0
2010-12-15T20:17:03Z
[ "python", "ruby", "linux", "scripting", "release" ]
Return Django form contents on error
2,059,374
<p>All,</p> <p>I have a template page say x.html</p> <p>i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it.</p> <p>If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error.</p> <p>My...
0
2010-01-13T18:57:34Z
2,059,412
<p>from <a href="http://docs.djangoproject.com/en/1.1/topics/forms/#using-a-form-in-a-view" rel="nofollow">docs</a>:</p> <blockquote> <p>The standard pattern for processing a form in a view looks like this:</p> </blockquote> <pre><code>def contact(request): if request.method == 'POST': # If the form has been su...
2
2010-01-13T19:03:25Z
[ "python", "django" ]
Return Django form contents on error
2,059,374
<p>All,</p> <p>I have a template page say x.html</p> <p>i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it.</p> <p>If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error.</p> <p>My...
0
2010-01-13T18:57:34Z
2,059,417
<p>If you are using django forms, it would do validation itself and then return the values you need. <a href="http://docs.djangoproject.com/en/dev/topics/forms/#form-objects" rel="nofollow">Here</a> you can read about using forms and how they validate values. Basically, when you pass some values into the form and it's ...
0
2010-01-13T19:04:04Z
[ "python", "django" ]
Return Django form contents on error
2,059,374
<p>All,</p> <p>I have a template page say x.html</p> <p>i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it.</p> <p>If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error.</p> <p>My...
0
2010-01-13T18:57:34Z
2,059,428
<p>I'm not sure how you are processing your form information. However if you use the Form API built into Django, it takes care of much of this for you. For details take a look at the Django Docs for Forms <a href="http://docs.djangoproject.com/en/dev/topics/forms/#topics-forms-index" rel="nofollow">http://docs.django...
0
2010-01-13T19:04:56Z
[ "python", "django" ]
Return Django form contents on error
2,059,374
<p>All,</p> <p>I have a template page say x.html</p> <p>i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it.</p> <p>If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error.</p> <p>My...
0
2010-01-13T18:57:34Z
2,059,581
<p>Django will write the submitted values back as long as you provide the form object to the rendered template. For example, in your view, something like:</p> <pre><code># handle POST form = MyForm(request.POST) if form.is_valid(): # do something and redirect else: # render the template with the invalid form ...
2
2010-01-13T19:28:15Z
[ "python", "django" ]
Lazy SAX XML parser with stop/resume
2,059,455
<p>I am pretty sure the answer is no but of course there are cleverer guys than me!</p> <p>Is there a way to construct a <strong>lazy SAX based XML parser</strong> that can be stopped (e.g. raising an exception is a possible way of doing this) but also <strong>resumable</strong> ?</p> <p>I am looking for a possible s...
1
2010-01-13T19:07:26Z
2,059,524
<p>Expat can be stopped and is resumable. AFAIK Python SAX parser uses Expat. Does the API really not expose the stopping stuff to the Python side?? </p> <p>EDIT: nope, looks like the parser stopping isn't available from Python...</p>
0
2010-01-13T19:19:32Z
[ "python", "xml", "sax" ]
Python - temporarily modify the current process's environment
2,059,482
<p>I use the following code to temporarily modify environment variables. </p> <pre><code>@contextmanager def _setenv(**mapping): """``with`` context to temporarily modify the environment variables""" backup_values = {} backup_remove = set() for key, value in mapping.items(): if key in os.enviro...
8
2010-01-13T19:13:32Z
2,059,494
<pre><code>_environ = dict(os.environ) ... os.environ.clear() os.environ.update(_environ) </code></pre>
18
2010-01-13T19:16:00Z
[ "environment-variables", "python" ]
Python - temporarily modify the current process's environment
2,059,482
<p>I use the following code to temporarily modify environment variables. </p> <pre><code>@contextmanager def _setenv(**mapping): """``with`` context to temporarily modify the environment variables""" backup_values = {} backup_remove = set() for key, value in mapping.items(): if key in os.enviro...
8
2010-01-13T19:13:32Z
34,333,710
<p>I suggest you the following implementation:</p> <pre><code>import contextlib @contextlib.contextmanager def set_env(**environ): """ Temporarily set the process environment variables. &gt;&gt;&gt; with set_env(PLUGINS_DIR=u'test/plugins'): ... "PLUGINS_DIR" in os.environ True &gt;&gt;&g...
4
2015-12-17T11:40:50Z
[ "environment-variables", "python" ]
Python: sort the list
2,059,564
<p>I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?</p> <p>EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I sh...
0
2010-01-13T19:25:31Z
2,059,572
<pre><code>[sorted(x) for x in c] </code></pre>
1
2010-01-13T19:26:41Z
[ "python", "list", "sorting" ]
Python: sort the list
2,059,564
<p>I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?</p> <p>EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I sh...
0
2010-01-13T19:25:31Z
2,059,594
<p>Let's take a look at what's going on here:</p> <pre><code># Initialize the lists a = ['a','b','d','c'] b = [1,2,4,3] c = [[],[]] # Assign the lists to positions in c c[0]=a c[1]=b # Sort b, which was assigned to c[1] c[1].sort() print(c) </code></pre> <p>So, of course you could not expect <code>a</code> to get s...
4
2010-01-13T19:30:23Z
[ "python", "list", "sorting" ]
Python: sort the list
2,059,564
<p>I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?</p> <p>EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I sh...
0
2010-01-13T19:25:31Z
2,059,595
<p>This appears to be what you really want to do:</p> <pre><code>&gt;&gt;&gt; a = ['a', 'z', 'd', 'c'] &gt;&gt;&gt; b = [1, 2, 4, 3] &gt;&gt;&gt; c = zip(a, b) &gt;&gt;&gt; c [('a', 1), ('z', 2), ('d', 4), ('c', 3)] &gt;&gt;&gt; import operator &gt;&gt;&gt; c.sort(key=operator.itemgetter(1)) # this would be equi...
3
2010-01-13T19:30:23Z
[ "python", "list", "sorting" ]
Python: sort the list
2,059,564
<p>I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?</p> <p>EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I sh...
0
2010-01-13T19:25:31Z
2,059,608
<p>In Python, an <code>array</code> (of the sort you are using) is called <code>list</code>. As for your problem, change <code>c[1].sort()</code> to <code>c[0].sort()</code> and your list of strings will be sorted instead of the list of ints contained in c[1].</p>
0
2010-01-13T19:32:18Z
[ "python", "list", "sorting" ]
Python: sort the list
2,059,564
<p>I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?</p> <p>EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I sh...
0
2010-01-13T19:25:31Z
2,059,623
<p>The first thing that comes to mind for me on this is to use the numpy array, rather than the builtin <code>list</code> datatype.</p> <p>Something like:</p> <pre><code>&gt;&gt;&gt; from numpy import * &gt;&gt;&gt; a = array(['a', 'b', 'd', 'c']) &gt;&gt;&gt; a.sort() &gt;&gt;&gt; print a ['a' 'b' 'c' 'd'] &gt;&gt;&...
1
2010-01-13T19:35:00Z
[ "python", "list", "sorting" ]