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
migrating from one framework to another in python
2,396,294
<p>I'm having trouble deciding which python framework to use for my website. So I've decided to bite the bullet and use Django. My question is how easy (or difficult) will it be to migrate to a different framework in future if I have issues with Django ?</p>
0
2010-03-07T12:57:10Z
2,396,418
<p>For the template part, <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja</a> is pretty similar to Django, but stand-alone. It might help you to migrate your templates to another framework.</p> <p>(Or use it right away, I've found Jinja similar but quite superior to Django's default template system.)</p>
1
2010-03-07T13:37:33Z
[ "python", "django", "frameworks", "migrate" ]
Load huge data to memory using python
2,396,350
<p>I have to load large data to the memory, and the structure is list. How can I get another approach. thanx</p>
-4
2010-03-07T13:16:12Z
2,396,361
<p>If the data is too large to fit into your computer's memory you will have to read it piece by piece. If it is not too large you may still want to do this but it might suit your needs to read it into memory in its entirety. If you edit your question to explain more about your needs and what the characteristics of t...
0
2010-03-07T13:19:23Z
[ "python" ]
Load huge data to memory using python
2,396,350
<p>I have to load large data to the memory, and the structure is list. How can I get another approach. thanx</p>
-4
2010-03-07T13:16:12Z
2,396,377
<p>process the data line by line, eg</p> <pre><code>for line in open("file"): print "do processing of line:",line </code></pre> <p>Or if you really really want to load one whole chunk to memory, you can try memory mapping.</p> <pre><code>import mmap,os,re bigfile = open( "file2" ) length = os.stat(bigfile.fileno...
2
2010-03-07T13:24:42Z
[ "python" ]
Load huge data to memory using python
2,396,350
<p>I have to load large data to the memory, and the structure is list. How can I get another approach. thanx</p>
-4
2010-03-07T13:16:12Z
2,396,447
<p>Is there any structure to this data, like a big list of customer records, or is it just one big blob like an image, audio, or video data? If the former, you might want to restructure the data into a database. sqlite is included with Python as of Py2.5, and is sufficient for many data sorting and sifting tasks.</p>...
0
2010-03-07T13:46:02Z
[ "python" ]
Python: web login script, what's the problem?
2,396,382
<p>this is the script >></p> <pre><code>import ClientForm import urllib2 request = urllib2.Request("http://ritaj.birzeit.edu") response = urllib2.urlopen(request) forms = ClientForm.ParseResponse(response, backwards_compat=False) response.close() form = forms[0] print form sooform = str(raw_input("Form Name: ")) use...
1
2010-03-07T13:25:15Z
2,396,452
<p>the actual address seems to be using <code>https</code> instead of <code>http</code>. check the <a href="http://docs.python.org/library/urllib2.html" rel="nofollow">urllib2</a> doc to see if it handles HTTPS( i believe you need ssl)</p>
0
2010-03-07T13:48:30Z
[ "python", "browser" ]
Python: web login script, what's the problem?
2,396,382
<p>this is the script >></p> <pre><code>import ClientForm import urllib2 request = urllib2.Request("http://ritaj.birzeit.edu") response = urllib2.urlopen(request) forms = ClientForm.ParseResponse(response, backwards_compat=False) response.close() form = forms[0] print form sooform = str(raw_input("Form Name: ")) use...
1
2010-03-07T13:25:15Z
2,397,305
<p>The only <code>&lt;form&gt;</code> tag in the HTML served at that URL (save it to a file and look for yourself!) is:</p> <pre><code>&lt;form method="GET" action="http://www.google.com/u/ritaj"&gt; </code></pre> <p>which does a customized Google search and has nothing to do with logging in (plus, for some reason, C...
1
2010-03-07T18:10:51Z
[ "python", "browser" ]
App Engine, Python: problem updating datastore record
2,396,437
<p>I need to update a record in the datastore, but instead of updated record I get always a new record.</p> <p>My model:</p> <pre><code>class PageModel(db.Model): title = db.StringProperty() content = db.TextProperty() reference = db.SelfReferenceProperty() user = db.UserProperty(auto_current_user =...
2
2010-03-07T13:44:02Z
2,396,497
<p>Why are you created a new PageModel everytime? instead edit the one you got by id i.e. CP ? e.g.</p> <pre><code>edited_page = CP edited_page.title = title edited_page.content = content edited_page.type = type edited_page.reference = reference edited_page.template = template edited_page.put() </code></pre>
3
2010-03-07T14:06:03Z
[ "python", "google-app-engine", "gae-datastore" ]
Using one Scrapy spider for several websites
2,396,529
<p>I need to create a user configurable web spider/crawler, and I'm thinking about using Scrapy. But, I can't hard-code the domains and allowed URL regex:es -- this will instead be configurable in a GUI.</p> <p>How do I (as simple as possible) create a spider or a set of spiders with Scrapy where the domains and allow...
9
2010-03-07T14:13:38Z
2,397,014
<p>Shameless self promotion on <a href="http://github.com/hinoglu/Domo/" rel="nofollow">domo</a>! you'll need to instantiate the crawler as given in the examples, for your project.</p> <p>Also you'll need to make the crawler configurable on runtime, which is simply passing the configuration to crawler, and overriding ...
3
2010-03-07T16:47:14Z
[ "python", "web-crawler", "scrapy" ]
Using one Scrapy spider for several websites
2,396,529
<p>I need to create a user configurable web spider/crawler, and I'm thinking about using Scrapy. But, I can't hard-code the domains and allowed URL regex:es -- this will instead be configurable in a GUI.</p> <p>How do I (as simple as possible) create a spider or a set of spiders with Scrapy where the domains and allow...
9
2010-03-07T14:13:38Z
2,397,107
<p>What you need is to dynamically create spider classes, subclassing your favorite generic spider class as supplied by <code>scrapy</code> (<code>CrawlSpider</code> subclasses with your <code>rules</code> added, or <code>XmlFeedSpider</code>, or whatever) and adding <code>domain_name</code>, <code>start_urls</code>, a...
4
2010-03-07T17:17:02Z
[ "python", "web-crawler", "scrapy" ]
Using one Scrapy spider for several websites
2,396,529
<p>I need to create a user configurable web spider/crawler, and I'm thinking about using Scrapy. But, I can't hard-code the domains and allowed URL regex:es -- this will instead be configurable in a GUI.</p> <p>How do I (as simple as possible) create a spider or a set of spiders with Scrapy where the domains and allow...
9
2010-03-07T14:13:38Z
2,397,334
<p><strong>WARNING: This answer was for Scrapy v0.7, spider manager api changed a lot since then.</strong></p> <p>Override default SpiderManager class, load your custom rules from a database or somewhere else and instanciate a custom spider with your own rules/regexes and domain_name</p> <p>in mybot/settings.py:</p> ...
9
2010-03-07T18:19:52Z
[ "python", "web-crawler", "scrapy" ]
GTK StatusIcon: Coordinates of left-click?
2,396,557
<p>how do I get the x/y-coordinates of a left click in a Gtk <a href="http://www.pygtk.org/docs/pygtk/class-gtkstatusicon.html" rel="nofollow">StatusIcon</a>?</p> <p>This is my first GTK app and I'm stuck. Is there any way to get details about the last button event that occurred? Or is it possible to pass those detail...
0
2010-03-07T14:23:05Z
2,396,617
<p>Since the status icon isn't a widget, it's a bit roundabout. You might be able to pass in some kind of widget as part of the user parameter object and get the global mouse position on activate. See <a href="http://faq.pygtk.org/index.py?req=show&amp;file=faq23.024.htp" rel="nofollow">here</a> on how you might.</p>...
1
2010-03-07T14:42:13Z
[ "python", "linux", "gtk", "pygtk" ]
In a pylons web app, should a cookie be set from a model class or a controller?
2,396,804
<p>Trying to figure out the best way to do this:</p> <p>Should I do something like:</p> <pre><code>def index(self): if request.POST: u = User(id) u.setCookie() #All session logic in def setCookie() </code></pre> <p>Or set the cookie in the controller like:</p> <pre><code>def index(self): if ...
0
2010-03-07T15:38:00Z
2,399,040
<p>I think traditionally you would want the model to be concerned with data persistence and validation but not http related stuff like cookies. Which leaves the controller to be the more appropriate place in my opinion. </p> <p>A reason(not the only one) I can think of for this is that it might be required someday tha...
2
2010-03-08T02:59:28Z
[ "python", "pylons" ]
In a pylons web app, should a cookie be set from a model class or a controller?
2,396,804
<p>Trying to figure out the best way to do this:</p> <p>Should I do something like:</p> <pre><code>def index(self): if request.POST: u = User(id) u.setCookie() #All session logic in def setCookie() </code></pre> <p>Or set the cookie in the controller like:</p> <pre><code>def index(self): if ...
0
2010-03-07T15:38:00Z
3,075,451
<p>I share also Tom's opinion, you should try to avoid to much dependencies in different classes. So the controller should to all the http (request, response) related stuff. Also for testing it is much easier.</p>
0
2010-06-19T12:23:06Z
[ "python", "pylons" ]
How to encode HTML non-ASCII data to UTF-8 in Python
2,396,925
<p>I tried to do that, and I found this errors: </p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; x = 'Ingl\xeas' &gt;&gt;&gt; x 'Ingl\xeas' &gt;&gt;&gt; print x Ingl�s &gt;&gt;&gt; x.decode('utf8') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "...
2
2010-03-07T16:15:16Z
2,396,944
<p>You need to know how the input data is encoded before you decode it. In some of you're attempts, you're trying to decode it from UTF-8, but Python throws an exception because the input isn't valid UTF-8. It looks like it might be latin-1. This works for me:</p> <pre><code>&gt;&gt;&gt; x = 'Ingl\xeas' &gt;&gt;&gt...
7
2010-03-07T16:22:28Z
[ "python", "unicode", "utf-8" ]
How to encode HTML non-ASCII data to UTF-8 in Python
2,396,925
<p>I tried to do that, and I found this errors: </p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; x = 'Ingl\xeas' &gt;&gt;&gt; x 'Ingl\xeas' &gt;&gt;&gt; print x Ingl�s &gt;&gt;&gt; x.decode('utf8') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "...
2
2010-03-07T16:15:16Z
2,396,951
<pre><code>Ingl\xeas </code></pre> <p>is not UTF-8 but (probably) Windows-1252- or latin1-encoded. So you first need to decode it. Only then you can encode it to UTF-8.</p> <p>Therefore:</p> <pre><code>&gt;&gt;&gt; x = 'Ingl\xeas' &gt;&gt;&gt; print x.decode("cp1252") Inglês </code></pre> <p>Similarly,</p> <pre><...
0
2010-03-07T16:24:07Z
[ "python", "unicode", "utf-8" ]
How to encode HTML non-ASCII data to UTF-8 in Python
2,396,925
<p>I tried to do that, and I found this errors: </p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; x = 'Ingl\xeas' &gt;&gt;&gt; x 'Ingl\xeas' &gt;&gt;&gt; print x Ingl�s &gt;&gt;&gt; x.decode('utf8') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "...
2
2010-03-07T16:15:16Z
2,398,605
<p>Some observations:</p> <p>(1) <code>latin1</code> will decode ANY 8-bit byte without throwing an exception. Use <code>latin1</code> only when you have exhausted all other possibilities. Use <a href="http://chardet.feedparser.org/" rel="nofollow">chardet</a> to help deciding what a particular file or webpage or XML ...
0
2010-03-08T00:19:09Z
[ "python", "unicode", "utf-8" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
2,396,949
<p>Have two sets of templates, one for mobile, one for desktop. Store the filenames in a pair of dictionaries, and use the <code>User-agent</code> header to detect which set should be used. Also allow manual selection of which site to use via a session entry.</p>
9
2010-03-07T16:23:30Z
[ "python", "css", "django", "mobile", "templates" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
2,397,185
<p>If you place a class on your body (Django uses something similar to specify what column style to use), you could use the same templates but simply use different stylesheets. I'm not sure what main differences you are using separate templates for, but this might allow you to cut down on re-coding the templates multi...
1
2010-03-07T17:39:25Z
[ "python", "css", "django", "mobile", "templates" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
2,397,499
<p>There are different strategies.</p> <p>If you've a lot of views that renders to template files for the web version, and don't want to rewrite all views checking if the request is coming from a mobile user-agent, you'd be better writing a Middleware.</p> <p>A workflow could be like this:</p> <pre><code>def process...
0
2010-03-07T18:57:47Z
[ "python", "css", "django", "mobile", "templates" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
2,708,824
<p>The answer depends heavily on the type of your target audience. If you target for modern mobile browsers equivalents to their desktop counterparts (such as WebKit-based), all you need is specific stylesheet with appropriate <a href="http://www.w3.org/TR/css3-mediaqueries/#media1" rel="nofollow">media query</a> (you ...
0
2010-04-25T15:57:16Z
[ "python", "css", "django", "mobile", "templates" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
2,708,883
<p>You might want to check out <a href="http://code.google.com/p/mobilesniffer/" rel="nofollow">mobilesniffer</a> and <a href="http://code.google.com/p/django-bloom/" rel="nofollow">django-bloom</a> to see if they fit your purposes.</p>
0
2010-04-25T16:16:36Z
[ "python", "css", "django", "mobile", "templates" ]
In Django, what is the best way to manage both a mobile and desktop site?
2,396,938
<p>I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.</p> <p>Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.</p>
4
2010-03-07T16:18:48Z
4,152,520
<p>best practice: use <a href="http://code.google.com/p/minidetector/" rel="nofollow">minidetector</a> to add the extra info to the request, then use django's built in request context to pass it to your templates like so.</p> <pre><code>from django.shortcuts import render_to_response from django.template import Reques...
0
2010-11-11T08:26:24Z
[ "python", "css", "django", "mobile", "templates" ]
import strategy whithin django applications
2,397,055
<p>I would like to know what is the best import strategy whithin django reusable applications.</p> <p>Say I have an application called <code>usefulapp</code>. Inside my app, I will need to access, say, the models. Should I use an explicit import as:</p> <pre><code>import usefulapp.models </code></pre> <p>or simply, ...
3
2010-03-07T17:00:19Z
2,397,066
<p>The second approach assumes that <code>.</code> is in <code>sys.path</code> before any other directories that may contain a <code>models</code> module. There is no requirement that <code>.</code> be in it at all, so importing either via <a href="http://docs.python.org/tutorial/modules.html#intra-package-references" ...
3
2010-03-07T17:02:21Z
[ "python", "django", "import" ]
import strategy whithin django applications
2,397,055
<p>I would like to know what is the best import strategy whithin django reusable applications.</p> <p>Say I have an application called <code>usefulapp</code>. Inside my app, I will need to access, say, the models. Should I use an explicit import as:</p> <pre><code>import usefulapp.models </code></pre> <p>or simply, ...
3
2010-03-07T17:00:19Z
2,397,272
<p>I personally, try to keep the convention of always importing from the app.</p> <p>Don't import from the project, because project name can change; your app can be used in some other project (at least you are supposed to make apps like that).</p> <p>Don't import from <code>models</code> directly because, as Ignacio ...
3
2010-03-07T18:02:25Z
[ "python", "django", "import" ]
Is the content between anchor tags (a) in html seen as a branch in lxml?
2,397,064
<p>I am trying to get some content in html documents. Some of the documents have a table of contents that very nicely indicates where in the document the content I want to strip out is located. That is either the value or text_content of the tag are easily identifiable and point to what I need. For example I might h...
0
2010-03-07T17:01:58Z
2,397,189
<p>No, there is not a single branch between siblings. However, you can just iterate over their parent and extract (can be done in various ways, depending on how you already have handles for the anchor tags). Note the handling of text and tail to avoid losing data. Modifying <em>example_doc</em> to see the results ma...
1
2010-03-07T17:40:36Z
[ "python", "html", "lxml" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
2,397,146
<pre><code>[[foo for x in xrange(10)] for y in xrange(10)] </code></pre>
19
2010-03-07T17:29:40Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
2,397,147
<p>Usually when you want multidimensional arrays you don't want a list of lists, but rather a numpy array or possibly a dict.</p> <p>For example, with numpy you would do something like</p> <pre><code>import numpy a = numpy.empty((10, 10)) a.fill(foo) </code></pre>
17
2010-03-07T17:29:46Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
2,397,150
<p>You can use a <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a>:</p> <pre><code>x = [[foo for i in range(10)] for j in range(10)] # x is now a 10x10 array of 'foo' (which can depend on i and j if you want) </code></pre>
112
2010-03-07T17:30:38Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
2,397,192
<p>A pattern that often came up in Python was</p> <pre><code>bar = [] for item in some_iterable: bar.append(SOME EXPRESSION) </code></pre> <p>which helped motivate the introduction of list comprehensions, which convert that snippet to</p> <pre><code>bar = [SOME EXPRESSION for item in some_iterable] </code></pre>...
214
2010-03-07T17:43:11Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
2,398,187
<p><strong>This way is faster</strong> than the nested list comprehensions</p> <pre><code>[x[:] for x in [[foo] * 10] * 10] # for immutable foo! </code></pre> <p>Here are some python3 timings, for small and large lists</p> <pre><code>$python3 -m timeit '[x[:] for x in [[1] * 10] * 10]' 1000000 loops, best of 3: 1...
86
2010-03-07T22:10:37Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
9,120,051
<p>If it's a sparsely-populated array, you might be better off using a dictionary keyed with a tuple:</p> <pre><code>dict = {} key = (a,b) dict[key] = value ... </code></pre>
6
2012-02-02T21:11:54Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
10,901,316
<p>As @Arnab and @Mike pointed out, an array is not a list. Few differences are 1) arrays are fixed size during initialization 2) arrays normally support lesser operations than a list.</p> <p>Maybe an overkill in most cases, but here is a basic 2d array implementation that leverages hardware array implementation using...
1
2012-06-05T16:40:48Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
13,198,376
<p>This is the best I've found for teaching new programmers, and without using additional libraries. I'd like something better though.</p> <pre><code>def initialize_twodlist(value): list=[] for row in range(10): list.append([value]*10) return list </code></pre>
0
2012-11-02T15:33:00Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
20,526,889
<p>To initialize a two-dimensional array in Python:</p> <pre><code>a = [[x for x in range(columns)] for y in range(rows)] </code></pre>
22
2013-12-11T18:15:29Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
24,279,056
<p>use the simplest think to create this.</p> <pre><code>wtod_list = [] </code></pre> <p>and add the size:</p> <pre><code>wtod_list = [[0 for x in xrange(10))] for x in xrange(10)] </code></pre> <p>or if we want to declare the size firstly. we only use:</p> <pre><code> wtod_list = [[0 for x in xrange(10))] for x...
2
2014-06-18T07:05:09Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
25,601,284
<p>You can do just this:</p> <pre><code>[[element] * numcols] * numrows </code></pre> <p>For example:</p> <pre><code>&gt;&gt;&gt; [['a'] *3] * 2 [['a', 'a', 'a'], ['a', 'a', 'a']] </code></pre> <p>But this has a undesired side effect:</p> <pre><code>&gt;&gt;&gt; b = [['a']*3]*3 &gt;&gt;&gt; b [['a', 'a', 'a'], ['a...
8
2014-09-01T08:00:26Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
35,347,145
<p>Here is an easier way :</p> <pre><code>import numpy as np twoD = np.array([[]*m]*n) </code></pre> <p>For initializing all cells with any 'x' value use :</p> <pre><code>twoD = np.array([[x]*m]*n </code></pre>
1
2016-02-11T18:23:25Z
[ "python", "multidimensional-array" ]
How to initialize a two-dimensional array in Python?
2,397,141
<p>I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this:</p> <pre><code>def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo)...
140
2010-03-07T17:27:54Z
35,872,626
<pre><code>Matrix={} for i in range(0,3): for j in range(0,3): Matrix[i,j] = raw_input("Enter the matrix:") </code></pre>
0
2016-03-08T16:24:53Z
[ "python", "multidimensional-array" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,397,311
<p>Well, WebKit is open source so you could use its own parser (in the WebCore component), if any language is acceptable</p>
1
2010-03-07T18:12:48Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,397,326
<p>You can drive a browser of your choice with <a href="http://seleniumhq.org/projects/remote-control/" rel="nofollow">SeleniumRC</a>.</p>
2
2010-03-07T18:18:06Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,397,541
<p>You may want to take a look at Mechanize module:</p> <p><a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">http://wwwsearch.sourceforge.net/mechanize/</a></p>
2
2010-03-07T19:14:11Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,397,572
<p>Ian Bicking once wrote that surprisingly lxml could be better at parsing soups than BeautifulSoup: <a href="http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/" rel="nofollow">http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/</a> (Just mentioning fo...
1
2010-03-07T19:22:25Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,397,665
<p><a href="http://code.google.com/p/pywebkitgtk/source/checkout" rel="nofollow">pyWebKitGTK</a> looks like it might be of some help.</p> <p>Also here is a dude that had to do the same thing but get the export of the content after javascript ran, <a href="http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript...
4
2010-03-07T19:47:34Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,398,430
<p>Use <a href="http://www.crummy.com/software/BeautifulSoup/"><code>BeautifulSoup</code></a> as a tree builder for <a href="http://code.google.com/p/html5lib/"><code>html5lib</code></a>:</p> <pre><code>from html5lib import HTMLParser, treebuilders parser = HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup")...
10
2010-03-07T23:23:04Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,532,622
<p>have you tried <a href="http://scrapy.org/" rel="nofollow">scrapy</a>? </p> <blockquote> <p>Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to moni...
3
2010-03-28T10:56:28Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
2,665,291
<p>From the documentation it seems that <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html#Other%20Built-In%20Parsers" rel="nofollow">ICantBelieveItsBeautifulSoup</a> parser is what you want:</p> <blockquote> <p>ICantBelieveItsBeautifulSoup is also a subclass of BeautifulSoup. It has HTML h...
0
2010-04-19T05:14:03Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
6,059,633
<p>this one looks good to me I am using it myself : link [http://code.google.com/p/webscraping/]</p>
0
2011-05-19T13:42:24Z
[ "python", "firefox", "webkit", "web-scraping" ]
Web scraping with Python
2,397,295
<p>I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I ...
10
2010-03-07T18:07:24Z
11,443,366
<p>you can use lxml parser, in the beautifulsoup, and you use xpath to find the data in unformatted html page, you can copy the xpath when you inspect element using firebug.</p> <p>You can check out this tutorial: <a href="http://www.youtube.com/watch?v=PgWfF-Ut0zM" rel="nofollow">http://www.youtube.com/watch?v=PgWfF-...
1
2012-07-12T00:08:34Z
[ "python", "firefox", "webkit", "web-scraping" ]
Django: using variables as array indices?
2,397,324
<p>I am trying to create a template that will put items in a table.</p> <p><strong>Controller:</strong></p> <pre><code>items = Item.all().order('name').fetch(10) template_values = {'items': items, 'headers': ['Name', 'Price', 'Quantity']} render('Views/table.html', self, template_value...
0
2010-03-07T18:17:52Z
2,397,460
<p>I'm not sure if there is an existing template tag/filter that will accomplish what you want. You could look into writing a custom template tag or filter which accepts the items list and the current header and returns the value after the look-up. Have a look at <a href="http://docs.djangoproject.com/en/dev/howto/cu...
1
2010-03-07T18:48:40Z
[ "python", "django", "google-app-engine", "templating" ]
Django: using variables as array indices?
2,397,324
<p>I am trying to create a template that will put items in a table.</p> <p><strong>Controller:</strong></p> <pre><code>items = Item.all().order('name').fetch(10) template_values = {'items': items, 'headers': ['Name', 'Price', 'Quantity']} render('Views/table.html', self, template_value...
0
2010-03-07T18:17:52Z
2,397,487
<p>You could tweak the view to do:</p> <pre><code>items = Item.all().order('name').fetch(10) headers = ['Name', 'Price', 'Quantity'] viewitems = [[getattr(x, h) for h in headers] for x in items] template_values = {'items': viewitems, 'headers': headers} render('Views/table.html', self, template_val...
2
2010-03-07T18:55:24Z
[ "python", "django", "google-app-engine", "templating" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,397,470
<p>Give <a href="http://www.djangoproject.com/" rel="nofollow">Django</a> a look.</p> <p>From the website:</p> <p>"Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design."</p>
3
2010-03-07T18:52:03Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,397,473
<p>The new standard is <strong><a href="http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface" rel="nofollow">WSGI</a></strong> (Web Server Gateway Interface) and it is supported with <a href="http://code.google.com/p/modwsgi/" rel="nofollow">mod_wsgi</a> for Apache.<br></p> <blockquote> <p>The Web Server Gatewa...
13
2010-03-07T18:52:49Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,398,831
<p>Also take a look at <a href="http://pinaxproject.com/" rel="nofollow">pinax</a>. It is built on top of DJango and it will give you a good head start with your web site.</p>
-1
2010-03-08T01:37:36Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,398,928
<p>These kinds of questions usually result in every python web framework known to man being mentioned once or twice. As Desintegr pointed out, wsgi is the standard interface for python web applications. However, if it is a tad too low level for your tastes, I recommend <a href="http://pylonsproject.org/" rel="nofollow"...
7
2010-03-08T02:14:48Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,398,944
<p>You can try out <a href="http://spyce.sourceforge.net/docs/doc-intro_design.html" rel="nofollow">Spyce</a>. </p> <blockquote> <p>Spyce is a server-side language that supports elegant and efficient Python-based dynamic HTML generation. Spyce allows embedding Python in pages similar to how JSP embeds Java, but ...
-1
2010-03-08T02:21:02Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,402,353
<p>You can avoid both mod_python and mod_wsgi by running Tornado (tornadoweb.org). It's the server used by friendfeed. So, map a URL to a class, then create a class that defines get()/post() methods (or whatever HTTP methods you want to support), and "just run it" like a normal python app. Tornado is the web server, an...
0
2010-03-08T15:24:55Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
2,414,948
<p>Actually, it sounds like this may be an excellent use-case for <a href="http://github.com/defnull/bottle">bottle</a>: </p> <p>Example code that does something</p> <pre><code>from bottle import route, run @route('/') def index(): return 'Hello World!' run(host='localhost', port=8080) </code></pre>
5
2010-03-10T06:27:22Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
7,221,454
<p>A list of python web frameworks <a href="http://wiki.python.org/moin/WebFrameworks" rel="nofollow">is given here</a>. If you are looking for something lightweight, <a href="http://www.slideshare.net/r1chardj0n3s/web-microframework-battle" rel="nofollow">this slideshow</a> compares 10 micro frameworks and should be ...
3
2011-08-28T14:07:33Z
[ "python", "http", "jython" ]
Web Server frameworks for Python Web Apps
2,397,445
<p>I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It...
8
2010-03-07T18:46:20Z
28,221,434
<p>You can try Appier (<a href="https://github.com/hivesolutions/appier" rel="nofollow">https://github.com/hivesolutions/appier</a>). Here's a sample app:</p> <pre><code>import appier class HelloApp(appier.App): @appier.route("/", "GET") def hello(self): return "Hello World" HelloApp().serve() </cod...
0
2015-01-29T17:58:23Z
[ "python", "http", "jython" ]
How to have multiple python programs append rows to the same file?
2,397,576
<p>I've got multiple python processes (typically 1 per core) transforming large volumes of data that they are each reading from dedicated sources, and writing to a single output file that each opened in append mode.</p> <p>Is this a safe way for these programs to work?</p> <p>Because of the tight performance requirem...
4
2010-03-07T19:22:58Z
2,397,580
<p>Have you considered using the <A HREF="http://docs.python.org/library/multiprocessing.html" rel="nofollow">multiprocessing</A> module to coordinate between the running programs in a thread-like manner? See in particular the queue interface; you can place each completed work item on a queue when completed, and have a...
4
2010-03-07T19:25:08Z
[ "python", "concurrency" ]
How to have multiple python programs append rows to the same file?
2,397,576
<p>I've got multiple python processes (typically 1 per core) transforming large volumes of data that they are each reading from dedicated sources, and writing to a single output file that each opened in append mode.</p> <p>Is this a safe way for these programs to work?</p> <p>Because of the tight performance requirem...
4
2010-03-07T19:22:58Z
2,397,607
<p>Your procedure is "safe" in that no crashes will result, but data coming (with very unlucky timing) from different processes <em>could</em> get mixed up -- e.g., process 1 is appending a long string of <code>a</code>s, process 2 a long string of <code>b</code>, you <em>could</em> end up in the file with lots of <cod...
4
2010-03-07T19:32:03Z
[ "python", "concurrency" ]
What's the easiest way to convert a list of hex byte strings to a list of hex integers?
2,397,687
<p>I have a list of hex bytes strings like this</p> <p>['BB', 'A7', 'F6', '9E'] (as read from a text file)</p> <p>How do I convert that list to this format?</p> <p>[0xBB, 0xA7, 0xF6, 0x9E]</p>
6
2010-03-07T19:52:24Z
2,397,695
<pre><code>[int(x, 16) for x in L] </code></pre>
5
2010-03-07T19:54:00Z
[ "python" ]
What's the easiest way to convert a list of hex byte strings to a list of hex integers?
2,397,687
<p>I have a list of hex bytes strings like this</p> <p>['BB', 'A7', 'F6', '9E'] (as read from a text file)</p> <p>How do I convert that list to this format?</p> <p>[0xBB, 0xA7, 0xF6, 0x9E]</p>
6
2010-03-07T19:52:24Z
2,397,698
<p><code>[0xBB, 0xA7, 0xF6, 0x9E]</code> is the same as <code>[187, 167, 158]</code>. So there's no special 'hex integer' form or the like.</p> <p>But you can convert your hex strings to ints:</p> <pre><code>&gt;&gt;&gt; [int(x, 16) for x in ['BB', 'A7', 'F6', '9E']] [187, 167, 246, 158] </code></pre> <p>See also <a...
3
2010-03-07T19:54:29Z
[ "python" ]
What's the easiest way to convert a list of hex byte strings to a list of hex integers?
2,397,687
<p>I have a list of hex bytes strings like this</p> <p>['BB', 'A7', 'F6', '9E'] (as read from a text file)</p> <p>How do I convert that list to this format?</p> <p>[0xBB, 0xA7, 0xF6, 0x9E]</p>
6
2010-03-07T19:52:24Z
2,398,100
<p>Depending on the format in the text file, it may be easier to convert directly</p> <pre><code>&gt;&gt;&gt; b=bytearray('BBA7F69E'.decode('hex')) </code></pre> <p>or</p> <pre><code>&gt;&gt;&gt; b=bytearray('BB A7 F6 9E'.replace(' ','').decode('hex')) &gt;&gt;&gt; b bytearray(b'\xbb\xa7\xf6\x9e') &gt;&gt;&gt; b[0] ...
4
2010-03-07T21:45:05Z
[ "python" ]
How can I create an array/list of dictionaries in python?
2,397,754
<p>I have a dictionary as follows:</p> <pre><code>{'A':0,'C':0,'G':0,'T':0} </code></pre> <p>I want to create an array with many dictionaries in it, as follows:</p> <pre><code>[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] </code></pre> <p>This is my code:</p> <pre><code>weight...
23
2010-03-07T20:07:02Z
2,397,770
<p>Use </p> <pre><code>weightMatrix = [] for k in range(motifWidth): weightMatrix.append({'A':0,'C':0,'G':0,'T':0}) </code></pre>
3
2010-03-07T20:11:54Z
[ "python", "list", "arrays", "dictionary" ]
How can I create an array/list of dictionaries in python?
2,397,754
<p>I have a dictionary as follows:</p> <pre><code>{'A':0,'C':0,'G':0,'T':0} </code></pre> <p>I want to create an array with many dictionaries in it, as follows:</p> <pre><code>[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] </code></pre> <p>This is my code:</p> <pre><code>weight...
23
2010-03-07T20:07:02Z
2,397,774
<p>I assume that <code>motifWidth</code> contains an integer.</p> <p>In Python, lists do not change size unless you tell them to. Hence, Python throws an exception when you try to change an element that isn't there. I believe you want:</p> <pre><code>weightMatrix = [] for k in range(motifWidth): weightMatrix.ap...
3
2010-03-07T20:12:04Z
[ "python", "list", "arrays", "dictionary" ]
How can I create an array/list of dictionaries in python?
2,397,754
<p>I have a dictionary as follows:</p> <pre><code>{'A':0,'C':0,'G':0,'T':0} </code></pre> <p>I want to create an array with many dictionaries in it, as follows:</p> <pre><code>[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] </code></pre> <p>This is my code:</p> <pre><code>weight...
23
2010-03-07T20:07:02Z
2,397,775
<pre><code>weightMatrix = [{'A':0,'C':0,'G':0,'T':0} for k in range(motifWidth)] </code></pre>
22
2010-03-07T20:12:08Z
[ "python", "list", "arrays", "dictionary" ]
How can I create an array/list of dictionaries in python?
2,397,754
<p>I have a dictionary as follows:</p> <pre><code>{'A':0,'C':0,'G':0,'T':0} </code></pre> <p>I want to create an array with many dictionaries in it, as follows:</p> <pre><code>[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] </code></pre> <p>This is my code:</p> <pre><code>weight...
23
2010-03-07T20:07:02Z
13,550,417
<p>This is how I did it and it works:</p> <pre><code>dictlist = [dict() for x in range(n)] </code></pre> <p>It gives you a list of n empty dictionaries.</p>
20
2012-11-25T11:03:08Z
[ "python", "list", "arrays", "dictionary" ]
How can I create an array/list of dictionaries in python?
2,397,754
<p>I have a dictionary as follows:</p> <pre><code>{'A':0,'C':0,'G':0,'T':0} </code></pre> <p>I want to create an array with many dictionaries in it, as follows:</p> <pre><code>[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] </code></pre> <p>This is my code:</p> <pre><code>weight...
23
2010-03-07T20:07:02Z
20,301,532
<p>Dictionary:</p> <pre><code>dict = {'a':'a','b':'b','c':'c'} </code></pre> <p>array of dictionary</p> <pre><code>arr = (dict,dict,dict) arr ({'a': 'a', 'c': 'c', 'b': 'b'}, {'a': 'a', 'c': 'c', 'b': 'b'}, {'a': 'a', 'c': 'c', 'b': 'b'}) </code></pre>
-2
2013-11-30T14:42:57Z
[ "python", "list", "arrays", "dictionary" ]
excluding fields from json serialization in python using jsonpickle
2,397,757
<p>I am using jsonpickle to serialize an object to json. The object has certain fields that point to other objects. I'd like to selectively <em>not</em> include those in the serialization, so that the resulting json file is essentially pure human-readable text without any funny representations of objects. Is there a wa...
0
2010-03-07T20:07:57Z
2,398,474
<p>I think what you might be looking for is the unpicklable argument (see <a href="http://jsonpickle.github.com/api.html#jsonpickle-high-level-api" rel="nofollow">this doc</a> for details). In short, if this argument is set to <code>False</code>, jsonpickle will not output custom python classes to JSON. It should only ...
2
2010-03-07T23:36:22Z
[ "python", "json", "serialization", "jsonpickle" ]
How can I show figures separately in matplotlib?
2,397,791
<p>Say that I have two figures in matplotlib, with one plot per figure:</p> <pre><code>import matplotlib.pyplot as plt f1 = plt.figure() plt.plot(range(0,10)) f2 = plt.figure() plt.plot(range(10,20)) </code></pre> <p>Then I show both in one shot</p> <pre><code>plt.show() </code></pre> <p>Is there a way to show the...
28
2010-03-07T20:18:04Z
2,397,838
<p>Sure. Add an <code>Axes</code> using <code>add_subplot</code>. (Edited <code>import</code>.) (Edited <code>show</code>.)</p> <pre><code>import matplotlib.pyplot as plt f1 = plt.figure() f2 = plt.figure() ax1 = f1.add_subplot(111) ax1.plot(range(0,10)) ax2 = f2.add_subplot(111) ax2.plot(range(10,20)) plt.show() </co...
28
2010-03-07T20:28:54Z
[ "python", "matplotlib" ]
How can I show figures separately in matplotlib?
2,397,791
<p>Say that I have two figures in matplotlib, with one plot per figure:</p> <pre><code>import matplotlib.pyplot as plt f1 = plt.figure() plt.plot(range(0,10)) f2 = plt.figure() plt.plot(range(10,20)) </code></pre> <p>Then I show both in one shot</p> <pre><code>plt.show() </code></pre> <p>Is there a way to show the...
28
2010-03-07T20:18:04Z
2,397,900
<p>Perhaps you need to read about <a href="http://matplotlib.sourceforge.net/users/shell.html" rel="nofollow">interactive usage of Matplotlib</a>. However, if you are going to build an app, you should be using the <a href="http://matplotlib.sourceforge.net/users/artists.html" rel="nofollow">API</a> and embedding the fi...
2
2010-03-07T20:45:41Z
[ "python", "matplotlib" ]
How can I show figures separately in matplotlib?
2,397,791
<p>Say that I have two figures in matplotlib, with one plot per figure:</p> <pre><code>import matplotlib.pyplot as plt f1 = plt.figure() plt.plot(range(0,10)) f2 = plt.figure() plt.plot(range(10,20)) </code></pre> <p>Then I show both in one shot</p> <pre><code>plt.show() </code></pre> <p>Is there a way to show the...
28
2010-03-07T20:18:04Z
2,399,978
<p>With Matplotlib prior to version 1.0.1, <code>show()</code> <a href="http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show">should only be called once per program</a>, even if it seems to work within certain environments (some backends, on some platforms, etc.).</p> <p>The relevant drawing function is actua...
19
2010-03-08T08:07:34Z
[ "python", "matplotlib" ]
Using PyQt4 - QTableView with SQLAlchemy using QSqlTableModel (or not)
2,397,987
<p>i'm starting to learn Qt for python and as i was wondering after reading this post : <a href="http://stackoverflow.com/questions/1659756/qt-pyqt-qtableview-not-populating-when-changing-databases">qt - pyqt QTableView not populating when changing databases.</a> if there was a way to use SQLAlchemy sessions instead of...
4
2010-03-07T21:11:04Z
4,165,827
<p>Take a look at <a href="http://www.python-camelot.com/" rel="nofollow">Camelot</a> . It does much more:)</p> <p>I happily found it when the frustration and angst, generated by Q*View and Q*Model experience forced me to start implementing my own ones on the basis of SqlAlchemy. And it was half implemented, when I fo...
2
2010-11-12T14:53:41Z
[ "python", "sqlite", "sqlalchemy", "pyqt4" ]
How to use HTTP method DELETE on Google App Engine?
2,398,012
<p>I can use this verb in the Python Windows SDK. But not in production. Why? What am I doing wrong?</p> <p>The error message includes (only seen via firebug or fiddler)</p> <blockquote> <p>Malformed request</p> </blockquote> <p>or something like that</p> <p>My code looks like:</p> <pre><code>from google.appengi...
7
2010-03-07T21:21:50Z
2,969,890
<p>Your handler looks OK, are you sure you're sending the request correctly? Using jQuery, this works for me (both using dev_appserver and google app engine production):</p> <pre><code>$('#delete-button').click(function() { $.ajax({ 'type': 'DELETE', 'url': '/some/url/that/handles/delete' }) })...
3
2010-06-03T21:24:14Z
[ "python", "google-app-engine", "http", "http-delete" ]
saving and loading objects from file using jsonpickle
2,398,078
<p>I have the following simple methods for writing a python object to a file using jsonpickle:</p> <pre><code>def json_serialize(obj, filename, use_jsonpickle=True): f = open(filename, 'w') if use_jsonpickle: import jsonpickle json_obj = jsonpickle.encode(obj) f.write(json_obj) else...
11
2010-03-07T21:39:11Z
2,398,228
<p>Make sure that <code>use_jsonpickle == True</code> in <code>json_load_file()</code>. It seems that you serialize using <code>jsonpickle</code> and load using <code>json</code>.</p> <pre><code>&gt;&gt;&gt; import jsonpickle &gt;&gt;&gt; class A(object): ... def __init__(self, name): ... self.name = name ......
2
2010-03-07T22:22:50Z
[ "python", "json", "serialization", "jsonpickle" ]
saving and loading objects from file using jsonpickle
2,398,078
<p>I have the following simple methods for writing a python object to a file using jsonpickle:</p> <pre><code>def json_serialize(obj, filename, use_jsonpickle=True): f = open(filename, 'w') if use_jsonpickle: import jsonpickle json_obj = jsonpickle.encode(obj) f.write(json_obj) else...
11
2010-03-07T21:39:11Z
2,405,814
<p>The correct answer was that I was not inheriting from <code>object</code>. Without inheriting from <code>object</code>, jsonpickle cannot correctly decode classes that take one or more arguments in the constructor, it seems. I am by no means an expert but making it <code>Foo(object):</code> rather than <code>Foo:</...
17
2010-03-09T00:27:23Z
[ "python", "json", "serialization", "jsonpickle" ]
using os.system in python to run program with parameters
2,398,095
<p>How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn</p> <p>I'm trying the following at the minute without success</p> <pre><code>vpnfile2 = '/etc/init.d/openvpn' cfgFile = 'client.ovpn' os.system('sudo \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"') </code><...
2
2010-03-07T21:43:56Z
2,398,125
<p>use the <code>subprocess</code> module</p> <pre><code>import subprocess subprocess.call(['sudo', vpnFile2, '--cd', vpnpath, '--config', cfgFile]) </code></pre>
8
2010-03-07T21:51:48Z
[ "python" ]
using os.system in python to run program with parameters
2,398,095
<p>How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn</p> <p>I'm trying the following at the minute without success</p> <pre><code>vpnfile2 = '/etc/init.d/openvpn' cfgFile = 'client.ovpn' os.system('sudo \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"') </code><...
2
2010-03-07T21:43:56Z
8,724,072
<p>This question has been posted awhile ago, but if someone comes across this (like me), there is another way to get privileges using the os.system method ..</p> <p>It will only work if you are running this in a GUI environment. You can simply try to call it with 'gksu' or ('kdesu' or 'kdesudo'), it would look like th...
1
2012-01-04T08:40:25Z
[ "python" ]
using os.system in python to run program with parameters
2,398,095
<p>How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn</p> <p>I'm trying the following at the minute without success</p> <pre><code>vpnfile2 = '/etc/init.d/openvpn' cfgFile = 'client.ovpn' os.system('sudo \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"') </code><...
2
2010-03-07T21:43:56Z
12,649,151
<p>If there is some reason you want to use os.system, instead of subprocess, I usually wash it through bash, so <code>os.system('''sudo bash -c "command to run"''')</code> (Or sh or whatever shell you have). It processes the arguments better in many cases.</p>
0
2012-09-29T01:28:16Z
[ "python" ]
Test if value is Decimal
2,398,141
<p>In some Python (v3) code I am creating lists of <a href="http://docs.python.org/3.1/library/decimal.html#module-decimal" rel="nofollow">Decimals</a> from user input, like this:</p> <pre><code>input = [] # later populated with strings by user with values like '1.45984000E+001' decimals = [Decimal(c) for c in input] ...
0
2010-03-07T21:56:38Z
2,398,154
<p>Don't. Catch the exception thrown by the constructor. If that means turning the list comprehension into a for loop then so be it.</p>
0
2010-03-07T21:59:57Z
[ "python", "decimal" ]
Test if value is Decimal
2,398,141
<p>In some Python (v3) code I am creating lists of <a href="http://docs.python.org/3.1/library/decimal.html#module-decimal" rel="nofollow">Decimals</a> from user input, like this:</p> <pre><code>input = [] # later populated with strings by user with values like '1.45984000E+001' decimals = [Decimal(c) for c in input] ...
0
2010-03-07T21:56:38Z
2,398,181
<h3>Catch exception</h3> <pre><code>decimals = [] for s in input: try: decimals.append(Decimal(s)) except InvalidOperation: pass </code></pre> <h3>Use helper function</h3> <pre><code>from itertools import imap def parse_decimal(s): try: return Decimal(s) except InvalidOperation: retu...
3
2010-03-07T22:08:48Z
[ "python", "decimal" ]
Python - BaseHTTPServer.HTTPServer Concurrency & Threading
2,398,144
<p>Is there a way to make BaseHTTPServer.HTTPServer be multi-threaded like SocketServer.ThreadingTCPServer?</p>
13
2010-03-07T21:56:54Z
2,398,283
<p>You can simply use the threading mixin using both of those classes to make it multithread :)</p> <p>It won't help you much in performance though, but it's atleast multithreaded.</p> <pre><code>from SocketServer import ThreadingMixIn from BaseHTTPServer import HTTPServer class MultiThreadedHTTPServer(ThreadingMixI...
19
2010-03-07T22:36:52Z
[ "python", "multithreading", "httpserver", "basehttpserver", "socketserver" ]
How to check if datetime is older than 20 seconds
2,398,205
<p>This is my first time here so I hope I post this question at the right place. :)</p> <p>I need to build flood control for my script but I'm not good at all this datetime to time conversions with UTC and stuff. I hope you can help me out. I'm using the Google App Engine with Python. I've got a datetimeproperty at th...
15
2010-03-07T22:14:22Z
2,398,242
<p>You can use the datetime.timedelta datatype, like this:</p> <pre><code>import datetime lastplus = q.get() if lastplus.date &lt; datetime.datetime.now()-datetime.timedelta(seconds=20): print "Go" </code></pre> <p>Read more about it here: <a href="http://docs.python.org/library/datetime.html">http://docs.python....
28
2010-03-07T22:25:24Z
[ "python", "google-app-engine", "datetime", "time" ]
How to check if datetime is older than 20 seconds
2,398,205
<p>This is my first time here so I hope I post this question at the right place. :)</p> <p>I need to build flood control for my script but I'm not good at all this datetime to time conversions with UTC and stuff. I hope you can help me out. I'm using the Google App Engine with Python. I've got a datetimeproperty at th...
15
2010-03-07T22:14:22Z
2,398,243
<p>Try this:</p> <pre><code>from datetime import timedelta, datetime if lastplus.date &lt; datetime.utcnow() + timedelta(seconds = -20): print "fee fie fo foo!" </code></pre>
1
2010-03-07T22:25:32Z
[ "python", "google-app-engine", "datetime", "time" ]
Rebuilding website from Django 0.96 to Django 1.2
2,398,299
<p>I've got a website done in <strong>Django 0.96</strong> (done in 2007), and now we are thinking about rebuilding it (not just migrating) for <strong>Django 1.2</strong> . </p> <p>Can anyone point me to the new (and worth the while) widgets, plugins and other stuff for <strong>Django 1.2</strong> (released in april ...
4
2010-03-07T22:40:10Z
2,398,314
<p>Of course, there's the <a href="http://docs.djangoproject.com/en/dev/releases/" rel="nofollow">release notes</a>, but the rest is just stuff you... find.</p> <p><a href="http://south.aeracode.org/" rel="nofollow">South</a> is for schema migration, not debugging.</p>
0
2010-03-07T22:47:38Z
[ "python", "django", "website" ]
Rebuilding website from Django 0.96 to Django 1.2
2,398,299
<p>I've got a website done in <strong>Django 0.96</strong> (done in 2007), and now we are thinking about rebuilding it (not just migrating) for <strong>Django 1.2</strong> . </p> <p>Can anyone point me to the new (and worth the while) widgets, plugins and other stuff for <strong>Django 1.2</strong> (released in april ...
4
2010-03-07T22:40:10Z
2,398,377
<p>You probably heard about the <a href="http://github.com/robhudson/django-debug-toolbar" rel="nofollow">django-debug-toolbar</a></p>
2
2010-03-07T23:06:49Z
[ "python", "django", "website" ]
Rebuilding website from Django 0.96 to Django 1.2
2,398,299
<p>I've got a website done in <strong>Django 0.96</strong> (done in 2007), and now we are thinking about rebuilding it (not just migrating) for <strong>Django 1.2</strong> . </p> <p>Can anyone point me to the new (and worth the while) widgets, plugins and other stuff for <strong>Django 1.2</strong> (released in april ...
4
2010-03-07T22:40:10Z
2,398,409
<p>The Django API is amazingly stable so you may not have to rewrite it at all (unless you really want to).</p> <p>I have a site I did in 2007 using 0.97-pre -- at least I think that's what they called it, it was trunk 6688. Anyway, I have ported the site twice, once to 1.0 and then to 1.1.1. The only "major" thing we...
5
2010-03-07T23:17:59Z
[ "python", "django", "website" ]
Hexadecimals in python
2,398,468
<p>I don't know python and I'm porting a library to C#, I've encountered the following lines of code that is used in some I/O operation but I'm not sure what it is, my guess is that it's a hexadecimal but I don't know why it's inside a string, neither what the backslashes do?</p> <pre><code> sep1 = '\x04H\xfe\x13' ...
1
2010-03-07T23:33:06Z
2,398,477
<p>They're escape sequences. In Python, <code>\xNN</code> within a (non-raw) string is treated as the character <code>0xNN</code>.</p>
5
2010-03-07T23:36:46Z
[ "python" ]
Hexadecimals in python
2,398,468
<p>I don't know python and I'm porting a library to C#, I've encountered the following lines of code that is used in some I/O operation but I'm not sure what it is, my guess is that it's a hexadecimal but I don't know why it's inside a string, neither what the backslashes do?</p> <pre><code> sep1 = '\x04H\xfe\x13' ...
1
2010-03-07T23:33:06Z
2,398,483
<p>The backslashes are escape characters. They allow you to insert special characters (IE a quotation mark) inside a string. \xNN is hexadecimal, like you say.</p> <p>It looks like they're using a string in place of an array to me. I'm not really sure why someone would do this... but oh well</p> <p>I'm not familiar w...
0
2010-03-07T23:38:27Z
[ "python" ]
Hexadecimals in python
2,398,468
<p>I don't know python and I'm porting a library to C#, I've encountered the following lines of code that is used in some I/O operation but I'm not sure what it is, my guess is that it's a hexadecimal but I don't know why it's inside a string, neither what the backslashes do?</p> <pre><code> sep1 = '\x04H\xfe\x13' ...
1
2010-03-07T23:33:06Z
2,398,582
<p>The same escape sequence works in most languages. You'll just have to use the proper string quotes, in C# that's the double quote (").</p>
0
2010-03-08T00:12:06Z
[ "python" ]
Hexadecimals in python
2,398,468
<p>I don't know python and I'm porting a library to C#, I've encountered the following lines of code that is used in some I/O operation but I'm not sure what it is, my guess is that it's a hexadecimal but I don't know why it's inside a string, neither what the backslashes do?</p> <pre><code> sep1 = '\x04H\xfe\x13' ...
1
2010-03-07T23:33:06Z
2,399,787
<p>As others have stated it's escaped characters that can't really be represented as US ASCII characters. Why do you do this then? </p> <p>The IO device needs a sequence of bytes with these special hexadecimal values for some reason. </p> <p>Some languages does not support a native type for bytes, and then we have a ...
0
2010-03-08T07:05:12Z
[ "python" ]
How to organize Python modules for PyPI to support 2.x and 3.x
2,398,626
<p>I have a Python module that I would like to upload to PyPI. So far, it is working for Python 2.x. It shouldn't be too hard to write a version for 3.x now.</p> <p>But, after following guidelines for making modules in these places:</p> <ul> <li><a href="http://docs.python.org/distutils/index.html">Distributing Pytho...
14
2010-03-08T00:25:57Z
2,398,718
<p>The simplest solution is to use a single source distribution.</p>
2
2010-03-08T00:58:43Z
[ "python", "python-3.x", "software-distribution", "python-2.x" ]
How to organize Python modules for PyPI to support 2.x and 3.x
2,398,626
<p>I have a Python module that I would like to upload to PyPI. So far, it is working for Python 2.x. It shouldn't be too hard to write a version for 3.x now.</p> <p>But, after following guidelines for making modules in these places:</p> <ul> <li><a href="http://docs.python.org/distutils/index.html">Distributing Pytho...
14
2010-03-08T00:25:57Z
2,729,050
<p>I found that <a href="http://code.google.com/p/httplib2/source/browse/setup.py"><code>setup.py</code> for <code>httplib2</code></a> seems to have an elegant way to support Python 2.x and 3.x. So I decided to copy that method.</p> <p>The task is to craft a single <code>setup.py</code> for the package distribution th...
14
2010-04-28T11:40:05Z
[ "python", "python-3.x", "software-distribution", "python-2.x" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
2,399,145
<p>You could use <a href="http://docs.python.org/3.1/library/threading.html?highlight=timer#timer-objects"><code>threading.Timer</code></a>, but that also schedules a one-off event, similarly to the <code>.enter</code> method of scheduler objects.</p> <p>The normal pattern (in any language) to transform a one-off sche...
22
2010-03-08T03:35:34Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
13,151,104
<p>My humble take on the subject:</p> <pre><code>from threading import Timer class RepeatedTimer(object): def __init__(self, interval, function, *args, **kwargs): self._timer = None self.function = function self.interval = interval self.args = args self.kwargs...
12
2012-10-31T04:03:59Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
13,312,737
<p>You could use the <a href="http://packages.python.org/APScheduler/" rel="nofollow">Advanced Python Scheduler</a>. It even has a cron-like interface.</p>
4
2012-11-09T17:11:54Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
16,786,718
<p>You could use <a href="https://github.com/dbader/schedule">schedule</a>. It works on Python 2.7 and 3.3 and is rather lightweight:</p> <pre><code>import schedule def job(): print("I'm working...") schedule.every(10).minutes.do(job) schedule.every().hour.do(job) schedule.every().day.at("10:30").do(job) while 1...
6
2013-05-28T07:54:29Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
18,906,292
<p>Based on MestreLion answer, it solve a little problem with multithreading: </p> <pre><code>from threading import Timer, Lock class Periodic(object): """ A periodic task running in threading.Timers """ def __init__(self, interval, function, *args, **kwargs): self._lock = Lock() sel...
4
2013-09-19T22:59:45Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]
Schedule a repeating event in Python 3
2,398,661
<p>I'm trying to schedule a repeating event to run every minute in Python 3.</p> <p>I've seen class <code>sched.scheduler</code> but I'm wondering if there's another way to do it. I've heard mentions I could use multiple threads for this, which I wouldn't mind doing.</p> <p>I'm basically requesting some JSON and then...
17
2010-03-08T00:35:52Z
18,942,977
<p>Use <a href="http://celeryproject.org/" rel="nofollow">Celery</a>.</p> <pre><code>from celery.task import PeriodicTask from datetime import timedelta class ProcessClicksTask(PeriodicTask): run_every = timedelta(minutes=30) def run(self, **kwargs): #do something </code></pre>
3
2013-09-22T10:45:43Z
[ "python", "python-3.x", "scheduled-tasks", "timing" ]