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
Which methods implement the buffer interface in Python?
2,079,272
<p>I have a custom class with a <code>serialize</code> method, and I want to be able to write this class directly to files and have the return value of the <code>serialize</code> method get written, in Python 2.6. (I'm not trying to <a href="http://docs.python.org/library/pickle.html">pickle</a> my objects, this is som...
13
2010-01-16T23:08:40Z
2,079,312
<p>The documentation says,</p> <blockquote> <p>Python objects implemented in C can export a group of functions called the “buffer interface.”</p> </blockquote> <p>Your Foo class is not implemented in C, is it...?</p>
1
2010-01-16T23:18:03Z
[ "python", "buffer" ]
Which methods implement the buffer interface in Python?
2,079,272
<p>I have a custom class with a <code>serialize</code> method, and I want to be able to write this class directly to files and have the return value of the <code>serialize</code> method get written, in Python 2.6. (I'm not trying to <a href="http://docs.python.org/library/pickle.html">pickle</a> my objects, this is som...
13
2010-01-16T23:08:40Z
2,079,345
<p>Peculiarly, there are no special methods that a pure-Python coded class can directly implement to support the <code>buffer</code> interface -- that would have been <a href="http://www.python.org/dev/peps/pep-0298/">PEP 298</a>, but it was withdrawn.</p> <p>I'm afraid you'll have to use some explicit attribute or me...
11
2010-01-16T23:23:57Z
[ "python", "buffer" ]
Missing datetime.time.__sub__?
2,079,323
<p>Why can't subtract two time objects? For example, 12:00 - 11:00 = 1:00</p> <pre> <code> from datetime import time time(12,00) - time(11,00) # -> timedelta(hours=1) </code> </pre> <p>It seems that <code>datetime.time.__sub__</code> is missing</p> <pre> TypeError: unsupported operand type(s) for -: 'datetime.time' ...
2
2010-01-16T23:20:09Z
2,079,356
<p>The <code>time</code> objects have no date, so (e.g.) the <code>12:00</code> might be (say) on a Wed and the <code>11:00</code> on the preceding Tue, making the difference 25 hours, not one (any multiple of 24 might be added or subtracted). If you know they're actually on the same date, just apply any arbitrary dat...
6
2010-01-16T23:26:05Z
[ "python", "datetime", "time", "missing-features" ]
Missing datetime.time.__sub__?
2,079,323
<p>Why can't subtract two time objects? For example, 12:00 - 11:00 = 1:00</p> <pre> <code> from datetime import time time(12,00) - time(11,00) # -> timedelta(hours=1) </code> </pre> <p>It seems that <code>datetime.time.__sub__</code> is missing</p> <pre> TypeError: unsupported operand type(s) for -: 'datetime.time' ...
2
2010-01-16T23:20:09Z
2,079,363
<p>You can get your desired result by</p> <pre><code>t1 = time(12, 0) t2 = time(11, 0) td = timedelta(hours=t1.hour-t2.hour, minutes=t1.minute-t2.minute) </code></pre>
0
2010-01-16T23:28:41Z
[ "python", "datetime", "time", "missing-features" ]
How to copy a file via the browser to Amazon S3 using Python (and boto)?
2,079,594
<p>Creating a file (key) into Amazon S3 using Python (and boto) is not a problem. With this code, I can connect to a bucket and create a key with a specific content:</p> <pre><code>bucket_instance = connection.get_bucket('bucketname') key = bucket_instance.new_key('testfile.txt') key.set_contents_from_string('Content ...
1
2010-01-17T01:08:15Z
2,079,641
<p>do you mean this one? <a href="http://stackoverflow.com/questions/81451/upload-files-in-google-app-engine">http://stackoverflow.com/questions/81451/upload-files-in-google-app-engine</a></p>
0
2010-01-17T01:27:45Z
[ "python", "google-app-engine", "amazon-s3", "amazon-ec2", "boto" ]
How to copy a file via the browser to Amazon S3 using Python (and boto)?
2,079,594
<p>Creating a file (key) into Amazon S3 using Python (and boto) is not a problem. With this code, I can connect to a bucket and create a key with a specific content:</p> <pre><code>bucket_instance = connection.get_bucket('bucketname') key = bucket_instance.new_key('testfile.txt') key.set_contents_from_string('Content ...
1
2010-01-17T01:08:15Z
2,080,662
<p>You can't do this with boto, because what you're asking for is purely client-side - there's no direct involvement from the server except to generate the form to post.</p> <p>What you need to use is Amazon's <a href="http://doc.s3.amazonaws.com/proposals/post.html" rel="nofollow">browser-based upload with POST</a> s...
2
2010-01-17T10:27:24Z
[ "python", "google-app-engine", "amazon-s3", "amazon-ec2", "boto" ]
String matching in Python
2,079,676
<p>does anyone know which string matching algorithm is implemented in Python?</p>
3
2010-01-17T01:44:02Z
2,079,685
<p>Per <a href="http://svn.python.org/view/python/trunk/Objects/stringlib/fastsearch.h?revision=77470&amp;view=markup">the sources</a>, it's a</p> <blockquote> <p>fast search/count implementation, based on a mix between boyer-moore and horspool, with a few more bells and whistles on the top. for some more b...
9
2010-01-17T01:50:31Z
[ "python", "string-matching" ]
String matching in Python
2,079,676
<p>does anyone know which string matching algorithm is implemented in Python?</p>
3
2010-01-17T01:44:02Z
2,079,768
<p>I assume you're talking about CPython. In that case, you could always <a href="http://svn.python.org/view/python/trunk/" rel="nofollow">check the source</a> (see <a href="http://svn.python.org/view/python/trunk/Objects/stringlib/fastsearch.h?view=markup" rel="nofollow">fastsearch.h</a>).</p>
1
2010-01-17T02:30:16Z
[ "python", "string-matching" ]
Data modeling advice for a forum application on Google App Engine
2,079,763
<p>I'm writing a simple forum-like application on Google App Engine and trying to avoid scalability issues. I'm new to this non-RBDMS approach, i'd like to avoid pitfalls from the beginning.<br> The forum design is pretty simple, posts and replies will be the only concepts. What will be the best approach to the problem...
3
2010-01-17T02:28:18Z
2,079,777
<p>Firstly, why don't you use <code>user = db.UserProperty()</code> instead of <code>user = db.StringProperty()</code>?</p> <p>Secondly, I'm quite sure you should use whatever it works and is more readable and test the performance later, for three reasons:</p> <ol> <li>KISS (Keep it simple)</li> <li>Early optimizatio...
2
2010-01-17T02:37:55Z
[ "python", "google-app-engine", "data-modeling" ]
Data modeling advice for a forum application on Google App Engine
2,079,763
<p>I'm writing a simple forum-like application on Google App Engine and trying to avoid scalability issues. I'm new to this non-RBDMS approach, i'd like to avoid pitfalls from the beginning.<br> The forum design is pretty simple, posts and replies will be the only concepts. What will be the best approach to the problem...
3
2010-01-17T02:28:18Z
2,079,817
<p>You might want to take a look at <a href="http://stackoverflow.com/questions/548793/a-good-tutorial-on-creating-a-php-forum-from-scratch/548826#548826">a good tutorial on creating a php forum from scratch</a>. Sure that one is about PHP but it also covers the general overview of forum design.</p> <p>Basically, don'...
-2
2010-01-17T03:01:39Z
[ "python", "google-app-engine", "data-modeling" ]
Caching sitemaps in django
2,079,786
<p>I implemented a simple sitemap class using django's default sitemap app. As it was taking a long time to execute, I added manual caching:</p> <pre><code>class ShortReviewsSitemap(Sitemap): changefreq = "hourly" priority = 0.7 def items(self): # try to retrieve from cache result = get_ca...
8
2010-01-17T02:46:37Z
2,499,180
<p>50k it's not a hardcore parameter. :)</p> <p>You can use this class instead django.contrib.sitemaps.GenericSitemap</p> <pre><code>class LimitGenericSitemap(GenericSitemap): limit = 2000 </code></pre>
13
2010-03-23T10:45:19Z
[ "python", "django", "caching", "sitemap" ]
Caching sitemaps in django
2,079,786
<p>I implemented a simple sitemap class using django's default sitemap app. As it was taking a long time to execute, I added manual caching:</p> <pre><code>class ShortReviewsSitemap(Sitemap): changefreq = "hourly" priority = 0.7 def items(self): # try to retrieve from cache result = get_ca...
8
2010-01-17T02:46:37Z
2,527,519
<p>Assuming you don't need all those pages in your sitemap then reducing the limit to get the file size down will work fine as described in the previous answer.</p> <p>If you do want a very large sitemap and do want to use memcached you could split the content up into multiple chunks, store them under individual keys ...
2
2010-03-26T23:48:02Z
[ "python", "django", "caching", "sitemap" ]
Caching sitemaps in django
2,079,786
<p>I implemented a simple sitemap class using django's default sitemap app. As it was taking a long time to execute, I added manual caching:</p> <pre><code>class ShortReviewsSitemap(Sitemap): changefreq = "hourly" priority = 0.7 def items(self): # try to retrieve from cache result = get_ca...
8
2010-01-17T02:46:37Z
3,519,779
<p>I have about 200,000 pages on my site, so I had to have the index no matter what. I ended up doing the above hack, limiting the sitemap to 250 links, and also implementing a file-based cache. </p> <p>The basic algorithm is this:</p> <ul> <li>Try to load the sitemap from a file on disk</li> <li>If that fails, gener...
1
2010-08-19T07:56:59Z
[ "python", "django", "caching", "sitemap" ]
Caching sitemaps in django
2,079,786
<p>I implemented a simple sitemap class using django's default sitemap app. As it was taking a long time to execute, I added manual caching:</p> <pre><code>class ShortReviewsSitemap(Sitemap): changefreq = "hourly" priority = 0.7 def items(self): # try to retrieve from cache result = get_ca...
8
2010-01-17T02:46:37Z
34,123,447
<p>You can serve sitemaps also in gzip format, which makes them a lot smaller. XML is suited perfectly for gzip compression. What I sometimes do: Create the gzipped sitemap file(s) in a cronjob and render them as often as necessary. Usually, once a day will suffice. The code for this may look like this. Just make sure ...
0
2015-12-06T22:10:22Z
[ "python", "django", "caching", "sitemap" ]
What happen when I add a Django app to INSTALLED_APPS?
2,079,898
<p>Here is the situation. I have a django project with two installed apps. Both apps appear to function properly if they are installed independently of each other.</p> <p>However if I list both apps in the settings.INSTALLED_APPS the reverse() function seems to break for urls in the first app. So this leads me to b...
3
2010-01-17T03:56:01Z
2,079,935
<p>Without seeing the code, it's hard to say for sure, but I would guess that the <code>urls.py</code> of app_1 and app_2 contain the same name for different urls, e.g.:</p> <pre><code>app_1/urls.py: ... url(r'^app_1/foo/$', 'app_1.views.foo', name='foo') ... app_2/urls.py: ... url(r'^app_2/foo/$', 'app_2.v...
0
2010-01-17T04:18:54Z
[ "python", "django" ]
What happen when I add a Django app to INSTALLED_APPS?
2,079,898
<p>Here is the situation. I have a django project with two installed apps. Both apps appear to function properly if they are installed independently of each other.</p> <p>However if I list both apps in the settings.INSTALLED_APPS the reverse() function seems to break for urls in the first app. So this leads me to b...
3
2010-01-17T03:56:01Z
2,080,483
<p>Nothing particular happens when you add an app to INSTALLED_APPS, but the main thing that affects you is that its views are checked when you call <code>reverse()</code>. </p> <p>The way reverse works is to import <em>all</em> the views in the project, and see which ones match the URL name you have given. However, i...
4
2010-01-17T09:02:30Z
[ "python", "django" ]
Undesired python feedparser instantiation relic
2,080,071
<p>Question: How do I kill an instantiation or insure i'm creating a new instantiation of the python universal feedparser?</p> <hr> <p>Info:</p> <p>I'm working on a program right now that downloads and catalogs large numbers of blogs. It has worked well so for except for an unfortunate bug. My code is set up to take...
2
2010-01-17T05:25:40Z
2,080,073
<p>The problem is <code>posts=[]</code>. Default arguments are calculated at compile time, not runtime, so mutations to the object remain for the lifetime of the class. Instead use <code>posts=None</code> and test:</p> <pre><code>if posts is None: self.posts = [] </code></pre>
1
2010-01-17T05:28:10Z
[ "python", "feedparser" ]
Undesired python feedparser instantiation relic
2,080,071
<p>Question: How do I kill an instantiation or insure i'm creating a new instantiation of the python universal feedparser?</p> <hr> <p>Info:</p> <p>I'm working on a program right now that downloads and catalogs large numbers of blogs. It has worked well so for except for an unfortunate bug. My code is set up to take...
2
2010-01-17T05:25:40Z
2,080,129
<p>As what Ignacio said, any mutations that happen to the default arguments in the function list will stay for the life of the class. </p> <p>From <a href="http://docs.python.org/reference/compound_stmts.html#function-definitions" rel="nofollow">http://docs.python.org/reference/compound_stmts.html#function-definition...
0
2010-01-17T05:59:39Z
[ "python", "feedparser" ]
How to print nicely?
2,080,101
<p>I have two classes:</p> <pre><code>class Dog(object): def __init__(self, name): self.name = name class Toy(object): def play(self): print "Squeak!" </code></pre> <p>I need to come up with a method called play(self, toy, n) for class Dog. It prints "Yip! " (with a space) followed by the out...
1
2010-01-17T05:47:16Z
2,080,127
<p>In your example call, <code>Dog('big').play(0)</code>, you are <strong>not</strong> passing the <code>toy</code> argument -- that's what it's complaining about! Pass a toy argument before <code>n</code> and that will be better.</p> <p>Then you can start addressing the bugs in your <code>play</code> implementation:...
1
2010-01-17T05:58:58Z
[ "python", "printing" ]
How to print nicely?
2,080,101
<p>I have two classes:</p> <pre><code>class Dog(object): def __init__(self, name): self.name = name class Toy(object): def play(self): print "Squeak!" </code></pre> <p>I need to come up with a method called play(self, toy, n) for class Dog. It prints "Yip! " (with a space) followed by the out...
1
2010-01-17T05:47:16Z
2,081,368
<p>The first thing I see wrong is the you are attempting to call the class 'Toy' instead of the variable representing the instance 'toy' passed to you 'play' method. I don't believe you can instantiate a class and call one of its methods at the same time which is what 'Toy().play()' is trying to do. The other is that I...
0
2010-01-17T14:55:59Z
[ "python", "printing" ]
Python web hosting: Numpy, Matplotlib, Scientific Computing
2,080,110
<p>I write scientific software in Numpy/Scipy/Matplotlib. Having developed applications on my home computer, I am now interested in writing simple web applications. Example: user uploads image or audio file, my program processes it using Numpy/Scipy, and output is displayed on the browser using Matplotlib, or perhaps t...
39
2010-01-17T05:51:56Z
2,080,138
<p>App Engine does not support any of numpy, scipy, or matplotlib, alas.</p> <p>If you know exactly what OS and CPU your host is using, you could make an identical installation for yourself, download and install the same version of Python they're using, download the sources of packages you require and build them into ...
11
2010-01-17T06:04:59Z
[ "python", "numpy", "matplotlib", "scipy" ]
Python web hosting: Numpy, Matplotlib, Scientific Computing
2,080,110
<p>I write scientific software in Numpy/Scipy/Matplotlib. Having developed applications on my home computer, I am now interested in writing simple web applications. Example: user uploads image or audio file, my program processes it using Numpy/Scipy, and output is displayed on the browser using Matplotlib, or perhaps t...
39
2010-01-17T05:51:56Z
2,080,261
<p><strong>1: Installing third party packages to hosted spaces</strong> </p> <p>You can indeed install third party packages to your hosted space. If it's a pure python package, all that's needed is to unpack it to a directory and then add that directory to your PYTHONPATH environment variable or <code>sys.path</code>....
14
2010-01-17T07:10:09Z
[ "python", "numpy", "matplotlib", "scipy" ]
Python web hosting: Numpy, Matplotlib, Scientific Computing
2,080,110
<p>I write scientific software in Numpy/Scipy/Matplotlib. Having developed applications on my home computer, I am now interested in writing simple web applications. Example: user uploads image or audio file, my program processes it using Numpy/Scipy, and output is displayed on the browser using Matplotlib, or perhaps t...
39
2010-01-17T05:51:56Z
9,590,253
<p>I do not have privledges yet to comment but I can provide an "answer".</p> <p><strong>3: AppEngine</strong></p> <p>Numpy is now available on Google App Engine: <a href="https://code.google.com/p/googleappengine/issues/detail?id=190" rel="nofollow">https://code.google.com/p/googleappengine/issues/detail?id=190</a>...
5
2012-03-06T19:10:52Z
[ "python", "numpy", "matplotlib", "scipy" ]
Python web hosting: Numpy, Matplotlib, Scientific Computing
2,080,110
<p>I write scientific software in Numpy/Scipy/Matplotlib. Having developed applications on my home computer, I am now interested in writing simple web applications. Example: user uploads image or audio file, my program processes it using Numpy/Scipy, and output is displayed on the browser using Matplotlib, or perhaps t...
39
2010-01-17T05:51:56Z
10,657,046
<blockquote> <p>2 What hosting sites exist (either free or paid) which have Numpy/Matplotlib installed</p> </blockquote> <p><a href="http://www.pythonanywhere.com/">PythonAnywhere</a> offers web hosting and a simple in-browser IDE; lots of Python packages (including NumPy and Matplotlib) are <a href="https://www.pyt...
6
2012-05-18T17:20:31Z
[ "python", "numpy", "matplotlib", "scipy" ]
Django TemplateSyntaxError: too many values to unpack
2,080,186
<p>I'm working with a django form, and I have a choice field. I think the problem may be that the choices are fetched dynamically, and right now there's only one value. I'm getting the <code>TemplateSyntaxError: too many values to unpack</code>. Some of the other posts seem to say that having only one value is a proble...
4
2010-01-17T06:29:47Z
2,080,201
<p><code>choices</code> is supposed to be an iterable of 2-tuples. You are only appending a single string, which is causing chaos due to how strings and tuples interact (I'll give you details if you really care). Append 2-tuples instead.</p>
6
2010-01-17T06:40:58Z
[ "python", "django", "django-templates" ]
@register.filter in my code
2,080,296
<pre><code>from django import template register = template.Library() class_converter = { "textinput":"textinput textInput", "fileinput":"fileinput fileUpload" } @register.filter#&lt;-------- def is_checkbox(field): return field.field.widget.__class__.__name__.lower() == "checkboxinput" @register.filter#...
1
2010-01-17T07:25:53Z
2,080,303
<p>No. Simple decorators take the function they decorate as a parameter, and return a new function.</p> <pre><code>a = register.filter(a) </code></pre>
0
2010-01-17T07:29:32Z
[ "python", "django" ]
@register.filter in my code
2,080,296
<pre><code>from django import template register = template.Library() class_converter = { "textinput":"textinput textInput", "fileinput":"fileinput fileUpload" } @register.filter#&lt;-------- def is_checkbox(field): return field.field.widget.__class__.__name__.lower() == "checkboxinput" @register.filter#...
1
2010-01-17T07:25:53Z
2,080,306
<p>Not exactly. The decorator syntax:</p> <pre><code>@register.filter def a(): pass </code></pre> <p>is syntactic sugar for:</p> <pre><code>def a(): pass a = register.filter(a) </code></pre> <p>So <code>register.filter</code> in this case will be called with the first positional argument, 'name' being your f...
2
2010-01-17T07:30:17Z
[ "python", "django" ]
Django Form Submit Button
2,080,332
<p>I have a pretty simple file upload form class in django:</p> <pre><code>class UploadFileForm(forms.Form): category = forms.ChoiceField(get_category_list()) file = forms.FileField() </code></pre> <p>one problem is that when i do <code>{{ form.as_p }}</code>, It has no submit button. How do i add one?</p>
5
2010-01-17T07:40:01Z
2,080,336
<pre><code>&lt;input type="submit" value="Gogogo!" /&gt; </code></pre>
6
2010-01-17T07:41:00Z
[ "python", "django", "django-forms" ]
Python multiprocessing and a shared counter
2,080,660
<p>I'm having troubles with the multiprocessing module. I'm using a Pool of workers with its map method to load data from lots of files and for each of them I analyze data with with a custom function. Each time a file has been processed I would like to have a counter updated so that I can keep track of how many files r...
31
2010-01-17T10:26:42Z
2,080,668
<p>The problem is that the <code>counter</code> variable is not shared between your processes: each separate process is creating it's own local instance and incrementing that.</p> <p>See <a href="http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes">this section</a> of the documentation ...
37
2010-01-17T10:29:54Z
[ "python", "multiprocessing" ]
Python multiprocessing and a shared counter
2,080,660
<p>I'm having troubles with the multiprocessing module. I'm using a Pool of workers with its map method to load data from lots of files and for each of them I analyze data with with a custom function. Each time a file has been processed I would like to have a counter updated so that I can keep track of how many files r...
31
2010-01-17T10:26:42Z
21,681,534
<p>Counter class without the race-condition bug:</p> <pre><code>class Counter(object): def __init__(self): self.val = multiprocessing.Value('i', 0) def increment(self, n=1): with self.val.get_lock(): self.val.value += n @property def value(self): return self.val.va...
18
2014-02-10T15:42:38Z
[ "python", "multiprocessing" ]
Interaction with twisted.internet.reactor
2,080,757
<p>I am learning Twisted, especially its XMPP side. I am writing a Jabber client which must send and recieve messages. Here is my code: <a href="http://pastebin.com/m71225776" rel="nofollow">http://pastebin.com/m71225776</a> As I understood the workflow is like this: 1. I create handlers for important network events (i...
2
2010-01-17T11:10:04Z
2,081,972
<p>You just need to find what events will trigger sending a message.</p> <p>For example, in a GUI client, sending happens when the user types something. You should integrate with a graphics toolkit, using the Twisted reactor for its mainloop (there's a Gtk+ Twisted reactor for example). Then you'll be able to listen f...
2
2010-01-17T17:52:06Z
[ "python", "twisted" ]
Interaction with twisted.internet.reactor
2,080,757
<p>I am learning Twisted, especially its XMPP side. I am writing a Jabber client which must send and recieve messages. Here is my code: <a href="http://pastebin.com/m71225776" rel="nofollow">http://pastebin.com/m71225776</a> As I understood the workflow is like this: 1. I create handlers for important network events (i...
2
2010-01-17T11:10:04Z
2,096,102
<p>More accurately, you can't do anything with the reactor until it calls one of your callbacks. You don't call twisted, twisted calls you.</p> <p>One way to experiment is to have one of your setup handlers which you know will be called (or just test code put in after you start the reactor) call callLater() or loopin...
0
2010-01-19T18:47:17Z
[ "python", "twisted" ]
Authentication in django
2,080,854
<p>I'm trying to do connect form on my django website</p> <p>If in a shell I do :</p> <pre><code>$ ./manage.py shell Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) &gt;&gt;&gt; from django.contrib impo...
2
2010-01-17T11:51:34Z
2,081,599
<p>This works for me:</p> <p>In the top of your script:</p> <pre><code>from django.contrib.auth import authenticate, login, logout </code></pre> <p>Authentification:</p> <pre><code> user = authenticate(username=request.POST['username'], password=request.POST['password']) if user is not None: if ...
0
2010-01-17T16:11:50Z
[ "python", "django", "authentication" ]
Authentication in django
2,080,854
<p>I'm trying to do connect form on my django website</p> <p>If in a shell I do :</p> <pre><code>$ ./manage.py shell Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) &gt;&gt;&gt; from django.contrib impo...
2
2010-01-17T11:51:34Z
2,081,825
<p>ok I've found. Same as for the <a href="http://stackoverflow.com/questions/2078160/static-file-with-modwsgi-in-django">static files</a>, I needed to put my database outside my home folder and not next to my python files...</p>
0
2010-01-17T17:11:39Z
[ "python", "django", "authentication" ]
Python calling raw_input from a subprocess
2,081,124
<p>I'm calling a python script from the below one using subprocess. From the command line the user chooses which file to open using raw_input</p> <pre><code>import optparse import subprocess import readline import os def main(): options = {'0': './option_0.py', '1': './option_1.py', '2': ...
4
2010-01-17T13:40:27Z
2,081,156
<p>If I understand your question, you want to redirect the current standard input to the subprocess. Which can be done this way:</p> <pre><code>subprocess.Popen(file, stdin=sys.stdin, ...) </code></pre>
2
2010-01-17T13:52:30Z
[ "python", "subprocess" ]
Python calling raw_input from a subprocess
2,081,124
<p>I'm calling a python script from the below one using subprocess. From the command line the user chooses which file to open using raw_input</p> <pre><code>import optparse import subprocess import readline import os def main(): options = {'0': './option_0.py', '1': './option_1.py', '2': ...
4
2010-01-17T13:40:27Z
2,081,165
<p>Here's an example where the subprocess receives my input:</p> <pre><code>import subprocess import sys command = 'python -c \'print raw_input("Please make a selection: ")\'' sp = subprocess.Popen(command, shell = True, stdin = sys.stdin) sp.wait() </code></pre>
3
2010-01-17T13:55:29Z
[ "python", "subprocess" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,081,183
<p>You have to put strings into (double) quotes</p> <pre><code>list1 = ["a","b","c",...] </code></pre> <p>should work</p>
7
2010-01-17T13:59:24Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,081,193
<p>String literal should be enclosed in quotes :)</p> <pre><code>list1 = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] </code></pre>
2
2010-01-17T14:01:35Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,081,216
<p>In addition to using quotes properly, don't retype the alphabet.</p> <pre><code>&gt;&gt;&gt; import string &gt;&gt;&gt; string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' &gt;&gt;&gt; L = list(string.ascii_lowercase) &gt;&gt;&gt; print L ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ... &gt;&gt;&gt; help(stri...
12
2010-01-17T14:08:13Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,081,282
<p>Picking and choosing the best of the previous posts this is how I would do it since a string can be iterated.</p> <pre><code>&gt;&gt;&gt; import string &gt;&gt;&gt; for letter in string.ascii_lowercase: ... print(letter) ... </code></pre>
1
2010-01-17T14:27:09Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,081,554
<p>python interprets the members in your list as variables,you shoud enclose them in </p> <blockquote> <p>' or "</p> </blockquote>
1
2010-01-17T15:58:16Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,083,552
<p>When I need to make a list of characters, if they aren't already available in something defined in the std lib, and if I <em>really</em> need a list and not just a string, I use this form:</p> <pre><code>punc = list(r";:`~!@#$%^&amp;*()_-+=[]{}\|,./&lt;?&gt;") vowels = list("aeiou") # or sometimes list("aeiouy") <...
0
2010-01-18T02:14:48Z
[ "python", "syntax" ]
Python NameError
2,081,168
<pre><code>list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] for item in list1: print item </code></pre> <p>Not sure why the above code is throwing this error:</p> <pre><code>NameError: "name 'a' is not defined" </code></pre>
1
2010-01-17T13:56:24Z
2,089,764
<p>Every language needs to differentiate between constants and names/variables. The most confusing is when you have to differentiate between string constants and identifiers/names/variables.</p> <p>A shell (sh, bash, ksh, csh, cmd.com etc) tends to use constants; so you can just type a constant and you prefix a name/v...
1
2010-01-18T22:46:58Z
[ "python", "syntax" ]
install python Mysql module
2,081,240
<p>I've just installed python 2.6 on my win7 machine. Now I tried to install mysqldb. But when run "python setup.py install"</p> <pre><code> C:\MySQL-python-1.2.3c1&gt;python setup.py install Traceback (most recent call last): File "setup.py", line 15, in &lt;module&gt; metadata, options = get_config() Fil...
3
2010-01-17T14:15:23Z
2,081,486
<p>Are you running from an Administrator Mode command prompt? Also, do you have the MySQL headers available for the compiler to link against?</p> <p>If you are just an end-user, you will probably find it easier to use a pre-built MySQLdb binary, as compiling in Windows is a pain. Unfortunately the official site still ...
1
2010-01-17T15:40:59Z
[ "python", "mysql" ]
install python Mysql module
2,081,240
<p>I've just installed python 2.6 on my win7 machine. Now I tried to install mysqldb. But when run "python setup.py install"</p> <pre><code> C:\MySQL-python-1.2.3c1&gt;python setup.py install Traceback (most recent call last): File "setup.py", line 15, in &lt;module&gt; metadata, options = get_config() Fil...
3
2010-01-17T14:15:23Z
2,166,958
<p>I have compiled MySQLdb 1.2.3c1 once with python26, you could find it <a href="http://soemin.googlecode.com/files/MySQL-python-1.2.3c1.win32-py2.6.exe" rel="nofollow">here</a></p>
4
2010-01-30T05:22:42Z
[ "python", "mysql" ]
How to conduct buffer overflow in PHP/Python?
2,081,281
<p>Here is an example in c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void bad() { printf("Oh shit really bad~!\r\n"); } void foo() { char overme[4] = "WOW"; *(int*)(overme+8) = (int)bad; } int main() { foo(); } </code></pre>
5
2010-01-17T14:27:04Z
2,081,292
<p>As PHP is a scripting language and has no pointers and the string type is binary-safe such things won't work in PHP.</p> <p>But why would you want to do such a thing?</p> <p>(oh, there might be bugs in PHP resulting in a buffer overflow, but that's nothing that canbe relied upon in any way and usually is fixed qui...
2
2010-01-17T14:29:46Z
[ "php", "python", "buffer-overflow" ]
How to conduct buffer overflow in PHP/Python?
2,081,281
<p>Here is an example in c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void bad() { printf("Oh shit really bad~!\r\n"); } void foo() { char overme[4] = "WOW"; *(int*)(overme+8) = (int)bad; } int main() { foo(); } </code></pre>
5
2010-01-17T14:27:04Z
2,081,296
<p>Doing something similar in PHP will not result in the same behavior.</p> <p>PHP is interpreted and always checks whether the operation you are doing or not is valid.. So you can't - for example - overrun a buffer.</p>
1
2010-01-17T14:30:38Z
[ "php", "python", "buffer-overflow" ]
How to conduct buffer overflow in PHP/Python?
2,081,281
<p>Here is an example in c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void bad() { printf("Oh shit really bad~!\r\n"); } void foo() { char overme[4] = "WOW"; *(int*)(overme+8) = (int)bad; } int main() { foo(); } </code></pre>
5
2010-01-17T14:27:04Z
2,081,328
<p>Because php,python and every interpreted language first have to go through an interpreter and you dont have the full access to the memory this kind of languages will not let you to do some kind of games like the code you posted.</p>
1
2010-01-17T14:38:36Z
[ "php", "python", "buffer-overflow" ]
How to conduct buffer overflow in PHP/Python?
2,081,281
<p>Here is an example in c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void bad() { printf("Oh shit really bad~!\r\n"); } void foo() { char overme[4] = "WOW"; *(int*)(overme+8) = (int)bad; } int main() { foo(); } </code></pre>
5
2010-01-17T14:27:04Z
2,081,520
<p>The fact that Python and PHP are interpreted like suggested by others isn't actually the point. The point is that almost all of the APIs and language semantics that they expose are heavily error-checked making it impossible to have exploitable undefined behavior. Even if you compile the languages, it would still be ...
8
2010-01-17T15:47:54Z
[ "php", "python", "buffer-overflow" ]
How to conduct buffer overflow in PHP/Python?
2,081,281
<p>Here is an example in c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void bad() { printf("Oh shit really bad~!\r\n"); } void foo() { char overme[4] = "WOW"; *(int*)(overme+8) = (int)bad; } int main() { foo(); } </code></pre>
5
2010-01-17T14:27:04Z
2,082,906
<p>We're sorry: you've reached a weakness in Python. Unfortunately, it's by design, so little can be done about it. Perhaps you should stay with C.</p> <p>As Martin v. Löwis <a href="http://groups.google.com/group/comp.lang.python/msg/f1398385add2b428" rel="nofollow">said</a>:</p> <blockquote> <p>Python does not s...
1
2010-01-17T22:26:30Z
[ "php", "python", "buffer-overflow" ]
ftplib in combination with os.unlink in python
2,081,289
<p>With the following code I upload file.txt to a ftp server. When the file has been uploaded I delete it on my local machine.</p> <pre><code>import os from ftplib import FTP HOST = 'host.com' FTP_NAME = 'username' FTP_PASS = 'password' filepath = 'C:\file.txt' while True: try: ftp = FTP(HOST) ftp...
3
2010-01-17T14:28:33Z
2,081,459
<pre><code>import os from ftplib import FTP HOST = 'host.com' FTP_NAME = 'username' FTP_PASS = 'password' filepath = 'C:\file.txt' file = open(filepath, 'r') while True: try: ftp = FTP(HOST) ftp.login(FTP_NAME, FTP_PASS) ftp.storlines('STOR file.txt', file) except all_errors as ...
0
2010-01-17T15:33:30Z
[ "python", "exception" ]
ftplib in combination with os.unlink in python
2,081,289
<p>With the following code I upload file.txt to a ftp server. When the file has been uploaded I delete it on my local machine.</p> <pre><code>import os from ftplib import FTP HOST = 'host.com' FTP_NAME = 'username' FTP_PASS = 'password' filepath = 'C:\file.txt' while True: try: ftp = FTP(HOST) ftp...
3
2010-01-17T14:28:33Z
2,081,746
<p>You need to close (file and ftp session) on the <code>except</code> leg of the try/except, else the file keeps being referenced by the "old" timed-out ftp session (so as a consequence you need to open the file within the <code>while</code> loop, not outside it) -- it's not enough to close the file and ftp session on...
0
2010-01-17T16:51:07Z
[ "python", "exception" ]
ftplib in combination with os.unlink in python
2,081,289
<p>With the following code I upload file.txt to a ftp server. When the file has been uploaded I delete it on my local machine.</p> <pre><code>import os from ftplib import FTP HOST = 'host.com' FTP_NAME = 'username' FTP_PASS = 'password' filepath = 'C:\file.txt' while True: try: ftp = FTP(HOST) ftp...
3
2010-01-17T14:28:33Z
2,083,559
<p>I have still problems with the code, it is not as robust as I need it to be. For example it is possible that the login process fails. Maybe user+pass is wrong, maybe the sesrver is busy.</p> <pre><code>try: ftp = FTP(HOST) # HOST is a valid host address ftp.login('test', 'test111111') # WRONG user + pass to...
0
2010-01-18T02:19:11Z
[ "python", "exception" ]
How to add django modules to pydiction dictionary?
2,081,345
<p>I'm trying to use pydiction to autocomplete Python/Django statements in VIM Editor.</p> <p>When I try to add django modules to complete-dic using this:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/django </code></pre> <p>or:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/djang...
3
2010-01-17T14:47:11Z
2,094,695
<p>By glancing at the pydiction docs (I'm not a pydiction user) it appears that you are misusing the <code>pydiction.py</code> script.</p> <p>You are not supposed to provide a filename for import. You are supposed to provide a module name on which to base the dictionary creation. The module must already be in your pyt...
1
2010-01-19T15:44:18Z
[ "python", "django", "vim" ]
How to add django modules to pydiction dictionary?
2,081,345
<p>I'm trying to use pydiction to autocomplete Python/Django statements in VIM Editor.</p> <p>When I try to add django modules to complete-dic using this:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/django </code></pre> <p>or:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/djang...
3
2010-01-17T14:47:11Z
2,744,491
<p>For some reason doing</p> <pre><code>python pydiction.py django </code></pre> <p>will not add the submodules of django</p> <p>What I had to do was:</p> <pre><code>export DJANGO_SETTINGS_MODULE=settings export PYTHONPATH=/path/to/parent/of/settings </code></pre> <p>And then I had to add each submodule of django ...
0
2010-04-30T12:33:49Z
[ "python", "django", "vim" ]
How to add django modules to pydiction dictionary?
2,081,345
<p>I'm trying to use pydiction to autocomplete Python/Django statements in VIM Editor.</p> <p>When I try to add django modules to complete-dic using this:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/django </code></pre> <p>or:</p> <pre><code>python pydiction.py /usr/lib/pymodules/python2.6/djang...
3
2010-01-17T14:47:11Z
6,633,510
<p>It is quite some time since this question was raised. I faced similar problem. Following steps explain how to do it somewhat quickly.</p> <p>Since pydiction.py doesn't parse packages recursively, I had to use find and vi to build a package list for Django. Suppose you have installed django in <code>/usr/local/lib/p...
6
2011-07-09T07:55:48Z
[ "python", "django", "vim" ]
How to test type of an object in Python?
2,081,377
<pre><code>import string print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz print type(string.ascii_lowercase) # &lt;type 'str'&gt; print string.ascii_lowercase is str # False </code></pre> <p>Shouldn't it be <code>True</code>?</p>
3
2010-01-17T15:01:55Z
2,081,387
<p><code>string.ascii_lowercase is str</code> should not be <code>True</code>.</p> <p><code>type(string.ascii_lowercase) is str</code> is <code>True</code>.</p> <p>The <code>is</code> keyword checks object identity, not type.</p> <p>You may have seen code like <code>foo is None</code> often and thought that <code>No...
2
2010-01-17T15:06:08Z
[ "python", "types" ]
How to test type of an object in Python?
2,081,377
<pre><code>import string print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz print type(string.ascii_lowercase) # &lt;type 'str'&gt; print string.ascii_lowercase is str # False </code></pre> <p>Shouldn't it be <code>True</code>?</p>
3
2010-01-17T15:01:55Z
2,081,390
<p>The <code>is</code> operator compares the identity of two objects. This is what I believe it does behind the scenes:</p> <pre><code>id(string.ascii_lowercase) == id(str) </code></pre> <p>Actual strings are always going to have a different identity than the type <code>str</code>, so this will always be <code>False<...
4
2010-01-17T15:06:55Z
[ "python", "types" ]
How to test type of an object in Python?
2,081,377
<pre><code>import string print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz print type(string.ascii_lowercase) # &lt;type 'str'&gt; print string.ascii_lowercase is str # False </code></pre> <p>Shouldn't it be <code>True</code>?</p>
3
2010-01-17T15:01:55Z
2,081,391
<p>use: </p> <pre><code>&gt;&gt;&gt; isinstance('dfab', str) True </code></pre> <p><code>is</code> intended for identity testing.</p>
3
2010-01-17T15:06:56Z
[ "python", "types" ]
How to test type of an object in Python?
2,081,377
<pre><code>import string print string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz print type(string.ascii_lowercase) # &lt;type 'str'&gt; print string.ascii_lowercase is str # False </code></pre> <p>Shouldn't it be <code>True</code>?</p>
3
2010-01-17T15:01:55Z
2,081,393
<p>Don't you want <code>type(string.ascii_lowercase) is str</code> ?</p>
0
2010-01-17T15:07:43Z
[ "python", "types" ]
Prompt on file/directory delete
2,081,407
<p>When coding in Python, I often need to write a function like this one:</p> <pre><code>def delete_dir(dir_name): if os.path.exists(dir_name): reply = raw_input("Delete directory "+dir_name+"? [y/[n]] ") if reply=='y': os.system('rm -r '+dir_name) else: print "Abor...
1
2010-01-17T15:15:37Z
2,081,422
<p>to remove directory, use os module os.rmdir(), os.removedirs(). Or shutil.rmtree() .check the docs for more information</p>
1
2010-01-17T15:21:55Z
[ "python", "file-io", "prompt", "delete-directory" ]
Prompt on file/directory delete
2,081,407
<p>When coding in Python, I often need to write a function like this one:</p> <pre><code>def delete_dir(dir_name): if os.path.exists(dir_name): reply = raw_input("Delete directory "+dir_name+"? [y/[n]] ") if reply=='y': os.system('rm -r '+dir_name) else: print "Abor...
1
2010-01-17T15:15:37Z
2,081,543
<p>well ,you could save this function in a module and reuse it in other modules,that way ,your problem will be solved !</p>
0
2010-01-17T15:53:51Z
[ "python", "file-io", "prompt", "delete-directory" ]
Prompt on file/directory delete
2,081,407
<p>When coding in Python, I often need to write a function like this one:</p> <pre><code>def delete_dir(dir_name): if os.path.exists(dir_name): reply = raw_input("Delete directory "+dir_name+"? [y/[n]] ") if reply=='y': os.system('rm -r '+dir_name) else: print "Abor...
1
2010-01-17T15:15:37Z
2,081,581
<p>It wouldn't be a Python thing, but if you keep using os.system() to make the delete call, you can pass the <code>-i</code> parameter to <code>rm</code>. The man page explains it:</p> <blockquote> <p>-i &nbsp; &nbsp; &nbsp; prompt before every removal</p> </blockquote> <p>EDIT: I just read your code again and it ...
2
2010-01-17T16:05:29Z
[ "python", "file-io", "prompt", "delete-directory" ]
Technique for multi-language support of big static portions text in Django
2,081,436
<p>For small portions of text we use django standart {% trans %} tag</p> <p>What to do with big texts such as FAQ, terms and other static pages </p>
1
2010-01-17T15:26:30Z
2,081,665
<p>There is a <a href="http://docs.djangoproject.com/en/dev/topics/i18n/#in-template-code" rel="nofollow"><code>{% blocktrans %}</code></a> templatetag you can use.</p> <p>You could also write a simple templatetag yourself which includes anathor template based on the current language.</p> <pre><code>{% i18ninclude "f...
6
2010-01-17T16:28:12Z
[ "python", "django", "multilingual" ]
Technique for multi-language support of big static portions text in Django
2,081,436
<p>For small portions of text we use django standart {% trans %} tag</p> <p>What to do with big texts such as FAQ, terms and other static pages </p>
1
2010-01-17T15:26:30Z
2,081,689
<p>Take a look at <a href="http://bitbucket.org/hakanw/django-better-chunks/wiki/Home" rel="nofollow">django-better-chunks</a>. It allows to insert snippets of static HTML inside your templates and it has i18n support.</p> <p>For static pages I recommend to use some kind of CMS, for example <a href="http://www.django-...
2
2010-01-17T16:34:13Z
[ "python", "django", "multilingual" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
2,081,606
<p>You can use <a href="http://docs.python.org/library/urllib2.html" rel="nofollow">urllib2</a> to make the HTTP requests, and then you'll have web content.</p> <p>You can get it like this:</p> <pre><code>import urllib2 response = urllib2.urlopen('http://example.com') html = response.read() </code></pre> <p><a href=...
7
2010-01-17T16:13:38Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
2,082,025
<p>Use urllib2 in combination with the brilliant <a href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup</a> library:</p> <pre><code>import urllib2 from BeautifulSoup import BeautifulSoup # or if you're using BeautifulSoup4: # from bs4 import BeautifulSoup soup = BeautifulSoup(urllib2.urlopen('http://ex...
166
2010-01-17T18:08:41Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
8,600,787
<p>I collected together scripts from my web scraping work into <a href="http://code.google.com/p/webscraping">this library</a>.</p> <p>Example script for your case:</p> <pre><code>from webscraping import download, xpath D = download.Download() html = D.get('http://example.com') for row in xpath.search(html, '//table...
16
2011-12-22T07:46:36Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
8,603,040
<p>I'd really recommend Scrapy, for reasons being elaborated in <a href="http://stackoverflow.com/questions/6283271/is-it-worth-learning-scrapy">this question - "Is it worth learning Scrapy?"</a>.</p> <p>Quote from the answer:</p> <blockquote> <ul> <li>Scrapy crawling is fastest than mechanize because uses asynch...
55
2011-12-22T11:12:48Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
13,660,474
<p>I use a combination of <a href="http://arshaw.com/scrapemark/" rel="nofollow">Scrapemark</a> (finding urls - py2) and <a href="http://code.google.com/p/httplib2/" rel="nofollow">httlib2</a> (downloading images - py2+3). The scrapemark.py has 500 lines of code, but uses regular expressions, so it may be not so fast, ...
4
2012-12-01T14:40:17Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
16,661,996
<p>I would strongly suggest checking out <a href="https://pypi.python.org/pypi/pyquery" rel="nofollow">pyquery</a>. It uses jquery-like (aka css-like) syntax which makes things really easy for those coming from that background.</p> <p>For your case, it would be something like:</p> <pre><code>from pyquery import * ht...
10
2013-05-21T04:09:58Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
21,858,192
<p>I just saw <a href="https://github.com/jmcarp/robobrowser" rel="nofollow">RoboBrowser</a> in <a href="http://pycoders.com/" rel="nofollow">Pycoder's Weekly</a>.</p> <blockquote> <p>A library for web scraping built on Requests and BeautifulSoup. Like Mechanize, but with tests, docs, and a Pythonic interface.</p> <...
2
2014-02-18T15:29:23Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
27,009,470
<p>Scrapy open source framework will help to web scrap in python.This open source and collaborative framework for extracting the data you need from websites. </p> <p>Web scraping is closely related to web indexing, which indexes information on the web using a bot or web crawler and is a universal technique adopted by ...
1
2014-11-19T05:16:04Z
[ "python", "screen-scraping" ]
Web scraping with Python
2,081,586
<p>I'd like to grab daily sunrise/sunset times from a web site. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available?</p>
164
2010-01-17T16:06:53Z
28,394,673
<p><strong>Make your life easier by using <code>CSS Selectors</code></strong></p> <p>I know I have come late to party but I have a nice suggestion for you.</p> <p>Using <code>BeautifulSoup</code> is already been suggested I would rather prefer using <code>CSS Selectors</code> to scrape data inside HTML</p> <pre><cod...
0
2015-02-08T13:52:29Z
[ "python", "screen-scraping" ]
Python raw strings and unicode : how to use Web input as regexp patterns?
2,081,622
<p><strong>EDIT : This question doesn't really make sense once you have picked up what the "r" flag means. More details <a href="http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-rstring-flags-in-python-and-what-are-raw-string-litter/2081708#2081708">here</a>. For people looking for a quick anwser, I add...
2
2010-01-17T16:17:03Z
2,081,729
<p>Apart from possibly having to encode Unicode properly (in Python 2.*), no processing is needed because there is no specific <em>type</em> for "raw strings" -- it's just a syntax for literals, i.e. for string constants, and you don't have any string constants in your code snippet, so there's nothing to "process".</p>...
3
2010-01-17T16:44:33Z
[ "python", "regex", "unicode", "rawstring" ]
Python raw strings and unicode : how to use Web input as regexp patterns?
2,081,622
<p><strong>EDIT : This question doesn't really make sense once you have picked up what the "r" flag means. More details <a href="http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-rstring-flags-in-python-and-what-are-raw-string-litter/2081708#2081708">here</a>. For people looking for a quick anwser, I add...
2
2010-01-17T16:17:03Z
2,081,808
<p>"r" flags just prevent Python from interpreting "\" in a string. Since the Web doesn't care about what kind of data it carries, your web input will be a bunch of bytes you are free to interpret the way you want.</p> <p>So to address this problem :</p> <ul> <li>be sure you use Unicode (e.g. utf-8) all long the way<...
1
2010-01-17T17:06:35Z
[ "python", "regex", "unicode", "rawstring" ]
Python raw strings and unicode : how to use Web input as regexp patterns?
2,081,622
<p><strong>EDIT : This question doesn't really make sense once you have picked up what the "r" flag means. More details <a href="http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-rstring-flags-in-python-and-what-are-raw-string-litter/2081708#2081708">here</a>. For people looking for a quick anwser, I add...
2
2010-01-17T16:17:03Z
2,081,821
<p>Note the following in your first example:</p> <pre><code>&gt;&gt;&gt; p1 = "pattern" &gt;&gt;&gt; p2 = u"pattern" &gt;&gt;&gt; p3 = r"pattern" &gt;&gt;&gt; p4 = ur"pattern" # it's ur"", not ru"" btw &gt;&gt;&gt; p1 == p2 == p3 == p4 True </code></pre> <p>While these constructs look different, they all do the same ...
1
2010-01-17T17:10:05Z
[ "python", "regex", "unicode", "rawstring" ]
What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
2,081,640
<p>While asking <a href="http://stackoverflow.com/questions/2081622/python-raw-strings-and-unicode-how-to-use-web-for-input-as-regexp-patterns">this question</a>, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.</p> <p>I know what an encoding is, and I know wha...
240
2010-01-17T16:22:57Z
2,081,652
<p>A "u" prefix denotes the value has type <code>unicode</code> rather than <code>str</code>.</p> <p>Raw string literals, with an "r" prefix, escape any escape sequences within them, so <code>len(r"\n")</code> is 2. Because they escape escape sequences, you cannot end a string literal with a single backslash: that's ...
14
2010-01-17T16:25:51Z
[ "python", "unicode", "rawstring" ]
What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
2,081,640
<p>While asking <a href="http://stackoverflow.com/questions/2081622/python-raw-strings-and-unicode-how-to-use-web-for-input-as-regexp-patterns">this question</a>, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.</p> <p>I know what an encoding is, and I know wha...
240
2010-01-17T16:22:57Z
2,081,655
<p>There are two types of string in python: the traditional <code>str</code> type and the newer <code>unicode</code> type. If you type a string literal without the <code>u</code> in front you get the old <code>str</code> type which stores 8-bit characters, and with the <code>u</code> in front you get the newer <code>un...
72
2010-01-17T16:26:17Z
[ "python", "unicode", "rawstring" ]
What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
2,081,640
<p>While asking <a href="http://stackoverflow.com/questions/2081622/python-raw-strings-and-unicode-how-to-use-web-for-input-as-regexp-patterns">this question</a>, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.</p> <p>I know what an encoding is, and I know wha...
240
2010-01-17T16:22:57Z
2,081,708
<p>There's not really any "raw <em>string</em>"; there are raw <em>string literals</em>, which are exactly the string literals marked by a <code>'r'</code> before the opening quote.</p> <p>A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, <code>\</code>, is taken as mean...
271
2010-01-17T16:38:39Z
[ "python", "unicode", "rawstring" ]
What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
2,081,640
<p>While asking <a href="http://stackoverflow.com/questions/2081622/python-raw-strings-and-unicode-how-to-use-web-for-input-as-regexp-patterns">this question</a>, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.</p> <p>I know what an encoding is, and I know wha...
240
2010-01-17T16:22:57Z
9,576,583
<p>'raw string' means it is stored as it appears. for example, '\' is just a backslash instead of an escaping.</p>
19
2012-03-06T01:21:38Z
[ "python", "unicode", "rawstring" ]
What exactly do "u" and "r" string flags do in Python, and what are raw string literals?
2,081,640
<p>While asking <a href="http://stackoverflow.com/questions/2081622/python-raw-strings-and-unicode-how-to-use-web-for-input-as-regexp-patterns">this question</a>, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.</p> <p>I know what an encoding is, and I know wha...
240
2010-01-17T16:22:57Z
32,213,996
<p>Let me simply explain: In python, you can store string in 2 different types.</p> <p>The first one is <strong>ASCII</strong> which is <strong>str</strong> type in python, it uses 1 byte of memory. (256 characters, will store mostly English alphabets and simple symbols)</p> <p>The 2nd type is <strong>UNICODE</strong...
4
2015-08-25T21:01:29Z
[ "python", "unicode", "rawstring" ]
Approaching refactoring
2,081,745
<p>I have a very data-centric application, written in Python / PyQt. I'm planning to do some refactoring to really separate the UI from the core, mainly because there aren't any real tests in place yet, and that clearly has to change.</p> <p>There is some separation already, and I think I've done quite a few things th...
7
2010-01-17T16:50:51Z
2,081,774
<p>If you have not done so already, read "Working Effectively with Legacy Code" by Michael Feathers - it deals with exactly this sort of situation, and offers a wealth of techniques for dealing with it. </p> <p>One key point he makes is to try and get some tests in place before refactoring. Since it is not suitable ...
7
2010-01-17T16:58:56Z
[ "python", "unit-testing", "qt", "refactoring", "separation-of-concerns" ]
Approaching refactoring
2,081,745
<p>I have a very data-centric application, written in Python / PyQt. I'm planning to do some refactoring to really separate the UI from the core, mainly because there aren't any real tests in place yet, and that clearly has to change.</p> <p>There is some separation already, and I think I've done quite a few things th...
7
2010-01-17T16:50:51Z
2,082,334
<p>If you want to extract all GUI parts of your application from all the other parts in order to tests all your application, you should use the Model-View-Presenter: You can find some explanation <a href="http://msdn.microsoft.com/en-us/magazine/cc188690.aspx" rel="nofollow">here</a> and <a href="http://en.wikipedia.or...
2
2010-01-17T19:43:25Z
[ "python", "unit-testing", "qt", "refactoring", "separation-of-concerns" ]
Approaching refactoring
2,081,745
<p>I have a very data-centric application, written in Python / PyQt. I'm planning to do some refactoring to really separate the UI from the core, mainly because there aren't any real tests in place yet, and that clearly has to change.</p> <p>There is some separation already, and I think I've done quite a few things th...
7
2010-01-17T16:50:51Z
2,146,258
<p>I have done refactoring for large legacy code aiming UI/backend separation before. It's fun and rewarding.</p> <p>/praise ;)</p> <p>Whatever pattern one calls it or be it part of MVC it's invaluable to have a very clear <strong>API layer</strong>. If possible you may route all the UI requests through a dispatcher ...
1
2010-01-27T11:02:16Z
[ "python", "unit-testing", "qt", "refactoring", "separation-of-concerns" ]
Approaching refactoring
2,081,745
<p>I have a very data-centric application, written in Python / PyQt. I'm planning to do some refactoring to really separate the UI from the core, mainly because there aren't any real tests in place yet, and that clearly has to change.</p> <p>There is some separation already, and I think I've done quite a few things th...
7
2010-01-17T16:50:51Z
2,151,816
<p>One point, not mentioned so far and not really answering the question, but very important: Wherever possible you should put in the test <em>now, before you start refactoring</em>. The main point of test is that to detect if you break something.</p> <p>Refactoring is something where it is really valuable to exactly ...
1
2010-01-28T02:03:35Z
[ "python", "unit-testing", "qt", "refactoring", "separation-of-concerns" ]
Python list : How to sort by timestamp? ( App Engine related )
2,081,754
<p>I have ten entities in my Feed model ( this is an App Engine model)</p> <pre><code>class Feed(db.Model): sometext = db.StringProperty() timestamp = db.DateTimeProperty(auto_now=True) list_of_keys = ["key1","key2","key3".... "key10"] </code></pre> <p>so i call my entities using db.key() method:</p> <pre><code...
2
2010-01-17T16:53:54Z
2,081,760
<pre><code>import operator ... for feed in sorted(feeds, key=operator.attrgetter('timestamp'), reverse=True): print humanizeTimeDiff(feed.timestamp) </code></pre>
10
2010-01-17T16:55:49Z
[ "python", "google-app-engine" ]
How to create a custom django filter tag
2,081,834
<p>I am having trouble in getting my site to recognise custom template tags. I have the following dir structure:</p> <ul> <li>project_name <ul> <li>project_name <ul> <li>templatetags <ul> <li>_ _<em>init _</em> <em>.py</li> <li>getattribute.py</li> </ul></li> <li>views <ul> <li></em> _<em>init _</em> _.py</li> <li>in...
1
2010-01-17T17:13:47Z
2,081,935
<p>Ae you sure this is specifically to do with the template tag?</p> <p>It sounds like the project_name directory is not on your python path. The output on the error page should show your current python path, so you can check if it is as expected.</p> <p>Read this to learn how to fix it: <a href="http://djangotricks....
1
2010-01-17T17:38:36Z
[ "python", "django" ]
How to create a custom django filter tag
2,081,834
<p>I am having trouble in getting my site to recognise custom template tags. I have the following dir structure:</p> <ul> <li>project_name <ul> <li>project_name <ul> <li>templatetags <ul> <li>_ _<em>init _</em> <em>.py</li> <li>getattribute.py</li> </ul></li> <li>views <ul> <li></em> _<em>init _</em> _.py</li> <li>in...
1
2010-01-17T17:13:47Z
2,082,115
<p>Your project structure is, not to put too fine a point on it, a mess. Some of the many things you need to do:</p> <ul> <li>don't use the same name for the containing directory (project) and the inner one (which should be an app name).</li> <li><code>manage.py</code> and <code>settings.py</code> should be in the out...
1
2010-01-17T18:34:18Z
[ "python", "django" ]
How to create a custom django filter tag
2,081,834
<p>I am having trouble in getting my site to recognise custom template tags. I have the following dir structure:</p> <ul> <li>project_name <ul> <li>project_name <ul> <li>templatetags <ul> <li>_ _<em>init _</em> <em>.py</li> <li>getattribute.py</li> </ul></li> <li>views <ul> <li></em> _<em>init _</em> _.py</li> <li>in...
1
2010-01-17T17:13:47Z
2,082,163
<p>The error is becaus you have wrong your folder's structure, i think you must read the docs, this <a href="http://docs.djangoproject.com/en/1.1/intro/tutorial01/#intro-tutorial01" rel="nofollow">tutorial (part1)</a> explains the right structure:</p> <p>You have a project that isn't same thing that app:</p> <ul> <li...
2
2010-01-17T18:50:11Z
[ "python", "django" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,858
<p>File objects have a .readlines() method which will give you a list of the contents of the file, one line per list item. After that, you can just use normal list slicing techniques.</p> <p><a href="http://docs.python.org/library/stdtypes.html#file.readlines" rel="nofollow">http://docs.python.org/library/stdtypes.htm...
1
2010-01-17T17:18:33Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,860
<p>How about this:</p> <pre><code>&gt;&gt;&gt; with open('a', 'r') as fin: lines = fin.readlines() &gt;&gt;&gt; for i, line in enumerate(lines): if i &gt; 30: break if i == 26: dox() if i == 30: doy() </code></pre>
2
2010-01-17T17:18:45Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,861
<p>The quick answer:</p> <pre><code>f=open('filename') lines=f.readlines() print lines[25] print lines[29] </code></pre> <p>or:</p> <pre><code>lines=[25, 29] i=0 f=open('filename') for line in f: if i in lines: print i i+=1 </code></pre> <p>There is a more elegant solution for extracting many lines:...
70
2010-01-17T17:18:47Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,873
<p>If you don't mind importing then <a href="http://pydoc.org/2.5.1/fileinput.html" rel="nofollow">fileinput</a> does exactly what you need (this is you can read the line number of the current line)</p>
2
2010-01-17T17:21:58Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,880
<p>If the file to read is big, and you don't want to read the whole file in memory at once:</p> <pre><code>fp = open("file") for i, line in enumerate(fp): if i == 25: # 26th line elif i == 29: # 30th line elif i &gt; 29: break fp.close() </code></pre> <p>Note that <code>i == n-1</c...
111
2010-01-17T17:23:01Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,895
<p>You can do a <a href="http://docs.python.org/tutorial/inputoutput.html" rel="nofollow">seek()</a> call which positions your read head to a specified byte within the file. This won't help you unless you know exactly how many bytes (characters) are written in the file before the line you want to read. Perhaps your fil...
2
2010-01-17T17:26:08Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,920
<pre><code>def getitems(iterable, items): items = list(items) # get a list from any iterable and make our own copy # since we modify it if items: items.sort() for n, v in enumerate(iterable): if n == items[0]: yield v items.pop(0) if not items: b...
2
2010-01-17T17:33:49Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,081,973
<pre><code>f = open(filename, 'r') totalLines = len(f.readlines()) f.close() f = open(filename, 'r') lineno = 1 while lineno &lt; totalLines: line = f.readline() if lineno == 26: doLine26Commmand(line) elif lineno == 30: doLine30Commmand(line) lineno += 1 f.close() </code></pre>
-1
2010-01-17T17:52:11Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,082,121
<p>I prefer this approach because it's more general-purpose, i.e. you can use it on a file, on the result of <code>f.readlines()</code>, on a <code>StringIO</code> object, whatever:</p> <pre><code>def read_specific_lines(file, lines_to_read): """file is any iterable; lines_to_read is an iterable containing int valu...
1
2010-01-17T18:37:36Z
[ "python", "file", "line" ]
Reading specific lines only (Python)
2,081,836
<p>I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?</p> <p>Thanks</p>
85
2010-01-17T17:14:11Z
2,082,131
<p>A fast and compact approach could be:</p> <pre><code>def picklines(thefile, whatlines): return [x for i, x in enumerate(thefile) if i in whatlines] </code></pre> <p>this accepts any open file-like object <code>thefile</code> (leaving up to the caller whether it should be opened from a disk file, or via e.g a soc...
18
2010-01-17T18:42:19Z
[ "python", "file", "line" ]