title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
How to design an application in a modular way?
1,865,727
<p><strong>I am looking for pointers, suggestions, links, warnings, ideas and even anecdotical accounts about <em>"how to design an application in a modular way"</em>. I am going to use python for this project, but advice does not need to necessarily refer to this language, although I am only willing to implement a des...
22
2009-12-08T09:17:22Z
1,865,766
<p>Well, probably the first place to start is to sit down and figure out what the plug-in might need to fulfill its purpose.</p> <p>You'd want to consider two main aspects in your design.</p> <ul> <li>How will your framework pass requests / receive responses from the plug-in?</li> <li>What helper classes or modules m...
2
2009-12-08T09:25:23Z
[ "python", "design-patterns", "oop", "modularity", "software-design" ]
How to design an application in a modular way?
1,865,727
<p><strong>I am looking for pointers, suggestions, links, warnings, ideas and even anecdotical accounts about <em>"how to design an application in a modular way"</em>. I am going to use python for this project, but advice does not need to necessarily refer to this language, although I am only willing to implement a des...
22
2009-12-08T09:17:22Z
1,865,845
<ul> <li>design the api for your app, carefully (<a href="http://www.youtube.com/watch?v=aAb7hSCtvGw" rel="nofollow">How To Design A Good API and Why it Matters</a>)</li> <li>make everything, which could be used independently a module, then group and build larger parts out of the simple parts (KISS)</li> <li>don't repe...
1
2009-12-08T09:42:57Z
[ "python", "design-patterns", "oop", "modularity", "software-design" ]
How to design an application in a modular way?
1,865,727
<p><strong>I am looking for pointers, suggestions, links, warnings, ideas and even anecdotical accounts about <em>"how to design an application in a modular way"</em>. I am going to use python for this project, but advice does not need to necessarily refer to this language, although I am only willing to implement a des...
22
2009-12-08T09:17:22Z
1,866,360
<p>As you will deliver some basic functionality with your app, make sure that you code the part that should be extendable/replaceable already as a plugin by yourself. Then you'll best get a feeling about how your API should look like.</p> <p>And to prove that the API is good, you should write a second and third plugin...
8
2009-12-08T11:25:18Z
[ "python", "design-patterns", "oop", "modularity", "software-design" ]
How to design an application in a modular way?
1,865,727
<p><strong>I am looking for pointers, suggestions, links, warnings, ideas and even anecdotical accounts about <em>"how to design an application in a modular way"</em>. I am going to use python for this project, but advice does not need to necessarily refer to this language, although I am only willing to implement a des...
22
2009-12-08T09:17:22Z
1,866,505
<p>Try to keep things loosely coupled, and use interfaces liberally to help.</p> <p>I'd start the design with the <em>Separation of Concerns</em>. The major architectural layers are:</p> <ul> <li>Problem Domain (aka. Engine, Back-end): the domain classes, which do all the actual work, have domain knowledge implement...
12
2009-12-08T11:57:54Z
[ "python", "design-patterns", "oop", "modularity", "software-design" ]
How to design an application in a modular way?
1,865,727
<p><strong>I am looking for pointers, suggestions, links, warnings, ideas and even anecdotical accounts about <em>"how to design an application in a modular way"</em>. I am going to use python for this project, but advice does not need to necessarily refer to this language, although I am only willing to implement a des...
22
2009-12-08T09:17:22Z
1,866,551
<p>Look into the listener-subscriber pattern. Sooner or later, your app will be complex enough that you need to implement callbacks. When you hit that limit, use listener-subscriber (there's an implementation in wxPython).</p> <p>For example, several modules will want to watch for new data from a number of feeds. Modu...
1
2009-12-08T12:06:25Z
[ "python", "design-patterns", "oop", "modularity", "software-design" ]
Which Python language rule allows the descriptor to be found first?
1,865,902
<p>I bumped into the following last night and I'm still at a loss as to explain it:</p> <pre><code>class Foo(object): @property def dave(self): vars(self)['dave'] = 1 return 2 f = Foo() print f.dave print f.dave </code></pre> <p>Running this code produces:</p> <pre><code>2 2 </code></pre> <...
2
2009-12-08T09:52:51Z
1,866,039
<blockquote> <p>My understanding of attribute access is that the instance dictionary is checked before the class dictionary, and the dictionary of any bases</p> </blockquote> <p>Data descriptors are an exception:</p> <blockquote> <p>For instance bindings, the precedence of descriptor invocation depends on the whi...
5
2009-12-08T10:20:35Z
[ "python", "rules" ]
Is there any python package that could configure IP address of network interface?
1,865,920
<p>I am writing a server application which allow remote client to show/add/change/delete IP addresses of network interfaces of the machine where the host is running.</p> <p>The OS is Linux(CentOS 5.2), so I could do that by simply parse and edit configure file. But is there any package that could simplify the job?</p...
3
2009-12-08T09:57:11Z
1,870,624
<p>It looks like the open source project <a href="http://code.google.com/p/confparse/" rel="nofollow">confparse</a> should be able to easily do what you're looking for. In fact, one of their examples is parsing and modifying <code>/etc/sysconfig/network-scripts/ifcfg-eth0</code> with ease.</p> <p>If you find this isn...
2
2009-12-08T23:29:25Z
[ "python", "linux" ]
How do I mimic browser back arrow in my application page?
1,865,981
<p>request.path gives me the clicked url. I need a return link on that page which on clicking, should return to Referrer page, just as it would happen in case of browser back arrow . I do not maintain any session and do not want to hardcode the referrer page url. </p>
1
2009-12-08T10:08:34Z
1,866,008
<p>history.go(-1)</p> <p><a href="http://www.devguru.com/Technologies/Ecmascript/Quickref/history%5Fgo.html" rel="nofollow">http://www.devguru.com/Technologies/Ecmascript/Quickref/history%5Fgo.html</a></p>
5
2009-12-08T10:14:19Z
[ "python", "html", "django" ]
How do I mimic browser back arrow in my application page?
1,865,981
<p>request.path gives me the clicked url. I need a return link on that page which on clicking, should return to Referrer page, just as it would happen in case of browser back arrow . I do not maintain any session and do not want to hardcode the referrer page url. </p>
1
2009-12-08T10:08:34Z
1,866,010
<p>How about trying Javascript instead?</p> <pre><code> &lt;a href="#" onclick="history.go(-1);"&gt;Go back&lt;/a&gt; </code></pre>
1
2009-12-08T10:14:37Z
[ "python", "html", "django" ]
How do I mimic browser back arrow in my application page?
1,865,981
<p>request.path gives me the clicked url. I need a return link on that page which on clicking, should return to Referrer page, just as it would happen in case of browser back arrow . I do not maintain any session and do not want to hardcode the referrer page url. </p>
1
2009-12-08T10:08:34Z
1,866,015
<p>You can do so using Javascript:</p> <pre><code>&lt;span onclick='javascript: history.back()'&gt;Previous page&lt;/span&gt; </code></pre>
0
2009-12-08T10:16:16Z
[ "python", "html", "django" ]
How to create a .py file for Google App Engine?
1,866,147
<p>I am just at the very start of what I think is gonna be a long journey exploring the world of applications in <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a> using <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a>. </p> <p>I have ...
0
2009-12-08T10:41:45Z
1,866,155
<blockquote> <p>I am kind of puzzled here: How do I create a .py file? Is it like I need to create a notepad file (.txt) and name it helloworld.py?</p> </blockquote> <p>I'm not on windows, but thats how it works on other operating systems: Create a file with an editor, then <strong>save as</strong> ...</p> <p>ps. a...
1
2009-12-08T10:44:13Z
[ "python", "google-app-engine", "file", "makefile" ]
How to create a .py file for Google App Engine?
1,866,147
<p>I am just at the very start of what I think is gonna be a long journey exploring the world of applications in <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a> using <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a>. </p> <p>I have ...
0
2009-12-08T10:41:45Z
1,866,158
<p>A .py file is a text file containing Python syntax. Use your favourite programming editor, or even NotePad.</p> <p>Regarding your second problem, there's an option in Windows Explorer about hiding file extensions. Make sure it isn't checked -- you might well actually have renamed your file helloworld.py.txt but o...
2
2009-12-08T10:45:01Z
[ "python", "google-app-engine", "file", "makefile" ]
How to create a .py file for Google App Engine?
1,866,147
<p>I am just at the very start of what I think is gonna be a long journey exploring the world of applications in <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a> using <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a>. </p> <p>I have ...
0
2009-12-08T10:41:45Z
1,866,185
<p>You have to be aware that the ending signifies <strong>recognition</strong> of files, not <strong>content</strong>. Name a file <code>.py</code> simply hints to the user (and the GUI) that it most likely is a python file.</p> <p>That being said, python files are merely text files. You can simply create a text file ...
1
2009-12-08T10:52:07Z
[ "python", "google-app-engine", "file", "makefile" ]
How to create a .py file for Google App Engine?
1,866,147
<p>I am just at the very start of what I think is gonna be a long journey exploring the world of applications in <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a> using <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a>. </p> <p>I have ...
0
2009-12-08T10:41:45Z
1,866,186
<p>You will need a better editor than Notepad. With Notepad, use Save As..., and type "helloworld.py" in the dialog, <strong>including</strong> quotes so that the file extension is .py instead of .txt</p>
1
2009-12-08T10:52:08Z
[ "python", "google-app-engine", "file", "makefile" ]
How to create a .py file for Google App Engine?
1,866,147
<p>I am just at the very start of what I think is gonna be a long journey exploring the world of applications in <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a> using <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a>. </p> <p>I have ...
0
2009-12-08T10:41:45Z
1,866,310
<p>When you downloaded and installed Python, you also installed <a href="http://en.wikipedia.org/wiki/IDLE%5F%28Python%29" rel="nofollow">IDLE</a>. You can use this to easily write, run, debug and save .py files with syntax highlighting. To get started, just open IDLE, and select <code>File -&gt; New Window</code>.</...
3
2009-12-08T11:13:35Z
[ "python", "google-app-engine", "file", "makefile" ]
Highlight a row in wxGrid (with wxPython) when a cell in that row changes programatically
1,866,321
<p>How do I highlight a row after an update of the underlying PyGridTableBase? I can't seem to get my head around it.</p> <p>I am able to create alternate row highlighting lines when the table is first drawn:</p> <pre><code>### Style the table a little. def GetAttr(self, row, col, prop): attr = gridlib.GridCe...
3
2009-12-08T11:16:17Z
1,868,077
<p>You are doing the right thing, the only problem that comes to mind is that you perhaps didn't manually refresh the grid after the <code>GridTableBase</code> update. Here is a small working example that will hopefully help you out.</p> <pre><code>import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols...
1
2009-12-08T16:22:46Z
[ "python", "wxpython" ]
How should I organise my functions with pyparsing?
1,866,329
<p>I am parsing a file with python and <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a> (it's the report file for <a href="http://www.power.uwaterloo.ca/~fmilano/psat.htm" rel="nofollow">PSAT</a> in Matlab but that isn't important). <a href="http://github.com/kerspoon/laos/blob/master/psat%5Frepo...
1
2009-12-08T11:17:38Z
1,867,317
<p>I could go either way on using a single big method to create your parser vs. taking it in steps the way you have it now.</p> <p>I can see that you have defined some useful helper utilities, such as slit ("suppress Literal", I presume), stringtolits, and decimaltable. This looks good to me.</p> <p>I like that you ...
2
2009-12-08T14:29:02Z
[ "python", "refactoring", "coding-style", "pyparsing" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,349
<p>There is also the method of doing it using filter</p> <pre><code> list = filter(lambda x: 'X' not in x and 'N' not in x, list) </code></pre>
2
2009-12-08T11:22:46Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,363
<p>For basis purpose</p> <pre><code>&gt;&gt;&gt; [x for x in ['AAT','XAC','ANT','TTA'] if "X" not in x and "N" not in x] ['AAT', 'TTA'] </code></pre> <p>But if you have huge amount of data, I suggest you to use dict or set</p> <p>And If you have many characters other than X and N, you may do like this</p> <pre><cod...
5
2009-12-08T11:25:34Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,378
<ol> <li><code>filter(lambda x: 'N' not in x or 'X' not in x, your_list)</code></li> <li><code>your_list = [x for x in your_list if 'N' not in x or 'X' not in x]</code></li> </ol>
2
2009-12-08T11:29:09Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,417
<p>Another not fastest way but I think it reads nicely</p> <pre><code>&gt;&gt;&gt; [x for x in ['AAT','XAC','ANT','TTA'] if not any(y in x for y in "XN")] ['AAT', 'TTA'] &gt;&gt;&gt; [x for x in ['AAT','XAC','ANT','TTA'] if not set("XN")&amp;set(x)] ['AAT', 'TTA'] </code></pre> <p>This way will be faster for long co...
4
2009-12-08T11:39:16Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,595
<p>As S.Mark requested here is my version. It's probably slower but does make it easier to change what gets removed.</p> <pre><code>def filter_genome(genome, killlist = set("X N".split()): return [codon for codon in genome if 0 == len(set(codon) | killlist)] </code></pre>
0
2009-12-08T12:16:29Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,866,977
<p>Any reason for duplicating the entire list? How about:</p> <pre><code>&gt;&gt;&gt; def pred(item, haystack="XN"): ... return any(needle in item for needle in haystack) ... &gt;&gt;&gt; lst = ['AAT', 'XAC', 'ANT', 'TTA'] &gt;&gt;&gt; idx = 0 &gt;&gt;&gt; while idx &lt; len(lst): ... if pred(lst[idx]): ... ...
0
2009-12-08T13:28:15Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,867,040
<p>I like gnibbler’s memoization approach a lot. Either method using memoization should be identically fast in the big picture on large data sets, as the memo dictionary should quickly be filled and the actual test should be rarely performed. With this in mind, we should be able to improve the performance even more f...
1
2009-12-08T13:40:20Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,867,131
<p>It is (asympotically) faster to use a regular expression than searching many times in the same string for a certain character: in fact, with a regular expression the sequences is only be read at most once (instead of twice when the letters are not found, in gnibbler's original answer, for instance). With gnibbler's...
0
2009-12-08T13:57:05Z
[ "python", "string" ]
Removing an element from a list based on a predicate
1,866,343
<p>I want to remove an element from list, such that the element contains <code>'X'</code> or <code>'N'</code>. I have to apply for a large genome. Here is an example:</p> <p>input:</p> <pre><code>codon=['AAT','XAC','ANT','TTA'] </code></pre> <p>expected output: </p> <pre><code>codon=['AAT','TTA'] </code></pre>
4
2009-12-08T11:21:08Z
1,868,341
<p>If you're dealing with extremely large lists, you want to use methods that don't involve traversing the entire list any more than you absolutely need to. </p> <p>Your best bet is likely to be creating a filter function, and using <code>itertools.ifilter</code>, e.g.:</p> <pre><code>new_seq = itertools.ifilter(lam...
1
2009-12-08T17:00:39Z
[ "python", "string" ]
Syntax Error exec-ing python code from database
1,866,439
<p>I'm loading some python code from a database (it's doing dynamic mapping of values that I can change at runtime without a code redeploy).</p> <p>In my code, I'm doing this to execute the database code:</p> <pre><code>if lMapping: print lMapping exec lMapping lValue = mapping(lValue, lCsvRow) </code></pre> <...
0
2009-12-08T11:43:49Z
1,866,498
<p>Do you BLOB or other <em>binary</em> type column to store code? Otherwise database might change line endings and exec will break with <code>SyntaxError</code>:</p> <pre><code>&gt;&gt;&gt; s='''\ ... print 'ok' ... ''' &gt;&gt;&gt; s "print 'ok'\n" &gt;&gt;&gt; exec s ok &gt;&gt;&gt; exec s.replace('\n', '\r\n') Tra...
1
2009-12-08T11:56:47Z
[ "python", "exec" ]
Authenticate imaplib.IMAP4_SSL against an Exchange imap server with AUTH=NTLM
1,866,460
<p>Yesterday, the IT department made changes to the Exchange server. I was previously able to use <code>imaplib</code> to fetch messages from the server. But now it seems they have turned off the authentication mechanism I was using. From the output below, it looks as if the server now supports NTLM authentication only...
2
2009-12-08T11:47:54Z
1,867,034
<p>I was able to use the <a href="http://code.google.com/p/python-ntlm/" rel="nofollow">python-ntlm</a> project. </p> <p><code>python-ntlm</code> implements NTLM authentication for HTTP. It was easy to add NTLM authentication for IMAP by extending this project.</p> <p>I submitted <a href="http://code.google.com/p/pyt...
4
2009-12-08T13:39:20Z
[ "python", "exchange-server", "imap", "ntlm" ]
how to set the unique identifier key dynamically from a list containing unique numbers from a csv file?
1,866,727
<p>I would like to know if there is a way to set the key of an entity in google app engine dynamically from a list of unique numbers stored in a csv file and bulkload it to the datastore. Also is it possible to have an entity model with out any properties and just use the the set of predefined unique numbers as keys. T...
0
2009-12-08T12:43:55Z
1,869,608
<p>Yes, I think that is completely possible. </p> <p>Only keep in mind that a key has to be unique across the whole application, so if you use it as the key for an entity in the data model you design to keep the keys, you won't be able to use it in your application's real data models entities (wherever you intend to u...
1
2009-12-08T20:25:42Z
[ "python", "google-app-engine" ]
Django to use different settings.py file based on subdomains
1,866,760
<p>How can Django use different settings.py file based on subdomains. </p> <p>Can these utilities ("django-admin", "python manage.py") still be used if there were different settings connecting to different databases.</p>
2
2009-12-08T12:49:13Z
1,867,788
<p>ok you have two dimensions you need to cover with your settings:</p> <ol> <li>Domain (site)</li> <li>Current Machine</li> </ol> <p>Here is what I recommend:</p> <p><code>universal_settings.py</code> - all the settings you want to inherit everywhere (all machines, all domains)</p> <p><code>local_settings.py</code...
5
2009-12-08T15:42:37Z
[ "python", "django", "subdomains" ]
searching all fields in a table in django
1,866,847
<p>How to search all fields in a table in django using filter clause ex:table.object.filter(any field in the table="sumthing")</p> <p>Thanks.</p>
2
2009-12-08T13:04:17Z
1,867,283
<p>I don't think the filter clause is suited to this kind of search. You might want to check out <a href="http://haystacksearch.org/" rel="nofollow">Haystack</a>.</p>
4
2009-12-08T14:24:12Z
[ "python", "django" ]
searching all fields in a table in django
1,866,847
<p>How to search all fields in a table in django using filter clause ex:table.object.filter(any field in the table="sumthing")</p> <p>Thanks.</p>
2
2009-12-08T13:04:17Z
1,867,469
<p>I agree with Alasdair, but the answer to your question is this though:</p> <pre><code>from django.db.models import CharField from django.db.models import Q fields = [f for f in table._meta.fields if isinstance(f, CharField)] queries = [Q(**{f.name: SEARCH_TERM}) for f in fields] qs = Q() for query in queries: ...
7
2009-12-08T14:51:21Z
[ "python", "django" ]
Simple Automatic Classification of the (R-->R) Functions
1,866,862
<p>Given data values of some real-time physical process (e.g. network traffic) it is to find a name of the function which "at best" matches with the data. </p> <p>I have a set of functions of type <code>y=f(t)</code> where <code>y</code> and <code>t</code> are real: </p> <pre><code>funcs = set([cos, tan, exp, log]) <...
1
2009-12-08T13:06:45Z
1,866,882
<p>See <a href="http://en.wikipedia.org/wiki/Curve%5Ffitting" rel="nofollow">Curve Fitting</a></p>
0
2009-12-08T13:11:33Z
[ "python", "algorithm", "function", "math" ]
Simple Automatic Classification of the (R-->R) Functions
1,866,862
<p>Given data values of some real-time physical process (e.g. network traffic) it is to find a name of the function which "at best" matches with the data. </p> <p>I have a set of functions of type <code>y=f(t)</code> where <code>y</code> and <code>t</code> are real: </p> <pre><code>funcs = set([cos, tan, exp, log]) <...
1
2009-12-08T13:06:45Z
1,866,951
<p>Scipy has <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html" rel="nofollow">functions</a> for fitting data, but they use polynomes or splines. You can use one of Gauß' many <a href="http://en.wikipedia.org/wiki/Least%5Fsquares" rel="nofollow">discoveries</a>, the method of <a href="htt...
1
2009-12-08T13:24:20Z
[ "python", "algorithm", "function", "math" ]
Simple Automatic Classification of the (R-->R) Functions
1,866,862
<p>Given data values of some real-time physical process (e.g. network traffic) it is to find a name of the function which "at best" matches with the data. </p> <p>I have a set of functions of type <code>y=f(t)</code> where <code>y</code> and <code>t</code> are real: </p> <pre><code>funcs = set([cos, tan, exp, log]) <...
1
2009-12-08T13:06:45Z
1,867,260
<p>Just write the error ( quadratic sum of error at each point for instance ) for each function of the set and choose the function giving the minimum.</p> <p>But you should still fit each function before choosing</p>
2
2009-12-08T14:19:16Z
[ "python", "algorithm", "function", "math" ]
Simple Automatic Classification of the (R-->R) Functions
1,866,862
<p>Given data values of some real-time physical process (e.g. network traffic) it is to find a name of the function which "at best" matches with the data. </p> <p>I have a set of functions of type <code>y=f(t)</code> where <code>y</code> and <code>t</code> are real: </p> <pre><code>funcs = set([cos, tan, exp, log]) <...
1
2009-12-08T13:06:45Z
1,867,318
<p>I would try an approach based on fitting too. For each of the four test functions (f1-4 see below), the values of a and b that minimizes the squared error. </p> <pre><code>f1(t) = a*cos(b*t) f2(t) = a*tan(b*t) f3(t) = a*exp(b*t) f4(t) = a*log(b*t) </code></pre> <p>After fitting the squared error of the four functi...
1
2009-12-08T14:29:03Z
[ "python", "algorithm", "function", "math" ]
Mechanize submit login form from http to https
1,866,888
<p>I have a web page containing a login form which loads via HTTP, but it submits the data via HTTPS. </p> <p>I'm using python-mechanize to log into this site, but it seems that the data is submitted via HTTP.</p> <p>My code is looks like this:</p> <pre><code>import mechanize b = mechanize.Browser() b.open('http://s...
10
2009-12-08T13:13:02Z
1,867,072
<p><code>mechanize</code> uses <code>urllib2</code> internally and the later had a bug: <a href="http://bugs.python.org/issue1424152" rel="nofollow">HTTPS over (Squid) Proxy fails</a>. The bug is fixed in Python 2.6.3, so updating Python should solve your problem.</p>
2
2009-12-08T13:45:15Z
[ "python", "forms", "post", "https", "mechanize" ]
Mechanize submit login form from http to https
1,866,888
<p>I have a web page containing a login form which loads via HTTP, but it submits the data via HTTPS. </p> <p>I'm using python-mechanize to log into this site, but it seems that the data is submitted via HTTP.</p> <p>My code is looks like this:</p> <pre><code>import mechanize b = mechanize.Browser() b.open('http://s...
10
2009-12-08T13:13:02Z
1,867,227
<p>Ok, it seems to be a bug in mechanize</p> <p><a href="http://sourceforge.net/mailarchive/forum.php?thread%5Fname=alpine.DEB.2.00.0910062211230.8646%40alice&amp;forum%5Fname=wwwsearch-general" rel="nofollow">http://sourceforge.net/mailarchive/forum.php?thread%5Fname=alpine.DEB.2.00.0910062211230.8646%40alice&amp;for...
1
2009-12-08T14:13:56Z
[ "python", "forms", "post", "https", "mechanize" ]
Insert python variables into SQLITE DB from another cursor object
1,867,018
<p>Using an example from the Python DOCs: </p> <pre><code>stocks = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00), ('2006-04-06', 'SELL', 'IBM', 500, 53.00), ]: for t in stocks c.execute('insert into stocks values (?,?,?,?,?)', t) </code></pre>...
0
2009-12-08T13:36:30Z
1,867,053
<p>Tuples are immutable, but you can easily extract their contents and form new tuples. Also, I'm not sure, but I don't think the execute() call absolutely must have a tuple. Can't any sequence, including lists, work as well?</p> <p>Anyway, here's what you need:</p> <pre><code>for t in stocks: c.execute('insert...
1
2009-12-08T13:41:55Z
[ "python", "sqlite3" ]
Insert python variables into SQLITE DB from another cursor object
1,867,018
<p>Using an example from the Python DOCs: </p> <pre><code>stocks = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00), ('2006-04-06', 'SELL', 'IBM', 500, 53.00), ]: for t in stocks c.execute('insert into stocks values (?,?,?,?,?)', t) </code></pre>...
0
2009-12-08T13:36:30Z
1,867,176
<p>I assume you meant to use <code>stocks</code> instead of <code>t</code> for the <code>executemany</code> version For the executemany version, you can also do</p> <pre><code>c.executemany('insert into stocks (?,?,?,?,?,?)', (t + ('Arb value',) for t in stocks)) </code></pre> <p>Using a generator expression instead ...
0
2009-12-08T14:04:58Z
[ "python", "sqlite3" ]
Python reference problem
1,867,068
<p>I'm experiencing a (for me) very weird problem in Python.</p> <p>I have a class called Menu: (snippet)</p> <pre><code>class Menu: """Shows a menu with the defined items""" menu_items = {} characters = map(chr, range(97, 123)) def __init__(self, menu_items): self.init_menu(menu_items) ...
3
2009-12-08T13:44:19Z
1,867,089
<p>The <code>menu_items</code> dict is a class attribute that's shared between all <code>Menu</code> instances. Initialize it like this instead, and you should be fine:</p> <pre><code>class Menu: """Shows a menu with the defined items""" characters = map(chr, range(97, 123)) def __init__(self, menu_items...
16
2009-12-08T13:48:31Z
[ "python", "reference" ]
Python reference problem
1,867,068
<p>I'm experiencing a (for me) very weird problem in Python.</p> <p>I have a class called Menu: (snippet)</p> <pre><code>class Menu: """Shows a menu with the defined items""" menu_items = {} characters = map(chr, range(97, 123)) def __init__(self, menu_items): self.init_menu(menu_items) ...
3
2009-12-08T13:44:19Z
1,867,141
<p>Since Pär answered your question here is some random advice: <code>dict</code> and <code>zip</code> are extremely useful functions :-)</p> <pre><code>class Menu: """Shows a menu with the defined items""" characters = map(chr, range(97, 123)) def __init__(self, menu_items): self.menu_items = di...
5
2009-12-08T13:58:13Z
[ "python", "reference" ]
Insights on SystemError: com_backpatch: offset too large
1,867,070
<p>In python, <code>"SystemError: com_backpatch: offset too large"</code> is thrown when executing the code generated by the following:</p> <pre><code>f = open("test.py", "w") f.write("def fn():\n a =1000\n") for a in xrange(3000): if a == 0: f.write(" if a == "+str(a)+": \n print "+str(a)+"\n") ...
0
2009-12-08T13:44:29Z
1,867,203
<p>Accourding to this: <a href="http://www.cgl.ucsf.edu/pipermail/chimera-dev/2007/000404.html" rel="nofollow">http://www.cgl.ucsf.edu/pipermail/chimera-dev/2007/000404.html</a></p> <blockquote> <p>The Python bytecode compiler has a limitation of a maximum of a 16 bit offset in a jump instruction. This means ...
2
2009-12-08T14:09:50Z
[ "python" ]
Insights on SystemError: com_backpatch: offset too large
1,867,070
<p>In python, <code>"SystemError: com_backpatch: offset too large"</code> is thrown when executing the code generated by the following:</p> <pre><code>f = open("test.py", "w") f.write("def fn():\n a =1000\n") for a in xrange(3000): if a == 0: f.write(" if a == "+str(a)+": \n print "+str(a)+"\n") ...
0
2009-12-08T13:44:29Z
1,867,253
<p>Looks like you are hitting a python interpreter limit. It appears that the branch from the start of the if to the end is too far - probably because offset is limited to 16 bits. If you change the "elif" to "if" then the problem goes away.</p> <p>You need to reduce the size of the "if/elif" chain.</p> <p>HIH</p> <...
0
2009-12-08T14:18:04Z
[ "python" ]
Insights on SystemError: com_backpatch: offset too large
1,867,070
<p>In python, <code>"SystemError: com_backpatch: offset too large"</code> is thrown when executing the code generated by the following:</p> <pre><code>f = open("test.py", "w") f.write("def fn():\n a =1000\n") for a in xrange(3000): if a == 0: f.write(" if a == "+str(a)+": \n print "+str(a)+"\n") ...
0
2009-12-08T13:44:29Z
1,950,936
<p>JFYI, this script worked for me in a Debian Testing host with a 32bits userspace and kernel and Python 2.5.4.</p> <pre><code>$ ls -ln total 4 -rw-r--r-- 1 1000 1000 270 2009-12-23 02:53 gentest.py $ python gentest.py $ ls -ln total 216 -rw-r--r-- 1 1000 1000 270 2009-12-23 02:53 gentest.py -rw-r--r-- 1 1000 100...
0
2009-12-23T06:01:50Z
[ "python" ]
Python Traits UI (Enthought)
1,867,194
<p>I am working with some code that uses Traits UI to show a dialog from which the user is able to select two files:</p> <pre><code>class Files(HasTraits): filename_1 = File(exists=True) filename_2 = File(exists=True) traits_ui = View( 'filename_1', 'filename_2', title = 'Select Geometry...
3
2009-12-08T14:09:15Z
4,184,084
<p>At some point after Traits 3.2, a new trait was added to the <a href="http://www.cs.nott.ac.uk/~jvb/ETS34_API/enthought.traits.ui.editors.file_editor.ToolkitEditorFactory.html" rel="nofollow">FileEditor ToolkitEditorFactory</a> that enables you set whether editing the trait is an 'open' or 'save' dialog. Try this:</...
2
2010-11-15T11:57:00Z
[ "python", "user-interface", "traits", "enthought" ]
Set an object's superclass at __init__?
1,867,258
<p>Is it possible, when instantiating an object, to pass-in a class which the object should derive from?</p> <p>For instance:</p> <pre><code>class Red(object): def x(self): print '#F00' class Blue(object): def x(self): print '#00F' class Circle(object): def __init__(self, parent): ...
4
2009-12-08T14:18:59Z
1,867,386
<p>Perhaps what you are looking for is a class factory:</p> <pre><code>#!/usr/bin/env python class Foo(object): def x(self): print('y') def Bar(parent=Foo): class Adoptee(parent): def __init__(self): self.x() return Adoptee() obj=Bar(parent=Foo) </code></pre>
7
2009-12-08T14:39:44Z
[ "python", "oop" ]
Set an object's superclass at __init__?
1,867,258
<p>Is it possible, when instantiating an object, to pass-in a class which the object should derive from?</p> <p>For instance:</p> <pre><code>class Red(object): def x(self): print '#F00' class Blue(object): def x(self): print '#00F' class Circle(object): def __init__(self, parent): ...
4
2009-12-08T14:18:59Z
1,867,396
<p>If you really need it, then you could use <code>type</code> constructor, e.g. within a factory function (or inside <code>__new__</code> method, but this is probably safer approach):</p> <pre><code>class Foo(object): def x(self): print 'y' class Bar(object): def __init__(self): self.x() def...
0
2009-12-08T14:41:23Z
[ "python", "oop" ]
Set an object's superclass at __init__?
1,867,258
<p>Is it possible, when instantiating an object, to pass-in a class which the object should derive from?</p> <p>For instance:</p> <pre><code>class Red(object): def x(self): print '#F00' class Blue(object): def x(self): print '#00F' class Circle(object): def __init__(self, parent): ...
4
2009-12-08T14:18:59Z
1,867,410
<p>It sounds like you are trying to use inheritance for something that it isn't meant for. If you would explain why you want to do this, maybe a more idiomatic and robust way to achieve your goals can be found.</p>
1
2009-12-08T14:43:10Z
[ "python", "oop" ]
Set an object's superclass at __init__?
1,867,258
<p>Is it possible, when instantiating an object, to pass-in a class which the object should derive from?</p> <p>For instance:</p> <pre><code>class Red(object): def x(self): print '#F00' class Blue(object): def x(self): print '#00F' class Circle(object): def __init__(self, parent): ...
4
2009-12-08T14:18:59Z
1,867,965
<p>I agree with @AntsAasma. You should probably consider using <a href="http://en.wikipedia.org/wiki/Dependency%5Finjection" rel="nofollow">dependency injection</a>. Atleast in the example given (which I'm sure is greatly simplified to illustrate your problem), the color of a shape is better represented by via a <a h...
4
2009-12-08T16:09:20Z
[ "python", "oop" ]
Set an object's superclass at __init__?
1,867,258
<p>Is it possible, when instantiating an object, to pass-in a class which the object should derive from?</p> <p>For instance:</p> <pre><code>class Red(object): def x(self): print '#F00' class Blue(object): def x(self): print '#00F' class Circle(object): def __init__(self, parent): ...
4
2009-12-08T14:18:59Z
1,868,288
<p>As everybody else says, that's a pretty weird usage, but, if you really want it, it's surely feasible (except for the mysterious <code>Bar</code> that you pull out of thin air in comments;-). For example:</p> <pre><code>class Circle(object): def __init__(self, parent): self.__class__ = type('Circle', (...
0
2009-12-08T16:52:19Z
[ "python", "oop" ]
PKI verification across Java and Python
1,867,355
<p>I am trying to implement a PKI verification scheme, where a message string is signed with a private key on server, the signature is stored on the client along with the message string. The client then verifies the signature using a public key.</p> <p>The restrictions of my environment are, the server is Google App E...
2
2009-12-08T14:33:44Z
1,867,656
<p>These are different operations. In Python, you need to use <code>hashAndSign</code>. The default happens to be SHA1 hash.</p>
1
2009-12-08T15:21:34Z
[ "java", "python", "google-app-engine", "cryptography" ]
PKI verification across Java and Python
1,867,355
<p>I am trying to implement a PKI verification scheme, where a message string is signed with a private key on server, the signature is stored on the client along with the message string. The client then verifies the signature using a public key.</p> <p>The restrictions of my environment are, the server is Google App E...
2
2009-12-08T14:33:44Z
1,880,409
<p><a href="http://www.keyczar.org/" rel="nofollow">Keyczar</a> should work fine on App Engine, and is available in both Java and Python flavours.</p>
1
2009-12-10T11:36:46Z
[ "java", "python", "google-app-engine", "cryptography" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,382
<p>Perhaps not what you want, but I'll suggest it anyway.</p> <pre><code>import os a = os.path.getsize("C:/TestFolder/Input/1.avi") </code></pre> <p>Alternatively for an opened file you can use the <a href="http://docs.python.org/library/os.html#os.fstat">fstat</a> function, which can be used on an opened file. It ta...
15
2009-12-08T14:38:24Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,399
<p>Most reliable would be create a wrapping class which would check file's size when you open it, track write and seek operations, count current size based on those operations and prevent from exceeding size limit.</p>
2
2009-12-08T14:41:27Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,404
<p>Or, if the file is already open:</p> <pre><code>&gt;&gt;&gt; fsock = open('/etc/hosts', 'rb').read() &gt;&gt;&gt; len(fsock) 444 </code></pre> <p>That's how many bytes the file is.</p>
2
2009-12-08T14:42:18Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,422
<p><a href="http://docs.python.org/library/os.html#os.fstat"><code>os.fstat(file_obj.fileno()).st_size</code></a> should do the trick. I think that it will return the bytes written. You can always do a flush before hand if you are concerned about buffering.</p>
5
2009-12-08T14:44:52Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,442
<p>I'm not familiar with python, but doesn't the stream object (or whatever you get when opening a file) have a property that contains the current position of the stream? </p> <p>Similar to what you get with the <a href="http://www.cplusplus.com/reference/clibrary/cstdio/ftell/" rel="nofollow">ftell()</a> C function, ...
4
2009-12-08T14:47:45Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
1,867,637
<p>You could start with something like this:</p> <pre><code>class TrackedFile(file): def __init__(self, filename, mode): self.size = 0 super(TrackedFile, self).__init__(filename, mode) def write(self, s): self.size += len(s) super(TrackedFile, self).write(s) </code></pre> <p>Th...
4
2009-12-08T15:17:58Z
[ "python", "linux", "file", "filesystems", "ext2" ]
How do I determine an open file's size in Python?
1,867,357
<p>There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't...
9
2009-12-08T14:33:47Z
23,707,088
<p>Though this is an old question, I think that Isak has the simplest solution. Here's how to do it in Python:</p> <pre><code># Assuming f is an open file &gt;&gt;&gt; pos = f.seek() # Save the current position &gt;&gt;&gt; f.seek(0, 2) # Seek to the end of the file &gt;&gt;&gt; length = f.tell() # The current posi...
3
2014-05-17T02:47:18Z
[ "python", "linux", "file", "filesystems", "ext2" ]
Django TemplateSyntaxError: current transaction is aborted, what does this exception mean? Does postgresql 8.4 work fine with django?
1,867,407
<p>The full text of the error is:</p> <blockquote> <p>TemplateSyntaxError at /</p> <p>Caught an exception while rendering: current transaction is aborted, commands ignored until end of transaction block</p> </blockquote> <p>I've recently reinstalled all the software on my computer. The code used to work ...
7
2009-12-08T14:42:44Z
1,867,711
<p>That exception means that there was an error in some SQL that is getting executed. Since Django runs all the SQL inside of a database transaction, all the SQL that is being executed after the error gets ignored. So:</p> <pre><code>BEGIN; SELECT * FROM table; SELECT missing_column FROM table WHERE id = 1; -- genera...
13
2009-12-08T15:30:50Z
[ "python", "django", "postgresql" ]
Django TemplateSyntaxError: current transaction is aborted, what does this exception mean? Does postgresql 8.4 work fine with django?
1,867,407
<p>The full text of the error is:</p> <blockquote> <p>TemplateSyntaxError at /</p> <p>Caught an exception while rendering: current transaction is aborted, commands ignored until end of transaction block</p> </blockquote> <p>I've recently reinstalled all the software on my computer. The code used to work ...
7
2009-12-08T14:42:44Z
4,946,051
<p>If you're moving from a different DB, note that there might be some considerations to bear in mind with the way that Django with Postgres uses transactions. Have a read here:</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/db/transactions/?from=olddocs#handling-exceptions-within-postgresql-transactions...
4
2011-02-09T14:17:57Z
[ "python", "django", "postgresql" ]
What is "Python interpreter None"?
1,867,657
<p>I just created my first application for Google App Engines, which is called "Hello World". It was the first lesson from Google App Engines How-to-get-started tutorial. I tasted it on my computer (I am using Windows XP), and it was working just fine - whenever I would open a new window with my web browser (FireFox), ...
3
2009-12-08T15:21:45Z
1,867,688
<p>You need to set the python interpreter in <code>Window-&gt;Preferences-&gt;Google-&gt;AppEngine</code> on your eclipse platform ( of course, I am assuming you are using the GAE Eclipse plugin, if not, disregard my suggestion).</p>
1
2009-12-08T15:26:18Z
[ "python", "google-app-engine", "app-launcher" ]
What is "Python interpreter None"?
1,867,657
<p>I just created my first application for Google App Engines, which is called "Hello World". It was the first lesson from Google App Engines How-to-get-started tutorial. I tasted it on my computer (I am using Windows XP), and it was working just fine - whenever I would open a new window with my web browser (FireFox), ...
3
2009-12-08T15:21:45Z
1,867,690
<p>Well, it is basically saying that it cannot find your just installed python interpreter.</p> <p>It looks like you are on Windows so check your environment variables, particularly PATH and PYTHONPATH, they are probably still pointing to the previous 2.6 installation folder.</p>
3
2009-12-08T15:26:28Z
[ "python", "google-app-engine", "app-launcher" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,734
<p>like this?</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>A for loop with one command can be written as a single line.</p> <p><strong>EDIT</strong></p> <p>Or maybe slightly cleaner:</p> <pre><code>for _ in xrange(nIterations): y = f(y) </code></pre> <p>Since you don't want to have a some...
5
2009-12-08T15:34:24Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,744
<p>So like this you mean? </p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>While this might seem nice and pretty, I'd argue (as has been done in the comments below your post) that this <strong>doesn't</strong> improve readability, and is best off left as 2 lines.</p>
2
2009-12-08T15:35:13Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,758
<p>Just stick it all on one line like this: <code>for i in range(nIterations): y = f(y)</code></p> <p>The decision to have code on one line or multiple has been an argument for years - there is no performance increase - just lay it out how you like it and how <strong>you</strong> can read it best.</p>
0
2009-12-08T15:36:35Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,767
<p>Your question lacks context, but this could be rewritten using <code>map</code> function or list comprehension (both one-liners)</p>
0
2009-12-08T15:38:47Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,780
<p>Not exactly one line, but once you define the power operation for functions:</p> <pre><code>def f_pow(f, n): if n == 1: return f else: return lambda x: f_pow(f, n-1)(f(x)) </code></pre> <p>you can write this:</p> <pre><code>f_pow(f, nIterations)(y) </code></pre>
0
2009-12-08T15:40:30Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,806
<p><code>reduce(lambda y,_: f(y),xrange(niterations),y)</code></p>
0
2009-12-08T15:46:09Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,817
<p>Ok this is probably a very weird an incomprehensible use of the <code>reduce</code> function, so for real code I'd stick with what you have. But just for the fun of it, here goes:</p> <pre><code>reduce(lambda a, b: f(a), range(nIterations), y) </code></pre>
0
2009-12-08T15:47:43Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,851
<p>If you make <code>y</code> mutable, then you can use list comprehension. But this isn't something I'd use in real code, unless <strong>really</strong> necessary.</p> <pre><code>def f(y): y[0] += 5 y = [0] [f(y) for _ in xrange(10)] print y[0] # =&gt; 50 </code></pre>
0
2009-12-08T15:52:44Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,862
<p>While I'd suggest you keep the original code snippet as it is much clearer, you can accomplish this with a single line of code using the reduce function:</p> <pre><code>reduce(lambda a,b: f(a), xrange(nIterations), y) </code></pre>
0
2009-12-08T15:53:43Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,867,906
<p>You can create such snippets using semicolons <strong>;</strong> if you need to execute more than one instruction inside the loop, here is an example:</p> <pre><code>for i in xrange(nIterations): x=f(i); y=f(x); z=f(y) </code></pre>
0
2009-12-08T15:59:40Z
[ "python" ]
Iteration in a single line
1,867,715
<p>I have some code of the form:</p> <pre><code>for i in range(nIterations): y = f(y) </code></pre> <p>Where <code>f</code> is a function defined elsewhere. hopefully the idea of that code is that after it's run <code>y</code> will have had <code>f</code> applied to it <code>nIterations</code> times.</p> <p>Is ...
0
2009-12-08T15:31:44Z
1,868,042
<p><code>y=[f(y) for i in range(niteration)]</code></p> <p>hope that helps ;)</p>
0
2009-12-08T16:18:59Z
[ "python" ]
Conway's Life: Iterate over a closed universe in Python
1,867,716
<p>I have just started learning Python, and I'm trying to write a program for Conway's Game of Life. I'm trying to create a closed universe with boundary conditions(which are the opposite side/corner). I think I have done this, but I'm not iterating over the loop when it runs, and can't work out how to do this. Thanks ...
0
2009-12-08T15:31:52Z
1,867,795
<p>your problem could be the <a href="http://docs.python.org/reference/simple%5Fstmts.html#break" rel="nofollow">break</a> statement</p> <p>Edit: Also, you probably want just 1 loop, first you initialize <code>universe_array</code> <code>n_iter</code> times, doing nothing after the first time. then you apply the rules...
1
2009-12-08T15:44:02Z
[ "python" ]
Conway's Life: Iterate over a closed universe in Python
1,867,716
<p>I have just started learning Python, and I'm trying to write a program for Conway's Game of Life. I'm trying to create a closed universe with boundary conditions(which are the opposite side/corner). I think I have done this, but I'm not iterating over the loop when it runs, and can't work out how to do this. Thanks ...
0
2009-12-08T15:31:52Z
1,867,855
<p>as I understand you want to create an iterator function. What you need for this is to replace your <code>return</code> with <code>yield</code>.</p> <p>As example if you wish to make an iterator-function, which returns the range from <code>0</code> to <code>x</code> do this:</p> <pre><code> def count(x=0): n =...
0
2009-12-08T15:53:12Z
[ "python" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
1,867,890
<p>Dictionaries will use an order that makes searching efficient, and you cant change that,</p> <p>You could just use a list of objects (a 2 element tuple in a simple case, or even a class), and append items to the end. You can then use linear search to find items in it.</p> <p>Alternatively you could create or use a...
4
2009-12-08T15:57:43Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
1,867,967
<p>Generally, you can design a class that behaves like a dictionary, mainly be implementing the methods <code>__contains__</code>, <code>__getitem__</code>, <code>__delitem__</code>, <code>__setitem__</code> and some more. That class can have any behaviour you like, for example prividing a sorted iterator over the key...
1
2009-12-08T16:09:27Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
6,216,877
<p>I had a similar problem when developing a Django project. I couldn't use OrderedDict, because I was running an old version of python, so the simple solution was to use Django's SortedDict class:</p> <p><a href="https://code.djangoproject.com/wiki/SortedDict" rel="nofollow">https://code.djangoproject.com/wiki/Sorted...
2
2011-06-02T15:46:31Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
10,982,022
<p>python dictionaries are unordered. If you want an ordered dictionary, try <a href="http://docs.python.org/library/collections.html#collections.OrderedDict">collections.OrderedDict</a>.</p> <p>Note that OrderedDict was introduced into the standard library in python 2.7. If you have an older version of python, you ...
14
2012-06-11T14:25:10Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
10,982,037
<pre><code>from collections import OrderedDict OrderedDict((word, True) for word in words) </code></pre> <p>contains </p> <pre><code>OrderedDict([('He', True), ('will', True), ('be', True), ('the', True), ('winner', True)]) </code></pre> <p>If the values are <code>True</code> (or any other immutable object), you can...
73
2012-06-11T14:26:02Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
17,477,904
<p>if you would like to have a dictionary in a specific order, you can also create a list of lists, where the first item will be the key, and the second item will be the value and will look like this example </p> <pre><code>&gt;&gt;&gt; list =[[1,2],[2,3]] &gt;&gt;&gt; for i in list: ... print i[0] ... print ...
1
2013-07-04T20:47:11Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
24,252,364
<p>I came across this post while trying to figure out how to get OrderedDict to work. PyDev for Eclipse couldn't find OrderedDict at all, so I ended up deciding to make a tuple of my dictionary's key values as I would like them to be ordered. When I needed to output my list, I just iterated through the tuple's values a...
1
2014-06-16T21:17:04Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
28,231,217
<p>Rather than explaining the theoretical part, I'll give a simple example.</p> <pre><code>&gt;&gt;&gt; from collections import OrderedDict &gt;&gt;&gt; my_dictionary=OrderedDict() &gt;&gt;&gt; my_dictionary['foo']=3 &gt;&gt;&gt; my_dictionary['aol']=1 &gt;&gt;&gt; my_dictionary OrderedDict([('foo', 3), ('aol', 1)]) <...
52
2015-01-30T07:29:33Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
39,282,935
<p>You can't really do what you want with a dictionary. You already have the dictionary <code>d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}</code>created. I found there was no way to keep in order once it is already created. What I did was make a json file instead with the object:</p> <pre><code>{"ac":33,"gw":20...
1
2016-09-02T01:05:58Z
[ "python", "dictionary", "order" ]
Python dictionary, how to keep keys/values in same order as declared?
1,867,861
<p>new to Python and had a question about dictionaries. I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.</p> <p>So if I have the dictionary:</p> ...
109
2009-12-08T15:53:35Z
39,537,308
<p>From Python 3.6 onwards, the standard <code>dict</code> type maintains insertion order by default.</p> <p>Defining</p> <pre><code>d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10} </code></pre> <p>will result in a dictionary with the keys in the order listed in the source code.</p> <p>This was achieved by usin...
1
2016-09-16T17:40:58Z
[ "python", "dictionary", "order" ]
python permission error
1,868,213
<p>I have a file <code>a.txt</code> in Mac OS, which has write perms to everybody:</p> <pre><code>sh-3.2# ls -hal a.txt -rw-rw-rw- 1 root wheel 0B Dec 8 11:34 a.txt sh-3.2# pwd /var/root </code></pre> <p>however in python it gives me an error:</p> <pre><code>&gt;&gt;&gt; fob=open("/var/root/a.txt","w") Trac...
2
2009-12-08T16:42:03Z
1,868,235
<p>It's likely that you don't have write permission on the directory that the file is in.</p>
1
2009-12-08T16:44:32Z
[ "python", "permissions" ]
python permission error
1,868,213
<p>I have a file <code>a.txt</code> in Mac OS, which has write perms to everybody:</p> <pre><code>sh-3.2# ls -hal a.txt -rw-rw-rw- 1 root wheel 0B Dec 8 11:34 a.txt sh-3.2# pwd /var/root </code></pre> <p>however in python it gives me an error:</p> <pre><code>&gt;&gt;&gt; fob=open("/var/root/a.txt","w") Trac...
2
2009-12-08T16:42:03Z
1,868,238
<p>I'm going to guess that the permissions on the <code>/var/root</code> directory are too strict for the user you are running as.</p>
2
2009-12-08T16:45:01Z
[ "python", "permissions" ]
python permission error
1,868,213
<p>I have a file <code>a.txt</code> in Mac OS, which has write perms to everybody:</p> <pre><code>sh-3.2# ls -hal a.txt -rw-rw-rw- 1 root wheel 0B Dec 8 11:34 a.txt sh-3.2# pwd /var/root </code></pre> <p>however in python it gives me an error:</p> <pre><code>&gt;&gt;&gt; fob=open("/var/root/a.txt","w") Trac...
2
2009-12-08T16:42:03Z
1,972,731
<p>Just a wild guess: since the file already exists there, is it possible that opening with mode "w+" or "r+" instead of "w" allows you to write to the file?</p> <p>I don't have an OS X computer available, and it's most probable that it's an issue of the directory permissions, however I'd try the mode change, just in ...
0
2009-12-29T03:10:15Z
[ "python", "permissions" ]
Properly specifying path for git pull from my local development machine
1,868,393
<p>I'm trying to setup Fabric so that I can automatically deploy my Django app to my web server. </p> <p>What I want to do is to pull the data from my Development machine (os X) to the server. </p> <p>How do I correctly specify my path in the git url?</p> <p>This is the error I'm getting: </p> <pre><code>$ git p...
1
2009-12-08T17:06:45Z
1,868,457
<p>On your server, create a folder called <code>myapp</code>. Chdir to this folder, and then run</p> <pre><code>server ~/myapp$ git init </code></pre> <p>Then, let git know about your server. After this, push to the server's repository from your local machine.</p> <pre><code>local ~/myapp$ git remote add origin user...
1
2009-12-08T17:16:57Z
[ "python", "git" ]
Find two of the same character in a string with regular expressions
1,868,481
<p>This is in reference to a question I asked before <a href="http://stackoverflow.com/questions/1849185/how-to-do-conditional-character-replacement-within-a-string">here</a></p> <p>I received a solution to the problem in that question but ended up needing to go with regex for this particular part.</p> <p>I need a re...
1
2009-12-08T17:20:21Z
1,868,502
<p>Search for <code>([aeiou])\1</code> and replace it with <code>\1:</code></p> <p>I don't know about python, but you should be able to make the regex case insensitive and global with something like <code>/([aeiou])\1/gi</code></p>
1
2009-12-08T17:23:39Z
[ "python", "regex", "replace" ]
Find two of the same character in a string with regular expressions
1,868,481
<p>This is in reference to a question I asked before <a href="http://stackoverflow.com/questions/1849185/how-to-do-conditional-character-replacement-within-a-string">here</a></p> <p>I received a solution to the problem in that question but ended up needing to go with regex for this particular part.</p> <p>I need a re...
1
2009-12-08T17:20:21Z
1,868,504
<p>Try this:</p> <pre><code>re.sub(r'([aeiou])\1', r'\1:', str) </code></pre>
7
2009-12-08T17:23:46Z
[ "python", "regex", "replace" ]
Find two of the same character in a string with regular expressions
1,868,481
<p>This is in reference to a question I asked before <a href="http://stackoverflow.com/questions/1849185/how-to-do-conditional-character-replacement-within-a-string">here</a></p> <p>I received a solution to the problem in that question but ended up needing to go with regex for this particular part.</p> <p>I need a re...
1
2009-12-08T17:20:21Z
1,868,518
<p>You'll need to use a back reference in your search expression. Try something like: <code>([a-z])+\1</code> (or <code>([a-z])\1</code> for just a double). </p>
0
2009-12-08T17:25:38Z
[ "python", "regex", "replace" ]
Find two of the same character in a string with regular expressions
1,868,481
<p>This is in reference to a question I asked before <a href="http://stackoverflow.com/questions/1849185/how-to-do-conditional-character-replacement-within-a-string">here</a></p> <p>I received a solution to the problem in that question but ended up needing to go with regex for this particular part.</p> <p>I need a re...
1
2009-12-08T17:20:21Z
1,868,589
<p><strong>What NOT to do:</strong> As noted, this will match any two vowels together. Leaving this answer as an example of what NOT to do. The correct answer (in this case) is to use backreferences as mentioned in numerous other answers.</p> <pre><code>import re data = ["took","waaeek","raaag"] for s in data: p...
0
2009-12-08T17:37:33Z
[ "python", "regex", "replace" ]
In Google App Engines, how to display the HTML source of a page of a fetched URL in Python?
1,868,587
<p>On Google App Engine I found this code that is fetching a web page's URL:</p> <pre><code>from google.appengine.api import urlfetch url = "http://www.google.com/" result = urlfetch.fetch(url) if result.status_code == 200: doSomethingWithResult(result.content) </code></pre> <p>Is this the right code to fecth that ...
0
2009-12-08T17:37:19Z
1,868,695
<p>Yes, <code>result.content</code> will contain the raw content of that page. You should check the <code>Content-Type</code> header and verify that it's either <code>text/html</code> or <code>application/xhtml+xml</code>.</p> <p>To write the content of that page to the response, first write your status and headers a...
5
2009-12-08T17:54:02Z
[ "python", "html", "google-app-engine", "url", "fetch" ]
Why does my Python class claim that I have 2 arguments instead of 1?
1,868,685
<pre><code>#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission = os.stat(file) user_read() user_write() user_exec() def user_read(): """Return True if 'file' is readable by us...
5
2009-12-08T17:52:50Z
1,868,698
<p><code>self</code> is an implicit first argument to all class member functions. So the <code>i.set(place)</code> call actually calls <code>set(i, place)</code>. You need to take this into account when defining your class, and write <code>def set(self, file)</code> instead.</p>
1
2009-12-08T17:54:15Z
[ "python", "class-method" ]
Why does my Python class claim that I have 2 arguments instead of 1?
1,868,685
<pre><code>#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission = os.stat(file) user_read() user_write() user_exec() def user_read(): """Return True if 'file' is readable by us...
5
2009-12-08T17:52:50Z
1,868,701
<p>In a class, you need to take into account the <code>self</code> parameter for method members.</p>
0
2009-12-08T17:54:30Z
[ "python", "class-method" ]
Why does my Python class claim that I have 2 arguments instead of 1?
1,868,685
<pre><code>#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission = os.stat(file) user_read() user_write() user_exec() def user_read(): """Return True if 'file' is readable by us...
5
2009-12-08T17:52:50Z
1,868,702
<p>The first argument for a python class method is the <code>self</code> variable. If you call <code>classInstance.method(parameter)</code>, the method is invoked as <code>method(self, parameter)</code>.</p> <p>So, when you're defining your class, do something like this: </p> <pre><code>class MyClass(Object): de...
16
2009-12-08T17:54:37Z
[ "python", "class-method" ]
Why does my Python class claim that I have 2 arguments instead of 1?
1,868,685
<pre><code>#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission = os.stat(file) user_read() user_write() user_exec() def user_read(): """Return True if 'file' is readable by us...
5
2009-12-08T17:52:50Z
1,868,705
<p>You need to explicitly pass <code>self</code> variable, which represents an instance of a class, e.g.:</p> <pre><code>def set(self, file): filepermission = os.stat(file) self.user_read() self.user_write() self.user_exec() </code></pre> <p>It doesn't have to be called <code>self</code> but it's a go...
2
2009-12-08T17:54:49Z
[ "python", "class-method" ]