title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
limit downloaded page size
1,224,910
<p>Is there a way to limit amount of data downloaded by python's urllib2 module ? Sometimes I encounter with broken sites with sort of /dev/random as a page and it turns out that they use up all memory on a server.</p>
3
2009-08-03T22:20:20Z
1,224,950
<p><code>urllib2.urlopen</code> returns a file-like object, and you can (at least in theory) <code>.read(N)</code> from such an object to limit the amount of data returned to N bytes at most.</p> <p>This approach is not entirely fool-proof, because an actively-hostile site may go to quite some lengths to fool a reason...
3
2009-08-03T22:34:24Z
[ "python", "urllib2" ]
Query strange behaviour. Google App Engine datastore
1,224,939
<p>I have a model like this:</p> <pre><code>class Group(db.Model): name = db.StringProperty() description = db.TextProperty() </code></pre> <p>Sometimes when executing queries like:</p> <pre><code>groups = Group.all().order("name").fetch(20) </code></pre> <p>or</p> <pre><code>groups = Group.all() </code></pre>...
1
2009-08-03T22:28:24Z
1,224,967
<p><code>all</code> is indeed an attribute (specifically an executable one, a method) but as Group inherits from Model it should have that attribute; clearly something strange is going on, for example the name Group at the point does not refer to the object you think it does. I suggest putting a <code>try</code> / <cod...
4
2009-08-03T22:40:22Z
[ "python", "google-app-engine", "gae-datastore" ]
Can anyone explain this strange python turtle occurance?
1,224,944
<p>If you don't know, python turtle is an application for helping people learn python. You are given a python interpreter and an onscreen turtle that you can pass directions to using python. </p> <p>go(10) will cause the turtle to move 10 pixels turn(10) will cause it to turn 10 degrees clockwise</p> <p>now look at t...
1
2009-08-03T22:31:29Z
1,225,007
<p>When debugging a problem like this, it might be worthwhile to print out the value of each instruction as you perform it. Hopefully your turtle environment has a way to print values to some window on the screen. You might do something like this:</p> <pre><code>while(1): r = random.randint(1,10) print "goin...
7
2009-08-03T22:52:40Z
[ "python", "random" ]
Can anyone explain this strange python turtle occurance?
1,224,944
<p>If you don't know, python turtle is an application for helping people learn python. You are given a python interpreter and an onscreen turtle that you can pass directions to using python. </p> <p>go(10) will cause the turtle to move 10 pixels turn(10) will cause it to turn 10 degrees clockwise</p> <p>now look at t...
1
2009-08-03T22:31:29Z
1,225,041
<p>Try printing out the values. Here's a little Python program based on your code snippet that does this:</p> <pre><code>import random while(1): distance = random.randint(1,10) angle = random.randint(-90, 90) print distance, angle </code></pre> <p>I tried this myself, and at no point does angle "get stuck". I s...
0
2009-08-03T23:01:40Z
[ "python", "random" ]
Can anyone explain this strange python turtle occurance?
1,224,944
<p>If you don't know, python turtle is an application for helping people learn python. You are given a python interpreter and an onscreen turtle that you can pass directions to using python. </p> <p>go(10) will cause the turtle to move 10 pixels turn(10) will cause it to turn 10 degrees clockwise</p> <p>now look at t...
1
2009-08-03T22:31:29Z
1,230,057
<p>I am the creator of <a href="http://pythonturtle.com" rel="nofollow">PythonTurtle</a>.</p> <p>First of all, I'm really honored to see the first question about it in StackOverflow.</p> <p>About your question: I tried running the code, and it didn't produce the bug, but since this is involving randomness, I can't re...
1
2009-08-04T21:38:46Z
[ "python", "random" ]
Create properties using lambda getter and setter
1,224,962
<p>I have something like this:</p> <pre><code>class X(): def __init__(self): self.__name = None def _process_value(self, value): # do something pass def get_name(self): return self.__name def set_name(self, value): self.__name = self._process_value(value) ...
3
2009-08-03T22:39:33Z
1,224,977
<p>Your problem is that lambda's body must be an expression and assignment is a statement (a strong, deep distinction in Python). If you insist on perpetrating <code>lambda</code>s you'll meet many such cases and learn the workarounds (there's usually one, though not always), such as, in this case:</p> <pre><code>name...
14
2009-08-03T22:44:28Z
[ "python", "lambda", "properties" ]
Create properties using lambda getter and setter
1,224,962
<p>I have something like this:</p> <pre><code>class X(): def __init__(self): self.__name = None def _process_value(self, value): # do something pass def get_name(self): return self.__name def set_name(self, value): self.__name = self._process_value(value) ...
3
2009-08-03T22:39:33Z
13,780,286
<p>If you are extending a <code>list</code>, you can also use <code>__setitem__</code>, like this:</p> <pre><code>class Position(list): def __init__(self,x=0, y=0, z=0): super(Position, self).__init__((x,y,z)) x = property(lambda self: self[0], lambda self,value: self.__setitem__(0, v...
0
2012-12-08T18:07:14Z
[ "python", "lambda", "properties" ]
How to setup mod_python configuration variables?
1,224,978
<p>I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related:</p> <ol> <li><p>I need a way to configure variables that will be available in Python while running. I currently just hav...
0
2009-08-03T22:44:52Z
1,225,721
<ol> <li><p>Using <code>PythonOption</code> lets you configure stuff that may need to change from server to server. I wouldn't use it too much, though, because messing with the Apache configuration directives is kind of a pain (plus it requires reloading the server). You might consider something like using <code>Pytho...
1
2009-08-04T04:24:15Z
[ "python", "configuration", "mod-python" ]
How to setup mod_python configuration variables?
1,224,978
<p>I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related:</p> <ol> <li><p>I need a way to configure variables that will be available in Python while running. I currently just hav...
0
2009-08-03T22:44:52Z
1,226,722
<ol> <li><p>I need a way to configure variables that will be available in Python while running. </p> <p>Do what Django does. Use a simple importable script of Python assignment statements.</p></li> <li><p>I need a way to store state on the server. </p> <p>Do what Django does. Use SQLite3.</p></li> </ol> <p>Also, r...
0
2009-08-04T10:20:49Z
[ "python", "configuration", "mod-python" ]
Haskell equivalent of Python's "Construct"
1,225,053
<p><a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a> is a DSL implemented in Python used to describe data structures (binary and textual). Once you have the data structure described, construct can parse and build it for you. Which is good ("DRY", "Declarative", "Denotational-Semantics"...)</p> <p...
2
2009-08-03T23:05:15Z
1,225,340
<p>I don't know anything about Python or Construct, so this is probably not what you are searching for, but for simple data structures you can always just derive read:</p> <pre><code>data Test a = I Int | S a deriving (Read,Show) </code></pre> <p>Now, for the expression</p> <pre><code>read "S 123" :: Test Double </c...
0
2009-08-04T01:09:45Z
[ "python", "parsing", "haskell", "dsl", "construct" ]
Haskell equivalent of Python's "Construct"
1,225,053
<p><a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a> is a DSL implemented in Python used to describe data structures (binary and textual). Once you have the data structure described, construct can parse and build it for you. Which is good ("DRY", "Declarative", "Denotational-Semantics"...)</p> <p...
2
2009-08-03T23:05:15Z
1,227,818
<p>I'd say it depends what you want to do, and if you need to comply with any existing format.</p> <p><a href="http://hackage.haskell.org/package/binary" rel="nofollow">Data.Binary</a> will (surprise!) help you with binary data, both reading and writing. You can either write the code to read/write yourself, or let go ...
1
2009-08-04T14:21:23Z
[ "python", "parsing", "haskell", "dsl", "construct" ]
Haskell equivalent of Python's "Construct"
1,225,053
<p><a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a> is a DSL implemented in Python used to describe data structures (binary and textual). Once you have the data structure described, construct can parse and build it for you. Which is good ("DRY", "Declarative", "Denotational-Semantics"...)</p> <p...
2
2009-08-03T23:05:15Z
1,346,693
<p>Currently (afaik) there is no equivalent to Construct in Haskell.</p> <p>One can be implemented using Template Haskell.</p>
0
2009-08-28T12:26:29Z
[ "python", "parsing", "haskell", "dsl", "construct" ]
How can I determine the monitor refresh rate?
1,225,057
<p>Is there a cross platform way to get the monitor's refresh rate in python (2.6)? I'm using Pygame and PyOpenGL, if that helps.</p> <p>I don't need to change the refresh rate, I just need to know what it is.</p>
2
2009-08-03T23:07:02Z
1,225,763
<p>I am not sure about the platform you use, but on window you can use ctypes or win32api to get details about devices e.g. using win32api</p> <pre><code>import win32api def printInfo(device): print device.DeviceName, device.DeviceString settings = win32api.EnumDisplaySettings(device.DeviceName, 0) for va...
3
2009-08-04T04:40:50Z
[ "python", "pygame", "pyopengl" ]
Flexible 'em' style Fonts with wxPython
1,225,248
<p>Im looking for a way to present a flexible font, that will increase and decrease in size according to to the size of the screen resolution. I want to be able to do this without the HTML window class. Is there a way? I thought I've done quite a bit of googling without success.</p> <p><strong>EDIT</strong> This s...
4
2009-08-04T00:23:23Z
1,246,679
<p>Maybe something like this? You can scale any wx.Window in this way. Not sure if this is exactly what you mean though.</p> <pre><code>import wx def scale(widget, percentage): font = widget.GetFont() font.SetPointSize(int(font.GetPointSize() * percentage / 100.0)) widget.SetFont(font) class Frame(wx.F...
1
2009-08-07T19:49:59Z
[ "python", "fonts", "wxpython", "font-size" ]
Python: How to import part of a namespace
1,225,481
<p>I have a structure such this works :</p> <pre><code>import a.b.c a.b.c.foo() </code></pre> <p>and this also works :</p> <pre><code>from a.b import c c.foo() </code></pre> <p>but this doesn't work :</p> <pre><code>from a import b.c b.c.foo() </code></pre> <p>nor does :</p> <pre><code>from a import b b.c.foo() ...
3
2009-08-04T02:25:08Z
1,225,490
<p>In your '<code>b</code>' package, you need to add '<code>import c</code>' so that it is always accessible as part of <code>b</code>.</p>
2
2009-08-04T02:27:44Z
[ "python", "import", "namespaces" ]
Python: How to import part of a namespace
1,225,481
<p>I have a structure such this works :</p> <pre><code>import a.b.c a.b.c.foo() </code></pre> <p>and this also works :</p> <pre><code>from a.b import c c.foo() </code></pre> <p>but this doesn't work :</p> <pre><code>from a import b.c b.c.foo() </code></pre> <p>nor does :</p> <pre><code>from a import b b.c.foo() ...
3
2009-08-04T02:25:08Z
1,225,506
<pre><code>from a import b from a.b import c b.c = c </code></pre>
2
2009-08-04T02:37:29Z
[ "python", "import", "namespaces" ]
Python: How to import part of a namespace
1,225,481
<p>I have a structure such this works :</p> <pre><code>import a.b.c a.b.c.foo() </code></pre> <p>and this also works :</p> <pre><code>from a.b import c c.foo() </code></pre> <p>but this doesn't work :</p> <pre><code>from a import b.c b.c.foo() </code></pre> <p>nor does :</p> <pre><code>from a import b b.c.foo() ...
3
2009-08-04T02:25:08Z
1,225,515
<pre><code>import a.b.c from a import b b.c.foo() </code></pre> <p>The order of the import statements doesn't matter.</p>
0
2009-08-04T02:42:13Z
[ "python", "import", "namespaces" ]
Python: How to import part of a namespace
1,225,481
<p>I have a structure such this works :</p> <pre><code>import a.b.c a.b.c.foo() </code></pre> <p>and this also works :</p> <pre><code>from a.b import c c.foo() </code></pre> <p>but this doesn't work :</p> <pre><code>from a import b.c b.c.foo() </code></pre> <p>nor does :</p> <pre><code>from a import b b.c.foo() ...
3
2009-08-04T02:25:08Z
1,225,522
<p>Just rename it:</p> <pre><code> from a.b import c as BAR BAR.foo() </code></pre>
9
2009-08-04T02:46:04Z
[ "python", "import", "namespaces" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,225,565
<p>You can use <a href="http://search.cpan.org/dist/Spreadsheet-ParseExcel/lib/Spreadsheet/ParseExcel.pm" rel="nofollow"><code>Spreadsheet::ParseExcel</code></a> to read a spreadsheet. Unfortunately that is all I can help you with because, frankly, the description of your problem makes no sense.</p>
6
2009-08-04T03:08:04Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,225,581
<p>You could use Visual Basic for Applications to <a href="http://msdn.microsoft.com/en-us/library/aa221353%28office.11%29.aspx" rel="nofollow">loop over the cells</a> and then <a href="http://www.vbaexpress.com/kb/getarticle.php?kb%5Fid=805" rel="nofollow">save to a text file</a>.</p> <p>OR</p> <p>Save the file as a...
-1
2009-08-04T03:13:13Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,225,629
<p>In python, you can use <a href="http://pypi.python.org/pypi/xlrd" rel="nofollow">xlrd</a> to read an Excel spreadsheet into data you can work with. See the <a href="http://www.lexicon.net/sjmachin/README.html" rel="nofollow">README</a> for sample usage. You can then use <a href="http://pypi.python.org/pypi/xlwt" r...
1
2009-08-04T03:35:48Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,226,481
<p>in Excel, save your file as CSV.</p> <p>in Python, use the CSV reader module to read it (read the python docs, search for csv)</p> <p>now you say you have rows of maybe 20 columns and you want to put columns 1..10 in file A and columns 11..20 in file B, yes ?</p> <p>open 2 csv writers (let's call them A and B)</p...
1
2009-08-04T09:24:09Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,228,425
<p>I think the <a href="http://www.lexicon.net/sjmachin/xlrd.htm" rel="nofollow">xlrd</a> and xlwt modules are the way to go in Python.</p> <pre><code># Read the first 5 rows and columns of an excel file import xlrd # Import the package book = xlrd.open_workbook("sample.xls") # Open an .xls file sheet = book.sheet_by_...
0
2009-08-04T16:08:30Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,229,443
<p>As others have commented your question is almost totally incomprehensible. Based on the difficulty you have describing your issue, you might want to take a look at <a href="http://stackoverflow.com/questions/1120016/hash-arrays-combining-files-how/1122992#1122992">this post</a>.</p> <p>Some here have suggested sa...
1
2009-08-04T19:29:02Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
1,230,735
<p>Use Python and xlrd &amp; xlwt. See <a href="http://www.python-excel.org" rel="nofollow">http://www.python-excel.org</a></p> <p>The following script should do what you want:</p> <pre><code>import xlrd, xlwt, sys def raj_split(in_path, out_stem): in_book = xlrd.open_workbook(in_path) in_sheet = in_book.she...
2
2009-08-05T01:21:47Z
[ "python", "perl" ]
Need to make multiple files from a single excel file
1,225,550
<p>I have a excel file. With many columns . I need to make multiple files using this </p> <p>Eg: 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2. So these are the excel columns with each having many rows. I need one file which would contain 0 0 0 0 0 1 1 1 1 1 2 then second will contain only the second no 0 0 0 0 0 1 1 ...
-1
2009-08-04T03:01:19Z
2,236,866
<p>Actually, this is exactly what xlutils' filter functionality was designed for.</p> <p>See the 'xutils.filter' section in the tutorial on <a href="http://www.python-excel.org" rel="nofollow">http://www.python-excel.org</a></p> <p>There's also more here:</p> <p><a href="https://secure.simplistix.co.uk/svn/xlutils/t...
0
2010-02-10T12:54:47Z
[ "python", "perl" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,225,590
<p>I found a <a href="http://snippets.dzone.com/posts/show/4024" rel="nofollow">pretty good snippet</a> when I wanted to do this same thing (and the example uses gmail). Also check out the <a href="http://www.google.com/search?q=Checking+email+with+Python&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:unof...
1
2009-08-04T03:18:07Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,225,592
<p>Gmail provides an atom <a href="https://mail.google.com/mail/feed/atom">feed</a> for new email messages. You should be able to monitor this by authenticating with py cURL (or some other net library) and pulling down the feed. Making a GET request for each new message should mark it as read, so you won't have to keep...
14
2009-08-04T03:18:24Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,225,707
<p>While not Python-specific, I've always loved <a href="http://www.procmail.org/">procmail</a> wherever I could install it...!</p> <p>Just use as some of your action lines for conditions of your choice <code>| pathtoyourscript</code> (vertical bar AKA pipe followed by the script you want to execute in those cases) an...
7
2009-08-04T04:17:40Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,225,847
<p>I recently solved this problem by using procmail and python</p> <p>Read the documentation for procmail. You can tell it to send all incoming email to a python script like this in a special procmail config file</p> <pre><code>:0: | ./scripts/ppm_processor.py </code></pre> <p>Python has an "email" package available...
0
2009-08-04T05:49:42Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,228,982
<p>People seem to be pumped up about Lamson:</p> <p><a href="https://github.com/zedshaw/lamson" rel="nofollow">https://github.com/zedshaw/lamson</a></p> <p>It's an SMTP server written entirely in Python. I'm sure you could leverage that to do everything you need - just forward the gmail messages to that SMTP server ...
4
2009-08-04T17:51:44Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
1,231,136
<p>Gmail provides the ability to connect over POP, which you can turn on in the gmail settings panel. Python can make connections over POP pretty easily:</p> <pre><code>import poplib from email import parser pop_conn = poplib.POP3_SSL('pop.gmail.com') pop_conn.user('username') pop_conn.pass_('password') #Get messages...
52
2009-08-05T04:04:04Z
[ "python", "email" ]
Checking email with Python
1,225,586
<p>I am interested to trigger a certain action upon receiving an email from specific address with specific subject. In order to be able to do so I need to implement monitoring of my mailbox, checking every incoming mail (in particular, i use gmail). what is the easiest way to do that?</p> <p>Thank you, Sasha</p>
38
2009-08-04T03:15:46Z
20,632,538
<pre><code>print('please enter str') def email(n=str): if ('@' and '.') in n: z=n.find('@') c=n.find('.') if type(n[:z] and n[z:c] and n[c:])==str: print('valid email') else: print('unvalid email') else: print('unvalid email') </code></pre>
-6
2013-12-17T11:09:06Z
[ "python", "email" ]
Inserting the same value multiple times when formatting a string
1,225,637
<p>I have a string of this form</p> <pre><code>s='arbit' string='%s hello world %s hello world %s' %(s,s,s) </code></pre> <p>All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)</p>
67
2009-08-04T03:42:20Z
1,225,648
<pre><code>incoming = 'arbit' result = '%(s)s hello world %(s)s hello world %(s)s' % {'s': incoming} </code></pre> <p>You may like to have a read of this to get an understanding: <a href="http://docs.python.org/library/stdtypes.html#string-formatting" rel="nofollow">String Formatting Operations</a>.</p>
30
2009-08-04T03:46:06Z
[ "python", "string", "format" ]
Inserting the same value multiple times when formatting a string
1,225,637
<p>I have a string of this form</p> <pre><code>s='arbit' string='%s hello world %s hello world %s' %(s,s,s) </code></pre> <p>All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)</p>
67
2009-08-04T03:42:20Z
1,225,656
<p>You can use <a href="http://www.python.org/dev/peps/pep-3101/">advanced string formatting</a>, available in Python 2.6 and Python 3.x:</p> <pre><code>incoming = 'arbit' result = '{0} hello world {0} hello world {0}'.format(incoming) </code></pre>
127
2009-08-04T03:49:01Z
[ "python", "string", "format" ]
Inserting the same value multiple times when formatting a string
1,225,637
<p>I have a string of this form</p> <pre><code>s='arbit' string='%s hello world %s hello world %s' %(s,s,s) </code></pre> <p>All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)</p>
67
2009-08-04T03:42:20Z
1,225,681
<p>You can use the dictionary type of formatting:</p> <pre><code>s='arbit' string='%(key)s hello world %(key)s hello world %(key)s' % {'key': s,} </code></pre>
13
2009-08-04T03:59:59Z
[ "python", "string", "format" ]
Inserting the same value multiple times when formatting a string
1,225,637
<p>I have a string of this form</p> <pre><code>s='arbit' string='%s hello world %s hello world %s' %(s,s,s) </code></pre> <p>All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)</p>
67
2009-08-04T03:42:20Z
1,225,724
<p>Depends on what you mean by better. This works if your goal is removal of redundancy.</p> <pre><code>s='foo' string='%s bar baz %s bar baz %s bar baz' % (3*(s,)) </code></pre>
10
2009-08-04T04:26:12Z
[ "python", "string", "format" ]
Inserting the same value multiple times when formatting a string
1,225,637
<p>I have a string of this form</p> <pre><code>s='arbit' string='%s hello world %s hello world %s' %(s,s,s) </code></pre> <p>All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)</p>
67
2009-08-04T03:42:20Z
12,084,650
<pre><code>&gt;&gt;&gt; s1 ='arbit' &gt;&gt;&gt; s2 = 'hello world '.join( [s]*3 ) &gt;&gt;&gt; print s2 arbit hello world arbit hello world arbit </code></pre>
3
2012-08-23T04:14:55Z
[ "python", "string", "format" ]
How to control Webpage dialog with python
1,225,686
<p>When I try to automatically download a file from some webpage using Python, I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is that I don't know how to control Webpage Dialog with Python. I tri...
0
2009-08-04T04:03:09Z
1,226,061
<p>You can't, and you don't want to. When you ask a question, try explaining what you are trying to achieve, and not just the task immediately before you. You are likely barking down the wrong path. There is some other way of doing what you are trying to do.</p>
0
2009-08-04T07:15:07Z
[ "python", "dialog", "webpage" ]
How to control Webpage dialog with python
1,225,686
<p>When I try to automatically download a file from some webpage using Python, I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is that I don't know how to control Webpage Dialog with Python. I tri...
0
2009-08-04T04:03:09Z
15,988,283
<p>The title 'Webpage Dialog' suggests that is a Javascript-generated input box, hence why you can't access it via winGuiAuto. What you're asking directly is unlikely to be possible.</p> <p>However, making the assumption that what you want to do is just download this data from the site, why are you using the GUI at al...
0
2013-04-13T13:24:39Z
[ "python", "dialog", "webpage" ]
How to control Webpage dialog with python
1,225,686
<p>When I try to automatically download a file from some webpage using Python, I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is that I don't know how to control Webpage Dialog with Python. I tri...
0
2009-08-04T04:03:09Z
25,562,482
<p>It is better to use selenium Python bindings:</p> <pre><code>from selenium import webdriver from selenium.webdriver.common import alert from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import TimeoutException cla...
0
2014-08-29T06:02:51Z
[ "python", "dialog", "webpage" ]
Terminate long running python threads
1,226,091
<p>What is the recommended way to terminate unexpectedly long running threads in python ? I can't use SIGALRM, since</p> <blockquote> <p>Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always p...
5
2009-08-04T07:25:14Z
1,226,100
<p>If you are trying to kill a thread whose code you do not have control over, it depends if the thread is in a blocking call or not. In my experience if the thread is properly blocking, there is no recommended and portable way of doing this.</p> <p>I've run up against this when trying to work with code in the standa...
1
2009-08-04T07:28:09Z
[ "python", "multithreading" ]
Terminate long running python threads
1,226,091
<p>What is the recommended way to terminate unexpectedly long running threads in python ? I can't use SIGALRM, since</p> <blockquote> <p>Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always p...
5
2009-08-04T07:25:14Z
1,226,101
<p>Use synchronization objects and ask the thread to terminate. Basically, write co-operative handling of this.</p> <p>If you start yanking out the thread beneath the python interpreter, all sorts of odd things can occur, and it's not just in Python either, most runtimes have this problem.</p> <p>For instance, let's ...
1
2009-08-04T07:28:40Z
[ "python", "multithreading" ]
Terminate long running python threads
1,226,091
<p>What is the recommended way to terminate unexpectedly long running threads in python ? I can't use SIGALRM, since</p> <blockquote> <p>Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always p...
5
2009-08-04T07:25:14Z
1,227,814
<p>Since abruptly killing a thread that's in a blocking call is not feasible, a better approach, when possible, is to avoid using threads in favor of other multi-tasking mechanisms that don't suffer from such issues.</p> <p>For the OP's specific case (the threads' job is to download web pages, and some threads block f...
6
2009-08-04T14:20:53Z
[ "python", "multithreading" ]
Terminate long running python threads
1,226,091
<p>What is the recommended way to terminate unexpectedly long running threads in python ? I can't use SIGALRM, since</p> <blockquote> <p>Some care must be taken if both signals and threads are used in the same program. The fundamental thing to remember in using signals and threads simultaneously is: always p...
5
2009-08-04T07:25:14Z
1,806,891
<p>As Alex Martelli suggested, you could use the multiprocessing module. It is very similar to the Threading module so that should get you off to a start easily. Your code could be like this for example:</p> <pre><code>import multiprocessing def get_page(*args, **kwargs): # your web page downloading code goes her...
5
2009-11-27T05:18:22Z
[ "python", "multithreading" ]
Django ManyToMany relation add() error
1,226,290
<p>I've got a model that looks like this,</p> <pre><code>class PL(models.Model): locid = models.AutoField(primary_key=True) mentionedby = models.ManyToManyField(PRT) class PRT(models.Model): tid = .. </code></pre> <p>The resulting many to many table in mysql is formed as,</p> <pre><code>+---------------...
2
2009-08-04T08:26:19Z
1,226,317
<p>Are these the complete models? I can only assume that something's been overriden somewhere, that probably shouldn't have been.</p> <p>Can you post the full code?</p>
0
2009-08-04T08:33:25Z
[ "python", "django", "django-models", "many-to-many" ]
Django ManyToMany relation add() error
1,226,290
<p>I've got a model that looks like this,</p> <pre><code>class PL(models.Model): locid = models.AutoField(primary_key=True) mentionedby = models.ManyToManyField(PRT) class PRT(models.Model): tid = .. </code></pre> <p>The resulting many to many table in mysql is formed as,</p> <pre><code>+---------------...
2
2009-08-04T08:26:19Z
1,843,427
<p>Adding <code>prt</code> directly should work on first try. How are you retrieving <code>pl</code> and <code>prt</code>? Assuming you have some data in your database, try those commands from the Django shell and see if it works. There seems to be some missing information from the question. After running <code>pyt...
5
2009-12-03T22:26:13Z
[ "python", "django", "django-models", "many-to-many" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,226,683
<p>Using CPython's threading model will not give you any performance improvement, because the threads are not actually executed in parallel, due to the way garbage collection is handled. Multiprocess would allow parallel execution. Obviously in this case you have to have multiple cores available to farm out your parall...
0
2009-08-04T10:10:58Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,226,759
<p>For small collections of data, simply create subprocesses with <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen" rel="nofollow">subprocess.Popen</a>.</p> <p>Each subprocess can simply get it's piece of data from stdin or from command-line arguments, do it's processing, and simply write the r...
7
2009-08-04T10:31:42Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,226,873
<p>You might consider looking into <a href="http://www.stackless.com/">Stackless Python</a>. If you have control over the function that takes a long time, you can just throw some <code>stackless.schedule()</code>s in there (saying yield to the next coroutine), or else you can <a href="http://www.stackless.com/wiki/Sch...
7
2009-08-04T11:06:41Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,227,204
<p>If you are truly compute bound, using the <a href="http://docs.python.org/library/multiprocessing.html#module-multiprocessing" rel="nofollow">multiprocessing module</a> is probably the lightest weight solution (in terms of both memory consumption and implementation difficulty.)</p> <p>If you are I/O bound, using th...
29
2009-08-04T12:26:14Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,227,628
<p>If you can easily partition and separate the data you have, it sounds like you should just do that partitioning externally, and feed them to several processes of your program. (i.e. several processes instead of threads)</p>
0
2009-08-04T13:45:04Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
1,252,808
<p>IronPython has real multithreading, unlike CPython and it's GIL. So depending on what you're doing it may be worth looking at. But it sounds like your use case is better suited to the multiprocessing module.</p> <p>To the guy who recommends stackless python, I'm not an expert on it, but it seems to me that he's tal...
0
2009-08-10T01:31:12Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
2,137,311
<p>Tasks runs like sequentially but you have the illusion that are run in parallel. Tasks are good when you use for file or connection I/O and because are lightweights.</p> <p>Multiprocess with Pool may be the right solution for you because processes runs in parallel so are very good with intensive computing because e...
9
2010-01-26T03:13:17Z
[ "python", "multithreading", "multiprocess" ]
multiprocess or threading in python?
1,226,584
<p>I have a python application that grabs a collection of data and for each piece of data in that collection it performs a task. The task takes some time to complete as there is a delay involved. Because of this delay, I don't want each piece of data to perform the task subsequently, I want them to all happen in parall...
31
2009-08-04T09:47:19Z
2,137,331
<p>You may want to look at <a href="http://www.twistedmatrix.com" rel="nofollow">Twisted</a>. It is designed for asynchronous network tasks. </p>
0
2010-01-26T03:17:43Z
[ "python", "multithreading", "multiprocess" ]
What is a good configuration file library for c thats not xml (preferably has python bindings)?
1,227,031
<p>I am looking for a good config file library for c that is not xml. Optimally I would really like one that also has python bindings. The best option I have come up with is to use a JSON library in both c and python. What would you recommend, or what method of reading/writing configuration settings do you prefer?</p>
5
2009-08-04T11:48:37Z
1,227,227
<p>You could use a pure python solution like <a href="http://www.voidspace.org.uk/python/configobj.html" rel="nofollow">ConfigObj</a> and then simply use the CPython API to query for settings. This assumes that your application embeds Python. If it doesn't, and if you are shipping Python anyway, it might make sense t...
0
2009-08-04T12:29:52Z
[ "python", "c", "configuration-management" ]
What is a good configuration file library for c thats not xml (preferably has python bindings)?
1,227,031
<p>I am looking for a good config file library for c that is not xml. Optimally I would really like one that also has python bindings. The best option I have come up with is to use a JSON library in both c and python. What would you recommend, or what method of reading/writing configuration settings do you prefer?</p>
5
2009-08-04T11:48:37Z
1,227,256
<p>Despite being hated by techies and disowned by Microsoft, INI files are actually quite popular with users, as they are easy to understand and edit. They are also very simple to write parsers for, should your libraries not already support them.</p>
0
2009-08-04T12:35:56Z
[ "python", "c", "configuration-management" ]
What is a good configuration file library for c thats not xml (preferably has python bindings)?
1,227,031
<p>I am looking for a good config file library for c that is not xml. Optimally I would really like one that also has python bindings. The best option I have come up with is to use a JSON library in both c and python. What would you recommend, or what method of reading/writing configuration settings do you prefer?</p>
5
2009-08-04T11:48:37Z
1,227,272
<p><a href="http://www.yaml.org/">YaML</a> :)</p>
5
2009-08-04T12:38:53Z
[ "python", "c", "configuration-management" ]
What is a good configuration file library for c thats not xml (preferably has python bindings)?
1,227,031
<p>I am looking for a good config file library for c that is not xml. Optimally I would really like one that also has python bindings. The best option I have come up with is to use a JSON library in both c and python. What would you recommend, or what method of reading/writing configuration settings do you prefer?</p>
5
2009-08-04T11:48:37Z
1,227,797
<p>If you're not married to Python, try <a href="http://www.lua.org/" rel="nofollow">Lua</a>. It was originally designed for configuration.</p>
1
2009-08-04T14:17:38Z
[ "python", "c", "configuration-management" ]
Substitutions inside links in reST / Sphinx
1,227,037
<p>I am using Sphinx to document a webservice that will be deployed in different servers. The documentation is full of URL examples for the user to click and they should just work. My problem is that the host, port and deployment root will vary and the documentation will have to be re-generated for every deployment.</p...
16
2009-08-04T11:49:27Z
1,232,671
<p>You can write a Sphinx <a href="http://sphinx.pocoo.org/extensions.html" rel="nofollow">extension</a> that creates a <a href="http://sphinx.pocoo.org/markup/inline.html" rel="nofollow">role</a> like</p> <pre><code>:apilink:`path` </code></pre> <p>and generates the link from that. I never did this, so I can't help ...
1
2009-08-05T11:28:34Z
[ "python", "documentation", "substitution", "restructuredtext", "python-sphinx" ]
Substitutions inside links in reST / Sphinx
1,227,037
<p>I am using Sphinx to document a webservice that will be deployed in different servers. The documentation is full of URL examples for the user to click and they should just work. My problem is that the host, port and deployment root will vary and the documentation will have to be re-generated for every deployment.</p...
16
2009-08-04T11:49:27Z
1,233,122
<p>Ok, here's how I did it. First, <code>apilinks.py</code> (the Sphinx extension):</p> <pre><code>from docutils import nodes, utils def setup(app): def api_link_role(role, rawtext, text, lineno, inliner, options={}, content=[]): ref = app.config.apilinks_base + text node = n...
4
2009-08-05T13:03:10Z
[ "python", "documentation", "substitution", "restructuredtext", "python-sphinx" ]
Substitutions inside links in reST / Sphinx
1,227,037
<p>I am using Sphinx to document a webservice that will be deployed in different servers. The documentation is full of URL examples for the user to click and they should just work. My problem is that the host, port and deployment root will vary and the documentation will have to be re-generated for every deployment.</p...
16
2009-08-04T11:49:27Z
5,808,741
<p>New in Sphinx v1.0:</p> <p>sphinx.ext.extlinks – Markup to shorten external links</p> <p><a href="http://sphinx.pocoo.org/ext/extlinks.html">http://sphinx.pocoo.org/ext/extlinks.html</a></p> <p>The extension adds one new config value:</p> <p><strong>extlinks</strong></p> <p>This config value must be a diction...
18
2011-04-27T18:26:10Z
[ "python", "documentation", "substitution", "restructuredtext", "python-sphinx" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
1,227,141
<p>When comparing instances of objects, the <a href="http://docs.python.org/reference/datamodel.html#object.__cmp__" rel="nofollow"><code>__cmp__</code></a> function is called.</p> <p>If the == operator is not working for you by default, you can always redefine the <code>__cmp__</code> function for the object.</p> <p...
2
2009-08-04T12:13:58Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
1,227,147
<p>Implement the <code>__eq__</code> method in your class; something like this:</p> <pre><code>def __eq__(self, other): return self.path == other.path and self.title == other.title </code></pre> <p>Edit: if you want your objects to compare equal if and only if they have equal instance dictionaries:</p> <pre><cod...
3
2009-08-04T12:15:18Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
1,227,152
<p>You override the <a href="https://docs.python.org/2.7/reference/datamodel.html#object.__lt__">rich comparison operators</a> in your object.</p> <pre><code>class MyClass: def __lt__(self, other): # return comparison def __le__(self, other) # return comparison def __eq__(self, other) # return com...
29
2009-08-04T12:15:43Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
1,227,201
<p>Instance of a class when compared with == comes to non-equal. The best way is to ass the <strong>cmp</strong> function to your class which will do the stuff. </p> <p>If you want to do comparison by the content you can simply use cmp(obj1,obj2)</p> <p>In your case cmp(doc1,doc2) It will return -1 if the content wis...
-4
2009-08-04T12:25:15Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
1,227,325
<p>As usual with Python, it's <a href="http://en.wikipedia.org/wiki/KISS_principle">kiss</a> :</p> <pre><code>class Test(object): def __init__(self, attr1, attr2): self.attr1 = attr1 self.attr2 = attr2 def __str__(self): return str(self.__dict__) def __eq__(self, other): ...
144
2009-08-04T12:47:34Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
17,043,417
<p>I tried the initial example (see 7 above) and it did not work in ipython. Note that cmp(obj1,obj2) returns a "1" when implemented using two identical object instances. Oddly enough when I modify one of the attribute values and recompare, using cmp(obj1,obj2) the object continues to return a "1". (sigh...)</p> <p>...
-1
2013-06-11T11:52:01Z
[ "python", "object" ]
Compare object instances for equality by their attributes in Python
1,227,121
<p>What is the best way to compare two instances of some object for equality in Python? I'd like to be able to do something like</p> <p>Example:</p> <pre><code>doc1 = ErrorDocument(path='/folder',title='Page') doc2 = ErrorDocument(path='/folder',title='Page') if doc1 == doc2: # this should be True #do something ...
106
2009-08-04T12:09:38Z
35,652,262
<p>As a summary : </p> <ol> <li>It's advised to implement <code>__eq__</code> rather than <code>__cmp__</code>, except if you run python &lt;= 2.0 (<code>__eq__</code> has been added in 2.1)</li> <li>Don't forget to also implement <code>__ne__</code> (should be something like <code>return not self.__eq__(other)</code>...
1
2016-02-26T12:53:12Z
[ "python", "object" ]
can someone help me understand this short .py
1,227,448
<p>I'm trying to understand basic threading in python, I'm having trouble understanding how pooling works with the queue module. Heres the example server used in the howto I'm reading from: <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/2/" rel="nofollow">http://www.devshed.com/c/a/Python/Basic-Th...
2
2009-08-04T13:08:57Z
1,227,472
<p>The <code>pickledList</code> variable is available as a global variable in the <code>ClientThread</code> class. See <a href="http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules">Short Description of Python Scoping Rules</a>.</p>
4
2009-08-04T13:12:28Z
[ "python", "multithreading" ]
can someone help me understand this short .py
1,227,448
<p>I'm trying to understand basic threading in python, I'm having trouble understanding how pooling works with the queue module. Heres the example server used in the howto I'm reading from: <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/2/" rel="nofollow">http://www.devshed.com/c/a/Python/Basic-Th...
2
2009-08-04T13:08:57Z
1,227,473
<p>Threads don't have their own namespace. <code>pickledList</code> is defined as a global, so it is accessible to the object. Technically it should have had a <code>global pickledList</code> at the top of the function to make that clear, but it's not always needed.</p> <p><strong>EDIT</strong></p> <p>By make it cl...
-2
2009-08-04T13:12:42Z
[ "python", "multithreading" ]
Soaplib functions with default arguments
1,227,547
<p>I have to write soaplib method, that has many arguments. The idea is that the user should able able to choose, which arguments he wants to provide. Is that even possible?</p> <p>I know it is possible in python generally, but there is an error, when i try to set it up like normal python method with default arguments...
0
2009-08-04T13:30:17Z
1,331,676
<p>Create complex type</p> <pre><code>class Parameters(ClassSerializer): class types: param1 = primitive.String param2 = primitive.String param3 = primitive.String ... @soapmethod(Parameters, _returns=primitive.String, _outVariableName='return') def soSomething(self, parameters): if p...
0
2009-08-26T00:10:20Z
[ "python", "soap", "default-value" ]
python ctype recursive structures
1,228,158
<p>I've developped a DLL for a driver in C. I wrote a test program in C++ and the DLL works fine.</p> <p>Now I'd like to interract with this DLL using Python. I've successfully hidden most of the user defined C structures but there is one point where I have to use C structures. I'm rather new to python so I may get th...
7
2009-08-04T15:25:03Z
1,228,204
<p>You'll have to access <code>_fields_</code> statically after you've created it.</p> <pre><code>class EthercatDatagram(Structure) _fields_ = [...] EthercatDatagram._fields_.append(("next_command", EthercatDatagram)) </code></pre>
-1
2009-08-04T15:33:59Z
[ "python", "dll", "recursion", "structure", "ctypes" ]
python ctype recursive structures
1,228,158
<p>I've developped a DLL for a driver in C. I wrote a test program in C++ and the DLL works fine.</p> <p>Now I'd like to interract with this DLL using Python. I've successfully hidden most of the user defined C structures but there is one point where I have to use C structures. I'm rather new to python so I may get th...
7
2009-08-04T15:25:03Z
1,228,447
<p>You almost certainly want to declare next_command as a pointer. Having a structure that contains itself isn't possible (in any language).</p> <p>I think this is what you want:</p> <pre><code>class EthercatDatagram(Structure): pass EthercatDatagram._fields_ = [ ("header", EthercatDatagramHeader), ("pac...
14
2009-08-04T16:12:55Z
[ "python", "dll", "recursion", "structure", "ctypes" ]
python ctype recursive structures
1,228,158
<p>I've developped a DLL for a driver in C. I wrote a test program in C++ and the DLL works fine.</p> <p>Now I'd like to interract with this DLL using Python. I've successfully hidden most of the user defined C structures but there is one point where I have to use C structures. I'm rather new to python so I may get th...
7
2009-08-04T15:25:03Z
17,833,687
<p>The reason why </p> <pre><code>EthercatDatagram._fields_.append(("next_command", EthercatDatagram)) </code></pre> <p>does not work is that the machinery that creates the descriptor objects (see the source of the <a href="http://hg.python.org/cpython/file/tip/Modules/_ctypes/_ctypes.c" rel="nofollow"><code>PyCStruc...
0
2013-07-24T12:11:36Z
[ "python", "dll", "recursion", "structure", "ctypes" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
1,228,327
<p>Python strings are immutable, you change them by making a copy.<br /> The easiest way to do what you want is probably.</p> <pre><code>text = "Z" + text[1:] </code></pre> <p>The text[1:] return the string in text from position 1 to the end, positions count from 0 so '1' is the second character.</p> <p>edit: You ca...
16
2009-08-04T15:52:27Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
1,228,332
<pre><code>new = text[:1] + 'Z' + text[2:] </code></pre>
71
2009-08-04T15:53:00Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
1,228,597
<p>Don't modify strings.</p> <p>Work with them as lists; turn them into strings only when needed.</p> <pre><code>&gt;&gt;&gt; s = list("Hello zorld") &gt;&gt;&gt; s ['H', 'e', 'l', 'l', 'o', ' ', 'z', 'o', 'r', 'l', 'd'] &gt;&gt;&gt; s[6] = 'W' &gt;&gt;&gt; s ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] &g...
246
2009-08-04T16:41:07Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
1,229,342
<p>Like other people have said, generally Python strings are supposed to be immutable.</p> <p>However, if you are using CPython, the implementation at python.org, it is possible to use ctypes to modify the string structure in memory.</p> <p>Here is an example where I use the technique to clear a string.</p> <p><a hr...
3
2009-08-04T19:08:14Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
8,534,982
<p>This code is not mine. I couldn't recall the site form where, I took it. Interestingly, you can use this to replace one character or more with one or more charectors. Though this reply is very late, novices like me (anytime) might find it useful.</p> <h1>Change Text function.</h1> <pre><code>mytext = 'Hello Zorld'...
-4
2011-12-16T13:39:14Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
21,459,305
<p>Starting with python 2.6 and python 3 you can use bytearrays which are mutable (can be changed element-wise unlike strings):</p> <pre><code>s = "abcdefg" b_s = bytearray(s) b_s[1] = "Z" s = str(b_s) print s aZcdefg </code></pre> <p>edit: Changed str to s</p> <p>edit2: As Two-Bit Alchemist mentioned in the comment...
7
2014-01-30T14:34:49Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
22,149,018
<h2>Fastest method?</h2> <p>There are two ways. For the speed seekers I recommend 'Method 2' </p> <p><strong>Method 1</strong></p> <p>Given by this <a href="http://stackoverflow.com/a/1228597/2571620">answer</a></p> <pre><code>text = 'abcdefg' new = list(text) new[6] = 'W' ''.join(new) </code></pre> <p>Which is pr...
52
2014-03-03T14:15:24Z
[ "python" ]
Change one character in a string in Python?
1,228,299
<p>What is the easiest way in Python to replace a character in a string like: </p> <pre><code>text = "abcdefg"; text[1] = "Z"; ^ </code></pre>
172
2009-08-04T15:48:39Z
33,836,145
<p>Actually, with strings, you can do something like this:</p> <pre><code>oldStr = 'Hello World!' newStr = '' for i in oldStr: if 'a' &lt; i &lt; 'z': newStr += chr(ord(i)-32) else: newStr += i print(newStr) 'HELLO WORLD!' </code></pre> <p>Basically, I'm "adding"+"string...
0
2015-11-20T21:12:00Z
[ "python" ]
How to scale matplotlib subplot heights individually
1,228,315
<p>Using matplotlib/pylab....</p> <p>How do I plot 5 heatmaps as subplots which have the same number of columns but different row counts? In other words, I need each subplot's height to be scaled differently.</p> <p>Perhaps an image better illustrates the problem...</p> <p><img src="http://img98.imageshack.us/img98/...
5
2009-08-04T15:50:47Z
1,228,346
<p>I haven't tried this for any of my own work, but perhaps the <a href="http://matplotlib.sourceforge.net/mpl%5Ftoolkits/axes%5Fgrid/users/overview.html#axesgrid" rel="nofollow">matplotlib AxesGrid toolkit</a> might be what you are looking for.</p>
4
2009-08-04T15:54:50Z
[ "python", "matplotlib" ]
How to scale matplotlib subplot heights individually
1,228,315
<p>Using matplotlib/pylab....</p> <p>How do I plot 5 heatmaps as subplots which have the same number of columns but different row counts? In other words, I need each subplot's height to be scaled differently.</p> <p>Perhaps an image better illustrates the problem...</p> <p><img src="http://img98.imageshack.us/img98/...
5
2009-08-04T15:50:47Z
1,241,093
<p>Don't use <a href="http://matplotlib.sourceforge.net/api/pyplot%5Fapi.html#matplotlib.pyplot.subplot" rel="nofollow">subplot</a> but <a href="http://matplotlib.sourceforge.net/api/pyplot%5Fapi.html#matplotlib.pyplot.axes" rel="nofollow">axes</a> to create your subplots - the latter allows arbitrary positioning of th...
2
2009-08-06T20:06:59Z
[ "python", "matplotlib" ]
Problem with hash function: hash(1) == hash(1.0)
1,228,475
<p>I have an instance of <code>dict</code> with <code>int</code>s, <code>float</code>s, <code>string</code>s as keys, but the problem is when there are <code>a</code> as <code>int</code> and <code>b</code> as <code>float</code>, and <code>float(a) == b</code>, then their hash values are the same, and thats what I do NO...
1
2009-08-04T16:17:40Z
1,228,508
<p>This doesn't solve your problem, but from <a href="http://docs.python.org/library/numbers.html#notes-for-type-implementors" rel="nofollow">Python 2.6's number documentation</a>:</p> <blockquote> <p>Implementors should be careful to make equal numbers equal and hash them to the same values. </p> </blockquote> <p>...
1
2009-08-04T16:22:45Z
[ "python", "hash", "dictionary" ]
Problem with hash function: hash(1) == hash(1.0)
1,228,475
<p>I have an instance of <code>dict</code> with <code>int</code>s, <code>float</code>s, <code>string</code>s as keys, but the problem is when there are <code>a</code> as <code>int</code> and <code>b</code> as <code>float</code>, and <code>float(a) == b</code>, then their hash values are the same, and thats what I do NO...
1
2009-08-04T16:17:40Z
1,228,517
<p>Using a float as a dictionary key is 'unwise' it's impossible to guarantee that two floats will evaluate to the same value.</p> <p>The best thing is to multiply the keys to a predetermined number of decimal places and use that integer as the key.</p> <p>edit: Sorry it seems you don't want a dict with real number k...
6
2009-08-04T16:23:36Z
[ "python", "hash", "dictionary" ]
Problem with hash function: hash(1) == hash(1.0)
1,228,475
<p>I have an instance of <code>dict</code> with <code>int</code>s, <code>float</code>s, <code>string</code>s as keys, but the problem is when there are <code>a</code> as <code>int</code> and <code>b</code> as <code>float</code>, and <code>float(a) == b</code>, then their hash values are the same, and thats what I do NO...
1
2009-08-04T16:17:40Z
1,228,643
<p>Since <code>1 == 1.0</code>, it would horribly break the semantics of hashing (and therefore dicts and sets) if it were the case that <code>hash(1) != hash(1.0)</code>. More generally, it must ALWAYS be the case that <code>x == y</code> implies <code>hash(x) == hash(y)</code>, for ALL <code>x</code> and <code>y</cod...
7
2009-08-04T16:47:55Z
[ "python", "hash", "dictionary" ]
Problem with hash function: hash(1) == hash(1.0)
1,228,475
<p>I have an instance of <code>dict</code> with <code>int</code>s, <code>float</code>s, <code>string</code>s as keys, but the problem is when there are <code>a</code> as <code>int</code> and <code>b</code> as <code>float</code>, and <code>float(a) == b</code>, then their hash values are the same, and thats what I do NO...
1
2009-08-04T16:17:40Z
1,228,957
<p>If you really just need to know the difference, perhaps do something hackish like:</p> <pre><code>x = '1' y = 1 hash(type(x) + x) != hash(type(y) + y) </code></pre>
2
2009-08-04T17:47:43Z
[ "python", "hash", "dictionary" ]
Retrieving the return value of a Python script
1,228,550
<p>I have an external C# program which executes a Python script using the <code>Process</code> class.</p> <p>My script returns a numerical code and I want to retrieve it from my C# program. Is this possible?</p> <p>The problem is, I'm getting the return code of <code>python.exe</code> instead of the code returned fro...
2
2009-08-04T16:31:22Z
1,228,569
<p>The interpreter does not return the value at the top of Python's stack, unless you do this:</p> <pre><code>if __name__ == "__main__": sys.exit(main()) </code></pre> <p>or if you make a call to <code>sys.exit</code> elsewhere.</p> <p><a href="http://www.artima.com/weblogs/viewpost.jsp?thread=4829">Here's a lot...
7
2009-08-04T16:35:14Z
[ "c#", "python" ]
Globally-scoped variable: can its value change before it is picked up by the thread?
1,228,655
<p>In the following code, you see that <code>pickledList</code> is being used by the thread and is set in the global scope. </p> <p>If the variable that the thread was using was set dynamically somewhere down below in that final while loop, is it possible that its value could change before the thread got to use it? Ho...
3
2009-08-04T16:49:40Z
1,228,963
<p>Option 1: you can pass arguments into each Thread when it is instantiated:</p> <pre><code>ClientThread(arg1, arg2, kwarg1="three times!").start() </code></pre> <p>in which case your <code>run</code> method will be called:</p> <pre><code>run(arg1, arg2, kwarg1="three times!") </code></pre> <p>by the Thread instan...
3
2009-08-04T17:49:05Z
[ "python", "multithreading", "scope" ]
Problem with Twisted python - sending binary data
1,228,722
<p>What I'm trying to do is fairly simple: send a file from client to server. First, the client sends information about the file - the size of it that is. Then it sends the actual file.</p> <p>This is what I've done so far:</p> <p>Server.py</p> <pre><code>from twisted.internet import reactor, protocol from twisted.p...
5
2009-08-04T17:02:17Z
1,228,952
<p>You've set your server to raw mode with setRawMode(), so the callback rawDataReceived is being called with the incoming data (not lineReceived). If you print the data you receive in rawDataReceived, you see everything including the file content, but as you call pickle to deserialize the data, it's being ignored.</p...
8
2009-08-04T17:46:56Z
[ "python", "file", "twisted", "send" ]
Python weakref callbacks and __del__ execution order
1,228,791
<p>In Python, is there a way to call a function after an object is finalized?</p> <p>I thought the callback in a weakref would do it, but it appears a weakref's callback is called once the object is garbage collected, but before the objects <code>__del__</code> method is called. This seems contrary to <a href="http:/...
2
2009-08-04T17:18:12Z
1,228,916
<p>If "do what you want" means "run code when a resource leaves the context" ( instead of, for example, "abuse the garbage collector to do stuff" ), you're looking in the wrong direction. Python abtracted that whole idea as <a href="http://www.python.org/dev/peps/pep-0343/" rel="nofollow">context-managers</a>, used wit...
2
2009-08-04T17:40:40Z
[ "python", "weak-references" ]
Python weakref callbacks and __del__ execution order
1,228,791
<p>In Python, is there a way to call a function after an object is finalized?</p> <p>I thought the callback in a weakref would do it, but it appears a weakref's callback is called once the object is garbage collected, but before the objects <code>__del__</code> method is called. This seems contrary to <a href="http:/...
2
2009-08-04T17:18:12Z
1,229,565
<p>Here is a solution that uses a serialized garbage loop on another thread. Its probably the closest solution you'll get.</p> <pre><code>import sys from threading import Thread from Queue import Queue import weakref alive = weakref.WeakValueDictionary() dump = Queue() def garbageloop(): while True: f = ...
0
2009-08-04T19:52:40Z
[ "python", "weak-references" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
1,229,084
<p>Unpickle from file when program loads, modify as a normal dictionary in memory while program is running, pickle to file when program exits? Not sure exactly what more you're asking for here.</p>
4
2009-08-04T18:13:07Z
[ "python", "dictionary", "persistence", "object-persistence" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
1,229,099
<p>If your keys (not necessarily the values) are strings, the <a href="http://docs.python.org/library/shelve.html">shelve</a> standard library module does what you want pretty seamlessly.</p>
15
2009-08-04T18:17:27Z
[ "python", "dictionary", "persistence", "object-persistence" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
1,229,114
<p>Assuming the keys and values have working implementations of <code>repr</code>, one solution is that you save the string representation of the dictionary (<code>repr(dict)</code>) to file. YOu can load it using the <code>eval</code> function (<code>eval(inputstring)</code>). There are two main disadvantages of thi...
1
2009-08-04T18:24:22Z
[ "python", "dictionary", "persistence", "object-persistence" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
1,229,320
<p>pickling has one disadvantage. it can be expensive if your dictionary has to be read and written frequently from disk and it's large. pickle dumps the stuff down (whole). unpickle gets the stuff up (as a whole). </p> <p>if you have to handle small dicts, pickle is ok. If you are going to work with something more co...
0
2009-08-04T19:03:14Z
[ "python", "dictionary", "persistence", "object-persistence" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
1,229,346
<p>My favorite method (which does not use standard python dictionary functions): Read/write YAML files using <a href="http://pyyaml.org/" rel="nofollow">PyYaml</a>. <a href="http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python/636138#636138">See this answer for det...
1
2009-08-04T19:09:22Z
[ "python", "dictionary", "persistence", "object-persistence" ]
With Python, can I keep a persistent dictionary and modify it?
1,229,068
<p>So, I want to store a dictionary in a persistent file. Is there a way to use regular dictionary methods to add, print, or delete entries from the dictionary in that file?</p> <p>It seems that I would be able to use cPickle to store the dictionary and load it, but I'm not sure where to take it from there.</p>
5
2009-08-04T18:10:52Z
12,058,834
<p>If using only strings as keys (as allowed by the <a href="http://docs.python.org/library/shelve.html" rel="nofollow"><code>shelve</code></a> module) is not enough, the <a href="http://stackoverflow.com/questions/1229068/with-python-can-i-keep-a-persistent-dictionary-and-modify-it">FileDict</a> might be a good way to...
0
2012-08-21T16:02:21Z
[ "python", "dictionary", "persistence", "object-persistence" ]