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
How to escape characters in Pango markup?
1,760,070
<p>My program has a <code>gtk.TreeView</code> which displays a <code>gtk.ListStore</code>. The <code>gtk.ListStore</code> contains strings like this: </p> <pre><code>"&lt;span size='medium'&gt;&lt;b&gt;"+site_title+"&lt;/b&gt;&lt;/span&gt;"+"\n"+URL </code></pre> <p>Where <code>URL</code> is (obviously) a URL string....
6
2009-11-19T00:17:10Z
1,760,572
<p>You need to escape the values. I'm not sure what exact format Pango requires, but it looks like HTML and the <a href="http://docs.python.org/library/cgi.html#cgi.escape" rel="nofollow">cgi.escape</a> function may be all you need.</p> <pre><code>import cgi print "&lt;span size='medium'&gt;&lt;b&gt;%s&lt;/b&gt;&lt;/...
2
2009-11-19T02:43:44Z
[ "python", "gtk", "pygtk", "pango" ]
How to escape characters in Pango markup?
1,760,070
<p>My program has a <code>gtk.TreeView</code> which displays a <code>gtk.ListStore</code>. The <code>gtk.ListStore</code> contains strings like this: </p> <pre><code>"&lt;span size='medium'&gt;&lt;b&gt;"+site_title+"&lt;/b&gt;&lt;/span&gt;"+"\n"+URL </code></pre> <p>Where <code>URL</code> is (obviously) a URL string....
6
2009-11-19T00:17:10Z
1,798,944
<p><a href="http://www.pygtk.org/docs/pygobject/glib-functions.html#function-glib--markup-escape-text"><code>glib.markup_escape_text</code></a> may be a more canonical approach when using GTK.</p>
13
2009-11-25T18:18:54Z
[ "python", "gtk", "pygtk", "pango" ]
mechanize can't login python
1,760,245
<p>I'm making auto-login script by use mechanize python.</p> <p>Before I was used mechanize with no problem, but <code>www.gmarket.co.kr</code> in this site I couldn't make it .</p> <p>whenever i try to login always login page was returned even with correct gmarket id , pass, i can't login and I saw some suspicious m...
2
2009-11-19T00:58:23Z
1,761,373
<p><code>mechanize</code> doesn't have the ability to interact with JavaScript. Probably <a href="http://github.com/davisp/python-spidermonkey" rel="nofollow"><code>spidermonkey</code></a> module will help you (I have no experience with it, but description is quite promising). Also you could handle such reload (e.g.<co...
1
2009-11-19T06:52:58Z
[ "python", "mechanize" ]
How can I get an album URL from the Picasa API?
1,760,329
<p>I really don't like the Picasa feed-oriented API. Why couldn't they create a REST interface?</p> <p>Does anyone know how I can get the URL for a particular album? For example, say I have:</p> <pre><code>gd_client = gdata.photos.service.PhotosService() albums = gd_client.GetUserFeed(user="user@gmail.com") album = a...
1
2009-11-19T01:20:31Z
1,760,378
<p>you can access an album's picasaweb url through calling <code>.GetHtmlLink().href</code> on an entry</p> <pre><code>album_urls = [album.GetHtmlLink().href for album in albums.entry] </code></pre>
6
2009-11-19T01:38:01Z
[ "python", "picasa" ]
How can I get an album URL from the Picasa API?
1,760,329
<p>I really don't like the Picasa feed-oriented API. Why couldn't they create a REST interface?</p> <p>Does anyone know how I can get the URL for a particular album? For example, say I have:</p> <pre><code>gd_client = gdata.photos.service.PhotosService() albums = gd_client.GetUserFeed(user="user@gmail.com") album = a...
1
2009-11-19T01:20:31Z
4,493,097
<p>Get the album URL by going to <code>http://picasaweb.google.com/[username]/[album title]</code>.</p>
-2
2010-12-20T19:27:54Z
[ "python", "picasa" ]
How can I get an album URL from the Picasa API?
1,760,329
<p>I really don't like the Picasa feed-oriented API. Why couldn't they create a REST interface?</p> <p>Does anyone know how I can get the URL for a particular album? For example, say I have:</p> <pre><code>gd_client = gdata.photos.service.PhotosService() albums = gd_client.GetUserFeed(user="user@gmail.com") album = a...
1
2009-11-19T01:20:31Z
9,244,184
<p>Here an example using PHP</p> <pre><code>function grey_picasa_stream($nr=9 ) { $feed = "https://picasaweb.google.com/data/feed/base/user/103900444418818987225/albumid/5702133232086465745"; $return = ""; $xml = simplexml_load_file($feed) or die ("Unable to load XML file"); $i = 0; ...
0
2012-02-11T21:21:59Z
[ "python", "picasa" ]
How can I render a ManyToManyField as checkboxes?
1,760,421
<p>I'm making a game link site, where users can post links to their favorite web game. When people post games they are supposed to check what category the game falls into. I decided to allow many categories for each game since some games can fall into many categories. So the question is, how do I handle this in m...
23
2009-11-19T01:52:25Z
1,760,476
<pre><code>class GameForm(forms.ModelForm): name = forms.CharField(max_length=15, label='Name') url = forms.URLField(label='URL', initial='http://') cats = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(),required=True) class Meta: model = Game ...
30
2009-11-19T02:11:59Z
[ "python", "django" ]
How can I render a ManyToManyField as checkboxes?
1,760,421
<p>I'm making a game link site, where users can post links to their favorite web game. When people post games they are supposed to check what category the game falls into. I decided to allow many categories for each game since some games can fall into many categories. So the question is, how do I handle this in m...
23
2009-11-19T01:52:25Z
1,760,592
<p>Here is how I solved it (Edit: and the admin thing)</p> <h2>Forms:</h2> <pre><code>cats = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=Category.objects.all()) </code></pre> <p>(It was the queryset part I couldn't find..)</p> <h2>View:</h2> <pre><code>cats = form.cleaned_data['cat...
30
2009-11-19T02:52:07Z
[ "python", "django" ]
How can I render a ManyToManyField as checkboxes?
1,760,421
<p>I'm making a game link site, where users can post links to their favorite web game. When people post games they are supposed to check what category the game falls into. I decided to allow many categories for each game since some games can fall into many categories. So the question is, how do I handle this in m...
23
2009-11-19T01:52:25Z
6,072,515
<p>+1 for the admin part in the answer @Grétar Jónsson. Just a minor correction:</p> <p>In the "Models:" section you also need to import models:</p> <pre><code>from django.db import models </code></pre> <p>(A bit silly that you need reputation to comment directly on other peoples posts)</p>
4
2011-05-20T13:21:37Z
[ "python", "django" ]
Python func_dict used to memoize; other useful tricks?
1,760,495
<p>A Python function object has an attribute dictionary called <code>func_dict</code> which is visible from outside the function and is mutable, but which is not modified when the function is called. (I learned this from answers to a question I asked yesterday (#1753232): thanks!) I was reading code (at <a href="htt...
6
2009-11-19T02:18:27Z
1,760,640
<p>Just be careful: the <code>fib.cache</code> trick only works if <code>fib</code> is indeed the name of the relevant function object in the scope that's active while it's executing (for example, when you decorate it as you have done, you must assign the starting value for the cache to the decorator wrapper, not to th...
7
2009-11-19T03:12:41Z
[ "python", "dictionary", "function", "fibonacci", "memoization" ]
Python func_dict used to memoize; other useful tricks?
1,760,495
<p>A Python function object has an attribute dictionary called <code>func_dict</code> which is visible from outside the function and is mutable, but which is not modified when the function is called. (I learned this from answers to a question I asked yesterday (#1753232): thanks!) I was reading code (at <a href="htt...
6
2009-11-19T02:18:27Z
1,981,770
<p>I've always used <a href="http://code.activestate.com/recipes/52201/" rel="nofollow">this technique</a> for memoizing. It uses the fact that you can <em>call an object</em> that's not a function, so long as that object has its <code>__call__</code> method defined. Neither behindthefall's method, nor Alex Martelli's ...
1
2009-12-30T18:48:43Z
[ "python", "dictionary", "function", "fibonacci", "memoization" ]
Django ajax formatting convention
1,760,538
<p>What's the correct way to do an ajax request, I've seen people using a returning render_to_string so that they can do all their formatting within python using the template language. eg~</p> <pre><code>return render_to_string('calendar.html', { 'self' : self, 'month' : self.today.month,}) </code></pre> <p>with this...
2
2009-11-19T02:33:33Z
1,763,105
<p>I've been using JSON exclusively for AJAX, with simplejson returning whatever data is really easy, it looks like this:</p> <pre><code>from django.utils import simplejson reply = simplejson.dumps({'comment_body': formatted_post, 'user_icon': request.user.profile.image.url }) return HttpResponse(reply, mimetype="appl...
2
2009-11-19T12:55:58Z
[ "python", "django", "json", "dom", "formatting" ]
Django ajax formatting convention
1,760,538
<p>What's the correct way to do an ajax request, I've seen people using a returning render_to_string so that they can do all their formatting within python using the template language. eg~</p> <pre><code>return render_to_string('calendar.html', { 'self' : self, 'month' : self.today.month,}) </code></pre> <p>with this...
2
2009-11-19T02:33:33Z
1,767,462
<p>I do both. Sometimes I have short template snippets that I <code>{% include %}</code> in the big page template. It is often more DRY to render these and return the html to insert into the DOM (since the code is already set-up) than to have to write JS to do it (again). Other times, I just generate some JSON and inj...
1
2009-11-19T23:54:52Z
[ "python", "django", "json", "dom", "formatting" ]
Django ValueError at /admin/
1,760,797
<p>I am running Django with mod_python on a Red Hat Linux box in production. A little while ago, for a reason unknown to me, the admin stopped working, throwing a 500 error. The error is as follows:</p> <pre><code>ValueError at /admin/ Empty module name Request Method: GET Exception Type: ValueError Exception Value: ...
2
2009-11-19T03:57:12Z
2,807,143
<p>I was just debugging this problem. The error arises when Django is attempting to set up the template context processors, and the root cause was a definition which should have been a tuple but was actually a string.</p> <p>This is what I had in my config file:</p> <pre><code>TEMPLATE_CONTEXT_PROCESSORS = ( 'django...
5
2010-05-10T23:32:00Z
[ "python", "django", "django-admin" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,760,897
<p>A few hints:</p> <ul> <li>The first parameter for the range() function is incorrect. It must be 1 not 2. The start index included, the ending one is excluded. This is btw a very good practice to avoid bugs.</li> <li>With the modulo operator you found a way of detecting every 5 values; you could use this to print...
6
2009-11-19T04:34:41Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,760,902
<p>I've never used Python, but this should be close if not right.</p> <pre><code>for i in range(1,51): print i; print ","; if (i%5==0) { print "\n";} </code></pre>
0
2009-11-19T04:36:21Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,760,909
<p>Also, remember that using <code>print</code> will cause a new line to start. Unless as noted above a comma is used afterwards.</p> <p>You can collect values using a string for printing out later.</p>
0
2009-11-19T04:37:42Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,760,933
<p>Some Hints</p> <ul> <li><p>To print 1 to 50, you need to pass n+1 for eg, 1 to 51 in <strong>range</strong> function, its <code>(i=1;i&lt;51,i++)</code> in C alike syntax</p></li> <li><p>print comma if you want between every digits</p></li> <li><p>to print line break for every 5, you can just use current <code>if i...
0
2009-11-19T04:46:28Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,761,004
<p>You can start almost like you did (except you need to start from 1 - that 2 is <strong>really</strong> weird!-):</p> <pre><code>for i in range(1,51): if i % 5 == 0: print i </code></pre> <p>but then you need to segue into an <code>else</code> clause for that <code>if</code>, because you <strong>do</strong...
2
2009-11-19T05:05:44Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,761,066
<pre><code>for i in range(1,51): if i%5 == 0: print i else: print i, ",", </code></pre>
0
2009-11-19T05:25:01Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,761,069
<pre><code>(there used to be python code here) </code></pre> <p>EDIT: My apologies, didn't realize this was homework. I reeeally need to check tags before answering. Here's an explanation of what needs to happen.</p> <p>Obviously you know that you're trying to output consecutive numbers from 1 to 50, so you'll need a...
0
2009-11-19T05:25:45Z
[ "python" ]
Printing 5 numbers in a row
1,760,879
<p>I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:</p> <p>1,2,3,4,5<br> 6,7,8,9,10 </p> <p>like that till 50 without using lists</p> <pre><code>for i in range(2,51): if i%5==0: print i </code></pre> <p>this is giving me 5,10,15,20</p> <p>Please help me </p>
0
2009-11-19T04:30:31Z
1,761,161
<pre><code>for i in range(1,51): if i%5 != 0: print str(i)+',' , # trailing comma means no \n else: print i </code></pre>
0
2009-11-19T05:50:46Z
[ "python" ]
extract the number and name of python method arguments
1,760,945
<p>How can I return the arguments of a function in a different module</p> <pre><code>#Module: functionss.py def simple(a, b, c): print "does something" #Module: extract.py #load the called module and function def get_args(module_name, function_name): modFile, modPath, modDesc = imp.find_module(module_name) ...
2
2009-11-19T04:48:36Z
1,760,952
<p>Use <a href="http://docs.python.org/library/inspect.html?highlight=inspect#inspect.getargspec" rel="nofollow">inspect.getargspec</a> for the "heavy lifting" (introspecting a function).</p> <p>Use <code>__import__</code> to import a module (given its <strong>module name</strong> -- <code>"functions.py"</code> is a t...
1
2009-11-19T04:50:19Z
[ "python", "function", "arguments" ]
Shorten Python imports?
1,760,963
<p>I'm working on a Django project. Let's call it <code>myproject</code>. Now my code is littered with <code>myproject.folder.file.function</code>. Is there anyway I can remove the need to prefix all my imports and such with <code>myproject.</code>? What if I want to rename my project later? It kind of annoys me that I...
0
2009-11-19T04:53:34Z
1,760,987
<p><code>from myproject.folder import file</code> (horrible name, btw, trampling over the builtin type <code>file</code>, but that's another rant;-), then use <code>file.function</code> -- if <code>file</code> (gotta <strong>hate</strong> that module name;-) is still too long for you, add e.g. <code>as fi</code> to the...
5
2009-11-19T05:00:55Z
[ "python", "django" ]
Shorten Python imports?
1,760,963
<p>I'm working on a Django project. Let's call it <code>myproject</code>. Now my code is littered with <code>myproject.folder.file.function</code>. Is there anyway I can remove the need to prefix all my imports and such with <code>myproject.</code>? What if I want to rename my project later? It kind of annoys me that I...
0
2009-11-19T04:53:34Z
1,760,995
<pre><code>import x as y import x.y as z from x import y as z </code></pre> <p>The "as" allows you to give your own name to an imported module. For example, <code>import os as System</code> would allow you to call the components of the <code>os</code> module like so:</p> <p><code>System.path.abspath('bla')</code></p>...
3
2009-11-19T05:03:10Z
[ "python", "django" ]
Shorten Python imports?
1,760,963
<p>I'm working on a Django project. Let's call it <code>myproject</code>. Now my code is littered with <code>myproject.folder.file.function</code>. Is there anyway I can remove the need to prefix all my imports and such with <code>myproject.</code>? What if I want to rename my project later? It kind of annoys me that I...
0
2009-11-19T04:53:34Z
1,761,106
<p>If you don't mind clobbering file:</p> <pre><code>from myproject.folder import file </code></pre> <p>If you want to make file something shorter:</p> <pre><code>import myproject.folder.file as f </code></pre> <p>You could create your own import statement if you were feeling really fancy:</p> <pre><code>head = 'm...
1
2009-11-19T05:34:36Z
[ "python", "django" ]
Pyinstaller ld-linux-x86-64.so.2 linking problem
1,761,140
<p>I'm trying to deploy my Python based application on another Linux host. Pyinstaller works flawlessly as long as I run the generated executable on my own system. </p> <p>On the target box I get this error message:</p> <blockquote> <p>/lib/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory</p> ...
1
2009-11-19T05:43:56Z
1,762,758
<p>This is not really a Python question, but a UNIX/Linux compile and link question.</p> <p>First of all, are you using the latest Pyinstaller. If not, then try that. If you still have the problem, then please <a href="http://www.pyinstaller.org/report/1" rel="nofollow">report the bug to the Pyinstaller developers her...
1
2009-11-19T11:47:45Z
[ "python", "linux", "deployment", "dll", "pyinstaller" ]
Select statement with SqlAlchemy
1,761,194
<p>Yes, very basic question. I've successfully created my db using declarative_base, and can perform inserts into the db too. I just have a few questions about SqlAlchemy sql statements.</p> <p>I've create a table called Location.<br> A few issues/questions (see code below): </p> <ol> <li><p>For statement, "print ro...
0
2009-11-19T06:03:22Z
1,761,321
<ol> <li>You code in sample is incomplete and has errors. So it's impossible to say for sure what is <code>Location</code> here. I assume it's a mapped class, so you are requesting a list of all <code>Location</code> <em>objects</em>, not <em>rows</em>. When you print an object you get its string representation. String...
2
2009-11-19T06:42:15Z
[ "python", "orm", "sqlalchemy" ]
Select statement with SqlAlchemy
1,761,194
<p>Yes, very basic question. I've successfully created my db using declarative_base, and can perform inserts into the db too. I just have a few questions about SqlAlchemy sql statements.</p> <p>I've create a table called Location.<br> A few issues/questions (see code below): </p> <ol> <li><p>For statement, "print ro...
0
2009-11-19T06:03:22Z
1,762,373
<p>The preferred way to interact with a db when using an ORM is not to use queries but to use objects that correspond to the tables you are manipulating, typically in conjunction with the session object. SELECT queries become get() or find() calls in some ORMs, query() calls in others. INSERT becomes creating a new obj...
1
2009-11-19T10:38:17Z
[ "python", "orm", "sqlalchemy" ]
Select statement with SqlAlchemy
1,761,194
<p>Yes, very basic question. I've successfully created my db using declarative_base, and can perform inserts into the db too. I just have a few questions about SqlAlchemy sql statements.</p> <p>I've create a table called Location.<br> A few issues/questions (see code below): </p> <ol> <li><p>For statement, "print ro...
0
2009-11-19T06:03:22Z
1,776,324
<p>Denis and Kylotan gave you good answers. I'm just gonna focus on point 2.</p> <p>Sometimes depends on your taste. There are times when you need database specific features that an ORM can't do, that's a case when you should use <code>Session(&lt;sql here&gt;).execute()</code> or <code>conn.execute(&lt;sql here&gt;)...
1
2009-11-21T18:36:17Z
[ "python", "orm", "sqlalchemy" ]
Displaying OpenCV iplimage data structures with wxPython
1,761,250
<p>Here is my current code (language is Python):</p> <pre><code>newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap() wx.StaticBitmap(self, -1, wxImage, (0,0), (wxImage.GetWidth(), wxImage.GetHeight())) </co...
5
2009-11-19T06:20:02Z
1,761,265
<p>You could do with StringIO</p> <pre><code>stream = cStringIO.StringIO(data) wxImage = wx.ImageFromStream(stream) </code></pre> <p>you can check more detail in \wx\lib\embeddedimage.py</p> <p>just my 2 cents.</p>
0
2009-11-19T06:25:17Z
[ "python", "wxpython", "opencv", "format-conversion" ]
Displaying OpenCV iplimage data structures with wxPython
1,761,250
<p>Here is my current code (language is Python):</p> <pre><code>newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap() wx.StaticBitmap(self, -1, wxImage, (0,0), (wxImage.GetWidth(), wxImage.GetHeight())) </co...
5
2009-11-19T06:20:02Z
3,344,706
<p>What you have to do is:</p> <pre><code>frame = cv.QueryFrame(self.cam) # Get the frame from the camera cv.CvtColor(frame, frame, cv.CV_BGR2RGB) # Color correction # if you don't do this your image will be greenish wxImage = wx.EmptyImage(frame.width, frame.height) # If your camera doesn't g...
6
2010-07-27T14:35:48Z
[ "python", "wxpython", "opencv", "format-conversion" ]
Displaying OpenCV iplimage data structures with wxPython
1,761,250
<p>Here is my current code (language is Python):</p> <pre><code>newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap() wx.StaticBitmap(self, -1, wxImage, (0,0), (wxImage.GetWidth(), wxImage.GetHeight())) </co...
5
2009-11-19T06:20:02Z
16,866,833
<p>Yes, this question is old but I came here like everybody else searching for the answer. Several versions of wx, numpy, and opencv after the above solutions I figured I'd share a fast solution using cv2 and numpy images.</p> <p><strong>This is how to convert a NumPy array style image as used in OpenCV2 into a bitmap...
3
2013-05-31T22:17:54Z
[ "python", "wxpython", "opencv", "format-conversion" ]
How to uncheck a checkbox to stop infinite drawing in pyqt?
1,761,352
<p>My problem is I want to keep rotating the scene if the checkbox is checked, and stop this rotation immediately once it is unchecked. However, "keep rotating" means an infinite loop...</p> <p>So after entering the loop, the program gets kind of freezed and no longer react to my "uncheck" signal. Is there a way to in...
0
2009-11-19T06:48:53Z
1,761,548
<p>You must not implement this as a loop. This is defined to break the interaction of the program, as it prevents the "main loop" of the Qt application from running.</p> <p>Put your drawing code into an event handler (like redraw event), and use a timer to generate events at regular intervals (e.g. 10/s).</p>
1
2009-11-19T07:40:49Z
[ "python", "pyqt", "pyopengl" ]
Numpy equivalent of MATLAB's cell array
1,761,419
<p>I want to create a MATLAB-like cell array in Numpy. How can I accomplish this?</p>
8
2009-11-19T07:03:36Z
1,761,989
<p>Matlab cell arrays <a href="http://docs.scipy.org/doc/scipy/reference/tutorial/io.html#matlab-cell-arrays">are most similar</a> to Python lists, since they can hold any object - but scipy.io.loadmat imports them as numpy object arrays - which is an array with dtype=object.</p> <p>To be honest though you are just as...
14
2009-11-19T09:27:51Z
[ "python", "matlab", "numpy" ]
How to enable cookie support with pyWebKit?
1,761,473
<p>The documentation for pyWebKitGTK is pretty scarce. I've looked through their python <code>.def</code> files but they don't seem to contain the words cookie, session, (lib)soup or (lib)curl.. so maybe it isn't possible, huh. I've also looked through the WebKitGTK docs (for the C-based library) and aside from a brief...
2
2009-11-19T07:20:08Z
1,762,266
<p><strong>webkit</strong> is a html renderer, not a complete browser. I guess you must deal with persisting the cookies yourself.</p>
-1
2009-11-19T10:18:38Z
[ "python", "cookies", "webkit" ]
How to enable cookie support with pyWebKit?
1,761,473
<p>The documentation for pyWebKitGTK is pretty scarce. I've looked through their python <code>.def</code> files but they don't seem to contain the words cookie, session, (lib)soup or (lib)curl.. so maybe it isn't possible, huh. I've also looked through the WebKitGTK docs (for the C-based library) and aside from a brief...
2
2009-11-19T07:20:08Z
8,722,612
<p>I can confirm that WebKitGTK supports cookies, and that you can use them with pywebkitgtk and ctypes successfully- though I'm not sure if that was the case when this question was posted! </p> <p>To make cookies persist in a useful way, see my answer to <a href="http://stackoverflow.com/a/8722590/218160">this questi...
1
2012-01-04T05:55:48Z
[ "python", "cookies", "webkit" ]
Testing complex datatypes?
1,761,663
<p>What's are some ways of testing complex data types such as video, images, music, etc. I'm using TDD and wonder are there alternatives to "gold file" testing for rendering algorithms. I understand that there's ways to test other parts of the program that don't render and using those results you can infer. However, I'...
2
2009-11-19T08:05:39Z
1,762,591
<p>What's wrong with the "gold file" technique? It's part of your test fixture. Every test has a data fixture that's the equivalent to the "gold file" in a media-intensive application.</p> <p>When doing ordinary TDD of ordinary business applications, one often has a golden database fixture that must be used.</p> <p...
0
2009-11-19T11:12:45Z
[ "python", "image", "testing", "opencv" ]
Testing complex datatypes?
1,761,663
<p>What's are some ways of testing complex data types such as video, images, music, etc. I'm using TDD and wonder are there alternatives to "gold file" testing for rendering algorithms. I understand that there's ways to test other parts of the program that don't render and using those results you can infer. However, I'...
2
2009-11-19T08:05:39Z
1,763,214
<p>The idea how to test rendering is quite simple: to test a function use the <a href="http://en.wikipedia.org/wiki/Inverse%5Ffunction" rel="nofollow">inverse function</a> and check if the input and output match (match is not equality in your case):</p> <pre><code>f(f^-1(x)) = x </code></pre> <p>To test a rendering a...
2
2009-11-19T13:14:22Z
[ "python", "image", "testing", "opencv" ]
Read password from stdin
1,761,744
<p>Scenario: An interactive CLI Python program, that is in need for a password. That means also, there's no GUI solution possible.</p> <p>In bash I could get a password read in without re-prompting it on screen via</p> <pre><code>read -s </code></pre> <p>Is there something similar for Python? I.e.,</p> <pre><code>p...
146
2009-11-19T08:25:58Z
1,761,753
<pre><code>&gt;&gt;&gt; import getpass &gt;&gt;&gt; pw = getpass.getpass() </code></pre>
225
2009-11-19T08:29:02Z
[ "python", "passwords", "prompt", "interactive" ]
Read password from stdin
1,761,744
<p>Scenario: An interactive CLI Python program, that is in need for a password. That means also, there's no GUI solution possible.</p> <p>In bash I could get a password read in without re-prompting it on screen via</p> <pre><code>read -s </code></pre> <p>Is there something similar for Python? I.e.,</p> <pre><code>p...
146
2009-11-19T08:25:58Z
1,761,754
<p>Yes, <a href="http://docs.python.org/3.1/library/getpass.html"><code>getpass</code></a>: "Prompt the user for a password without echoing."</p> <p><strong>Edit:</strong> I had not played with this module myself yet, so this is what I just cooked up (wouldn't be surprised if you find similar code all over the place, ...
39
2009-11-19T08:29:10Z
[ "python", "passwords", "prompt", "interactive" ]
How to convert text into URL syntax on Python?
1,762,123
<p>I want to convert python str to URL syntax.</p> <p>For example</p> <pre><code>&gt;&gt;&gt; u'한글'.encode('utf-8') '\xed\x95\x9c\xea\xb8\x80' to '%ed%95%9c%ea%b8%80' </code></pre> <p>Thanks in advance.</p>
4
2009-11-19T09:52:02Z
1,762,156
<pre><code>&gt;&gt;&gt; import urllib2 &gt;&gt;&gt; urllib2.quote('한글') '%ED%95%9C%EA%B8%80' </code></pre>
10
2009-11-19T09:57:22Z
[ "python", "url" ]
How to integrate Disqus to Facebook Connect-enabled site (python Tornado app)
1,762,268
<p>I succesfully use Facebook auth for my Tornado-based site using FacebookMixin. I also have Facebook Connect auth enabled for my Disqus, which placed in page using javascript widget. When user already logged in using FB to my site, they still have to log to FB again when they want to comment using Disqus.</p> <p>How...
1
2009-11-19T10:18:51Z
1,762,475
<p>how did you integrate disqus into your site? are you using their <a href="http://wiki.disqus.net/API" rel="nofollow">api</a> in your backend?</p> <p>If I understand disqus correctly then you would need to post to their service usign the data of the authenticated users, else (if they use the javascript widget) they ...
0
2009-11-19T10:53:11Z
[ "python", "tornado", "disqus" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
1,762,469
<p>an ontology is a dictionary that defines URIs and agree on a meaning for each of them. A trivial ontology is just a bunch of URIs you pick more or less out of the blue. This method unfortunately does not give you any information about semantic relationships of your ontological entities. To explain these relationship...
1
2009-11-19T10:52:11Z
[ "python", "text", "computer-science", "ontology" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
1,762,481
<p><a href="http://seth-scripting.sourceforge.net/">http://seth-scripting.sourceforge.net/</a></p>
5
2009-11-19T10:53:24Z
[ "python", "text", "computer-science", "ontology" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
1,762,848
<p>You can use rdflib (<a href="http://www.rdflib.net/">http://www.rdflib.net/</a>) for storing triples. You then have to make your own decisions about the kind of ontology you want to build. Look at the OWL format for that.</p> <p>I toyed with python and ontologies during my PhD, so perhaps a quick scan through my t...
7
2009-11-19T12:06:50Z
[ "python", "text", "computer-science", "ontology" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
1,763,101
<p>If it makes sense to represent something as a directed graph, why not just use a graph class? Python-graph offers simple pure-python graph representations. NetworkX has a slightly harder to use C implementation. </p>
1
2009-11-19T12:55:36Z
[ "python", "text", "computer-science", "ontology" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
1,763,590
<p><a href="http://www.rdflib.net/" rel="nofollow">RDFLIB</a> is a mature implementation of a triple store, with plenty of <a href="http://bel-epa.com/rdflibdocs/index.html" rel="nofollow">documentation</a>. </p> <p>However, this library worked for me only for smaller projects. For example there is a nice RDF/XML cata...
2
2009-11-19T14:16:59Z
[ "python", "text", "computer-science", "ontology" ]
How to create an ontology in python?
1,762,291
<p>Are there libraries or certain "techniques" that you can use to create an ontology of elements?</p> <p>Or "design patterns"?</p> <p>I am talking about just a "graph" of things. Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and k...
12
2009-11-19T10:22:05Z
6,004,791
<blockquote> <p>Suppose I have a bunch of words. Certain words are "under" other words or "related" to other words. I need a good way to group them and know their relationship.</p> </blockquote> <p>Take a look at wordnet, which is available in RDF format i.e. according to an RDF Schema ontology. This is exac...
1
2011-05-14T21:08:58Z
[ "python", "text", "computer-science", "ontology" ]
force unpacking of certain egg directories
1,762,306
<p>I have an egg distribution of a PyQt application which i build myself, and it contains sphinx generated documentation. When i call the help file from the application it opens the sphinx index.html in a QtWebKit.QWebView window. Apparently, only the index.html file is extracted from the egg into the OS's egg-director...
5
2009-11-19T10:26:49Z
1,762,898
<p>Probable cause: not all the files are included in the egg in the first place.</p> <ul> <li><p>Check this by unzipping the .egg (you might need to rename it to a .zip file for that on windows). Check if all the contents are there.</p></li> <li><p>Look at how you made the egg. Do you use a MANIFEST.in file to tell s...
1
2009-11-19T12:17:04Z
[ "python", "setuptools", "python-sphinx", "egg" ]
force unpacking of certain egg directories
1,762,306
<p>I have an egg distribution of a PyQt application which i build myself, and it contains sphinx generated documentation. When i call the help file from the application it opens the sphinx index.html in a QtWebKit.QWebView window. Apparently, only the index.html file is extracted from the egg into the OS's egg-director...
5
2009-11-19T10:26:49Z
1,770,581
<pre><code>def get_help_url(self): from pkg_resources import resource_filename from doc import sphinx import os from PyQt4.QtCore import QUrl html_path = resource_filename(sphinx.__name__, os.path.join('build', 'html')) return QUrl(os.path.join(html_path, 'index.html')) </code></pre> <p>instea...
2
2009-11-20T13:58:12Z
[ "python", "setuptools", "python-sphinx", "egg" ]
force unpacking of certain egg directories
1,762,306
<p>I have an egg distribution of a PyQt application which i build myself, and it contains sphinx generated documentation. When i call the help file from the application it opens the sphinx index.html in a QtWebKit.QWebView window. Apparently, only the index.html file is extracted from the egg into the OS's egg-director...
5
2009-11-19T10:26:49Z
2,164,026
<p>I see that you've already found another way to do it, but for future reference, here's the non-workaround way to do it automatically, from the documentation at <a href="http://peak.telecommunity.com/DevCenter/setuptools#automatic-resource-extraction" rel="nofollow">http://peak.telecommunity.com/DevCenter/setuptools#...
4
2010-01-29T18:03:14Z
[ "python", "setuptools", "python-sphinx", "egg" ]
how to get Company contact page url
1,762,435
<p>Hi i have csv file which contains company url list like this www.google.com,www.ibm.com.....</p> <p>Here i want to get contactus or aboutus page url (example <a href="http://www.google.com/contact" rel="nofollow">http://www.google.com/contact</a>) for each url which is present in csv file i have one idea checking ...
1
2009-11-19T10:47:43Z
1,762,479
<p>I'd suggest using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a> to parse the page. Another alternative would be to setup a HIT on Mechanical Turk.</p>
2
2009-11-19T10:53:21Z
[ "python" ]
how to get Company contact page url
1,762,435
<p>Hi i have csv file which contains company url list like this www.google.com,www.ibm.com.....</p> <p>Here i want to get contactus or aboutus page url (example <a href="http://www.google.com/contact" rel="nofollow">http://www.google.com/contact</a>) for each url which is present in csv file i have one idea checking ...
1
2009-11-19T10:47:43Z
17,229,277
<p>scrapy is the best. The best thing about scrapy is that it is a open source. <a href="http://www.scrapy.org" rel="nofollow">scrapy documentation</a></p>
0
2013-06-21T06:50:52Z
[ "python" ]
Adding Lists Elements to 'Mega List'
1,762,446
<p>Let's say I have a list somewhere called <code>majorPowers</code> which contain these two lists:</p> <pre><code>axis=["germany","italy","japan"] allies=["russia","uk","us"] </code></pre> <p>I'd like to insert each of the elements of these lists, into a new <em>mega-list</em>. I'm currently doing this:</p> <pre><c...
0
2009-11-19T10:49:10Z
1,762,476
<pre><code>from itertools import chain ww2 = list(chain.from_iterable(majorPower)) </code></pre>
1
2009-11-19T10:53:11Z
[ "python", "list" ]
Adding Lists Elements to 'Mega List'
1,762,446
<p>Let's say I have a list somewhere called <code>majorPowers</code> which contain these two lists:</p> <pre><code>axis=["germany","italy","japan"] allies=["russia","uk","us"] </code></pre> <p>I'd like to insert each of the elements of these lists, into a new <em>mega-list</em>. I'm currently doing this:</p> <pre><c...
0
2009-11-19T10:49:10Z
1,762,487
<p>It is good that you ask this question, because it is <strong>bad form</strong> to misuse list comprehensions like that. The code you show uses <code>append</code>, not to generate the elements of <code>temp</code>, but because of its side effects. <strong>Avoid side effects in list comprehensions</strong>!</p> <p>S...
5
2009-11-19T10:54:13Z
[ "python", "list" ]
Adding Lists Elements to 'Mega List'
1,762,446
<p>Let's say I have a list somewhere called <code>majorPowers</code> which contain these two lists:</p> <pre><code>axis=["germany","italy","japan"] allies=["russia","uk","us"] </code></pre> <p>I'd like to insert each of the elements of these lists, into a new <em>mega-list</em>. I'm currently doing this:</p> <pre><c...
0
2009-11-19T10:49:10Z
1,762,503
<p>try extend</p> <pre><code>for t in majorPowers: ww2.extend(t) </code></pre> <p>or </p> <pre><code>sum(majorPowers,[]) </code></pre>
0
2009-11-19T10:56:06Z
[ "python", "list" ]
Adding Lists Elements to 'Mega List'
1,762,446
<p>Let's say I have a list somewhere called <code>majorPowers</code> which contain these two lists:</p> <pre><code>axis=["germany","italy","japan"] allies=["russia","uk","us"] </code></pre> <p>I'd like to insert each of the elements of these lists, into a new <em>mega-list</em>. I'm currently doing this:</p> <pre><c...
0
2009-11-19T10:49:10Z
1,762,541
<p>I would use reduce:</p> <pre><code>from operator import add ww2 = reduce(add, majorPowers) </code></pre>
0
2009-11-19T11:03:25Z
[ "python", "list" ]
How to find the position of an element in a list , in Python?
1,762,484
<pre><code>for s in stocks_list: print s </code></pre> <p>how do I know what "position" s is in? So that I can do <strong>stocks_list[4]</strong> in the future?</p>
7
2009-11-19T10:53:59Z
1,762,492
<pre><code>for index, s in enumerate(stocks_list): print index, s </code></pre>
13
2009-11-19T10:55:07Z
[ "python", "list" ]
How to find the position of an element in a list , in Python?
1,762,484
<pre><code>for s in stocks_list: print s </code></pre> <p>how do I know what "position" s is in? So that I can do <strong>stocks_list[4]</strong> in the future?</p>
7
2009-11-19T10:53:59Z
7,532,611
<p>If you know what you're looking for ahead of time you can use the index method:</p> <pre><code>&gt;&gt;&gt; stocks_list = ['AAPL', 'MSFT', 'GOOG'] &gt;&gt;&gt; stocks_list.index('MSFT') 1 &gt;&gt;&gt; stocks_list.index('GOOG') 2 </code></pre>
37
2011-09-23T17:19:14Z
[ "python", "list" ]
How to find the position of an element in a list , in Python?
1,762,484
<pre><code>for s in stocks_list: print s </code></pre> <p>how do I know what "position" s is in? So that I can do <strong>stocks_list[4]</strong> in the future?</p>
7
2009-11-19T10:53:59Z
12,731,819
<pre><code>[x for x in range(len(stocks_list)) if stocks_list[x]=='MSFT'] </code></pre>
2
2012-10-04T16:38:12Z
[ "python", "list" ]
Encoding a string to ascii
1,762,564
<p>I have a long string that I want to encode to ascii. I'm doing:</p> <pre><code>s = s.encode('ascii', 'replace') </code></pre> <p>but I get:</p> <pre><code>'ascii' codec can't decode byte 0xc3 in position 2646: ordinal not in range(128) </code></pre> <p>(I've also tried <code>'ignore'</code> but it doesn't help.)...
7
2009-11-19T11:09:32Z
1,762,576
<p>Your string is <strong>already encoded</strong> with some encoding. Before encoding it to ascii, you <strong>must decode it first</strong>.</p> <p>Python is <strong><em>implicity</em></strong> trying to decode it (That's why you get a <code>UnicodeDecodeError</code> not <code>UnicodeEncodeError</code>).</p> <p>You...
8
2009-11-19T11:10:46Z
[ "python", "encoding", "ascii" ]
Encoding a string to ascii
1,762,564
<p>I have a long string that I want to encode to ascii. I'm doing:</p> <pre><code>s = s.encode('ascii', 'replace') </code></pre> <p>but I get:</p> <pre><code>'ascii' codec can't decode byte 0xc3 in position 2646: ordinal not in range(128) </code></pre> <p>(I've also tried <code>'ignore'</code> but it doesn't help.)...
7
2009-11-19T11:09:32Z
5,605,887
<p><code>encode</code> should be used on <code>unicode</code> objects to convert it to a <code>str</code>. If you have a str object, you should use <code>decode</code> to convert it to a <code>unicode</code>.</p>
1
2011-04-09T15:24:21Z
[ "python", "encoding", "ascii" ]
Python Tesseract OCR question
1,762,565
<p>I have this image:</p> <p><img src="http://i232.photobucket.com/albums/ee169/codygman/ip.gif" alt="alt text"></p> <p>I want to read it to a string using python, which I didn't think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract.</p> <p>So I started reading images...
10
2009-11-19T11:09:34Z
1,762,688
<p>That looks like Eurostile font. Yes, you will have to train with each different font that is being used in your source images.</p>
0
2009-11-19T11:34:06Z
[ "python", "image-processing", "image-manipulation", "ocr", "tesseract" ]
Python Tesseract OCR question
1,762,565
<p>I have this image:</p> <p><img src="http://i232.photobucket.com/albums/ee169/codygman/ip.gif" alt="alt text"></p> <p>I want to read it to a string using python, which I didn't think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract.</p> <p>So I started reading images...
10
2009-11-19T11:09:34Z
1,763,824
<p>Just train the engine for the 10 digits and a '.' . That should do it. And make sure you change your image to grayscale before OCRing it.</p>
11
2009-11-19T14:51:37Z
[ "python", "image-processing", "image-manipulation", "ocr", "tesseract" ]
Python Tesseract OCR question
1,762,565
<p>I have this image:</p> <p><img src="http://i232.photobucket.com/albums/ee169/codygman/ip.gif" alt="alt text"></p> <p>I want to read it to a string using python, which I didn't think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract.</p> <p>So I started reading images...
10
2009-11-19T11:09:34Z
1,764,314
<p>There has been a lot of traffic on this topic in the <a href="http://groups.google.com/group/tesseract-ocr/topics" rel="nofollow">tesseract OCR discussion group</a> lately. You will need to use a "language" of just numbers. Many people have trained the engine that way before. It looks like you're trying to outwit a ...
1
2009-11-19T15:55:05Z
[ "python", "image-processing", "image-manipulation", "ocr", "tesseract" ]
Python Tesseract OCR question
1,762,565
<p>I have this image:</p> <p><img src="http://i232.photobucket.com/albums/ee169/codygman/ip.gif" alt="alt text"></p> <p>I want to read it to a string using python, which I didn't think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract.</p> <p>So I started reading images...
10
2009-11-19T11:09:34Z
2,718,721
<p>Training is hard and is not what is really needed here. The distinction between O and 0 and l and 1 are going to be hard, no matter the script. Limiting the OCR to choose only between numerical digits greatly simplifies the problem, if the context permits it.</p> <p>My interest in tesseract is in processing lots o...
5
2010-04-27T04:23:54Z
[ "python", "image-processing", "image-manipulation", "ocr", "tesseract" ]
Python Tesseract OCR question
1,762,565
<p>I have this image:</p> <p><img src="http://i232.photobucket.com/albums/ee169/codygman/ip.gif" alt="alt text"></p> <p>I want to read it to a string using python, which I didn't think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract.</p> <p>So I started reading images...
10
2009-11-19T11:09:34Z
18,649,080
<p>Recognizing small screen font may be hard for the general-purpose OCR which is optimized for reading large smooth font scanned from paper.</p> <p>You may better try special screenshot OCR like <a href="http://www.structurise.com" rel="nofollow">Textract SDK</a>. It will collect all local fonts and provide 100% prec...
1
2013-09-06T02:48:29Z
[ "python", "image-processing", "image-manipulation", "ocr", "tesseract" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,762,583
<pre><code>for fruit in thelist[1:]: print fruit </code></pre>
1
2009-11-19T11:12:17Z
[ "python", "list" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,762,584
<p>using python's elegant <a href="http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation#answer-509295">slices</a></p> <pre><code>&gt;&gt;&gt; for fruit in thelist[1:]: &gt;&gt;&gt; print fruit </code></pre>
11
2009-11-19T11:12:24Z
[ "python", "list" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,762,590
<pre><code>for fruit in thelist [1:]: </code></pre> <p>will start at the second element in the list.</p>
2
2009-11-19T11:12:37Z
[ "python", "list" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,762,594
<pre><code>for fruit in thelist[1:]: ... </code></pre> <p>this of course suppose you know at which index to start. but you can find the index easily:</p> <pre><code>for fruit in thelist[thelist.index('orange'):]: ... </code></pre>
14
2009-11-19T11:14:15Z
[ "python", "list" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,764,235
<p>Slices make copies of lists, so if there are <em>many</em> items, or if you don't want to separately search the list for the starting index, an iterator will let you search, and then continue from there:</p> <pre><code>&gt;&gt;&gt; thelist = ['apple','orange','banana','grapes'] &gt;&gt;&gt; fruit_iter = iter(thelis...
0
2009-11-19T15:46:24Z
[ "python", "list" ]
How do I do a "for each" , starting at a certain index of a list (Python)?
1,762,571
<p>Suppose I have this list:</p> <pre><code>thelist = ['apple','orange','banana','grapes'] for fruit in thelist: </code></pre> <p>This would go through all the fruits.</p> <p>However, what if I wanted to start at orange? Instead of starting at apple? Sure, I could do "if ...continue", but there must be a better way?...
1
2009-11-19T11:09:59Z
1,764,293
<p>As mentioned by Paul McGuire, slicing a list creates a copy in memory of the result. If you have a list with 500,000 elements then doing <code>l[2:]</code> is going to create a new 499,998 element list.</p> <p>To avoid this, use <code>itertools.islice</code>:</p> <pre><code>&gt;&gt;&gt; thelist = ['a', 'b', 'c'] ...
3
2009-11-19T15:52:42Z
[ "python", "list" ]
Why is it when I print something, there is always a unicode next to it? (Python)
1,762,690
<pre><code>[u'Iphones', u'dont', u'receieve', u'messages'] </code></pre> <p>Is there a way to print it without the "u" in front of it?</p>
4
2009-11-19T11:34:27Z
1,762,705
<p>What you are seeing is the <code>__repr__()</code> representation of the unicode string which includes the u to make it clear. If you don't want the u you could print the object (using <code>__str__</code>) - this works for me:</p> <pre><code>print [str(x) for x in l] </code></pre> <p>Probably better is to read up...
9
2009-11-19T11:38:35Z
[ "python" ]
Why is it when I print something, there is always a unicode next to it? (Python)
1,762,690
<pre><code>[u'Iphones', u'dont', u'receieve', u'messages'] </code></pre> <p>Is there a way to print it without the "u" in front of it?</p>
4
2009-11-19T11:34:27Z
1,762,729
<p>Python contains string classes for both unicode strings and regular strings. The u before a string indicates that it is a unicode string.</p> <pre><code>&gt;&gt;&gt; mystrings = [u'Iphones', u'dont', u'receieve', u'messages'] &gt;&gt;&gt; [str(s) for s in mystrings] ['Iphones', 'dont', 'receieve', 'messages'] &gt;...
4
2009-11-19T11:42:34Z
[ "python" ]
Scanning Keypress in Python
1,762,697
<p>I have paused a script for lets say 3500 seconds by using time module for ex time.sleep(3500).</p> <p>Now, my aim is to scan for keypresses while the script is on sleep, i mean its on this line. </p> <p>Its like I want to restart the script if a <strong>"keypress Ctrl+R"</strong> is pressed.</p> <p>For ex.. consi...
5
2009-11-19T11:36:29Z
1,762,833
<p>I am aware that this does not fully answer your question, but you could do the following:</p> <ol> <li>Put the program logic code in a function, say <code>perform_actions</code>. Call it when the program starts.</li> <li>After the code has been run, start listening for an <a href="http://docs.python.org/3.1/library...
4
2009-11-19T12:04:31Z
[ "python", "linux", "scripting" ]
Scanning Keypress in Python
1,762,697
<p>I have paused a script for lets say 3500 seconds by using time module for ex time.sleep(3500).</p> <p>Now, my aim is to scan for keypresses while the script is on sleep, i mean its on this line. </p> <p>Its like I want to restart the script if a <strong>"keypress Ctrl+R"</strong> is pressed.</p> <p>For ex.. consi...
5
2009-11-19T11:36:29Z
1,763,581
<p>in a for loop sleep 3500 times for 1 second checking if a key was pressed each time</p> <pre><code># sleep for 3500 seconds unless ctrl+r is pressed for i in range(3500): time.sleep(1) # check if ctrl+r is pressed # if pressed -&gt; do something # otherwise go back to sleep </code></pre>
-2
2009-11-19T14:16:17Z
[ "python", "linux", "scripting" ]
Scanning Keypress in Python
1,762,697
<p>I have paused a script for lets say 3500 seconds by using time module for ex time.sleep(3500).</p> <p>Now, my aim is to scan for keypresses while the script is on sleep, i mean its on this line. </p> <p>Its like I want to restart the script if a <strong>"keypress Ctrl+R"</strong> is pressed.</p> <p>For ex.. consi...
5
2009-11-19T11:36:29Z
1,763,681
<p>You may want to use Tkinter {needs X :(}</p> <pre><code>#!/usr/bin/env python from Tkinter import * # needs python-tk root = Tk() def hello(*ignore): print 'Hello World' root.bind('&lt;Control-r&gt;', hello) root.mainloop() # starts an X widget </code></pre> <p>This script prints <code>Hello World</code> t...
3
2009-11-19T14:31:40Z
[ "python", "linux", "scripting" ]
Create broken symlink with Python
1,762,831
<p>Using Python I want to create a symbolic link pointing to a path that does not exist. However os.symlink just complains about "OSError: [Errno 2] No such file or directory:".. This can easily be done with the <em>ln</em> program, but how to do it in Python without calling the <em>ln</em> program from Python?</p> <p...
4
2009-11-19T12:03:53Z
1,762,854
<p>Are you sure you are calling symlink with the right parameters?</p> <pre><code>os.symlink('/usr/bin/python', 'python') </code></pre> <p>This should create a symlink to /usr/bin/python from python in the current working directory.</p>
0
2009-11-19T12:08:17Z
[ "python", "symlink", "ln" ]
Create broken symlink with Python
1,762,831
<p>Using Python I want to create a symbolic link pointing to a path that does not exist. However os.symlink just complains about "OSError: [Errno 2] No such file or directory:".. This can easily be done with the <em>ln</em> program, but how to do it in Python without calling the <em>ln</em> program from Python?</p> <p...
4
2009-11-19T12:03:53Z
1,762,903
<p>The file doesn't need to exist in order to create the symlink. The following example demonstrates creating a symlink to a non-existing file:</p> <p>First, check that there's no file named <code>foobar</code> in <code>/home/wieslander/tmp</code>:</p> <pre><code>[wieslander@rizzo tmp]$ ls -l /home/wieslander/tmp/fo...
3
2009-11-19T12:19:25Z
[ "python", "symlink", "ln" ]
Create broken symlink with Python
1,762,831
<p>Using Python I want to create a symbolic link pointing to a path that does not exist. However os.symlink just complains about "OSError: [Errno 2] No such file or directory:".. This can easily be done with the <em>ln</em> program, but how to do it in Python without calling the <em>ln</em> program from Python?</p> <p...
4
2009-11-19T12:03:53Z
1,762,937
<p>Such error is raised when you try to create a symlink in non-existent directory. For example, the following code will fail if <code>/tmp/subdir</code> doesn't exist:</p> <pre><code>os.symlink('/usr/bin/python', '/tmp/subdir/python') </code></pre> <p>But this should run successfully:</p> <pre><code>src = '/usr/bin...
7
2009-11-19T12:24:41Z
[ "python", "symlink", "ln" ]
Create broken symlink with Python
1,762,831
<p>Using Python I want to create a symbolic link pointing to a path that does not exist. However os.symlink just complains about "OSError: [Errno 2] No such file or directory:".. This can easily be done with the <em>ln</em> program, but how to do it in Python without calling the <em>ln</em> program from Python?</p> <p...
4
2009-11-19T12:03:53Z
1,762,957
<p>This might be your answer:</p> <pre><code>$ python Python 2.5.2 (r252:60911, Dec 2 2008, 09:26:14) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import os &gt;&gt;&gt; os.symlink('/this/does/not/exist', 'broken...
0
2009-11-19T12:29:04Z
[ "python", "symlink", "ln" ]
String formatting expressions (Python)
1,763,184
<p>String formatting expressions:</p> <pre><code>'This is %d %s example!' % (1, 'nice') </code></pre> <p>String formatting method calls:</p> <pre><code>'This is {0} {1} example!'.format(1, 'nice') </code></pre> <p>I personally prefer the method calls (second example) for readability but since it is new, there is so...
5
2009-11-19T13:09:51Z
1,763,217
<p>I thought I read that the <code>%</code> operator is being deprecated in 3.1 already, so I'd stick with the <code>format()</code> function.</p> <p>See <a href="http://www.python.org/doc/3.0/whatsnew/3.0.html#pep-3101-a-new-approach-to-string-formatting" rel="nofollow">PEP 3101: A New Approach To String Formatting</...
6
2009-11-19T13:14:54Z
[ "python", "string", "printf", "deprecated" ]
String formatting expressions (Python)
1,763,184
<p>String formatting expressions:</p> <pre><code>'This is %d %s example!' % (1, 'nice') </code></pre> <p>String formatting method calls:</p> <pre><code>'This is {0} {1} example!'.format(1, 'nice') </code></pre> <p>I personally prefer the method calls (second example) for readability but since it is new, there is so...
5
2009-11-19T13:09:51Z
1,763,223
<p>Neither; the first one is used in a lot of places and the second one was just introduced. So the question is more which style you prefer. I actually prefer the <code>dict</code> based formatting:</p> <pre><code>d = { 'count': 1, 'txt': 'nice' } 'This is %(count)d %(txt)s example!' % d </code></pre> <p>It makes sur...
8
2009-11-19T13:16:43Z
[ "python", "string", "printf", "deprecated" ]
String formatting expressions (Python)
1,763,184
<p>String formatting expressions:</p> <pre><code>'This is %d %s example!' % (1, 'nice') </code></pre> <p>String formatting method calls:</p> <pre><code>'This is {0} {1} example!'.format(1, 'nice') </code></pre> <p>I personally prefer the method calls (second example) for readability but since it is new, there is so...
5
2009-11-19T13:09:51Z
1,764,449
<p>The original idea was to gradually switch to str.format() approach while allowing both ways:</p> <blockquote> <p>PEP 3101: <br/> The new system does not collide with any of the method names of the existing string formatting techniques, so both systems can co-exist until it comes time to deprecate the older syst...
5
2009-11-19T16:09:41Z
[ "python", "string", "printf", "deprecated" ]
len(object) or hasattr(object, __iter__)?
1,763,507
<p>(The following is python3-related (if that matter).)</p> <p>This this the code I've written (simplified) :</p> <blockquote> <pre><code>class MyClass: def __init__(self): self.__some_var = [] @property def some_var(self): return self.__some__var @some_var.setter def some_var(s...
0
2009-11-19T14:07:29Z
1,763,567
<p>There's no "performance cost" of <code>hasattr</code> that matters. It's fast enough that you would have a hard time measuring it.</p> <p>Please do not use <code>__</code> (double underscore) for your own attributes. It's confusing to the rest of us.</p> <p>It's usually best to use the <code>collections</code> A...
4
2009-11-19T14:14:34Z
[ "python" ]
len(object) or hasattr(object, __iter__)?
1,763,507
<p>(The following is python3-related (if that matter).)</p> <p>This this the code I've written (simplified) :</p> <blockquote> <pre><code>class MyClass: def __init__(self): self.__some_var = [] @property def some_var(self): return self.__some__var @some_var.setter def some_var(s...
0
2009-11-19T14:07:29Z
1,763,576
<p>In terms of efficiency:</p> <ul> <li>hasattr() would have a worst-case of O(1) (constant).</li> <li>append() is also a worst-case of O(1) (constant).</li> </ul>
0
2009-11-19T14:15:53Z
[ "python" ]
len(object) or hasattr(object, __iter__)?
1,763,507
<p>(The following is python3-related (if that matter).)</p> <p>This this the code I've written (simplified) :</p> <blockquote> <pre><code>class MyClass: def __init__(self): self.__some_var = [] @property def some_var(self): return self.__some__var @some_var.setter def some_var(s...
0
2009-11-19T14:07:29Z
1,763,660
<p>An alternative might be duck typing:</p> <p>Here I assume you want to evaluate (=make a list) out of the iterable, since it might be an iterator that you need to expand immediately to save.</p> <pre><code>@some_var.setter def some_var(self, new_value): try: self.__some_var = list(new_value) except ...
2
2009-11-19T14:27:52Z
[ "python" ]
How to find unique starts of strings?
1,763,510
<p>If I have a list of strings (eg 'blah 1', 'blah 2' 'xyz fg','xyz penguin'), what would be the best way of finding the unique starts of strings ('xyz' and 'blah' in this case)? The starts of strings can be multiple words.</p>
0
2009-11-19T14:07:32Z
1,763,622
<p>If you mean unique first words of strings (words being separated by space), this would be:</p> <pre><code>arr=['blah 1', 'blah 2' 'xyz fg','xyz penguin'] unique=list(set([x.split(' ')[0] for x in arr])) </code></pre>
2
2009-11-19T14:20:58Z
[ "python" ]
How to find unique starts of strings?
1,763,510
<p>If I have a list of strings (eg 'blah 1', 'blah 2' 'xyz fg','xyz penguin'), what would be the best way of finding the unique starts of strings ('xyz' and 'blah' in this case)? The starts of strings can be multiple words.</p>
0
2009-11-19T14:07:32Z
1,763,691
<p>Your question is confusing, as it is not clear what you really want. So I'll give three answers and hope that one of them at least partially answers your question.</p> <ol> <li><p>To get <em>all</em> unique prefixes of a given list of string, you can do:</p> <pre><code>&gt;&gt;&gt; l = ['blah 1', 'blah 2', 'xyz fg...
4
2009-11-19T14:32:33Z
[ "python" ]
Python: asynchronous tcp socketserver
1,763,549
<p>I'm looking <a href="http://docs.python.org/library/socketserver.html" rel="nofollow">http://docs.python.org/library/socketserver.html</a> to try and handle asynchronous requests with the socketserver in python. At the very bottom there is an example, but it doesn't make sense. It says you use port 0 which assigns a...
4
2009-11-19T14:12:28Z
1,763,570
<p>You need to retrieve the port that was assigned to the <code>socketserver</code> once the <code>bind</code> is done: in this case, this will probably be through <code>ip, port = server.server_address</code>.</p> <p>The arbitrary port is just if you want to create a server without specifying a port: the OS will assi...
2
2009-11-19T14:15:13Z
[ "python", "python-2.7", "networking", "asynchronous" ]
Python: asynchronous tcp socketserver
1,763,549
<p>I'm looking <a href="http://docs.python.org/library/socketserver.html" rel="nofollow">http://docs.python.org/library/socketserver.html</a> to try and handle asynchronous requests with the socketserver in python. At the very bottom there is an example, but it doesn't make sense. It says you use port 0 which assigns a...
4
2009-11-19T14:12:28Z
1,763,604
<p>In the example you linked, they are fetching the port:</p> <pre><code># Port 0 means to select an arbitrary unused port HOST, PORT = "localhost", 0 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) ip, port = server.server_address </code></pre> <p>However, you should really be looking at www.twi...
5
2009-11-19T14:18:12Z
[ "python", "python-2.7", "networking", "asynchronous" ]
Python: asynchronous tcp socketserver
1,763,549
<p>I'm looking <a href="http://docs.python.org/library/socketserver.html" rel="nofollow">http://docs.python.org/library/socketserver.html</a> to try and handle asynchronous requests with the socketserver in python. At the very bottom there is an example, but it doesn't make sense. It says you use port 0 which assigns a...
4
2009-11-19T14:12:28Z
1,763,607
<p>Since the client is implemented in the same script as the server, the port is known. In a real-world scenario, you should specify a port for your daemon. Besides letting your clients know on which port to connect, you may also need to know so that you can open firewalls between your clients and your server.</p>
9
2009-11-19T14:18:19Z
[ "python", "python-2.7", "networking", "asynchronous" ]
Python: asynchronous tcp socketserver
1,763,549
<p>I'm looking <a href="http://docs.python.org/library/socketserver.html" rel="nofollow">http://docs.python.org/library/socketserver.html</a> to try and handle asynchronous requests with the socketserver in python. At the very bottom there is an example, but it doesn't make sense. It says you use port 0 which assigns a...
4
2009-11-19T14:12:28Z
1,763,615
<pre><code>server = ThreadedTCPServer((HOST, 0), ThreadedTCPRequestHandler) ip, port = server.server_address ... client(ip, port, "Hello World 1") </code></pre> <p>The PORT value 0 says "I don't care what port number it is," so the server_address's <code>port</code> value is assigned by the ThreadedTCPServer() call....
0
2009-11-19T14:19:38Z
[ "python", "python-2.7", "networking", "asynchronous" ]
raw escape in python except last char
1,764,291
<p>Why in python I can't use:</p> <pre><code>r"c:\" </code></pre>
0
2009-11-19T15:52:30Z
1,764,317
<p>Use <code>"c:/"</code> or <code>"c:\\"</code>. Raw string literals are for escaping escape-sequences, not for including literal backslashes, though they do work that way, except in this exact case.</p>
1
2009-11-19T15:55:37Z
[ "python" ]
raw escape in python except last char
1,764,291
<p>Why in python I can't use:</p> <pre><code>r"c:\" </code></pre>
0
2009-11-19T15:52:30Z
1,764,334
<p>Its a known case I think, better use "c:\\" for that case.</p>
1
2009-11-19T15:57:18Z
[ "python" ]
raw escape in python except last char
1,764,291
<p>Why in python I can't use:</p> <pre><code>r"c:\" </code></pre>
0
2009-11-19T15:52:30Z
1,764,342
<p>From the <a href="https://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow">documentation</a>:</p> <blockquote> <p>... a raw string cannot end in a single backslash (since the backslash would escape the following quote character).</p> </blockquote> <p>.</p>
1
2009-11-19T15:58:02Z
[ "python" ]
raw escape in python except last char
1,764,291
<p>Why in python I can't use:</p> <pre><code>r"c:\" </code></pre>
0
2009-11-19T15:52:30Z
1,764,345
<p>When a string must contain the same quote character with which it starts, escaping that character is the only available workaround -- so the design alternative was either to make raw-string literals unable to contain their leading quote character, or keep the "backlash escapes" convention, even in string literals, j...
3
2009-11-19T15:58:24Z
[ "python" ]
raw escape in python except last char
1,764,291
<p>Why in python I can't use:</p> <pre><code>r"c:\" </code></pre>
0
2009-11-19T15:52:30Z
1,764,746
<p>Even with raw strings, <code>\"</code> causes the <code>"</code> not to be interpreted as the end of the string (though the backslash gets into your string), so <code>r"foo\"bar"</code> would be a legal string. This is convenient enough when writing regex but not great for writing paths. </p> <p>This is not a big d...
0
2009-11-19T16:46:28Z
[ "python" ]