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
Django - How to do tuple unpacking in a template 'for' loop
271,077
<p>In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:</p> <pre><code>[ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] </code></pre> <p>In plain old Python, I could iteration the list like this:</p> <pre><code>for pr...
41
2008-11-07T02:40:20Z
271,098
<p>Just send the template a list of product types and do something like:</p> <pre><code>{% for product_type in product_type_list %} {{ product_type }} {% for product in product_type.products.all %} {{ product }} {% endfor %} {% endfor %} </code></pre> <p>It's been a little while so I can't remembe...
2
2008-11-07T02:47:41Z
[ "python", "django", "templates", "tuples", "iterable-unpacking" ]
Django - How to do tuple unpacking in a template 'for' loop
271,077
<p>In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:</p> <pre><code>[ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] </code></pre> <p>In plain old Python, I could iteration the list like this:</p> <pre><code>for pr...
41
2008-11-07T02:40:20Z
271,128
<p>it would be best if you construct your data like {note the '(' and ')' can be exchanged for '[' and ']' repectively, one being for tuples, one for lists}</p> <pre><code>[ (Product_Type_1, ( product_1, product_2 )), (Product_Type_2, ( product_3, product_4 )) ] </code></pre> <p>and have the template do this:</p> ...
50
2008-11-07T03:11:51Z
[ "python", "django", "templates", "tuples", "iterable-unpacking" ]
Django - How to do tuple unpacking in a template 'for' loop
271,077
<p>In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:</p> <pre><code>[ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] </code></pre> <p>In plain old Python, I could iteration the list like this:</p> <pre><code>for pr...
41
2008-11-07T02:40:20Z
1,168,438
<p>You must used this way:</p> <pre><code>{% for product_type, products in product_list.items %} print product_type {% for product in products %} print product {% endfor %} {% endfor %} </code></pre> <p>Don't forget the variable items in the dictionary data</p>
3
2009-07-22T21:41:13Z
[ "python", "django", "templates", "tuples", "iterable-unpacking" ]
Django - How to do tuple unpacking in a template 'for' loop
271,077
<p>In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:</p> <pre><code>[ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] </code></pre> <p>In plain old Python, I could iteration the list like this:</p> <pre><code>for pr...
41
2008-11-07T02:40:20Z
4,756,748
<p>Another way is as follows. </p> <p>If one has a list of tuples say</p> <pre><code>mylst = [(a, b, c), (x, y, z), (l, m, n)], </code></pre> <p>then one can unpack this list in the template file in the following manner. In my case I had a list of tuples which contained the URL, title, and summary of a document.</p>...
48
2011-01-21T08:23:38Z
[ "python", "django", "templates", "tuples", "iterable-unpacking" ]
Django - How to do tuple unpacking in a template 'for' loop
271,077
<p>In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:</p> <pre><code>[ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] </code></pre> <p>In plain old Python, I could iteration the list like this:</p> <pre><code>for pr...
41
2008-11-07T02:40:20Z
35,512,819
<p>If you have a fixed number in your tuples, you could just use indexing. I needed to mix a dictionary and the values were tuples, so I did this:</p> <p>In the view:</p> <pre><code>my_dict = {'parrot': ('dead', 'stone'), 'lumberjack': ('sleep_all_night', 'work_all_day')} </code></pre> <p>In the template:</p> <pre>...
0
2016-02-19T18:53:19Z
[ "python", "django", "templates", "tuples", "iterable-unpacking" ]
How does one add default (hidden) values to form templates in Django?
271,244
<p>Given a Django.db models class:</p> <pre><code>class P(models.Model): type = models.ForeignKey(Type) # Type is another models.Model class name = models.CharField() </code></pre> <p>where one wishes to create a new P with a specified type, i.e. how does one make "type" to be a default, hidden field (from the ...
7
2008-11-07T04:27:14Z
271,252
<p>To have a default Foreign Key in a model:</p> <pre><code>mydefault = Type.objects.get(pk=3) class P(models.Model): type = models.ForeignKey(Type, default=mydefault) # Type is another models.Model class name = models.CharField() </code></pre> <p>Note that using <code>pk=x</code> is pretty ugly, as ideally yo...
5
2008-11-07T04:32:23Z
[ "python", "django", "django-templates", "django-urls" ]
How does one add default (hidden) values to form templates in Django?
271,244
<p>Given a Django.db models class:</p> <pre><code>class P(models.Model): type = models.ForeignKey(Type) # Type is another models.Model class name = models.CharField() </code></pre> <p>where one wishes to create a new P with a specified type, i.e. how does one make "type" to be a default, hidden field (from the ...
7
2008-11-07T04:27:14Z
271,303
<p>The widget <code>django.forms.widgets.HiddenInput</code> will render your field as hidden.</p> <p>In most cases, I think you'll find that any hidden form value could also be specified as a url parameter instead. In other words:</p> <pre><code>&lt;form action="new/{{your_hidden_value}}" method="post"&gt; .... &lt;...
8
2008-11-07T05:26:25Z
[ "python", "django", "django-templates", "django-urls" ]
How does one add default (hidden) values to form templates in Django?
271,244
<p>Given a Django.db models class:</p> <pre><code>class P(models.Model): type = models.ForeignKey(Type) # Type is another models.Model class name = models.CharField() </code></pre> <p>where one wishes to create a new P with a specified type, i.e. how does one make "type" to be a default, hidden field (from the ...
7
2008-11-07T04:27:14Z
1,937,451
<p>If you go with Andrew's approach of including the hidden value in the url and still want to use one of Django's built in form templates, there are ways for you to exclude the hidden field.</p> <p><a href="http://docs.djangoproject.com/en/1.1/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form" rel="nofoll...
2
2009-12-20T23:16:18Z
[ "python", "django", "django-templates", "django-urls" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
271,494
<p>It may be a good approach to start with a script, and call a compilation-based language from that script only for more advanced needs.</p> <p>For instance, <a href="http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html" rel="nofollow">calling java from ruby script</a> works quite well.</p> <pre><code>req...
2
2008-11-07T08:11:45Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
271,501
<p>You can build your program in one of the higher level languages for example Python or Ruby and then call modules that are compiled in the lower level language for the parts you need performance. You can choose a platform depending on the lower level language you want.</p> <p>For example if you want to do C++ for th...
2
2008-11-07T08:17:10Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
271,590
<p>First, a meta comment: I would highly recommend coding the entire thing in a high-level language, profiling like mad, and optimizing only where profiling shows it's necessary. First optimize the algorithm, then the code, then think about bringing in the heavy iron. Having an optimum algorithm and clean code will mak...
9
2008-11-07T09:14:32Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
271,642
<p><a href="http://www.boost.org/doc/libs/1_36_0/libs/python/doc/tutorial/doc/html/index.html">Boost.Python</a> provides an easy way to turn C++ code into Python modules. It's rather mature and works well in my experience. </p> <p>For example, the inevitable Hello World...</p> <pre><code>char const* greet() { retur...
14
2008-11-07T09:46:13Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
271,729
<p>I agree with the Idea of coding first in a high level language such as Python, Profiling and then Implementing any code that needs speeding up in C / C++ and wrapping it for use in the high level language.</p> <p>As an alternative to boost I would like to suggest <a href="http://www.swig.org/">SWIG</a> for creatin...
6
2008-11-07T10:42:10Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
272,163
<p>Perl has several ways to use other languages. Look at the <a href="http://search.cpan.org/perldoc?Inline" rel="nofollow">Inline::<em></a> family of modules on CPAN. Following the advice from others in this question, I'd write the whole thing in a single dynamic language (Perl, Python, Ruby, etc) and then optimize th...
4
2008-11-07T14:10:07Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
272,363
<p>If you choose Perl there are plenty of resources for interfacing other languages.</p> <p><a href="http://search.cpan.org/dist/Inline/C/C.podI" rel="nofollow">Inline::C</a><br> <a href="http://search.cpan.org/dist/Inline-CPP/" rel="nofollow">Inline::CPP</a><br> <a href="http://search.cpan.org/dist/Inline-Java/Java.p...
5
2008-11-07T15:11:13Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
276,021
<p>I have a different perspective, having had lots of luck with integrating C++ and Python for some real time live video image processing.</p> <p>I would say you should match the language to the task for each module. If you're responding to a network, do it in Python, Python can keep up with network traffic just fine...
1
2008-11-09T15:33:16Z
[ "java", "c++", "python", "ruby", "perl" ]
Linking languages
271,488
<p>I asked <a href="http://stackoverflow.com/questions/269417/which-language-should-i-use">a question</a> earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the inter...
15
2008-11-07T08:07:10Z
276,242
<p>If the problem domain is hard (and AI problems can often be hard), then I'd choose a language which is expressive or suited to the domain first, and then worry about speeding it up second. For example, Ruby has meta-programming primitives (ability to easily examine and modify the running program) which can make it v...
1
2008-11-09T18:18:50Z
[ "java", "c++", "python", "ruby", "perl" ]
Interactive console using Pydev in Eclipse?
271,625
<p>I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.</p> <p>For example, the code stopped at a br...
36
2008-11-07T09:34:27Z
271,692
<p>Double click on "action" or any other variable.</p> <p>ctrl+shift+D</p> <p>And if you're using watches, I cant imagine better interaction. You are able to see every change.</p>
1
2008-11-07T10:16:48Z
[ "python", "debugging", "console", "pydev", "interactive" ]
Interactive console using Pydev in Eclipse?
271,625
<p>I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.</p> <p>For example, the code stopped at a br...
36
2008-11-07T09:34:27Z
340,875
<p>This feature is documented here:</p> <p><a href="http://pydev.org/manual_adv_debug_console.html">http://pydev.org/manual_adv_debug_console.html</a></p>
29
2008-12-04T15:02:58Z
[ "python", "debugging", "console", "pydev", "interactive" ]
Interactive console using Pydev in Eclipse?
271,625
<p>I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.</p> <p>For example, the code stopped at a br...
36
2008-11-07T09:34:27Z
1,116,728
<p>When I set a break point and hit F11 Eclipse launches the debugger and prompts to open the "Debug Perspective". You can then open the Window-->Show View --> Expressions which opens the expressions view, you can then right click in the Expressions view windows and choose "Add Watch Expression" to add any expression(...
0
2009-07-12T19:38:48Z
[ "python", "debugging", "console", "pydev", "interactive" ]
Interactive console using Pydev in Eclipse?
271,625
<p>I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.</p> <p>For example, the code stopped at a br...
36
2008-11-07T09:34:27Z
2,117,923
<p>The console that opens in the debug perspective is in fact interactive, although it took me a while to realize it. You need to hit return twice after typing something and then it gets evaluated. More info on the Pydev site here: <a href="http://pydev.org/manual_adv_debug_console.html">http://pydev.org/manual_adv_de...
13
2010-01-22T14:38:10Z
[ "python", "debugging", "console", "pydev", "interactive" ]
Interactive console using Pydev in Eclipse?
271,625
<p>I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.</p> <p>For example, the code stopped at a br...
36
2008-11-07T09:34:27Z
31,548,800
<p>On a small monitor, you may not realize that the debug interactive console is different from the regular interactive console: it has a second command prompt at the bottom where you type, not at the top like the normal console.</p>
0
2015-07-21T20:16:44Z
[ "python", "debugging", "console", "pydev", "interactive" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
271,693
<p>Well, you parse the email header into a dictionary. And then you check if Content-Transfer-Encoding is set, and if it = "base64" or "base-64".</p>
0
2008-11-07T10:17:08Z
[ "python", "jython", "base64", "mime" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
271,705
<p>@gnud, @edg - Unless I misunderstand, he's asking about the filename, not the file content @setori - the Content-Trasfer-Encoding is telling you how the CONTENT of the file is encoded, not the "filename".</p> <p>I'm not an expert, but this part here in the filename is telling him about the characters that follow:</...
7
2008-11-07T10:24:32Z
[ "python", "jython", "base64", "mime" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
271,720
<p>The header value tells you this:</p> <pre> =?gb2312?B?uLGxvmhlbrixsb5nLnhscw==?= "=?" introduces an encoded value "gb2312" denotes the character encoding of the original value "B" denotes that B-encoding (equal to Base64) was used (the alternative is "Q", which refers to something close to quote...
21
2008-11-07T10:35:59Z
[ "python", "jython", "base64", "mime" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
271,832
<blockquote> <p>Please note both <code>Content-Transfer-Encoding</code> have base64</p> </blockquote> <p>Not relevant in this case, the <code>Content-Transfer-Encoding</code> only applies to the body payload, not to the headers.</p> <pre><code>=?gb2312?B?uLGxvmhlbrixsb5nLnhscw==?= </code></pre> <p>That's an <stron...
12
2008-11-07T11:38:38Z
[ "python", "jython", "base64", "mime" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
1,687,765
<p>There is a better way than bobince’s method to handle the output of <code>decode_header</code>. I found it here: <a href="http://mail.python.org/pipermail/email-sig/2007-March/000332.html" rel="nofollow">http://mail.python.org/pipermail/email-sig/2007-March/000332.html</a></p> <pre><code>name = unicode(email.head...
2
2009-11-06T13:58:52Z
[ "python", "jython", "base64", "mime" ]
how to tell if a string is base64 or not
271,657
<p>I have many emails coming in from different sources. they all have attachments, many of them have attachment names in chinese, so these names are converted to base64 by their email clients.</p> <p>When I receive these emails, I wish to decode the name. but there are other names which are not base64. How can I diffe...
6
2008-11-07T09:54:34Z
2,955,981
<p>Question: """Also I actually need to know what type of file it is ie .xls or .doc so I do need to decode the filename in order to correctly process the attachment, but as above, seems gb2312 is not supported in jython, know any roundabouts?"""</p> <p>Data:</p> <pre><code>Content-Type: application/vnd.ms-excel; na...
0
2010-06-02T08:14:45Z
[ "python", "jython", "base64", "mime" ]
How should I stress test / load test a client server application?
271,825
<p>I develop a client-server style, database based system and I need to devise a way to stress / load test the system. Customers inevitably want to know such things as:</p> <p>• How many clients can a server support?<br /> • How many concurrent searches can a server support?<br /> • How much data can we store i...
12
2008-11-07T11:35:00Z
271,839
<p><strong>Test 1</strong>: Connect and Disconnect clients like mad, to see how well you handle the init and end of sessions, and just how much your server will survive under spikes, also while doing this measure how many clients fail to connect. That is very important</p> <p><strong>Test 2</strong>: Connect clients a...
8
2008-11-07T11:44:40Z
[ "python", "database", "client-server", "load-testing", "stress-testing" ]
How should I stress test / load test a client server application?
271,825
<p>I develop a client-server style, database based system and I need to devise a way to stress / load test the system. Customers inevitably want to know such things as:</p> <p>• How many clients can a server support?<br /> • How many concurrent searches can a server support?<br /> • How much data can we store i...
12
2008-11-07T11:35:00Z
271,853
<p>If you are comfortable coding tests in Python, I've found <a href="http://funkload.nuxeo.org/">funkload</a> to be very capable. You don't say your server is http-based, so you may have to adapt their test facilities to your own client/server style. </p> <p>Once you have a test in Python, funkload can run it on ma...
6
2008-11-07T11:55:19Z
[ "python", "database", "client-server", "load-testing", "stress-testing" ]
How should I stress test / load test a client server application?
271,825
<p>I develop a client-server style, database based system and I need to devise a way to stress / load test the system. Customers inevitably want to know such things as:</p> <p>• How many clients can a server support?<br /> • How many concurrent searches can a server support?<br /> • How much data can we store i...
12
2008-11-07T11:35:00Z
271,891
<p>If you have the budget, LoadRunner would be perfect for this.</p>
0
2008-11-07T12:16:08Z
[ "python", "database", "client-server", "load-testing", "stress-testing" ]
How should I stress test / load test a client server application?
271,825
<p>I develop a client-server style, database based system and I need to devise a way to stress / load test the system. Customers inevitably want to know such things as:</p> <p>• How many clients can a server support?<br /> • How many concurrent searches can a server support?<br /> • How much data can we store i...
12
2008-11-07T11:35:00Z
271,918
<p>For performance you are looking at two things: latency (the responsiveness of the application) and throughput (how many ops per interval). For latency you need to have an acceptable benchmark. For throughput you need to have a minimum acceptable throughput.</p> <p>These are you starting points. For telling a client...
5
2008-11-07T12:25:41Z
[ "python", "database", "client-server", "load-testing", "stress-testing" ]
How should I stress test / load test a client server application?
271,825
<p>I develop a client-server style, database based system and I need to devise a way to stress / load test the system. Customers inevitably want to know such things as:</p> <p>• How many clients can a server support?<br /> • How many concurrent searches can a server support?<br /> • How much data can we store i...
12
2008-11-07T11:35:00Z
11,406,913
<p>On a related note: Twitter recently OpenSourced their <a href="https://github.com/twitter/iago/" rel="nofollow">load-testing framework</a>. Could be worth a go :)</p>
0
2012-07-10T05:04:21Z
[ "python", "database", "client-server", "load-testing", "stress-testing" ]
How to scan a webpage and get images and youtube embeds?
271,855
<p>I am building a web app where I need to get all the images and any flash videos that are embedded (e.g. youtube) on a given URL. I'm using Python.</p> <p>I've googled, but have not found any good information about this (probably because I don't know what this is called to search for), does anyone have any experienc...
2
2008-11-07T11:55:41Z
271,860
<p><a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> is a great screen-scraping library. Use urllib2 to fetch the page, and BeautifulSoup to parse it apart. Here's a code sample from their docs:</p> <pre><code>import urllib2 from BeautifulSoup import BeautifulSoup page = urlli...
7
2008-11-07T11:58:27Z
[ "python", "web-applications", "screen-scraping" ]
User Authentication in Django
272,042
<p>is there any way of making sure that, one user is logged in only once?</p> <p>I would like to avoid two different persons logging into the system with the same login/password.</p> <p>I guess I could do it myself by checking in the django_session table before logging in the user, but I rather prefer using the frame...
2
2008-11-07T13:21:04Z
272,071
<p>Logged in twice is ambiguous over HTTP. There's no "disconnecting" signal that's sent. You can frustrate people if you're not careful.</p> <p>If I shut down my browser and drop the cookies -- accidentally -- I might be prevented from logging in again. </p> <p>How would the server know it was me trying to re-log...
5
2008-11-07T13:37:07Z
[ "python", "django" ]
User Authentication in Django
272,042
<p>is there any way of making sure that, one user is logged in only once?</p> <p>I would like to avoid two different persons logging into the system with the same login/password.</p> <p>I guess I could do it myself by checking in the django_session table before logging in the user, but I rather prefer using the frame...
2
2008-11-07T13:21:04Z
272,470
<p>A site I did last year was concerned that usernames/passwords might be posted to a forum. I dealt with this by adding a model and a check to the login view that looked at how many unique IPs the name had been used from in the last X hours. I gave the site admins two values in settings.py to adjust the number of hour...
4
2008-11-07T15:39:19Z
[ "python", "django" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
272,605
<p>There is also <a href="http://darcs.idyll.org/~t/projects/figleaf/doc/" rel="nofollow">figleaf</a> which I think is based on Ned Batchelder's coverage.py. We use <a href="http://somethingaboutorange.com/mrl/projects/nose/" rel="nofollow">nose</a> as the driver for the testing. It all works pretty well. We write our ...
2
2008-11-07T16:22:17Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
273,246
<p>We use this <a href="http://www.djangosnippets.org/snippets/705/" rel="nofollow">Django coverage integration</a>, but instead of using the default coverage.py reporting, we generate some simple HTML: <a href="http://code.activestate.com/recipes/52298/" rel="nofollow">Colorize Python source using the built-in tokeni...
5
2008-11-07T19:10:05Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
288,711
<p><a href="http://code.google.com/p/testoob/" rel="nofollow">Testoob</a> has a neat "<code>--coverage</code>" command-line option to generate a coverage report.</p>
0
2008-11-13T23:12:22Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
324,468
<p>PyDev seems to allow code coverage from within Eclipse. </p> <p>I've yet to find how to integrate that with my own (rather complex) build process, so I use Ned Batchelder's coverage.py at the command line.</p>
4
2008-11-27T18:50:18Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
373,619
<p>NetBeans' new Python support has tightly integrated code coverage support - <a href="http://blogs.oracle.com/tor/entry/netbeans_screenshot_of_the_week6" rel="nofollow">more info here</a>.</p>
2
2008-12-17T03:58:49Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
Code Coverage and Unit Testing of Python Code
272,188
<p>I have already visited <a href="http://stackoverflow.com/questions/191673/preferred-python-unit-testing-framework">Preferred Python unit-testing framework</a>. I am not just looking at Python Unit Testing Framework, but also code coverage with respect to unit tests. So far I have only come across <a href="http://ned...
25
2008-11-07T14:19:49Z
2,705,889
<p>If you want interactive code coverage, where you can see your coverage stats change in real time, take a look at <a href="http://www.softwareverify.com/python/coverage/index.html" rel="nofollow">Python Coverage Validator</a>.</p>
1
2010-04-24T19:46:26Z
[ "python", "visual-studio-2008", "unit-testing", "code-coverage" ]
"File -X: does not exist" message from ipy.exe in Windows PowerShell
272,233
<p>If I type this line in an MS-DOS command prompt window: </p> <pre><code>ipy -X:ColorfulConsole </code></pre> <p>IronPython starts up as expected with the colorful console option enabled. However, if I type the same line in Windows PowerShell I get the message: </p> <pre><code>File -X: does not exist </code></pr...
1
2008-11-07T14:31:30Z
272,283
<p>try:</p> <pre><code>ipy '-X:ColorfulConsole' </code></pre> <p>Or whatever quoting mechanism is supported in Windows PowerShell - the shell is splitting your argument. </p> <p>Typing</p> <pre><code>ipy -X: ColorfulConsole </code></pre> <p>in MS-DOS command prompt window returns same response <code>File -X: does ...
5
2008-11-07T14:45:15Z
[ "python", "powershell", "ironpython" ]
Python's os.execvp equivalent for PHP
272,826
<p>I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.</p> <p>I've tried the following functions:</p> <ul> <li>system</li> <li>passthru</li> <li>exec</li> <li>shell_exec</l...
4
2008-11-07T17:21:55Z
272,845
<p>Give MySQL a script to run that's separate from the PHP script:</p> <pre><code>system('mysql -u root -pxxxx db_name &lt; script.mysql'); </code></pre>
0
2008-11-07T17:26:48Z
[ "php", "python", "shell", "command-line-interface" ]
Python's os.execvp equivalent for PHP
272,826
<p>I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.</p> <p>I've tried the following functions:</p> <ul> <li>system</li> <li>passthru</li> <li>exec</li> <li>shell_exec</l...
4
2008-11-07T17:21:55Z
272,894
<p>In addition to what acrosman said, you can also use the <code>-e</code> switch to pass SQL from the command line.</p> <pre><code>$sql = "....."; system("mysql -u root -pxxxx db_name -e \"$sql\""); </code></pre> <p>Also, I hope your not actually connecting to the database as root from a PHP application.</p>
0
2008-11-07T17:37:35Z
[ "php", "python", "shell", "command-line-interface" ]
Python's os.execvp equivalent for PHP
272,826
<p>I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.</p> <p>I've tried the following functions:</p> <ul> <li>system</li> <li>passthru</li> <li>exec</li> <li>shell_exec</l...
4
2008-11-07T17:21:55Z
273,992
<p>If you want shell commands to be interactive, use:</p> <pre><code>system("mysql -uroot -p db_name &gt; `tty`"); </code></pre> <p>That will work for most cases, but will break if you aren't in a terminal.</p>
3
2008-11-07T23:28:59Z
[ "php", "python", "shell", "command-line-interface" ]
Generating a WSDL using Python and SOAPpy
273,002
<p>First of all, I will admit I am a novice to web services, although I'm familiar with HTML and basic web stuff. I created a quick-and-dirty web service using Python that calls a stored procedure in a MySQL database, that simply returns a BIGINT value. I want to return this value in the web service, and I want to ge...
11
2008-11-07T18:05:08Z
274,366
<blockquote> <p>I want to generate a WSDL that I can give to the web folks, ....</p> </blockquote> <p>You can try <a href="http://soaplib.github.com/soaplib/2_0/" rel="nofollow">soaplib</a>. It has on-demand WSDL generation.</p>
1
2008-11-08T04:34:26Z
[ "python", "soap", "wsdl", "soappy", "zsi" ]
Generating a WSDL using Python and SOAPpy
273,002
<p>First of all, I will admit I am a novice to web services, although I'm familiar with HTML and basic web stuff. I created a quick-and-dirty web service using Python that calls a stored procedure in a MySQL database, that simply returns a BIGINT value. I want to return this value in the web service, and I want to ge...
11
2008-11-07T18:05:08Z
276,994
<p>When I tried to write Python web service last year, I ended up using <a href="http://pywebsvcs.sourceforge.net/">ZSI-2.0</a> (which is something like heir of SOAPpy) and a <a href="http://pywebsvcs.sourceforge.net/holger.pdf">paper available on its web</a>.</p> <p>Basically I wrote my WSDL file by hand and then use...
8
2008-11-10T03:25:11Z
[ "python", "soap", "wsdl", "soappy", "zsi" ]
Generating a WSDL using Python and SOAPpy
273,002
<p>First of all, I will admit I am a novice to web services, although I'm familiar with HTML and basic web stuff. I created a quick-and-dirty web service using Python that calls a stored procedure in a MySQL database, that simply returns a BIGINT value. I want to return this value in the web service, and I want to ge...
11
2008-11-07T18:05:08Z
8,336,051
<p>Sorry for the question few days ago. Now I can invoke the server successfully. A demo is provided:</p> <pre><code>def test_soappy(): """test for SOAPpy.SOAPServer """ #okay # it's good for SOAPpy.SOAPServer. # in a method,it can have morn than 2 ws server. server = SOAPProxy("http://localhos...
0
2011-12-01T02:59:53Z
[ "python", "soap", "wsdl", "soappy", "zsi" ]
Python memory debugging with GDB
273,043
<p>We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message "Python Fatal Error: GC Object already tracked," which would appear to be either a programming error on the part of the library, or a symptom of memory co...
4
2008-11-07T18:13:58Z
273,063
<p>If you have mac or sun box kicking around you could use <a href="http://en.wikipedia.org/wiki/DTrace" rel="nofollow">dtrace</a> and a version of python compiled with dtrace to figure out what the application was doing at the time. Note: in 10.5 python is pre-compiled with dtrace which is really nice and handy.</p> ...
1
2008-11-07T18:20:16Z
[ "python", "linux", "debugging", "openssl" ]
Python memory debugging with GDB
273,043
<p>We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message "Python Fatal Error: GC Object already tracked," which would appear to be either a programming error on the part of the library, or a symptom of memory co...
4
2008-11-07T18:13:58Z
273,111
<p>Yes, you can do this kind of thing:</p> <pre><code>(gdb) print PyRun_SimpleString("import traceback; traceback.print_stack()") File "&lt;string&gt;", line 1, in &lt;module&gt; File "/var/tmp/foo.py", line 2, in &lt;module&gt; i**2 File "&lt;string&gt;", line 1, in &lt;module&gt; $1 = 0 </code></pre> <p>I...
5
2008-11-07T18:36:04Z
[ "python", "linux", "debugging", "openssl" ]
Python memory debugging with GDB
273,043
<p>We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message "Python Fatal Error: GC Object already tracked," which would appear to be either a programming error on the part of the library, or a symptom of memory co...
4
2008-11-07T18:13:58Z
273,204
<p>If you're using CDLL to wrap a C library in python, and this is 64-bit linux, there's a good chance that you're CDLL wrapper is misconfigured. CDLL defaults to int return types on all platforms (should be a long long on 64-bit systems) and just expects you to pass the right arguments in. You may need to verify the...
0
2008-11-07T18:59:10Z
[ "python", "linux", "debugging", "openssl" ]
Python memory debugging with GDB
273,043
<p>We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message "Python Fatal Error: GC Object already tracked," which would appear to be either a programming error on the part of the library, or a symptom of memory co...
4
2008-11-07T18:13:58Z
273,212
<p>In addition to all above one can quickly implement an adhoc tracer via the <a href="http://www.python.org/doc/2.5.2/lib/module-trace.html" rel="nofollow">trace module</a>.</p>
0
2008-11-07T19:03:15Z
[ "python", "linux", "debugging", "openssl" ]
Testing Web Services Consumer
273,060
<p>Here are some tools that I have found to test web services consumers:</p> <p><a href="http://www.soapui.org/" rel="nofollow">http://www.soapui.org/</a> https://wsunit.dev.java.net/</p> <p>Are there any others? I would prefer testing frameworks that are written in Java or Python.</p>
2
2008-11-07T18:19:54Z
273,116
<p>The Grinder is right up your ally with both Java and Python, that handles most web services, (SOAP/REST/CORBA/RMI/JMS/EJB) etc.</p> <p><a href="http://grinder.sourceforge.net/" rel="nofollow">http://grinder.sourceforge.net/</a></p>
0
2008-11-07T18:36:57Z
[ "java", "python", "web-services", "testing", "integration-testing" ]
Testing Web Services Consumer
273,060
<p>Here are some tools that I have found to test web services consumers:</p> <p><a href="http://www.soapui.org/" rel="nofollow">http://www.soapui.org/</a> https://wsunit.dev.java.net/</p> <p>Are there any others? I would prefer testing frameworks that are written in Java or Python.</p>
2
2008-11-07T18:19:54Z
273,681
<p>I have used soapui by a maven plugin. It can create junit-linke reports to be run and analysed like unit tests. This can be easily integrated in continious build, also with the free distribution of soapui.</p>
1
2008-11-07T21:23:27Z
[ "java", "python", "web-services", "testing", "integration-testing" ]
Testing Web Services Consumer
273,060
<p>Here are some tools that I have found to test web services consumers:</p> <p><a href="http://www.soapui.org/" rel="nofollow">http://www.soapui.org/</a> https://wsunit.dev.java.net/</p> <p>Are there any others? I would prefer testing frameworks that are written in Java or Python.</p>
2
2008-11-07T18:19:54Z
276,338
<p>I've used <a href="http://www.codeplex.com/WebserviceStudio" rel="nofollow">Web Service Studio</a>.</p> <blockquote> <p><strong>Web Service Studio</strong> is a tool to invoke web methods interactively. The user can provide a WSDL endpoint. On clicking button Get the tool fetches the WSDL, generates .NET pro...
1
2008-11-09T19:20:43Z
[ "java", "python", "web-services", "testing", "integration-testing" ]
Testing Web Services Consumer
273,060
<p>Here are some tools that I have found to test web services consumers:</p> <p><a href="http://www.soapui.org/" rel="nofollow">http://www.soapui.org/</a> https://wsunit.dev.java.net/</p> <p>Are there any others? I would prefer testing frameworks that are written in Java or Python.</p>
2
2008-11-07T18:19:54Z
284,528
<p>You really need to be more specific: What is it that you want to test in your WS-consumer? That it calls the right WS? This looks a bit pointless - WS are a perfect place for mocking whatever may be called - without anything being called.</p> <p>In order to test the consumer you'd otherwise be writing a Webservice ...
0
2008-11-12T16:36:36Z
[ "java", "python", "web-services", "testing", "integration-testing" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
273,206
<p>Try the <a href="https://docs.python.org/2/library/os.path.html#os.path.exists"><code>os.path.exists</code></a> function</p> <pre><code>if not os.path.exists(dir): os.mkdir(dir) </code></pre>
10
2008-11-07T19:00:01Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
273,208
<p>Check out <a href="https://docs.python.org/3/library/os.html#os.makedirs">os.makedirs</a>: (It makes sure the complete path exists.) To handle the fact the directory might exist, catch OSError.</p> <pre><code>import os try: os.makedirs('./path/to/somewhere') except OSError: pass </code></pre>
40
2008-11-07T19:01:25Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
273,227
<p>I see two answers with good qualities, each with a small flaw, so I will give my take on it:</p> <p>Try <a href="https://docs.python.org/2/library/os.path.html#os.path.exists"><code>os.path.exists</code></a>, and consider <a href="https://docs.python.org/2/library/os.html#os.makedirs"><code>os.makedirs</code></a> f...
2,049
2008-11-07T19:06:07Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
273,679
<p>I have put the following down. It's not totally foolproof though.</p> <pre><code>import os dirname = 'create/me' try: os.makedirs(dirname) except OSError: if os.path.exists(dirname): # We are nearly safe pass else: # There was an error on creation, so make sure we know about it...
17
2008-11-07T21:23:03Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
444,023
<p>I would personally recommend that you use <code>os.path.isdir()</code> to test instead of <code>os.path.exists()</code>.</p> <pre><code>&gt;&gt;&gt; os.path.exists('/tmp/dirname') True &gt;&gt;&gt; os.path.exists('/tmp/dirname/filename.etc') True &gt;&gt;&gt; os.path.isdir('/tmp/dirname/filename.etc') False &gt;&gt...
63
2009-01-14T17:57:32Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
5,032,238
<p>Using try except and the right error code from errno module gets rid of the race condition and is cross-platform:</p> <pre><code>import os import errno def make_sure_path_exists(path): try: os.makedirs(path) except OSError as exception: if exception.errno != errno.EEXIST: raise ...
434
2011-02-17T17:17:25Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
14,364,249
<h3>Python 2.7:</h3> <p>While a naive solution may first use <a href="https://docs.python.org/2/library/os.path.html#os.path.isdir" rel="nofollow" title="os.path.isdir"><code>os.path.isdir</code></a> followed by <a href="https://docs.python.org/2/library/os.html#os.makedirs" rel="nofollow" title="os.makedirs"><code>os...
327
2013-01-16T17:31:19Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
18,562,920
<pre><code>import os if not os.path.isfile("test") and not os.path.isdir("test"): os.mkdir("test") </code></pre>
-3
2013-09-01T21:04:50Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
24,740,135
<p>The <a href="https://docs.python.org/2/library/os.html#files-and-directories">relevant python documentation</a> suggests the use of the <a href="https://docs.python.org/2/library/os.html#files-and-directories">EAFP coding style (Easier to Ask for Forgiveness than Permission)</a>. This means that the code</p> <pre><...
5
2014-07-14T15:29:07Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
28,100,717
<blockquote> <p><strong>Check if a directory exists and create it if necessary?</strong></p> </blockquote> <p>The direct answer to this is, assuming a simple situation where you don't expect other users or processes to be messing with your directory:</p> <pre><code>if not os.path.exists(d): os.makedirs(d) </cod...
4
2015-01-22T23:45:59Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
28,100,757
<h1>Insights on the specifics of this situation</h1> <p>You give a particular file at a certain path and you pull the directory from the file path. Then after making sure you have the directory, you attempt to open a file for reading. To comment on this code:</p> <blockquote> <pre><code>filename = "/my/directory/file...
15
2015-01-22T23:49:18Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
28,997,083
<p>In Python 3.4 you can also use the <a href="https://docs.python.org/3/library/pathlib.html">brand new <code>pathlib</code> module</a>:</p> <pre><code>from pathlib import Path path = Path("/my/directory/filename.txt") try: if not path.parent.exists(): path.parent.mkdir(parents=True) except OSError: #...
7
2015-03-11T20:50:01Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
35,262,209
<p>I saw <a href="http://stackoverflow.com/a/5032238/3051142">Heikki Toivonen</a> and <a href="http://stackoverflow.com/a/14364249">A-B-B</a>'s answers and thought of this variation. What do you think?</p> <pre><code>import os import errno def make_sure_path_exists(path): try: os.makedirs(path) except...
2
2016-02-08T04:08:06Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
36,289,129
<p>For a one-liner solution, you can use <code>IPython.utils.path.ensure_dir_exists()</code>:</p> <pre><code>from IPython.utils.path import ensure_dir_exists ensure_dir_exists(dir) </code></pre> <p>From the <a href="https://ipython.org/ipython-doc/3/api/generated/IPython.utils.path.html#IPython.utils.path.ensure_dir_...
3
2016-03-29T15:50:53Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
37,703,074
<p>You can use os.listdir for this:</p> <pre><code>import os if 'dirName' in os.listdir('parentFolderPath') print('Directory Exists') </code></pre>
2
2016-06-08T12:52:46Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
39,205,326
<pre><code>import os os.system("mkdir -p ./testdir") </code></pre> <p>-p does all the work for you, why go into all this try/catch stuff when both linux and windows powershell have mkdir -p for years. The only reason to do the check separately is if you want to print the info to a log or screen.</p>
0
2016-08-29T11:34:25Z
[ "python", "exception", "directory" ]
How to check if a directory exists and create it if necessary?
273,192
<p>What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:</p> <pre><code>filename = "/my/directory/filename.txt" dir = os.path.dirname(filename) try: os.stat(dir) except: os.mkdir(dir) f = fil...
1,739
2008-11-07T18:56:45Z
39,479,473
<p>You can use <a href="https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.mkpath" rel="nofollow"><code>mkpath</code></a></p> <pre><code># Create a directory and any missing ancestor directories. # If the directory already exists, do nothing. from distutils.dir_util import mkpath mkpath("test") <...
0
2016-09-13T21:44:56Z
[ "python", "exception", "directory" ]
Access second result set of stored procedure with SQL or other work-around? Python\pyodbc
273,203
<p>I'm using python\pyodbc and would like to access the second result set of a stored procedure. As near as I can tell, pyodbc does not support multiple result sets. Additionally, I can't modify the stored procedure. Are there any options to access the second result set using SQL or some other work-around? Perhaps crea...
6
2008-11-07T18:58:47Z
273,386
<p>There are a few possible methods <a href="http://www.sommarskog.se/share_data.html" rel="nofollow">here</a>. If the result sets are all the same, you might be able to use the INSERT...EXEC method. Otherwise OPENQUERY might work.</p>
0
2008-11-07T19:43:29Z
[ "python", "sql", "pyodbc" ]
Access second result set of stored procedure with SQL or other work-around? Python\pyodbc
273,203
<p>I'm using python\pyodbc and would like to access the second result set of a stored procedure. As near as I can tell, pyodbc does not support multiple result sets. Additionally, I can't modify the stored procedure. Are there any options to access the second result set using SQL or some other work-around? Perhaps crea...
6
2008-11-07T18:58:47Z
313,665
<p>No need for anything fancy. Just use nextset:</p> <pre><code> import pyodbc db = pyodbc.connect ("") q = db.cursor () q.execute (""" SELECT TOP 5 * FROM INFORMATION_SCHEMA.TABLES SELECT TOP 10 * FROM INFORMATION_SCHEMA.COLUMNS """) tables = q.fetchall () q.nextset () columns = q.fetchall () assert len (tables) ==...
6
2008-11-24T08:31:21Z
[ "python", "sql", "pyodbc" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,543
<p>C# and the .NET framework broke compatibility between versions 1.0 and 1.1 as well as between 1.1 and 2.0. Running applications in different versions required having multiple versions of the .NET runtime installed.</p> <p>At least they did include an upgrade wizard to upgrade source from one version to the next (i...
4
2008-11-07T20:27:16Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,547
<p>Perl 6 is also going through this type of split right now. Perl 5 programs won't run directly on Perl 6, but there will be a translator to translate the code into a form that may work (I don't think it can handle 100% of the cases).</p> <p><a href="http://en.wikipedia.org/wiki/Perl_6" rel="nofollow">Perl 6</a> even...
1
2008-11-07T20:28:09Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,554
<p>First, here is a <a href="http://video.google.com/videoplay?docid=1189446823303316785" rel="nofollow">video talk</a> about the changes Python will go through. Second, changes are no good. Third, I for one welcome evolution and believe it is necessary.</p>
1
2008-11-07T20:30:00Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,571
<p>The only language I can think of to attempt such a mid-stream change would be Perl. Of course, Python is beating Perl to that particular finish line by releasing first. It should be noted, however, that Perl's changes are much more extensive than Python's and likely will be harder to detangle.</p> <p>(There's a pri...
16
2008-11-07T20:37:22Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,576
<p>The python team has worked very hard to make the lack of backward compatibility as painless as possible, to the point where the 2.6 release of python was created with a mind towards a painless upgrade process. Once you have upgraded to 2.6 there are scripts that you can run that will move you to 3.0 without issue....
9
2008-11-07T20:37:50Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,646
<p>In the Lisp world it has happened a few times. of course, the language is so dynamic that usually evolution is simply deprecating part of the standard library and making standard another part.</p> <p>also, Lua 4 to 5 was pretty significant; but the language core is so minimal that even wide-reaching changes are do...
2
2008-11-07T21:09:55Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,661
<p>It's worth mentioning that backward compatibility incurs costs of its own. In some cases it's almost impossible to evolve a language in the ideal way if 100% backward compatibility is required. Java's implementation of generics (which erases type information at compile-time in order to be backwardly-compatible) is a...
7
2008-11-07T21:17:04Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,685
<p>I think there are many examples of backward compatibility breakages. Many of the languages that did this were either small or died out along the way.</p> <p>Many examples of this involved renaming the language.</p> <p>Algol 60 and Algol 68 were so different that the meetings on Algol 68 broke up into factions. T...
6
2008-11-07T21:24:29Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,688
<p>The price of insisting on near-absolute backwards compatibility is just too high. Spend two minutes programming in C++ if you want to see why.</p>
13
2008-11-07T21:25:59Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
273,994
<p>Wouldn't VB6 to VB.net be the biggest example of this? Or do you all consider them two separate languages? </p>
4
2008-11-07T23:29:44Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
274,338
<p>gcc regularly changes how it handles C++ almost every minor release. Of course, this is more a consequence of gcc tightening how they follow the rules, and less of C++ itself changing.</p>
0
2008-11-08T04:03:43Z
[ "python", "programming-languages", "python-3.x" ]
Python 3.0 and language evolution
273,524
<p>Python 3.0 breaks backwards compatibility with previous versions and splits the language into two paths (at least temporarily). Do you know of any other language that went through such a major design phase while in maturity?</p> <p>Also, do you believe that this is how programming languages should evolve or is the ...
13
2008-11-07T20:23:14Z
427,917
<p>The new version of the Ruby programming language will also break compatibility.</p> <p>And think of the libraries one might use: gtk, Qt, and so on (they also have incompatible versions).</p> <p>I think incompatibility is necessary sometimes (but not too often) to support progress.</p>
0
2009-01-09T12:38:57Z
[ "python", "programming-languages", "python-3.x" ]
python, basic question on loops
273,612
<p>It's a really basic question but i can't think at the second. How do i set up a loop that asks each time the function inside runs whether to do it again. So it runs it then says something like;</p> <p>"loop again? y/n"</p>
2
2008-11-07T20:55:33Z
273,618
<pre><code>while True: func() answer = raw_input( "Loop again? " ) if answer != 'y': break </code></pre>
13
2008-11-07T20:58:56Z
[ "python", "loops" ]
python, basic question on loops
273,612
<p>It's a really basic question but i can't think at the second. How do i set up a loop that asks each time the function inside runs whether to do it again. So it runs it then says something like;</p> <p>"loop again? y/n"</p>
2
2008-11-07T20:55:33Z
273,620
<pre><code>keepLooping = True while keepLooping: # do stuff here # Prompt the user to continue q = raw_input("Keep looping? [yn]: ") if not q.startswith("y"): keepLooping = False </code></pre>
6
2008-11-07T20:59:57Z
[ "python", "loops" ]
python, basic question on loops
273,612
<p>It's a really basic question but i can't think at the second. How do i set up a loop that asks each time the function inside runs whether to do it again. So it runs it then says something like;</p> <p>"loop again? y/n"</p>
2
2008-11-07T20:55:33Z
273,677
<p>There are two usual approaches, both already mentioned, which amount to:</p> <pre><code>while True: do_stuff() # and eventually... break; # break out of the loop </code></pre> <p>or</p> <pre><code>x = True while x: do_stuff() # and eventually... x = False # set x to False to break the loop </code>...
5
2008-11-07T21:22:20Z
[ "python", "loops" ]
python, basic question on loops
273,612
<p>It's a really basic question but i can't think at the second. How do i set up a loop that asks each time the function inside runs whether to do it again. So it runs it then says something like;</p> <p>"loop again? y/n"</p>
2
2008-11-07T20:55:33Z
321,040
<pre><code>While raw_input("loop again? y/n ") != 'n': do_stuff() </code></pre>
1
2008-11-26T15:12:09Z
[ "python", "loops" ]
Is there a cross-platform way of getting information from Python's OSError?
273,698
<p>On a simple directory creation operation for example, I can make an OSError like this:</p> <p>(Ubuntu Linux)</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; os.mkdir('foo') &gt;&gt;&gt; os.mkdir('foo') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; OSError: [Errno 17] File ...
31
2008-11-07T21:30:18Z
273,872
<p>The <code>errno</code> attribute on the error should be the same on all platforms. You will get <code>WindowsError</code> exceptions on Windows, but since this is a subclass of OSError the same "<code>except OSError:</code>" block will catch it. Windows does have its own error codes, and these are accessible as <cod...
48
2008-11-07T22:39:59Z
[ "python", "exception", "cross-platform" ]
How do I Create an instance of a class in another class in Python
273,937
<p>I am trying to learn Python and WxPython. I have been a SAS programmer for years. This OOP stuff is slowly coming together but I am still fuzzy on a lot of the concepts. Below is a section of code. I am trying to use a button click to create an instance of another class. Specifically-I have my main panel in one...
2
2008-11-07T23:03:53Z
274,004
<p>I don't know wxWidgets, but based on what I know of Python, I'm guessing that you need to change:</p> <pre><code>self.Bind(wx.EVT_MENU, self.subPanel(None, -1, 'TEST'),id=1) </code></pre> <p>to:</p> <pre><code>self.Bind(wx.EVT_MENU, subPanel(None, -1, 'TEST'),id=1) </code></pre> <p>"subPanel" is a globally defin...
1
2008-11-07T23:34:25Z
[ "python", "oop", "wxpython" ]
How do I Create an instance of a class in another class in Python
273,937
<p>I am trying to learn Python and WxPython. I have been a SAS programmer for years. This OOP stuff is slowly coming together but I am still fuzzy on a lot of the concepts. Below is a section of code. I am trying to use a button click to create an instance of another class. Specifically-I have my main panel in one...
2
2008-11-07T23:03:53Z
274,145
<p>You should handle the button click event, and create the panel in your button handler (like you already do with your OnQuit method).</p> <p>I think the following code basically does what you're after -- creates a new Frame when the button is clicked/menu item is selected.</p> <pre><code>import wx class MyFrame(wx...
1
2008-11-08T01:10:57Z
[ "python", "oop", "wxpython" ]
How do I Create an instance of a class in another class in Python
273,937
<p>I am trying to learn Python and WxPython. I have been a SAS programmer for years. This OOP stuff is slowly coming together but I am still fuzzy on a lot of the concepts. Below is a section of code. I am trying to use a button click to create an instance of another class. Specifically-I have my main panel in one...
2
2008-11-07T23:03:53Z
274,160
<p>You need an event handler in your bind expression</p> <pre><code>self.bind(wx.EVT_MENU, subPanel(None, -1, 'TEST'),id=1) </code></pre> <p>needs to be changed to:</p> <pre><code>self.bind(wx.EVT_MENU, &lt;event handler&gt;, &lt;id of menu item&gt;) </code></pre> <p>where your event handler responds to the event a...
0
2008-11-08T01:19:19Z
[ "python", "oop", "wxpython" ]
How do I resize an image using PIL and maintain its aspect ratio?
273,946
<p>Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.</p>
196
2008-11-07T23:08:04Z
273,962
<p>Define a maximum size. Then, compute a resize ratio by taking <code>min(maxwidth/width, maxheight/height)</code>.</p> <p>The proper size is <code>oldsize*ratio</code>.</p> <p>There is of course also a library method to do this: the method <code>Image.thumbnail</code>.<br> Below is an (edited) example from the <a h...
246
2008-11-07T23:15:37Z
[ "python", "image", "python-imaging-library", "thumbnails" ]
How do I resize an image using PIL and maintain its aspect ratio?
273,946
<p>Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.</p>
196
2008-11-07T23:08:04Z
364,789
<p>If you are trying to maintain the same aspect ratio, then wouldn't you resize by some percentage of the original size?</p> <p>For example, half the original size</p> <pre><code>half = 0.5 out = im.resize( [int(half * s) for s in im.size] ) </code></pre>
9
2008-12-13T03:43:00Z
[ "python", "image", "python-imaging-library", "thumbnails" ]
How do I resize an image using PIL and maintain its aspect ratio?
273,946
<p>Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.</p>
196
2008-11-07T23:08:04Z
451,580
<p>This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. It does this by determining what percentage 300 pixels is of the original width (img.size[0]) and then multiplying the original height (img.size[1]) by that percentag...
77
2009-01-16T19:12:07Z
[ "python", "image", "python-imaging-library", "thumbnails" ]
How do I resize an image using PIL and maintain its aspect ratio?
273,946
<p>Is there an obvious way to do this that I'm missing? I'm just trying to make thumbnails.</p>
196
2008-11-07T23:08:04Z
940,368
<p>I also recommend using PIL's thumbnail method, because it removes all the ratio hassles from you.</p> <p>One important hint, though: Replace</p> <pre><code>im.thumbnail(size) </code></pre> <p>with</p> <pre><code>im.thumbnail(size,Image.ANTIALIAS) </code></pre> <p>by default, PIL uses the Image.NEAREST filter fo...
39
2009-06-02T16:03:07Z
[ "python", "image", "python-imaging-library", "thumbnails" ]