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 |
|---|---|---|---|---|---|---|---|---|---|
AuiNotebook, where did the event happend | 578,800 | <p>How can I find out from which AuiNotebook page an event occurred?</p>
<p>EDIT: Sorry about that. Here are a code example. How do I find the notebook page
from witch the mouse was clicked in?</p>
<pre><code>#!/usr/bin/python
#12_aui_notebook1.py
import wx
import wx.lib.inspection
class MyFrame(wx.Frame):
def... | 1 | 2009-02-23T18:45:11Z | 580,522 | <p>For a mouse click you can assume the current selected page is the one that got the click. I added a few lines to your code. See comments</p>
<pre><code>def new_panel(self, nm):
pnl = wx.Panel(self)
# just to debug, I added a string attribute to the panel
# don't you love dynamic languages? :)
pnl.id... | 1 | 2009-02-24T05:02:36Z | [
"python",
"wxpython",
"wxwidgets"
] |
Need help understanding function passing in Python | 578,812 | <p>I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions.</p>
<p>Let's say I am trying to predict tomorrow's temperature based on today's and yesterday's temperature, and I have written the following function:</p>
<pre><code>def pr... | 4 | 2009-02-23T18:47:43Z | 578,855 | <p>Function passing in Python is easy, you just use the name of the function as a variable which contains the function itself.</p>
<pre><code>def predict(...):
...
minimize(predict, ..., mean_abs_error, ...)
</code></pre>
<p>As for the rest of the question: I'd suggest looking at the way <a href="http://docs.sci... | 2 | 2009-02-23T18:55:35Z | [
"python"
] |
Need help understanding function passing in Python | 578,812 | <p>I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions.</p>
<p>Let's say I am trying to predict tomorrow's temperature based on today's and yesterday's temperature, and I have written the following function:</p>
<pre><code>def pr... | 4 | 2009-02-23T18:47:43Z | 578,869 | <p>Here is an example of how to pass a function into another function. <code>apply_func_to</code> will take a function <code>f</code> and a number <code>num</code> as parameters and <code>return f(num)</code>.</p>
<pre><code>def my_func(x):
return x*x
def apply_func_to(f, num):
return f(num)
>>>app... | 13 | 2009-02-23T18:59:04Z | [
"python"
] |
Need help understanding function passing in Python | 578,812 | <p>I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions.</p>
<p>Let's say I am trying to predict tomorrow's temperature based on today's and yesterday's temperature, and I have written the following function:</p>
<pre><code>def pr... | 4 | 2009-02-23T18:47:43Z | 579,126 | <p>As <a href="http://stackoverflow.com/questions/578812/need-help-understanding-function-passing-in-python/578855#578855">David</a> and and <a href="http://stackoverflow.com/questions/578812/need-help-understanding-function-passing-in-python/578869#578869">Il-Bhima</a> note, functions can be passed into other function... | 1 | 2009-02-23T20:10:19Z | [
"python"
] |
Python package import error | 578,983 | <p>I'm trying to package my modules, but I can't seem to get it working. </p>
<p>My directory tree is something like the following:</p>
<pre><code>snappy/
__init__.py
main/
__init__.py
main.py
config.py
...
...
</code></pre>
<p>and the code I'm using is</p>
<pre><code>fro... | 3 | 2009-02-23T19:27:23Z | 578,993 | <p>Is the parent directory of <code>snappy</code> in <code>sys.path</code>? If it's not, that's the only thing I can think of that would be causing your error.</p>
| 5 | 2009-02-23T19:29:12Z | [
"python",
"package",
"python-import"
] |
Python package import error | 578,983 | <p>I'm trying to package my modules, but I can't seem to get it working. </p>
<p>My directory tree is something like the following:</p>
<pre><code>snappy/
__init__.py
main/
__init__.py
main.py
config.py
...
...
</code></pre>
<p>and the code I'm using is</p>
<pre><code>fro... | 3 | 2009-02-23T19:27:23Z | 579,000 | <p>It depends on where your script using the import resides and your system PYTHONPATH. Basically, to have that import working you should run your script (the one having the import) in the parent directory of snappy or your script should change sys.path to include it.</p>
<p>./alex</p>
| 5 | 2009-02-23T19:31:05Z | [
"python",
"package",
"python-import"
] |
Is there a pattern for propagating details of both errors and warnings? | 579,097 | <p>Is there a common pattern for propagating details of both errors and warnings? By <em>errors</em> I mean serious problems that should cause the flow of code to stop. By <em>warnings</em> I mean issues that merit informing the user of a problem, but are too trivial to stop program flow.</p>
<p>I currently use except... | 6 | 2009-02-23T19:58:15Z | 579,102 | <p>Serious errors should bubble up, warning should just be logged in place without throwing exceptions.</p>
| -1 | 2009-02-23T20:00:00Z | [
"python",
"design-patterns",
"error-handling",
"warnings"
] |
Is there a pattern for propagating details of both errors and warnings? | 579,097 | <p>Is there a common pattern for propagating details of both errors and warnings? By <em>errors</em> I mean serious problems that should cause the flow of code to stop. By <em>warnings</em> I mean issues that merit informing the user of a problem, but are too trivial to stop program flow.</p>
<p>I currently use except... | 6 | 2009-02-23T19:58:15Z | 579,117 | <p>Look into Python's <code>warnings</code> module, <a href="http://docs.python.org/library/warnings.html" rel="nofollow">http://docs.python.org/library/warnings.html</a></p>
<p>I don't think there's much you can say about this problem without specifying the language, as non-terminal error handling varies greatly from... | 7 | 2009-02-23T20:05:54Z | [
"python",
"design-patterns",
"error-handling",
"warnings"
] |
Instantiating a python class in C# | 579,272 | <p>I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?</p>
<p>The class looks (partially) like this:</p>
<p... | 36 | 2009-02-23T20:47:25Z | 579,368 | <p>I have searched high and low and I am afraid that there does not seem to be much information pertaining to this. I am pretty much certain that no one has devised a way to do this in the clean manner that you would like.</p>
<p>The main reason I think this is a problem is that in order to see the <code>PokerCard</c... | -4 | 2009-02-23T21:09:38Z | [
"c#",
".net",
"python",
"ironpython",
"cross-language"
] |
Instantiating a python class in C# | 579,272 | <p>I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?</p>
<p>The class looks (partially) like this:</p>
<p... | 36 | 2009-02-23T20:47:25Z | 579,609 | <p>IronPython classes are <em>not</em> .NET classes. They are instances of IronPython.Runtime.Types.PythonType which is the Python metaclass. This is because Python classes are dynamic and support addition and removal of methods at runtime, things you cannot do with .NET classes.</p>
<p>To use Python classes in C# you... | 46 | 2009-02-23T22:14:43Z | [
"c#",
".net",
"python",
"ironpython",
"cross-language"
] |
Instantiating a python class in C# | 579,272 | <p>I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?</p>
<p>The class looks (partially) like this:</p>
<p... | 36 | 2009-02-23T20:47:25Z | 2,722,635 | <p>Now that .Net 4.0 is released and has the dynamic type, this example should be updated. Using the same python file as in m-sharp's original answer:</p>
<pre class="lang-py prettyprint-override"><code>class Calculator(object):
def add(self, a, b):
return a + b
</code></pre>
<p>Here is how you would call... | 30 | 2010-04-27T15:39:13Z | [
"c#",
".net",
"python",
"ironpython",
"cross-language"
] |
Instantiating a python class in C# | 579,272 | <p>I've written a class in python that I want to wrap into a .net assembly via IronPython and instantiate in a C# application. I've migrated the class to IronPython, created a library assembly and referenced it. Now, how do I actually get an instance of that class?</p>
<p>The class looks (partially) like this:</p>
<p... | 36 | 2009-02-23T20:47:25Z | 17,046,283 | <p>I am updating the above example provided by Clever Human for compiled IronPython classes (dll) instead of IronPython source code in a .py file.</p>
<pre><code># Compile IronPython calculator class to a dll
clr.CompileModules("calculator.dll", "calculator.py")
</code></pre>
<p>C# 4.0 code with the new dynamic type ... | 0 | 2013-06-11T14:12:29Z | [
"c#",
".net",
"python",
"ironpython",
"cross-language"
] |
formatting long numbers as strings in python | 579,310 | <p>What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?</p>
<p>I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.</p>
<p>Is there some % string formatting operator available for that? Or that could be done only... | 10 | 2009-02-23T20:55:58Z | 579,343 | <p>No String Formatting Operator, <a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow">according to the docs</a>. I've never heard of such a thing, so you may have to roll your own, as you suggest.</p>
| 0 | 2009-02-23T21:02:49Z | [
"python",
"formatting",
"string",
"integer"
] |
formatting long numbers as strings in python | 579,310 | <p>What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?</p>
<p>I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.</p>
<p>Is there some % string formatting operator available for that? Or that could be done only... | 10 | 2009-02-23T20:55:58Z | 579,349 | <p>I don't think there are format operators for that, but you can simply divide by 1000 until the result is between 1 and 999 and then use a format string for 2 digits after comma. Unit is a single character (or perhaps a small string) in most cases, which you can store in a string or array and iterate through it after... | 0 | 2009-02-23T21:05:36Z | [
"python",
"formatting",
"string",
"integer"
] |
formatting long numbers as strings in python | 579,310 | <p>What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?</p>
<p>I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.</p>
<p>Is there some % string formatting operator available for that? Or that could be done only... | 10 | 2009-02-23T20:55:58Z | 579,373 | <p>I don't know of any built-in capability like this, but here are a couple of list threads that may help:</p>
<p><a href="http://coding.derkeiler.com/Archive/Python/comp.lang.python/2005-09/msg03327.html" rel="nofollow">http://coding.derkeiler.com/Archive/Python/comp.lang.python/2005-09/msg03327.html</a>
<a href="htt... | 0 | 2009-02-23T21:10:45Z | [
"python",
"formatting",
"string",
"integer"
] |
formatting long numbers as strings in python | 579,310 | <p>What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?</p>
<p>I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.</p>
<p>Is there some % string formatting operator available for that? Or that could be done only... | 10 | 2009-02-23T20:55:58Z | 579,376 | <p>I don't think there's a built-in function that does that. You'll have to roll your own, e.g.:</p>
<pre><code>def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', ... | 20 | 2009-02-23T21:11:36Z | [
"python",
"formatting",
"string",
"integer"
] |
WSGI byte ranges serving | 579,426 | <p>I'm looking into supporting <a href="http://en.wikipedia.org/wiki/Byte%5Fserving" rel="nofollow">HTTP/1.1 Byte serving</a> in WSGI server/application for:</p>
<ul>
<li>resuming partial downloads</li>
<li>multi-part downloads</li>
<li>better streaming</li>
</ul>
<p><a href="http://www.python.org/dev/peps/pep-0333/#... | 5 | 2009-02-23T21:22:48Z | 594,496 | <p>I think <a href="http://pythonpaste.org/webob/" rel="nofollow">webob</a> may do the trick, see the end of the <a href="http://pythonpaste.org/webob/file-example.html" rel="nofollow">file example</a> for a range request implementation which efficiently seeks into the file being served.</p>
| 3 | 2009-02-27T12:10:21Z | [
"python",
"http",
"http-headers",
"wsgi",
"middleware"
] |
WSGI byte ranges serving | 579,426 | <p>I'm looking into supporting <a href="http://en.wikipedia.org/wiki/Byte%5Fserving" rel="nofollow">HTTP/1.1 Byte serving</a> in WSGI server/application for:</p>
<ul>
<li>resuming partial downloads</li>
<li>multi-part downloads</li>
<li>better streaming</li>
</ul>
<p><a href="http://www.python.org/dev/peps/pep-0333/#... | 5 | 2009-02-23T21:22:48Z | 1,783,423 | <p>You just need to use WebOb and create the response as <code>Response(conditional_request=True)</code> or <a href="http://pythonpaste.org/webob/reference.html#conditional-wsgi-application" rel="nofollow">subclass the WebOb Response object</a> making <code>conditional_request=True</code> the default.</p>
<p>When <cod... | 0 | 2009-11-23T14:25:59Z | [
"python",
"http",
"http-headers",
"wsgi",
"middleware"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 579,537 | <p>You can certainly use various parts of Django in a stand-alone fashion. It is after-all just a collection of Python modules, which you can import to any other code you would like to use them.</p>
<p>I'd also recommend looking at <a href="http://www.sqlalchemy.org/">SQL Alchemy</a> if you are only after the ORM side... | 8 | 2009-02-23T21:51:51Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 579,548 | <p>I recommend <a href="http://sqlalchemy.org">SQLAlchemy</a>. It should do all the ORM stuff, as well as basic SQL stuff.</p>
| 5 | 2009-02-23T21:52:36Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 579,567 | <p>The short answer is: no, you can't use the Django ORM separately from Django.</p>
<p>The long answer is: yes, you can if you are willing to load large parts of Django along with it. For example, the database connection that is used by Django is opened when a request to Django occurs. This happens when a signal is s... | 9 | 2009-02-23T22:00:22Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 584,208 | <p>If you like Django's ORM, it's perfectly simple to use it "standalone"; I've <a href="http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/">written up several techniques for using parts of Django outside of a web context</a>, and you're free to use any of them (or roll your own).</p>
<p>Shane above s... | 72 | 2009-02-25T00:01:21Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 948,593 | <p>(I'm reporting my solution because my question said to be a duplicate)</p>
<p>Ah ok I figured it out and will post the solutions for anyone attempting to do the same thing.</p>
<p>This solution assumes that you want to create new models.</p>
<p>First create a new folder to store your files. We'll call it "standAl... | 6 | 2009-06-04T04:25:16Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 5,966,125 | <p>Take a look at <a href="http://pypi.python.org/pypi/django-standalone" rel="nofollow">django-standalone</a> which makes this setup pretty easy.</p>
<p>I also found <a href="http://jystewart.net/2008/02/18/using-the-django-orm-as-a-standalone-component/" rel="nofollow">this blog entry</a> quite useful.</p>
| 2 | 2011-05-11T14:43:54Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 20,968,008 | <p>I'm using django ORM without a settings file. Here's how: </p>
<p>In the stand-alone app launcher file: </p>
<pre><code>from django.conf import settings
from django.core.management import execute_from_command_line
#Django settings
settings.configure(DEBUG=False,
DATABASES = {
'default': {
... | 2 | 2014-01-07T09:28:37Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 22,545,158 | <p>This example is as simple as it gets. I already have a django app called thab up and running. I want to use the django orm in free standing python scripts and use the same models as I'm using for web programming. Here is an example:</p>
<pre><code># nothing in my sys.path contains my django project files
import sy... | 1 | 2014-03-20T21:09:57Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 32,492,362 | <p>I failed to run <a href="http://stackoverflow.com/a/948593/1145750">KeyboardInterrupt's answer</a> in Django 1.8.For recent <code>Django 1.8</code>, You can have a look at this, in which some parts come from KeyboardInterrupt's answer.</p>
<p>The folder structure is:</p>
<pre><code>.
âââ myApp
â  ââ... | 0 | 2015-09-10T02:56:27Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 33,218,173 | <p>Probably I'm quite late with my answer, but it's better late than never.</p>
<p>Try this simple package:
<a href="https://github.com/serglopatin/django-models-standalone" rel="nofollow">https://github.com/serglopatin/django-models-standalone</a></p>
<p>How to use:</p>
<ol>
<li><p>download</p></li>
<li><p>install<... | 0 | 2015-10-19T15:21:45Z | [
"python",
"django",
"orm"
] |
Using only the DB part of Django | 579,511 | <p>Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?</p>
<p>If not, what would you recommend as "the Python equivalent of Hibernate"?</p>
| 32 | 2009-02-23T21:45:54Z | 35,498,794 | <pre><code>import django
from django.conf import settings
from backend_mock.admin import settings as s
settings.configure(
DATABASES=s.DATABASES,
INSTALLED_APPS=('backend_mock.admin.mocker', )
)
django.setup()
</code></pre>
<p>take a look at this, it's working for django version gte 1.8.x</p>
| 1 | 2016-02-19T06:33:55Z | [
"python",
"django",
"orm"
] |
Wrapping objects to extend/add functionality while working around isinstance | 579,620 | <p>In Python, I've seen the recommendation to use holding or wrapping to extend the functionality of an object or class, rather than inheritance. In particular, I think that Alex Martelli spoke about this in his <a href="http://www.lecturefox.com/blog/python-design-patterns-by-alex-martelli" rel="nofollow">Python Desi... | 5 | 2009-02-23T22:17:28Z | 579,683 | <ul>
<li><p>Python 2.6 has the class-level decorators you desire. <a href="http://docs.python.org/3.0/whatsnew/2.6.html#pep-3129-class-decorators" rel="nofollow">http://docs.python.org/3.0/whatsnew/2.6.html#pep-3129-class-decorators</a></p></li>
<li><p>You could maybe use Metaclasses: <a href="http://www.google.com/sea... | 0 | 2009-02-23T22:37:25Z | [
"python",
"design-patterns"
] |
Wrapping objects to extend/add functionality while working around isinstance | 579,620 | <p>In Python, I've seen the recommendation to use holding or wrapping to extend the functionality of an object or class, rather than inheritance. In particular, I think that Alex Martelli spoke about this in his <a href="http://www.lecturefox.com/blog/python-design-patterns-by-alex-martelli" rel="nofollow">Python Desi... | 5 | 2009-02-23T22:17:28Z | 579,986 | <p>My first impulse would be to try to fix the offending code which uses <code>isinstance</code>. Otherwise you're just propagating its design mistakes in to your own design. Any reason you can't modify it?</p>
<p><strong>Edit:</strong>
So your justification is that you're writing framework/library code that you want ... | 0 | 2009-02-24T00:24:11Z | [
"python",
"design-patterns"
] |
Wrapping objects to extend/add functionality while working around isinstance | 579,620 | <p>In Python, I've seen the recommendation to use holding or wrapping to extend the functionality of an object or class, rather than inheritance. In particular, I think that Alex Martelli spoke about this in his <a href="http://www.lecturefox.com/blog/python-design-patterns-by-alex-martelli" rel="nofollow">Python Desi... | 5 | 2009-02-23T22:17:28Z | 581,263 | <p>If the library code you depend on uses <code>isinstance</code> and relies on inheritance why not follow this route? If you cannot change the library then it is probably best to stay consistend with it.</p>
<p>I also think that there are legitimate uses for <code>isinstance</code>, and with the introduction of <a hr... | 2 | 2009-02-24T10:35:52Z | [
"python",
"design-patterns"
] |
Wrapping objects to extend/add functionality while working around isinstance | 579,620 | <p>In Python, I've seen the recommendation to use holding or wrapping to extend the functionality of an object or class, rather than inheritance. In particular, I think that Alex Martelli spoke about this in his <a href="http://www.lecturefox.com/blog/python-design-patterns-by-alex-martelli" rel="nofollow">Python Desi... | 5 | 2009-02-23T22:17:28Z | 582,898 | <p>One thing to keep in mind is that you don't necessarily have to <em>use</em> anything in a base class if you go the inheritance route. You can make a stub class to inherit from that doesn't add any concrete implementation. I've done something like this several times:</p>
<pre><code>class Message(object):
pas... | 1 | 2009-02-24T18:17:58Z | [
"python",
"design-patterns"
] |
Wrapping objects to extend/add functionality while working around isinstance | 579,620 | <p>In Python, I've seen the recommendation to use holding or wrapping to extend the functionality of an object or class, rather than inheritance. In particular, I think that Alex Martelli spoke about this in his <a href="http://www.lecturefox.com/blog/python-design-patterns-by-alex-martelli" rel="nofollow">Python Desi... | 5 | 2009-02-23T22:17:28Z | 583,024 | <p>If you're writing a framework that needs to accept some sort of inputs from your API users, then there's no reason I can think of to use isinstance. Ugly as it might be, I always just check to see if it actually provides the interface I mean to use:</p>
<pre><code>def foo(bar):
if hasattr(bar, "baz") and hasat... | 0 | 2009-02-24T18:46:09Z | [
"python",
"design-patterns"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 579,715 | <p>Looks like you need to add win32clipboard to your site-packages. It's part of the <a href="http://sourceforge.net/projects/pywin32/">pywin32 package</a></p>
| 10 | 2009-02-23T22:43:08Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 3,429,034 | <p>You can also use ctypes to tap into the windows API and avoid the massive pywin32 package. This is what I use, (excuse the poor style, but the idea is there.) </p>
<pre><code>import ctypes
#Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard #Basic Clipbo... | 22 | 2010-08-07T03:33:26Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 4,203,897 | <p>Actually, <code>pywin32</code> and <code>ctypes</code> seem to be an overkill for this simple task. <code>Tkinter</code> is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.</p>
<p>If all you need is to put some text to system clipboa... | 196 | 2010-11-17T11:31:06Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 9,409,898 | <p>I didn't have a solution just a work around</p>
<p>Windows vista onwards has an inbuilt command called clip that takes the output of a command from command line and puts it into the clipboard. E.g. ipconfig | clip </p>
<p>So i made a function with the os module which takes the string and adds it to the clipboard u... | 53 | 2012-02-23T09:06:00Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 19,602,654 | <p>Widgets also have method named <code>.clipboard_get()</code> that returns the contents of the clipboard (unless some kind of error happens based on the type of data in the clipboard).</p>
<p>The <code>clipboard_get()</code> method is mentioned in this bug report:<br>
<a href="http://bugs.python.org/issue14777" rel=... | 2 | 2013-10-26T04:00:27Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 24,523,659 | <p>You can use <a href="http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/"><strong>pyperclip</strong></a> - cross-platform clipboard module. Or <a href="https://github.com/kennethreitz/xerox"><strong>Xerox</strong></a> - similar module, except requires the win32 Python module to ... | 17 | 2014-07-02T05:43:27Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 24,919,985 | <pre><code>from Tkinter import Tk
clip = Tk()
</code></pre>
| -7 | 2014-07-23T20:07:59Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 25,678,113 | <p>For some reason I've never been able to get the Tk solution to work for me. <a href="http://stackoverflow.com/a/3429034/5987">kapace's solution</a> is much more workable, but the formatting is contrary to my style and it doesn't work with Unicode. Here's a modified version.</p>
<pre><code>import ctypes
OpenClipboa... | 3 | 2014-09-05T03:11:03Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 27,291,478 | <p>I've tried various solutions, but this is the simplest one that passes <a href="https://gist.github.com/CTimmerman/133cb80100357dde92d8" rel="nofollow">my test</a>:</p>
<pre><code>#coding=utf-8
import win32clipboard # http://sourceforge.net/projects/pywin32/
def copy(text):
win32clipboard.OpenClipboard()
... | 4 | 2014-12-04T10:20:38Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 29,632,881 | <pre><code>import wx
def ctc(text):
if not wx.TheClipboard.IsOpened():
wx.TheClipboard.Open()
data = wx.TextDataObject()
data.SetText(text)
wx.TheClipboard.SetData(data)
wx.TheClipboard.Close()
ctc(text)
</code></pre>
| 0 | 2015-04-14T16:41:37Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 33,540,623 | <p>Code snippet to copy clipboard</p>
<p>Create a wrapper python code in a module named (<em>clipboard.py</em>)</p>
<pre><code>import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Clipboard
def setText(text):
Clipboard.SetText(text)
def getText():
return Clipboard.GetText()
</... | -1 | 2015-11-05T09:19:09Z | [
"python",
"clipboard"
] |
How do I copy a string to the clipboard on Windows using Python? | 579,687 | <p>I'm kind of new to Python and I'm trying to make a basic Windows application that builds a string out of user input then adds it to the clipboard. How do I copy a string to the clipboard using Python?</p>
| 123 | 2009-02-23T22:38:02Z | 36,178,407 | <p>You can use the excellent pandas, which has a built in clipboard support, but you need to pass through a DataFrame.</p>
<pre><code>import pandas as pd
df=pd.DataFrame(['Text to copy'])
df.to_clipboard(index=False,header=False)
</code></pre>
| 1 | 2016-03-23T12:36:54Z | [
"python",
"clipboard"
] |
Python ORM that auto-generates/updates tables and uses SQLite? | 579,770 | <p>I am doing some prototyping for a new desktop app i am writing in Python, and i want to use SQLite and an ORM to store data.</p>
<p>My question is, are there any ORM libraries that support auto-generating/updating the database schema and work with SQLite?</p>
| 9 | 2009-02-23T22:59:10Z | 579,787 | <p><a href="http://www.sqlalchemy.org/">SQLAlchemy</a> is a great choice in the Python ORM space that supports SQLite.</p>
| 16 | 2009-02-23T23:05:02Z | [
"python",
"sqlite",
"orm",
"auto-generate"
] |
Python ORM that auto-generates/updates tables and uses SQLite? | 579,770 | <p>I am doing some prototyping for a new desktop app i am writing in Python, and i want to use SQLite and an ORM to store data.</p>
<p>My question is, are there any ORM libraries that support auto-generating/updating the database schema and work with SQLite?</p>
| 9 | 2009-02-23T22:59:10Z | 580,219 | <p>SQLAlchemy, when used with the <a href="http://code.google.com/p/sqlalchemy-migrate/" rel="nofollow">sqlalchemy-migrate</a> library.</p>
| 2 | 2009-02-24T02:18:30Z | [
"python",
"sqlite",
"orm",
"auto-generate"
] |
Change basic (immutable) types inside a function in Python? | 579,782 | <p>I am using a C++ SDK where there is a function like (it has a python wrapper, but not docs):</p>
<pre><code>getPos ( int uvId, float & u, float & v ) const
</code></pre>
<p>How do I specify in Python so that the passed variables are changed?</p>
<p>I tried this example to see if I could modify floats insi... | 0 | 2009-02-23T23:03:12Z | 579,788 | <p>For simple cases have the function return the new value.</p>
<p>For more complicated cases you can pass in a object or a list and have that changed:</p>
<pre><code>def foobar(alist):
alist[0] = 10
blist = [42]
foobar(blist)
print blist[0]
</code></pre>
<p>Edit:</p>
<p>For wrapping C++ references there isn't... | 0 | 2009-02-23T23:05:20Z | [
"python",
"variables",
"pass-by-reference"
] |
Change basic (immutable) types inside a function in Python? | 579,782 | <p>I am using a C++ SDK where there is a function like (it has a python wrapper, but not docs):</p>
<pre><code>getPos ( int uvId, float & u, float & v ) const
</code></pre>
<p>How do I specify in Python so that the passed variables are changed?</p>
<p>I tried this example to see if I could modify floats insi... | 0 | 2009-02-23T23:03:12Z | 579,805 | <p>In Python:</p>
<pre><code>def getPos(uvID):
# compute u, v
return u, v
#
u, v = getPos(uvID)
</code></pre>
| 5 | 2009-02-23T23:11:23Z | [
"python",
"variables",
"pass-by-reference"
] |
Change basic (immutable) types inside a function in Python? | 579,782 | <p>I am using a C++ SDK where there is a function like (it has a python wrapper, but not docs):</p>
<pre><code>getPos ( int uvId, float & u, float & v ) const
</code></pre>
<p>How do I specify in Python so that the passed variables are changed?</p>
<p>I tried this example to see if I could modify floats insi... | 0 | 2009-02-23T23:03:12Z | 579,817 | <p>As far I know, Python doesn't support call-by-reference, so the exact code you are suggesting doesn't work (obviously).</p>
<p>The tool (or person) that generated the Python wrapper for the C++ function must have done something special to support this function (hopefully, or you won't be able to use it). Do you kno... | 2 | 2009-02-23T23:16:00Z | [
"python",
"variables",
"pass-by-reference"
] |
Change basic (immutable) types inside a function in Python? | 579,782 | <p>I am using a C++ SDK where there is a function like (it has a python wrapper, but not docs):</p>
<pre><code>getPos ( int uvId, float & u, float & v ) const
</code></pre>
<p>How do I specify in Python so that the passed variables are changed?</p>
<p>I tried this example to see if I could modify floats insi... | 0 | 2009-02-23T23:03:12Z | 580,024 | <p>You're going to have to resort to <a href="https://docs.python.org/library/ctypes.html" rel="nofollow">ctypes</a>.</p>
<p>Specifically, see <a href="https://docs.python.org/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference" rel="nofollow">https://docs.python.org/library/ctypes.html#passing-poi... | 2 | 2009-02-24T00:38:15Z | [
"python",
"variables",
"pass-by-reference"
] |
What's the Pythonic way to combine two sequences into a dictionary? | 579,856 | <p>Is there a more concise way of doing this in Python?:</p>
<pre><code>def toDict(keys, values):
d = dict()
for k,v in zip(keys, values):
d[k] = v
return d
</code></pre>
| 7 | 2009-02-23T23:33:26Z | 579,862 | <p>Yes:</p>
<pre><code>dict(zip(keys,values))
</code></pre>
| 42 | 2009-02-23T23:35:34Z | [
"python"
] |
What's the Pythonic way to combine two sequences into a dictionary? | 579,856 | <p>Is there a more concise way of doing this in Python?:</p>
<pre><code>def toDict(keys, values):
d = dict()
for k,v in zip(keys, values):
d[k] = v
return d
</code></pre>
| 7 | 2009-02-23T23:33:26Z | 579,928 | <p>If <code>keys</code>' size may be larger then <code>values</code>' one then you could use <a href="http://docs.python.org/library/itertools.html#itertools.izip_longest" rel="nofollow"><code>itertools.izip_longest</code></a> (Python 2.6) which allows to specify a default value for the rest of the keys:</p>
<pre><cod... | 4 | 2009-02-23T23:59:41Z | [
"python"
] |
Need help debugging python html generator | 580,397 | <p>The program is supposed to take user input, turn it into html and pass it into the clipboard.</p>
<p>Start the program with welcome_msg()</p>
<p>If you enter 1 in the main menu, it takes you through building an anchor tag. You'll add the link text, the url, then the title. After you enter the title, I get the foll... | 1 | 2009-02-24T03:45:09Z | 580,467 | <p>In your <code>make_link</code> function you construct a <code>link_output</code>, but you don't actually return it as the functions result. Use <code>return</code> to do this:</p>
<pre><code>def make_link(in_link):
...
if title == '':
link_output = ...
else:
link_output = ...
return link_output
</co... | 3 | 2009-02-24T04:21:41Z | [
"python",
"pywin32"
] |
Persisting variable value in Python | 580,866 | <pre><code>for i in range(0,3):
j = 0
print 'incrementing '
j += 1
print j
</code></pre>
<p>prints</p>
<pre><code>incrementing
1
incrementing
1
incrementing
1
</code></pre>
<p>How can I persist the value of 'j' so that it prints:</p>
<pre><code>1
2
3
</code></pre>
| 0 | 2009-02-24T08:01:21Z | 580,873 | <p>You should not reset <code>j</code> to zero in the loop:</p>
<pre><code>j = 0
for i in range(0,3):
print 'incrementng '
j += 1
print j
</code></pre>
| 16 | 2009-02-24T08:04:19Z | [
"python",
"variables",
"loops"
] |
Persisting variable value in Python | 580,866 | <pre><code>for i in range(0,3):
j = 0
print 'incrementing '
j += 1
print j
</code></pre>
<p>prints</p>
<pre><code>incrementing
1
incrementing
1
incrementing
1
</code></pre>
<p>How can I persist the value of 'j' so that it prints:</p>
<pre><code>1
2
3
</code></pre>
| 0 | 2009-02-24T08:01:21Z | 580,913 | <p>A very dirty hack if you want to put this in a function: default arguments!
In Python, if a default argument is an array, it becomes "static" and mutable, you can keep it through different calls, like this:</p>
<pre><code>def f(j = [0]):
j[0] += 1
print('incrementing', j[0])
f() # prints "incrementing 1"
f... | -6 | 2009-02-24T08:23:04Z | [
"python",
"variables",
"loops"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 580,955 | <p>You can use the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=49AE8576-9BB9-4126-9761-BA8011FABF38&displaylang=en" rel="nofollow">filever.exe</a> tool to do that. Here'a thread that explains <a href="http://osdir.com/ml/python.windows/2004-02/msg00012.html" rel="nofollow">how to do it from Py... | 2 | 2009-02-24T08:40:08Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 594,794 | <p>I found this solution at "timgolden" site. Works fine.</p>
<pre><code>from win32api import GetFileVersionInfo, LOWORD, HIWORD
def get_version_number (filename):
info = GetFileVersionInfo (filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOW... | 4 | 2009-02-27T13:40:42Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 1,237,635 | <p>Better to add a try/except in case the file has no version number attribute.</p>
<p>filever.py</p>
<pre><code>
from win32api import GetFileVersionInfo, LOWORD, HIWORD
def get_version_number (filename):
try:
info = GetFileVersionInfo (filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVer... | 16 | 2009-08-06T08:34:19Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 2,310,098 | <p>You can use the <code>pyWin32</code> module from <a href="http://sourceforge.net/projects/pywin32/">http://sourceforge.net/projects/pywin32/</a>:</p>
<pre><code>from win32com.client import Dispatch
ver_parser = Dispatch('Scripting.FileSystemObject')
info = ver_parser.GetFileVersion(path)
if info == 'No Version In... | 9 | 2010-02-22T10:06:42Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 7,993,095 | <p>Here is a function which reads all file attributes as a dictionary:</p>
<pre><code>import win32api
#==============================================================================
def getFileProperties(fname):
#==============================================================================
"""
Read all prope... | 13 | 2011-11-03T10:08:02Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 16,076,661 | <p>Here is a version that also works in non Windows environments, using the <a href="https://code.google.com/p/pefile/">pefile-module</a>:</p>
<pre><code>import pefile
def LOWORD(dword):
return dword & 0x0000ffff
def HIWORD(dword):
return dword >> 16
def get_product_version(path):
pe = pefile.... | 6 | 2013-04-18T07:11:09Z | [
"python",
"windows",
"file-attributes"
] |
Python windows File Version attribute | 580,924 | <p>Last time I asked a similar question but that was about svn related versioning info. Now I am wondering how to query windows "File version" attribute about eg. a dll. I payed attention to wmi and win32file modules as well without success.</p>
| 16 | 2009-02-24T08:26:26Z | 38,924,793 | <p>Here is a version that does not require any additional libraries. I could not use win32api like everyone had suggested:</p>
<p>From: <a href="https://mail.python.org/pipermail//python-list/2006-November/402797.html" rel="nofollow">https://mail.python.org/pipermail//python-list/2006-November/402797.html</a></p>
<p>... | 0 | 2016-08-12T18:58:24Z | [
"python",
"windows",
"file-attributes"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 581,004 | <p>I suspect that the regex will also do what you are trying to do ... only much better :)</p>
<p>so the "|" would win</p>
| 1 | 2009-02-24T09:00:54Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 581,011 | <p>The two things will give slightly different results, unless it is guaranteed that a match will match one and only one regex. Otherwise if something matches 2 it will be counted twice.</p>
<p>In theory your solution ought to be quicker (if the expression are mutually exclusive) because the regex compiler ought to be... | 7 | 2009-02-24T09:03:55Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 581,015 | <p>I agree with amartynov but I wanted to add that you also might consider compiling the regex first (re.compile()), esp. in the second variant as then you might save some setup time in the loop. Maybe you can measure this as well while you are on it.</p>
<p>The reason I think the one shot performs better is that I as... | 0 | 2009-02-24T09:05:16Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 581,018 | <p>A single compile and search should yield faster results, on a lower scale of expressions the gain could be negligible but the more you run through the greater gain. Think of it as compiling once and matching vs compiling 10 times and matching.</p>
| 0 | 2009-02-24T09:06:22Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 581,029 | <p>I believe your first implementation will be faster:</p>
<ul>
<li>One of the key principles for Python performance is "move logic to the C level" -- meaning built-in functions (written in C) are faster than pure-Python implementations. So, when the loop is performed by the built-in Regex module, it should be faster<... | 1 | 2009-02-24T09:10:06Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 582,227 | <p>To understand how re module works - compile _sre.c in debug mode (put #define VERBOSE at 103 line in _sre.c and recompile python). After this you ill see something like this:</p>
<pre>
<code>
>>> import re
>>> p = re.compile('(a)|(b)|(c)')
>>> p.search('a'); print '\n\n'; p.search('b')
|0xb7f9ab10|(nil)|SEARCH
pref... | 5 | 2009-02-24T15:40:35Z | [
"python",
"regex",
"performance"
] |
regex '|' operator vs separate runs for each sub-expression | 580,993 | <p>I've got a fairly large string (~700k) against which I need to run 10 regexes and count all the matches of any of the regexes. My quick and dirty impl was to do something like re.search('(expr1)|(expr2)|...'), but I was wondering if we'd see any performance gains by matching in a loop instead:</p>
<p>In other word... | 5 | 2009-02-24T08:55:29Z | 583,320 | <p>The fewer passes the better: It'll just use more memory, which is typically not an issue.</p>
<p>If anything can be left to the interpreter to handle, it will always find a faster solution (both in time to implement and time to execute) than the typical human counterpart.</p>
| 0 | 2009-02-24T19:59:17Z | [
"python",
"regex",
"performance"
] |
Python web programming | 581,038 | <p>Good morning.</p>
<p>As the title indicates, I've got some questions about using python for web development.</p>
<ul>
<li>What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win env... | 12 | 2009-02-24T09:15:24Z | 581,077 | <p>So here are my thoughts about it:</p>
<p>I am using <a href="http://pythonpaste.org/" rel="nofollow">Python Paste</a> for developing my app and eventually also running it (or any other python web server). I am usually not using mod_python or mod_wsgi as it makes development setup more complex.</p>
<p>I am using <a... | 6 | 2009-02-24T09:26:54Z | [
"python",
"cherrypy"
] |
Python web programming | 581,038 | <p>Good morning.</p>
<p>As the title indicates, I've got some questions about using python for web development.</p>
<ul>
<li>What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win env... | 12 | 2009-02-24T09:15:24Z | 581,155 | <p>+1 to MrTopf's answer, but I'll add some additional opinions.</p>
<p><strong>Webserver</strong></p>
<p>Apache is the webserver that will give you the most configurability. Avoid mod_python because it is basically unsupported. On the other hand, <a href="http://code.google.com/p/modwsgi/" rel="nofollow">mod_wsgi</... | 6 | 2009-02-24T09:54:17Z | [
"python",
"cherrypy"
] |
Python web programming | 581,038 | <p>Good morning.</p>
<p>As the title indicates, I've got some questions about using python for web development.</p>
<ul>
<li>What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win env... | 12 | 2009-02-24T09:15:24Z | 581,356 | <p><strong>What is the best setup for a development environment?</strong></p>
<p>Doesn't much matter. We use Django, which runs in Windows and Unix nicely. For production, we use Apache in Red Hat.</p>
<p><strong>Is having to reload webserver to see the changes considered normal?</strong></p>
<p>Yes. Not clear wh... | 8 | 2009-02-24T11:05:43Z | [
"python",
"cherrypy"
] |
Python web programming | 581,038 | <p>Good morning.</p>
<p>As the title indicates, I've got some questions about using python for web development.</p>
<ul>
<li>What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win env... | 12 | 2009-02-24T09:15:24Z | 582,467 | <p>When you use <code>mod_python</code> on a threaded Apache server (the default on Windows), CherryPy runs in the same process as Apache. In that case, you almost certainly <strong>don't</strong> want CP to restart the process.</p>
<p>Solution: use <code>mod_rewrite</code> or <code>mod_proxy</code> so that CherryPy r... | 1 | 2009-02-24T16:33:18Z | [
"python",
"cherrypy"
] |
Python web programming | 581,038 | <p>Good morning.</p>
<p>As the title indicates, I've got some questions about using python for web development.</p>
<ul>
<li>What is the best setup for a development environment, more specifically, what webserver to use, how to bind python with it. Preferably, I'd like it to be implementable in both, *nix and win env... | 12 | 2009-02-24T09:15:24Z | 582,691 | <p>Look at Google App Engine. From their website: </p>
<blockquote>
<p>Google App Engine lets you run your
web applications on Google's
infrastructure. App Engine
applications are easy to build, easy
to maintain, and easy to scale as your
traffic and data storage needs grow.
With App Engine, there are n... | 2 | 2009-02-24T17:30:04Z | [
"python",
"cherrypy"
] |
Double buffering with wxpython | 581,085 | <p>I'm working on an multiplatform application with wxpython and I had flickering problems on windows, while drawing on a Panel.
I used to draw on a buffer (wx.Bitmap) during mouse motions events and my OnPaint method was composed of just on line:</p>
<pre><code>dc = wx.BufferedPaintDC(self, self.buffer)
</code></pre>... | 10 | 2009-02-24T09:29:15Z | 582,568 | <p>There is a high probability that the SetDoubleBuffered actually makes your panel use a buffered dc automatically, the documentation doesn't mention that those classes are deprecated (and I rather think they would if that were the case).</p>
<p>About wxPython in Action... 2006 was a long time ago... it is possible t... | 5 | 2009-02-24T17:00:58Z | [
"python",
"user-interface",
"wxpython",
"doublebuffered"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 581,225 | <p>This output seems correct to me since you have absolute error estimate here. The integral value of sin(x) is indeed should have value of zero for full period (any interval of 2*pi length) in both ordinary and numeric integration and your results is close to that value.<br />
To evaluate arc length you should calcula... | 4 | 2009-02-24T10:22:12Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 581,250 | <p>The <code>quad</code> function is a function from an old Fortran library. It works by judging by the flatness and slope of the function it is integrating how to treat the step size it uses for numerical integration in order to maximize efficiency. What this means is that you may get slightly different answers from o... | 7 | 2009-02-24T10:32:18Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 581,254 | <p>I think it is probably machine precision since both answers are effectively zero.</p>
<p>If you want an answer from the horse's mouth I would post this question on the <a href="http://www.nabble.com/Scipy-User-f33045.html">scipy discussion board</a></p>
| 6 | 2009-02-24T10:32:55Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 581,313 | <p>I would say that a number O(10^-14) is effectively zero. What's your tolerance?</p>
<p>It might be that the algorithm underlying quad isn't the best. You might try another method for integration and see if that improves things. A 5th order Runge-Kutta can be a very nice general purpose technique.</p>
<p>It coul... | 6 | 2009-02-24T10:52:04Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 584,185 | <pre><code>0.0 == 2.3e-16 (absolute error tolerance 4.4e-14)
</code></pre>
<p>Both answers are the same and <em>correct</em> i.e., zero within the given tolerance.</p>
| 3 | 2009-02-24T23:53:11Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Can someone explain why scipy.integrate.quad gives different results for equally long ranges while integrating sin(X)? | 581,186 | <p>I am trying to numerically integrate an arbitrary (known when I code) function in my program
using numerical integration methods. I am using Python 2.5.2 along with SciPy's numerical integration package. In order to get a feel for it, i decided to try integrating sin(x) and observed this behavior-</p>
<pre><code>&g... | 3 | 2009-02-24T10:10:29Z | 1,848,828 | <p>The difference comes from the fact that sin(x)=-sin(-x) exactly even in finite precision. Whereas finite precision only gives sin(x)~sin(x+2*pi) approximately. Sure it would be nice if quad were smart enough to figure this out, but it really has no way of knowing apriori that the integral over the two intervals you ... | 2 | 2009-12-04T18:33:05Z | [
"python",
"integration",
"scipy",
"numerical-methods"
] |
Python C-API Object Initialisation | 581,281 | <p>What is the correct way to initialise a python object into already existing memory (like the inplace new in c++)</p>
<p>I tried this code however it causes an access violation with a debug build because the _ob_prev and _ob_next are not set..</p>
<pre><code>//PyVarObject *mem; -previously allocated memory
Py_INCR... | 1 | 2009-02-24T10:43:49Z | 588,598 | <p>Taking your question at face value, you have a few options. The quick and dirty method is to put an extra Py_INCREF into your initialisation code. Assuming you have no refcount bugs, the refcount will never return to zero, deallocation code will never be called, and there should be no crash. (In fact this may be ... | 0 | 2009-02-26T00:38:03Z | [
"python",
"c",
"python-3.x",
"python-c-api"
] |
Python C-API Object Initialisation | 581,281 | <p>What is the correct way to initialise a python object into already existing memory (like the inplace new in c++)</p>
<p>I tried this code however it causes an access violation with a debug build because the _ob_prev and _ob_next are not set..</p>
<pre><code>//PyVarObject *mem; -previously allocated memory
Py_INCR... | 1 | 2009-02-24T10:43:49Z | 699,132 | <p>You can look at Py_NoneStruct for how this is done. Your code looks basically right.</p>
<p>It is a refcounting bug. Statically allocated objects can never be freed, so _Py_ForgetReference should never be called.</p>
<p>If you want to be able to free them you must use use a custom allocator instead of static ini... | 0 | 2009-03-30T21:35:31Z | [
"python",
"c",
"python-3.x",
"python-c-api"
] |
Python C-API Object Initialisation | 581,281 | <p>What is the correct way to initialise a python object into already existing memory (like the inplace new in c++)</p>
<p>I tried this code however it causes an access violation with a debug build because the _ob_prev and _ob_next are not set..</p>
<pre><code>//PyVarObject *mem; -previously allocated memory
Py_INCR... | 1 | 2009-02-24T10:43:49Z | 1,964,895 | <p>What are doing is pretty horrible. Unless this code path is really performance critical I'd advise you to allocate your objects on the heap as is normally done.</p>
| 1 | 2009-12-27T01:32:02Z | [
"python",
"c",
"python-3.x",
"python-c-api"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,348 | <p>Use empty braces.</p>
<pre><code>int a = 5;
if (a == 5) {}
else {
Console.Write("Hello World");
}
</code></pre>
| 16 | 2009-02-24T11:03:27Z | [
"c#",
"python"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,350 | <p>Empty block: </p>
<pre><code>{}
</code></pre>
| 6 | 2009-02-24T11:03:58Z | [
"c#",
"python"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,357 | <p>Why not just say:</p>
<pre><code>if (a != 5)
{
Console.Write("Hello World");
}
</code></pre>
| 11 | 2009-02-24T11:05:56Z | [
"c#",
"python"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,359 | <p>Either use an empty block as suggested in other answers, or reverse the condition:</p>
<pre><code>if (a != 5)
{
Console.WriteLine("Hello world");
}
</code></pre>
<p>or more mechanically:</p>
<pre><code>if (!(a == 5))
{
Console.WriteLine("Hello world");
}
</code></pre>
| 3 | 2009-02-24T11:06:48Z | [
"c#",
"python"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,361 | <p>A better question would be why you would want to do such a thing. If you're not planning on doing anything then leave it out, rather.</p>
<pre><code>int a = 5;
if (a != 5) {
Console.Write("Hello World");
}
</code></pre>
| 1 | 2009-02-24T11:07:57Z | [
"c#",
"python"
] |
Similar to Pass in Python for C# | 581,343 | <p>In python we can .. </p>
<pre><code>a = 5
if a == 5:
pass #Do Nothing
else:
print "Hello World"
</code></pre>
<p>I wonder if it a similar way to do this in C#</p>
| 12 | 2009-02-24T11:00:30Z | 581,365 | <p>Is <code>pass</code> used in the context of a loop? If so, use the <code>continue</code> statement:</p>
<pre><code>for (var i = 0; i < 10; ++i)
{
if (i == 5)
{
continue;
}
Console.WriteLine("Hello World");
}
</code></pre>
| 4 | 2009-02-24T11:10:15Z | [
"c#",
"python"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 581,879 | <p>A temporary file is something that lasts for a single program run.</p>
<p>What you need is not, therefore, a temporary file.</p>
<p>Also, beware of multiple users on a single machine - just deleting anything with the 'temp' pattern could be anti-social, doubly so if the directory is not located securely out of the... | 3 | 2009-02-24T14:12:06Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 581,902 | <p>Creating the folder with a 4-digit random number is insecure, and you also need to worry about collisions with other instances of your program. </p>
<p>A much better way is to create the folder using <a href="http://docs.python.org/library/tempfile.html"><code>tempfile.mkdtemp</code></a>, which does exactly what yo... | 16 | 2009-02-24T14:18:50Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 581,905 | <p><code>tempfile</code> is just fine, but to be on a safe side you'd need to safe a directory name somewhere until the next run, for example pickle it. then read it in the next run and delete directory. and you are not required to have <code>/tmp</code> for the root, <code>tempfile.mkdtemp</code> has an optional <code... | 1 | 2009-02-24T14:19:27Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 581,972 | <p>"I need to create a folder that I use only once, but need to have it exist until the next run."</p>
<p>"Incidentally, the reason that I need the folder around is that I start a process ..."</p>
<p>Not incidental, at all. Crucial.</p>
<p>It appears you have the following design pattern.</p>
<pre><code>mkdir some... | 4 | 2009-02-24T14:40:04Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 582,243 | <p>What you've suggested is dangerous. You may have race conditions if anyone else is trying to create those directories -- including other instances of your application. Also, deleting anything containing "temp" may result in deleting more than you intended. As others have mentioned, <a href="http://docs.python.org... | 8 | 2009-02-24T15:43:23Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 1,414,419 | <p>The best way of creating the temporary file name is either using tempName.TemporaryFile(mode='w+b', suffix='.tmp', prifix='someRandomNumber' dir=None)
or u can use mktemp() function.</p>
<p>The mktemp() function will not actually create any file, but will provide a unique filename (actually does not contain PID). <... | 1 | 2009-09-12T06:00:59Z | [
"python",
"temporary-files"
] |
In Python, how do I make a temp file that persists until the next run? | 581,851 | <p>I need to create a folder that I use only once, but need to have it exist until the next run. It seems like I should be using the tmp_file module in the standard library, but I'm not sure how to get the behavior that I want. </p>
<p>Currently, I'm doing the following to create the directory: </p>
<pre><code>rand... | 3 | 2009-02-24T14:03:26Z | 4,330,740 | <p>You can also automatically register an function to completely remove the temporary directory on any exit (with or without error) by doing :</p>
<pre><code>import atexit
import shutil
import tempfile
# create your temporary directory
d = tempfile.mkdtemp()
# suppress it when python will be closed
atexit.register(l... | 2 | 2010-12-02T01:04:20Z | [
"python",
"temporary-files"
] |
How can you profile a Python script? | 582,336 | <p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p>
<p>What is a good way to profile how long a python program takes t... | 702 | 2009-02-24T16:01:26Z | 582,337 | <p>Python includes a profiler called cProfile. It not only gives the total running time, but also times each function separately, and tells you how many times each function was called, making it easy to determine where you should make optimizations.</p>
<p>You can call it from within your code, or from the interpreter... | 755 | 2009-02-24T16:01:40Z | [
"python",
"performance",
"profiling",
"time-complexity"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.