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 can I get optparse's OptionParser to ignore invalid options? | 1,885,161 | <p>In python's <CODE><a href="http://docs.python.org/library/optparse.html#optparse.OptionParser">OptionParser</a></CODE>, how can I instruct it to ignore undefined options supplied to method <CODE>parse_args</CODE>?</p>
<p>e.g.<BR>
I've only defined option <CODE>--foo</CODE> for my <CODE>OptionParser</CODE> instance,... | 18 | 2009-12-11T00:53:21Z | 9,307,174 | <p>Here's one way to have unknown arguments added to the result <code>args</code> of <code>OptionParser.parse_args</code>, with a simple subclass.</p>
<pre><code>from optparse import (OptionParser,BadOptionError,AmbiguousOptionError)
class PassThroughOptionParser(OptionParser):
"""
An unknown option pass-thro... | 33 | 2012-02-16T07:28:18Z | [
"python",
"optparse"
] |
How can I get optparse's OptionParser to ignore invalid options? | 1,885,161 | <p>In python's <CODE><a href="http://docs.python.org/library/optparse.html#optparse.OptionParser">OptionParser</a></CODE>, how can I instruct it to ignore undefined options supplied to method <CODE>parse_args</CODE>?</p>
<p>e.g.<BR>
I've only defined option <CODE>--foo</CODE> for my <CODE>OptionParser</CODE> instance,... | 18 | 2009-12-11T00:53:21Z | 12,751,949 | <p>Python 2.7 (which didn't exist when this question was asked) now provides the <a href="http://docs.python.org/library/argparse.html#module-argparse">argparse</a> module. You may be able to use <a href="http://docs.python.org/library/argparse.html#argparse.ArgumentParser.parse_known_args"><code>ArgumentParser.parse_k... | 6 | 2012-10-05T18:22:19Z | [
"python",
"optparse"
] |
How can I get optparse's OptionParser to ignore invalid options? | 1,885,161 | <p>In python's <CODE><a href="http://docs.python.org/library/optparse.html#optparse.OptionParser">OptionParser</a></CODE>, how can I instruct it to ignore undefined options supplied to method <CODE>parse_args</CODE>?</p>
<p>e.g.<BR>
I've only defined option <CODE>--foo</CODE> for my <CODE>OptionParser</CODE> instance,... | 18 | 2009-12-11T00:53:21Z | 13,870,300 | <p>This is <code>pass_through.py</code> example from <a href="http://optik.sourceforge.net/" rel="nofollow">Optik distribution</a>.</p>
<pre><code>#!/usr/bin/env python
# "Pass-through" option parsing -- an OptionParser that ignores
# unknown options and lets them pile up in the leftover argument
# list. Useful for ... | 3 | 2012-12-13T23:21:53Z | [
"python",
"optparse"
] |
How do I un-escape a backslash-escaped string in python? | 1,885,181 | <p>Suppose I have a string which is a backslash-escaped version of another string. Is there an easy way, in Python, to unescape the string? I could, for example, do:</p>
<pre><code>>>> escaped_str = '"Hello,\\nworld!"'
>>> raw_str = eval(escaped_str)
>>> print raw_str
Hello,
world!
>>... | 39 | 2009-12-11T01:00:01Z | 1,885,197 | <pre><code>>>> print '"Hello,\\nworld!"'.decode('string_escape')
"Hello,
world!"
</code></pre>
| 73 | 2009-12-11T01:04:17Z | [
"python",
"string",
"escaping"
] |
How do I un-escape a backslash-escaped string in python? | 1,885,181 | <p>Suppose I have a string which is a backslash-escaped version of another string. Is there an easy way, in Python, to unescape the string? I could, for example, do:</p>
<pre><code>>>> escaped_str = '"Hello,\\nworld!"'
>>> raw_str = eval(escaped_str)
>>> print raw_str
Hello,
world!
>>... | 39 | 2009-12-11T01:00:01Z | 1,885,211 | <p>You can use <code>ast.literal_eval</code> which is safe:</p>
<blockquote>
<p>Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the
following Python literal structures: strings, numbers, tuples, lists,
dicts, booleans, and None. (E... | 12 | 2009-12-11T01:07:36Z | [
"python",
"string",
"escaping"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 1,885,262 | <pre><code># pylint: disable-msg=C0103
</code></pre>
<p>Put it in the scope where you want these warnings to be ignored. You can also make the above an end-of-line comment, to disable the message only for that line of code.</p>
<p>IIRC it is true that pylint interprets all module-level variables as being 'constants'.... | 38 | 2009-12-11T01:17:24Z | [
"python",
"pylint"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 23,876,731 | <p>Seems to me a bit of refactor might help. Pylint in looking at this as a module, so it would be reasonable not to expect to see variables at this level. Conversely it doesn't complain about vars in classes or functions. The following paradigm seems quite common and solves the issue:</p>
<pre><code>def main():
'... | 4 | 2014-05-26T19:40:27Z | [
"python",
"pylint"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 25,264,508 | <p>In newer versions of pylint this line is now</p>
<pre><code># pylint: disable=C0103
</code></pre>
<p>the enable message is as simple</p>
<pre><code># pylint: enable=C0103
</code></pre>
| 4 | 2014-08-12T12:37:46Z | [
"python",
"pylint"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 25,655,390 | <p>If you disable a message locally in your file then Pylint will report another different warning!</p>
<pre><code>Locally disabling invalid-name (C0103) [I:locally-disabled]
</code></pre>
<p>If your intention is for a clean lint run, and surely that should be the target otherwise why are you bothering, then you can... | 1 | 2014-09-04T00:02:50Z | [
"python",
"pylint"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 29,971,152 | <p>As other answers have indicated you can disable a specific PyLint warning (such C0103) as by including the following line:</p>
<pre><code># pylint: disable=C0103
</code></pre>
<p>but this generates the <code>Locally disabling invalid-name</code> warning. Note that this secondary warning could be useful if you wan... | 0 | 2015-04-30T15:00:28Z | [
"python",
"pylint"
] |
How do I tell PyLint "it's a variable, not a constant" to stop message C0103? | 1,885,251 | <p>I have a module-level variable in my Python 2.6 program named "_log", which PyLint complains about:</p>
<pre><code>C0103: Invalid name "_log" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
</code></pre>
<p>Having read <a href="http://stackoverflow.com/questions/709490/python-code-convention-using-pylint/709709#7097... | 31 | 2009-12-11T01:15:52Z | 32,705,306 | <p>You can also specify a comma separated list of "good-names" that are always allowed in your pylintrc, eg:</p>
<pre><code>[BASIC]
good-names=_log
</code></pre>
| 5 | 2015-09-21T22:10:37Z | [
"python",
"pylint"
] |
How slow are Python/django exceptions? | 1,885,292 | <p>is python exception slow?
I'm kind using python exceptions to structure programm follow in my web application, and I'm wondering how throwing exceptions will affect performance of my application. what is your thoughts?</p>
<p>which one of the following statements is less expensive in terms of memory and cpu?</p>
... | 5 | 2009-12-11T01:31:49Z | 1,885,301 | <p>To really understand the performance of your system, you'll have to profile it. But Python is a language that encourages using exceptions like this, so they don't have an unusual overhead as they do in some other languages.</p>
<p>For example, sometimes people debate this choice:</p>
<pre><code>if hasattr(obj, "a... | 10 | 2009-12-11T01:34:09Z | [
"python",
"django",
"exception"
] |
How slow are Python/django exceptions? | 1,885,292 | <p>is python exception slow?
I'm kind using python exceptions to structure programm follow in my web application, and I'm wondering how throwing exceptions will affect performance of my application. what is your thoughts?</p>
<p>which one of the following statements is less expensive in terms of memory and cpu?</p>
... | 5 | 2009-12-11T01:31:49Z | 1,885,316 | <p><a href="http://paltman.com/2008/jan/18/try-except-performance-in-python-a-simple-test/">This guy</a> did a nice little writeup of testing try/except speed with dicts, I sure it would have some analogue in your case. Either way, doing a nice profile will give you the best info.</p>
| 8 | 2009-12-11T01:37:37Z | [
"python",
"django",
"exception"
] |
How slow are Python/django exceptions? | 1,885,292 | <p>is python exception slow?
I'm kind using python exceptions to structure programm follow in my web application, and I'm wondering how throwing exceptions will affect performance of my application. what is your thoughts?</p>
<p>which one of the following statements is less expensive in terms of memory and cpu?</p>
... | 5 | 2009-12-11T01:31:49Z | 1,885,333 | <p>Handling exceptions will be the least of your worries with regards to performance. I would suggest, however, that you use a shortcut provided by Django for you:</p>
<pre><code>from django.shortcuts import get_object_or_404
artist = get_object_or_404(Artist, id=id)
</code></pre>
<p>Which either assigns the object ... | 14 | 2009-12-11T01:44:55Z | [
"python",
"django",
"exception"
] |
How to join lists element-wise in Python? | 1,885,300 | <pre><code>l1 = [4, 6, 8]
l2 = [a, b, c]
</code></pre>
<p>result = <code>[(4,a),(6,b),(8,c)]</code></p>
<p>How do I do that?</p>
| 3 | 2009-12-11T01:34:06Z | 1,885,309 | <p>The <a href="http://docs.python.org/library/functions.html#zip" rel="nofollow"><code>zip</code></a> standard function does this for you:</p>
<pre><code>>>> l1 = [4, 6, 8]
>>> l2 = ["a", "b", "c"]
>>> zip(l1, l2)
[(4, 'a'), (6, 'b'), (8, 'c')]
</code></pre>
<p>If you're using Python 3.x, ... | 12 | 2009-12-11T01:35:32Z | [
"python",
"list"
] |
How to join lists element-wise in Python? | 1,885,300 | <pre><code>l1 = [4, 6, 8]
l2 = [a, b, c]
</code></pre>
<p>result = <code>[(4,a),(6,b),(8,c)]</code></p>
<p>How do I do that?</p>
| 3 | 2009-12-11T01:34:06Z | 1,885,310 | <p>Use <a href="http://docs.python.org/library/functions.html#zip" rel="nofollow"><code>zip</code></a>.</p>
<pre><code>l1 = [1, 2, 3]
l2 = [4, 5, 6]
>>> zip(l1, l2)
[(1, 4), (2, 5), (3, 6)]
</code></pre>
<p>Note that if your lists are of different lengths, the result will be truncated to the length of the sh... | 10 | 2009-12-11T01:36:01Z | [
"python",
"list"
] |
How to join lists element-wise in Python? | 1,885,300 | <pre><code>l1 = [4, 6, 8]
l2 = [a, b, c]
</code></pre>
<p>result = <code>[(4,a),(6,b),(8,c)]</code></p>
<p>How do I do that?</p>
| 3 | 2009-12-11T01:34:06Z | 1,885,312 | <pre><code>>>> l1 = [4, 6, 8]; l2 = ['a', 'b', 'c']
>>> zip(l1, l2)
[(4, 'a'), (6, 'b'), (8, 'c')]
</code></pre>
| 1 | 2009-12-11T01:36:14Z | [
"python",
"list"
] |
How to join lists element-wise in Python? | 1,885,300 | <pre><code>l1 = [4, 6, 8]
l2 = [a, b, c]
</code></pre>
<p>result = <code>[(4,a),(6,b),(8,c)]</code></p>
<p>How do I do that?</p>
| 3 | 2009-12-11T01:34:06Z | 1,888,204 | <p>If the lists are the same length, or if you want the length of the list to be the length of the shorter list, then use zip, as other people have pointed out.</p>
<p>If the lists are different lengths, then you can use <code>map</code> with a transformation function of <code>None</code>:</p>
<pre><code>>>>... | 0 | 2009-12-11T13:52:57Z | [
"python",
"list"
] |
Parsing multilevel text list | 1,885,314 | <p>I need to parse text lists:</p>
<pre><code>1 List name
1 item
2 item
3 item
2 List name
1 item
2 item
3 item
3 List name
1 item
2 item
3 item
</code></pre>
<p>I was trying to use regular expression to split first level list:</p>
<pre><code>import re
def re_show(pat, s):
print re.compile(pat, re.S).sub("{\g&... | 0 | 2009-12-11T01:36:38Z | 1,885,337 | <p>I suspect I'm missing the point, but isn't this simply a question of looking for <code>List</code>?</p>
| 0 | 2009-12-11T01:46:10Z | [
"python",
"regex",
"parsing"
] |
Parsing multilevel text list | 1,885,314 | <p>I need to parse text lists:</p>
<pre><code>1 List name
1 item
2 item
3 item
2 List name
1 item
2 item
3 item
3 List name
1 item
2 item
3 item
</code></pre>
<p>I was trying to use regular expression to split first level list:</p>
<pre><code>import re
def re_show(pat, s):
print re.compile(pat, re.S).sub("{\g&... | 0 | 2009-12-11T01:36:38Z | 1,885,439 | <p>here's one way using dictionary</p>
<pre><code>f=open("myfile")
d={}
e=0
for line in f:
line=line.rstrip()
if "List" in line:
e=e+1
d.setdefault(e,[])
d[e].append(line)
f.close()
for i ,j in d.iteritems():
print i,j
</code></pre>
| 1 | 2009-12-11T02:19:48Z | [
"python",
"regex",
"parsing"
] |
Parsing multilevel text list | 1,885,314 | <p>I need to parse text lists:</p>
<pre><code>1 List name
1 item
2 item
3 item
2 List name
1 item
2 item
3 item
3 List name
1 item
2 item
3 item
</code></pre>
<p>I was trying to use regular expression to split first level list:</p>
<pre><code>import re
def re_show(pat, s):
print re.compile(pat, re.S).sub("{\g&... | 0 | 2009-12-11T01:36:38Z | 1,885,447 | <pre><code>class ListParser:
def __init__(self, s):
self.str = s.split("\n")
print self.str
self.answer = []
def parse(self):
self.nextLine()
self.topList()
return
def topList(self):
while(len(self.str) > 0):
self.topListItem()
def topListItem(self):
l = self.nextLine()
print "TOP: " + l... | 1 | 2009-12-11T02:22:29Z | [
"python",
"regex",
"parsing"
] |
Parsing multilevel text list | 1,885,314 | <p>I need to parse text lists:</p>
<pre><code>1 List name
1 item
2 item
3 item
2 List name
1 item
2 item
3 item
3 List name
1 item
2 item
3 item
</code></pre>
<p>I was trying to use regular expression to split first level list:</p>
<pre><code>import re
def re_show(pat, s):
print re.compile(pat, re.S).sub("{\g&... | 0 | 2009-12-11T01:36:38Z | 1,885,713 | <p>Do you have control over the list format? With just a little editing, you could turn that into config file format, and use the <a href="http://docs.python.org/library/configparser.html" rel="nofollow">ConfigParser</a> module.</p>
<p>Otherwise, how about with a little recursion?</p>
<pre><code>from collections impo... | 2 | 2009-12-11T04:05:46Z | [
"python",
"regex",
"parsing"
] |
Is it possible to keep the column order using the Python csv DictReader | 1,885,324 | <p>For example, my csv has columns as below:</p>
<blockquote>
<p>ID, ID2, Date, Job No, Code</p>
</blockquote>
<p>I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader.</p>
| 24 | 2009-12-11T01:41:10Z | 1,885,353 | <p>Python's <code>dict</code>s do NOT maintain order.</p>
<p>However, the instance of <code>csv.DictReader</code> that you're using (after you've read the first row!-) <strong>does</strong> have a <code>.fieldnames</code> list of strings, which <strong>IS</strong> in order.</p>
<p>So,</p>
<pre><code>for rowdict in m... | 32 | 2009-12-11T01:50:46Z | [
"python",
"csv"
] |
Is it possible to keep the column order using the Python csv DictReader | 1,885,324 | <p>For example, my csv has columns as below:</p>
<blockquote>
<p>ID, ID2, Date, Job No, Code</p>
</blockquote>
<p>I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader.</p>
| 24 | 2009-12-11T01:41:10Z | 7,815,157 | <pre><code>from csv import DictReader, DictWriter
with open("input.csv", 'r') as input_file:
reader = DictReader(f=input_file)
with open("output.csv", 'w') as output_file:
writer = DictWriter(f=output_file, fieldnames=reader.fieldnames)
for row in reader:
writer.writerow(row)
</code... | 5 | 2011-10-18T23:59:09Z | [
"python",
"csv"
] |
Is it possible to keep the column order using the Python csv DictReader | 1,885,324 | <p>For example, my csv has columns as below:</p>
<blockquote>
<p>ID, ID2, Date, Job No, Code</p>
</blockquote>
<p>I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader.</p>
| 24 | 2009-12-11T01:41:10Z | 33,350,300 | <p>I know this question is old...but if you use <code>DictReader</code>, you can pass it an ordered list with the fieldnames to the <code>fieldnames</code> param</p>
| 1 | 2015-10-26T16:02:39Z | [
"python",
"csv"
] |
Is it possible to keep the column order using the Python csv DictReader | 1,885,324 | <p>For example, my csv has columns as below:</p>
<blockquote>
<p>ID, ID2, Date, Job No, Code</p>
</blockquote>
<p>I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader.</p>
| 24 | 2009-12-11T01:41:10Z | 36,726,941 | <p>Unfortunatley the default DictReader does not allow for overriding the dict class, a custom DictReader would do the trick though</p>
<pre><code>import csv
class DictReader(csv.DictReader):
def __init__(self, *args, **kwargs):
self.dict_class = kwargs.pop(dict_class, dict)
super(DictReader, self... | 0 | 2016-04-19T18:46:09Z | [
"python",
"csv"
] |
Is it possible to keep the column order using the Python csv DictReader | 1,885,324 | <p>For example, my csv has columns as below:</p>
<blockquote>
<p>ID, ID2, Date, Job No, Code</p>
</blockquote>
<p>I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it's more of a problem with the reader.</p>
| 24 | 2009-12-11T01:41:10Z | 38,194,515 | <p>Make an <code>OrderedDict</code> from each row <code>dict</code> sorted by <code>DictReader.fieldnames</code>.</p>
<pre><code>import csv
from collections import OrderedDict
reader = csv.DictReader(open("file.csv"))
for row in reader:
sorted_row = OrderedDict(sorted(row.items(),
key=lambda item: reade... | 0 | 2016-07-05T02:46:06Z | [
"python",
"csv"
] |
Need example/help with GtkTextBuffer (of GtkTextView) serialize/deserialize | 1,885,552 | <p>I am trying to save user's bold/italic/font/etc tags in a GtkTextView.
Using GtkTextBuffer.get_text() does not return the tags.</p>
<p>The best documentation I have found on this is:
<a href="http://www.pygtk.org/docs/pygtk/class-gtktextbuffer.html#method-gtktextbuffer--register-serialize-format" rel="nofollow">htt... | 1 | 2009-12-11T03:02:02Z | 1,899,472 | <p>I haven't worked with GtkTextBuffer's serialization. Reading the documentation you linked, I would suggest trying the default serializer, by calling</p>
<pre><code>textbuffer.register_serialize_tagset()
</code></pre>
<p>This gives you GTK+'s built-in proprietary serializer. Being proprietary here means that it doe... | 1 | 2009-12-14T07:34:31Z | [
"python",
"gtk",
"pygtk",
"gtk2"
] |
Need example/help with GtkTextBuffer (of GtkTextView) serialize/deserialize | 1,885,552 | <p>I am trying to save user's bold/italic/font/etc tags in a GtkTextView.
Using GtkTextBuffer.get_text() does not return the tags.</p>
<p>The best documentation I have found on this is:
<a href="http://www.pygtk.org/docs/pygtk/class-gtktextbuffer.html#method-gtktextbuffer--register-serialize-format" rel="nofollow">htt... | 1 | 2009-12-11T03:02:02Z | 1,900,263 | <p>If you need to save the tags because you just want to copy the text into another text buffer, you can use <code>gtk.TextBuffer.insert_range()</code>.</p>
<p>If you need to save the text with tags into another format readable by other programs, I once wrote a library with a GTK text buffer serializer to and from RTF... | 2 | 2009-12-14T11:06:52Z | [
"python",
"gtk",
"pygtk",
"gtk2"
] |
Python 2.6: reading data from a Windows Console application. (os.system?) | 1,885,776 | <p>I have a Windows console application that returns some text. I want to read that text in a Python script. I have tried reading it by using os.system, but it is not working properly. </p>
<pre><code>import os
foo = os.system('test.exe')
</code></pre>
<p>Assuming that test.exe returns "bar", I want the variable foo ... | 4 | 2009-12-11T04:24:56Z | 1,885,778 | <p>Please use subprocess</p>
<pre><code>import subprocess
foo = subprocess.Popen('test.exe',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
</code></pre>
<p><a href="http://docs.python.org/library/subprocess.html#module-subprocess">http://docs.python.org/library/subprocess.html#module-subprocess</a></p>
| 8 | 2009-12-11T04:26:35Z | [
"python",
"windows",
"console",
"exe"
] |
Python 2.6: reading data from a Windows Console application. (os.system?) | 1,885,776 | <p>I have a Windows console application that returns some text. I want to read that text in a Python script. I have tried reading it by using os.system, but it is not working properly. </p>
<pre><code>import os
foo = os.system('test.exe')
</code></pre>
<p>Assuming that test.exe returns "bar", I want the variable foo ... | 4 | 2009-12-11T04:24:56Z | 1,885,805 | <p><strong>WARNING: <em>This only works on UNIX systems.</em></strong></p>
<p>I find that <code>subprocess</code> is overkill when all you want is output to be captured. I recommend the use of <code>commands.getoutput()</code>:</p>
<pre><code>>>> import commands
>>> foo = commands.getoutput('bar')
<... | 2 | 2009-12-11T04:34:52Z | [
"python",
"windows",
"console",
"exe"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,885,875 | <p>May be this:</p>
<pre><code>header_len = 4
header = open("file.txt").readlines()[:header_len]
</code></pre>
<p>But, it will be troublesome for long files.</p>
| 1 | 2009-12-11T05:02:25Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,885,884 | <p>I'm not sure what the Pylint rules are, but you could use the '_' throwaway variable name.</p>
<pre><code>header = ''
header_len = 4
for _ in range(1, header_len):
header += file_handle.readline()
</code></pre>
| 10 | 2009-12-11T05:04:29Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,885,909 | <pre><code>import itertools
header_lines = list(itertools.islice(file_handle, header_len))
# or
header = "".join(itertools.islice(file_handle, header_len))
</code></pre>
<p>Note that with the first, the newline chars will still be present, to strip them:</p>
<pre><code>header_lines = list(n.rstrip("\n")
... | 10 | 2009-12-11T05:12:20Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,885,999 | <p>My best answer is as follows:</p>
<p>file test.dat:</p>
<pre><code>This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
</code></pre>
<p>Python script:</p>
<pre><code>f = open('test.dat')
nlines = 4
header = "".join(f.readline() fo... | 4 | 2009-12-11T05:36:14Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,886,082 | <pre><code>s=""
f=open("file")
for n,line in enumerate(f):
if n<=3 : s=s+line
else:
# do something here to process the rest of the lines
print s
f.close()
</code></pre>
| -1 | 2009-12-11T05:59:03Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,886,435 | <p>I do not see any thing wrong with your solution, may be just replace i with _, I also do not like invoking itertools everywhere where simpler solution will work, it is like people using jQuery for trivial javascript tasks. anyway just to have itertools revenge here is my solution</p>
<p>as you want to read whole fi... | 1 | 2009-12-11T07:35:49Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,887,126 | <p>What about:</p>
<pre><code>header = []
for i,l in enumerate(file_handle):
if i <= 3:
header += l
continue
#proc rest of file here
</code></pre>
| 0 | 2009-12-11T10:20:17Z | [
"python"
] |
Pythonic way to ignore for loop control variable | 1,885,868 | <p>A Python program I'm writing is to read a set number of lines from the top of a file, and the program needs to preserve this header for future use. Currently, I'm doing something similar to the following:</p>
<pre><code> header = ''
header_len = 4
for i in range(1, header_len):
header += file_ha... | 10 | 2009-12-11T04:57:44Z | 1,887,169 | <pre><code>f = open('fname')
header = [next(f) for _ in range(header_len)]
</code></pre>
<p>Since you're going to write header back to the new files, you don't need to do anything with it. To write it back to the new file:</p>
<pre><code>open('new', 'w').writelines(header + list_of_lines)
</code></pre>
<p>if you kno... | 8 | 2009-12-11T10:25:53Z | [
"python"
] |
Is this control structure a code smell? | 1,886,002 | <p>This code seems to smell:</p>
<pre><code>result = None
for item in list:
if result is None:
result = item.foo(args)
else:
if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
result = item.foo(args)
</code></pre>
<p>The smelliest part is the uti... | 1 | 2009-12-11T05:36:23Z | 1,886,013 | <p>The logic is too complex. It's difficult to read what you are really doing.
Simplify that loop, you are doing too much in there.</p>
<p>This IMHO is already a pretty nasty smell.</p>
| 1 | 2009-12-11T05:40:09Z | [
"python",
"structure",
"control"
] |
Is this control structure a code smell? | 1,886,002 | <p>This code seems to smell:</p>
<pre><code>result = None
for item in list:
if result is None:
result = item.foo(args)
else:
if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
result = item.foo(args)
</code></pre>
<p>The smelliest part is the uti... | 1 | 2009-12-11T05:36:23Z | 1,886,029 | <p>The second-to-last line is so long, I lost patience with it. I'd make those two separate variables with meaningful names, so we can figure out what if x < y is supposed to mean.</p>
<p>How about this:</p>
<pre><code>if not list:
return None
def get_thing(bar):
# I don't know what this is, so... here:
... | 1 | 2009-12-11T05:43:00Z | [
"python",
"structure",
"control"
] |
Is this control structure a code smell? | 1,886,002 | <p>This code seems to smell:</p>
<pre><code>result = None
for item in list:
if result is None:
result = item.foo(args)
else:
if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
result = item.foo(args)
</code></pre>
<p>The smelliest part is the uti... | 1 | 2009-12-11T05:36:23Z | 1,886,035 | <pre><code>L = list # 'list' is a poor variable name, use something else
result = min((n.foo(args) for n in L),
key=lambda x: ClassFred.objects.get(arg1=x))
# if you don't have to use arg1 as a named parameter:
result = min((n.foo(args) for n in L), key=ClassFred.objects.get)
</code></pre>
<p>The min func... | 3 | 2009-12-11T05:44:45Z | [
"python",
"structure",
"control"
] |
Is this control structure a code smell? | 1,886,002 | <p>This code seems to smell:</p>
<pre><code>result = None
for item in list:
if result is None:
result = item.foo(args)
else:
if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
result = item.foo(args)
</code></pre>
<p>The smelliest part is the uti... | 1 | 2009-12-11T05:36:23Z | 1,886,056 | <p>So, I agree it seems like it could be refactored from a readability standpoint, but it should work. This would be my take on it:</p>
<pre><code># using lst instead of list & at least python 2.5
result = lst[0].foo(args) if lst else None
fxn = ClassFred.objects.get
for item in lst[1:]:
if fxn(arg1=result) &... | 1 | 2009-12-11T05:51:19Z | [
"python",
"structure",
"control"
] |
Is this control structure a code smell? | 1,886,002 | <p>This code seems to smell:</p>
<pre><code>result = None
for item in list:
if result is None:
result = item.foo(args)
else:
if ClassFred.objects.get(arg1=result) < ClassFred.objects.get(arg1=item.foo(args)):
result = item.foo(args)
</code></pre>
<p>The smelliest part is the uti... | 1 | 2009-12-11T05:36:23Z | 1,888,135 | <p>This may work, though, I don't think it'll smell better :p</p>
<pre><code>max([ClassFred.objects.get(arg1=item.foo(args)), item.foo(args) for item in list])[1]
</code></pre>
| 0 | 2009-12-11T13:39:00Z | [
"python",
"structure",
"control"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 1,886,125 | <p>Well, in the Python threading module, there are condition objects that are associated to locks. One method <code>acquire()</code> will return whatever value is returned from the underlying method. For more information: <a href="http://docs.python.org/library/threading.html#condition-objects" rel="nofollow">Python Co... | 1 | 2009-12-11T06:10:05Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 1,886,176 | <p>I suggest you instantiate a <a href="http://docs.python.org/library/queue.html?highlight=queue#Queue.Queue">Queue.Queue</a> before starting the thread, and pass it as one of the thread's args: before the thread finishes, it <code>.put</code>s the result on the queue it received as an argument. The parent can <code>... | 54 | 2009-12-11T06:24:14Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 1,886,436 | <p>Another approach is to pass a callback function to the thread. This gives a simple, safe and flexible way to return a value to the parent, anytime from the new thread.</p>
<pre><code># A sample implementation
import threading
import time
class MyThread(threading.Thread):
def __init__(self, cb):
thread... | 3 | 2009-12-11T07:35:54Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 1,891,891 | <p>If you were calling join() to wait for the thread to complete, you could simply attach the result to the Thread instance itself and then retrieve it from the main thread after the join() returns. </p>
<p>On the other hand, you don't tell us how you intend to discover that the thread is done and that the result is ... | 12 | 2009-12-12T01:27:10Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 16,144,524 | <p>You should pass a Queue instance as a parameter then you should .put() your return object into the queue. You can gather the return value via queue.get() whatever object you put.</p>
<p>Sample:</p>
<pre><code>queue = Queue.Queue()
thread_ = threading.Thread(
target=target_method,
na... | 5 | 2013-04-22T10:11:33Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 21,962,612 | <p>Use <strong>lambda</strong> to wrap your target thread function and pass its return value back to the parent thread using a <strong>queue</strong>. (Your original target function remains unchanged without extra queue parameter.)</p>
<p>Sample code:</p>
<pre><code>import threading
import queue
def dosomething(para... | 3 | 2014-02-23T00:34:44Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 28,158,800 | <p>POC:</p>
<pre><code>import random
import threading
class myThread( threading.Thread ):
def __init__( self, arr ):
threading.Thread.__init__( self )
self.arr = arr
self.ret = None
def run( self ):
self.myJob( self.arr )
def join( self ):
threading.Thread.join( s... | 2 | 2015-01-26T21:08:29Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 32,022,837 | <p>You can use synchronised <a href="https://docs.python.org/2/library/queue.html" rel="nofollow">queue</a> module.<br>
Consider you need to check a user infos from database with a known id:</p>
<pre><code>def check_infos(user_id, queue):
result = send_data(user_id)
queue.put(result)
</code></pre>
<p>Now you ... | 2 | 2015-08-15T08:10:36Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 34,132,917 | <p>I'm surprised nobody mentioned that you could just pass it a mutable:</p>
<pre><code>>>> thread_return={'success': False}
>>> from threading import Thread
>>> def task(thread_return):
... thread_return['success'] = True
...
>>> Thread(target=task, args=(thread_return,)).start()... | 5 | 2015-12-07T11:51:49Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 36,156,551 | <p>The following wrapper function will wrap an existing function and return an object which points both to the thread (so that you can call <code>start()</code>,<code>join()</code>, etc. on it) as well as access/view its eventual return value.</p>
<pre><code>def threadwrap(func,args,kwargs):
class res(object): resu... | 0 | 2016-03-22T14:04:04Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Return value from thread | 1,886,090 | <p>How do I get a thread to return a tuple or any value of my choice back to the parent in Python?</p>
| 47 | 2009-12-11T06:00:58Z | 37,872,715 | <p>Based on jcomeau_ictx's suggestion. The simplest one I came across. Requirement here was to get exit status staus from three different processes running on the server and trigger another script if all three are successful. This seems to be working fine</p>
<pre><code> class myThread(threading.Thread):
def ... | 1 | 2016-06-17T03:36:11Z | [
"python",
"multithreading",
"python-multithreading",
"exit-code"
] |
Should Pylons' development.ini be checked in? | 1,886,192 | <p>I'm learning about Pylons and I've read a few tutorials, but none of them have addressed collaboration practices. Starting on a practice project. I'd like to keep my code in a revision-control system (Git, specifically) as if it were an open-source project with multiple developers, in order to practice that aspect ... | 1 | 2009-12-11T06:29:53Z | 1,886,225 | <p>The most important aspect of collaboration is communicating with your teammates. See if you can come to a quick consensus on how to handle the situation.</p>
<p>My suggestion though, would be to pass around your completed ini file for the other devs to modify for their own purposes. If there are a lot of hand tuned... | 0 | 2009-12-11T06:38:52Z | [
"python",
"pylons"
] |
Should Pylons' development.ini be checked in? | 1,886,192 | <p>I'm learning about Pylons and I've read a few tutorials, but none of them have addressed collaboration practices. Starting on a practice project. I'd like to keep my code in a revision-control system (Git, specifically) as if it were an open-source project with multiple developers, in order to practice that aspect ... | 1 | 2009-12-11T06:29:53Z | 1,886,486 | <p>You could check it in as <code>sample.ini</code> for example so that everyone can copy to their own <code>development.ini</code> and modify as needed</p>
| 2 | 2009-12-11T07:51:49Z | [
"python",
"pylons"
] |
Should Pylons' development.ini be checked in? | 1,886,192 | <p>I'm learning about Pylons and I've read a few tutorials, but none of them have addressed collaboration practices. Starting on a practice project. I'd like to keep my code in a revision-control system (Git, specifically) as if it were an open-source project with multiple developers, in order to practice that aspect ... | 1 | 2009-12-11T06:29:53Z | 1,891,883 | <p>On a team development, we make an effort to ensure everyone has a common development environment, or we make adjustments to things (like database URLs) to allow people on different environments (we do Mac, Windows, and Linux) to share all files.</p>
<p>And our Pylons development.ini files are committed to subversio... | 2 | 2009-12-12T01:23:48Z | [
"python",
"pylons"
] |
Should Pylons' development.ini be checked in? | 1,886,192 | <p>I'm learning about Pylons and I've read a few tutorials, but none of them have addressed collaboration practices. Starting on a practice project. I'd like to keep my code in a revision-control system (Git, specifically) as if it were an open-source project with multiple developers, in order to practice that aspect ... | 1 | 2009-12-11T06:29:53Z | 1,894,559 | <p>You should check in development.ini. Most developers are smart enough to realize that if they want to run your application they need to make some tweaks. The development.ini will serve as a template. A file that has the database configured incorrectly is still useful since I can see that the system is trying to conn... | 0 | 2009-12-12T20:12:55Z | [
"python",
"pylons"
] |
How to do a string replace in a urlencoded string | 1,886,660 | <p>I have a string like <code>x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json"</code></p>
<p>If I do <code>x % 10</code> that fails as there are <code>%20f</code> etc which are being treated as format strings, so I have to do a... | 1 | 2009-12-11T08:42:10Z | 1,886,662 | <p>urldecode the string, do the formatting, and then urlencode it again:</p>
<pre><code>import urllib
x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json"
tmp = urllib.unquote(x)
tmp2 = tmp % (foo, bar)
x = urllib.quote(tmp2)
</co... | 5 | 2009-12-11T08:43:20Z | [
"python",
"string"
] |
How to do a string replace in a urlencoded string | 1,886,660 | <p>I have a string like <code>x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json"</code></p>
<p>If I do <code>x % 10</code> that fails as there are <code>%20f</code> etc which are being treated as format strings, so I have to do a... | 1 | 2009-12-11T08:42:10Z | 1,887,300 | <p>Otherwise you can use the new-style output formatting (available since v 2.6), which doesn't rely on %:</p>
<pre><code>x = 'http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D{0}&format=json'
x.format(10)
</code></pre>
<p>It also has the advance of... | 0 | 2009-12-11T10:53:26Z | [
"python",
"string"
] |
How to do a string replace in a urlencoded string | 1,886,660 | <p>I have a string like <code>x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json"</code></p>
<p>If I do <code>x % 10</code> that fails as there are <code>%20f</code> etc which are being treated as format strings, so I have to do a... | 1 | 2009-12-11T08:42:10Z | 1,887,521 | <p>In python string formatting, use <code>%%</code> to output a single % character (<a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow">docs</a>).</p>
<pre><code>>>> "%d%%" % 50
'50%'
</code></pre>
<p>So you could replace all the % with %%, except where you want... | 0 | 2009-12-11T11:41:57Z | [
"python",
"string"
] |
Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename | 1,886,744 | <p>I'd like to generate a dynamic Excel file on request from Django. The library pyExcelerator does this, but I haven't found any way to use the contents of the Excel file without generating a server-side temporary Excel file, reading it, using its contents and deleting it.</p>
<p>The problem is that pyExcelerator onl... | 3 | 2009-12-11T09:03:32Z | 1,886,797 | <p>Why can you not use the <code>tempfile</code> module?</p>
<p>How about:</p>
<pre><code>import tempfile
fd, filename = tempfile.mkstemp()
fd.close()
workbook.save(filename)
</code></pre>
| 3 | 2009-12-11T09:18:59Z | [
"python",
"django",
"excel",
"temporary-files",
"pyexcelerator"
] |
Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename | 1,886,744 | <p>I'd like to generate a dynamic Excel file on request from Django. The library pyExcelerator does this, but I haven't found any way to use the contents of the Excel file without generating a server-side temporary Excel file, reading it, using its contents and deleting it.</p>
<p>The problem is that pyExcelerator onl... | 3 | 2009-12-11T09:03:32Z | 1,886,836 | <p>pyExcelerator is unmaintained, but it has a fork, <a href="http://pypi.python.org/pypi/xlwt">xlwt</a>, which is maintained and has more features, including allowing you to save to any file-like object. This includes saving straight to a Django <code>HttpResponse</code>:</p>
<pre><code>from django.http import HttpRe... | 11 | 2009-12-11T09:24:59Z | [
"python",
"django",
"excel",
"temporary-files",
"pyexcelerator"
] |
IBoutlet with PyObjC and Interface Builder | 1,887,102 | <p>I'm writing a simple OSX app using Python and PyObjC. I designed the settings dialog using Interface Builder and I use ibtool to compile it, then load it from Python. The problem is how to access the controls I have in this window from the Python code? I played around with iPhone development a bit before and I remem... | 1 | 2009-12-11T10:16:27Z | 1,887,447 | <p>I haven't tried this, but you can also define outlets directly in IB. Open the Library panel, select Classes in the segmented control at the top and select your custom class you want to define an outlet for. Let's say you have a NSWindow subclass called MyWindow. Select the NSWindow class in the list, click on the a... | 1 | 2009-12-11T11:28:08Z | [
"python",
"cocoa",
"interface-builder",
"pyobjc"
] |
IBoutlet with PyObjC and Interface Builder | 1,887,102 | <p>I'm writing a simple OSX app using Python and PyObjC. I designed the settings dialog using Interface Builder and I use ibtool to compile it, then load it from Python. The problem is how to access the controls I have in this window from the Python code? I played around with iPhone development a bit before and I remem... | 1 | 2009-12-11T10:16:27Z | 1,889,278 | <p>First, the use of Xcode or not has nothing to do with NIB loading (beyond making it more convenient).</p>
<p>As Ole said, you can use IB to manually add the outlet's you need to file's owner or to the custom object instances that you have in the NIB file. By doing so, it will all "just work".</p>
<p>However, thi... | 3 | 2009-12-11T16:42:35Z | [
"python",
"cocoa",
"interface-builder",
"pyobjc"
] |
Unicode filenames on python 2.6 under Mac OS X | 1,887,215 | <p>I'm using os.walk to create a list of all music files under a folder. Some of these filenames are non-ascii, for example: </p>
<blockquote>
<p>01 ç©ºå³æ¯è².mp3</p>
</blockquote>
<p>I'm using the mutagen library to parse metadata for this file, and it professes complete unicode support. The filename is being r... | 1 | 2009-12-11T10:36:52Z | 1,887,241 | <p>Note that <code>walk(dir)</code> returns the filename <em>without path</em>. If you want to open the file, you must prepend <code>dir</code>:</p>
<pre><code>for dirpath, dirnames, filenames in os.walk(dir):
for filename in filenames:
path = os.path.join(dirpath, filename)
</code></pre>
| 2 | 2009-12-11T10:42:51Z | [
"python",
"unicode"
] |
IronPython examples | 1,887,290 | <p>I have heard IronPython for a long time, but never seen a real-life application using it. </p>
<p>Do you provide some examples to see its power?</p>
<p>Thanks.</p>
| 2 | 2009-12-11T10:52:12Z | 1,887,307 | <p>How did you miss <a href="http://www.resolversystems.com/" rel="nofollow">Resolver Systems</a>? Their <a href="http://www.resolversystems.com/products/resolver-one/" rel="nofollow">Rsolver One</a> spreadsheet application is IronPython.</p>
| 4 | 2009-12-11T10:55:49Z | [
"python",
"ironpython"
] |
Get data back from Jython scripts using JSR-223 | 1,887,320 | <p>I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script:</p>
<pre><code>class Multiplier:
def multiply(self, x, y):
return x * y
Multiplier().multiply(5, 7)
</code></pre>
<p>I should get back ... | 2 | 2009-12-11T10:59:40Z | 1,887,854 | <p><strong>UPDATE:</strong> I was actually missing the goal (and problem) of the OP in my initial answer that has been clarified in a comment. I'm updating my answer accordingly. </p>
<p>First update the <code>Multiplier.py</code> script as below:</p>
<pre><code>class Multiplier:
def multiply(self, x, y):
retu... | 3 | 2009-12-11T12:44:26Z | [
"java",
"python",
"jython",
"jsr223"
] |
Get data back from Jython scripts using JSR-223 | 1,887,320 | <p>I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script:</p>
<pre><code>class Multiplier:
def multiply(self, x, y):
return x * y
Multiplier().multiply(5, 7)
</code></pre>
<p>I should get back ... | 2 | 2009-12-11T10:59:40Z | 1,911,270 | <p>This is a Python language issue more than a Jython or JSR 223 issue. Python differentiates between expressions (which have values) and statements (which don't). The script you're passing is a statement. If you passed an expression, it'd have a value.</p>
<p>The reason you're seeing something different with Ruby ... | 4 | 2009-12-15T23:38:45Z | [
"java",
"python",
"jython",
"jsr223"
] |
Get data back from Jython scripts using JSR-223 | 1,887,320 | <p>I am using Jython 2.5.1 with JSR-223 (i.e. javax.script package) and I expect the last line of the Python script to be returned. For example, after evaluating this script:</p>
<pre><code>class Multiplier:
def multiply(self, x, y):
return x * y
Multiplier().multiply(5, 7)
</code></pre>
<p>I should get back ... | 2 | 2009-12-11T10:59:40Z | 7,654,788 | <p>I had the same problem. I have written a scripting library that supports JSR223 and have submited <a href="http://bugs.jython.org/issue1798" rel="nofollow">http://bugs.jython.org/issue1798</a>:</p>
<pre><code>$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", ... | 1 | 2011-10-04T22:34:36Z | [
"java",
"python",
"jython",
"jsr223"
] |
how to catch close,minimize and other events in this program(pyqt)? | 1,887,352 | <p>The problem here is i want to catch the minimize and close event of QTabWidget(by default this widget do not have any slot named close or minimize) to hide the widget.. so the only way the user can quit the application is by clicking exit from the tray icon menu..</p>
<pre><code>from PyQt4 import QtCore, QtGui
impo... | 2 | 2009-12-11T11:08:21Z | 1,994,601 | <p>I am not sure what you want to achieve. </p>
<p>Do you want to hide the tab widget using some public method of that class? If so you can simply call close() method.</p>
<p>If your requirement is such that you do NOT want to close the tab widget when the application is running then the following is applicable ---</... | 2 | 2010-01-03T10:50:37Z | [
"python",
"pyqt"
] |
how to catch close,minimize and other events in this program(pyqt)? | 1,887,352 | <p>The problem here is i want to catch the minimize and close event of QTabWidget(by default this widget do not have any slot named close or minimize) to hide the widget.. so the only way the user can quit the application is by clicking exit from the tray icon menu..</p>
<pre><code>from PyQt4 import QtCore, QtGui
impo... | 2 | 2009-12-11T11:08:21Z | 19,918,302 | <p>You can implement your own event filter.</p>
<pre><code>class custom(QWidget):
def __init__(self):
super(custom, self).__init__()
self.installEventFilter(self)
def eventFilter(self, qobject, qevent):
qtype = qevent.type()
if qtype == QEvent.Close or qtype == QEvent.WindowSta... | 0 | 2013-11-11T23:49:39Z | [
"python",
"pyqt"
] |
How to compare datetime in Django? | 1,887,354 | <p>Suppose I have:</p>
<pre><code>ds = datetime.datetime.now
dd = Entry.objects.get(pk=id).pub_date
</code></pre>
<p>How to compare 2 objects above? I want to get the time difference between them.
Please help me solve this problem. Thank you very much !</p>
| 4 | 2009-12-11T11:08:49Z | 1,887,367 | <p>I am assuming that <code>pub_date</code> is a <code>django.db.models.DateField</code>, which means you can treat it as a <code>datetime.date</code> object.</p>
<p>If you convert them to the same type (either <code>datetime.datetime</code> or <code>datetime.date</code>) and subtract one from the other, you will get... | 18 | 2009-12-11T11:11:06Z | [
"python",
"django",
"datetime",
"django-views"
] |
How to convert string to hexadecimal integer in Python? | 1,887,392 | <p>hi I get user argv from command line as follows: <code>'0x000aff00'</code></p>
<p>and I want python to treat it as hex directly...</p>
<pre><code>str = sys.argv[1]
</code></pre>
<p>how is it possible? thanks!</p>
| 3 | 2009-12-11T11:18:15Z | 1,887,418 | <p>Try: <code>i = int(sys.argv[1], 16)</code></p>
| 7 | 2009-12-11T11:21:53Z | [
"python",
"integer",
"hex"
] |
How to convert string to hexadecimal integer in Python? | 1,887,392 | <p>hi I get user argv from command line as follows: <code>'0x000aff00'</code></p>
<p>and I want python to treat it as hex directly...</p>
<pre><code>str = sys.argv[1]
</code></pre>
<p>how is it possible? thanks!</p>
| 3 | 2009-12-11T11:18:15Z | 1,887,627 | <pre><code>try:
i = int(sys.argv[1], 16)
except Exception,e:
print e
else:
# carry on
</code></pre>
| 0 | 2009-12-11T12:02:53Z | [
"python",
"integer",
"hex"
] |
Python memchached client library with CAS support | 1,887,431 | <p>I need to use <code>gets</code> and <code>cas</code> (check and set) commands of memcached from Python application. The only Python client library supporting them I found is Twisted. But Twisted requires quite different design of application, so it's not an option. Is there any other full-featured (not listed on <a ... | 2 | 2009-12-11T11:24:56Z | 1,889,090 | <p>I don't see <a href="http://pypi.python.org/pypi/pylibmc" rel="nofollow">pylibmc</a> listed there, but I have no idea if it supports those commands you need (edit: it doesn't, sorry).</p>
<p>/edit: if everything else fails, you could perhaps use <a href="http://code.google.com/p/python-libmemcached/issues/detail?id... | 2 | 2009-12-11T16:11:26Z | [
"python",
"memcached"
] |
Convert Python byte to "unsigned 8 bit integer" | 1,887,506 | <p>I am reading in a byte array/list from socket. I want Python to treat the first byte as an "unsigned 8 bit integer". How is it possible to get its integer value as an unsigned 8 bit integer?</p>
| 12 | 2009-12-11T11:38:47Z | 1,887,569 | <p>bytes/bytearray is a sequence of integers. If you just access an element by its index you'll have an integer:</p>
<pre><code>>>> b'abc'
b'abc'
>>> _[0]
97
</code></pre>
<p>By their very definition, bytes and bytearrays contain integers in the <code>range(0, 256)</code>. So they're "unsigned 8-bit... | 7 | 2009-12-11T11:49:44Z | [
"python",
"python-3.x"
] |
Convert Python byte to "unsigned 8 bit integer" | 1,887,506 | <p>I am reading in a byte array/list from socket. I want Python to treat the first byte as an "unsigned 8 bit integer". How is it possible to get its integer value as an unsigned 8 bit integer?</p>
| 12 | 2009-12-11T11:38:47Z | 1,887,570 | <p>Use the <a href="http://docs.python.org/3.1/library/struct.html#module-struct">struct</a> module.</p>
<pre><code>import struct
value = struct.unpack('B', data[0])[0]
</code></pre>
<p>Note that unpack always returns a tuple, even if you're only unpacking one item.</p>
<p>Also, have a look at <a href="http://stacko... | 17 | 2009-12-11T11:49:51Z | [
"python",
"python-3.x"
] |
Convert Python byte to "unsigned 8 bit integer" | 1,887,506 | <p>I am reading in a byte array/list from socket. I want Python to treat the first byte as an "unsigned 8 bit integer". How is it possible to get its integer value as an unsigned 8 bit integer?</p>
| 12 | 2009-12-11T11:38:47Z | 1,887,722 | <p>Another very reasonable and simple option, if you just need the first byteâs integer value, would be something like the following:</p>
<pre><code>value = ord(data[0])
</code></pre>
<p>If you want to unpack all of the elements of your received data at once (and theyâre not just a homogeneous array), or if you a... | 4 | 2009-12-11T12:19:44Z | [
"python",
"python-3.x"
] |
how to redirect complete output of a script | 1,887,618 | <p>I have a simple cronjob running every day at 18:35:</p>
<pre><code>05 18 * * * ~/job.sh 2>&1 >> ~/job.log
</code></pre>
<p>So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.:</p>
<pre><code>echo 'doing xyz'
p... | 3 | 2009-12-11T12:01:10Z | 1,887,652 | <p>Have you tried <code>~/job.sh &>> ~/job.log</code> ?</p>
| 3 | 2009-12-11T12:07:22Z | [
"python",
"shell",
"cron"
] |
how to redirect complete output of a script | 1,887,618 | <p>I have a simple cronjob running every day at 18:35:</p>
<pre><code>05 18 * * * ~/job.sh 2>&1 >> ~/job.log
</code></pre>
<p>So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.:</p>
<pre><code>echo 'doing xyz'
p... | 3 | 2009-12-11T12:01:10Z | 1,887,663 | <p>You need to redirect the output of the python script. Learning Python 2nd Edition recommends the following:</p>
<pre><code>import sys
sys.stdout = open('log.txt', 'a') # Redirects output to file
...
print x, y, x # shows up in log.txt
</code></pre>
<p>and goes on to say:</p>
<blockquote>
... | 2 | 2009-12-11T12:09:34Z | [
"python",
"shell",
"cron"
] |
how to redirect complete output of a script | 1,887,618 | <p>I have a simple cronjob running every day at 18:35:</p>
<pre><code>05 18 * * * ~/job.sh 2>&1 >> ~/job.log
</code></pre>
<p>So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.:</p>
<pre><code>echo 'doing xyz'
p... | 3 | 2009-12-11T12:01:10Z | 1,887,735 | <p>Arkaitz has the simplest solution. However, to see what's wrong with your snippet we need to go into the bash manual:</p>
<blockquote>
<p>Note that the order of redirections is significant. For example, the
command</p>
<pre><code> ls > dirlist 2>&1
</code></pre>
<p>directs both standard o... | 15 | 2009-12-11T12:21:40Z | [
"python",
"shell",
"cron"
] |
how to redirect complete output of a script | 1,887,618 | <p>I have a simple cronjob running every day at 18:35:</p>
<pre><code>05 18 * * * ~/job.sh 2>&1 >> ~/job.log
</code></pre>
<p>So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.:</p>
<pre><code>echo 'doing xyz'
p... | 3 | 2009-12-11T12:01:10Z | 1,887,751 | <p>I'm quite sure that this should work in a general case. The stdout file descriptor is changed by the OS when you use shell redirection. </p>
<p>However, if your Python script itself directly writes to the screen (perhaps by opening /dev/tty), it won't get captured in your log.txt. Is that the case? Does it fail eve... | 1 | 2009-12-11T12:26:32Z | [
"python",
"shell",
"cron"
] |
Python setuptools custom configuration | 1,887,641 | <p>I'm packaging up a Python module, and I would like users to be able to build the module with some custom options. Specifically, the package will do some extra magic if you provide it with certain executables that it can use.</p>
<p>Ideally, users would run <code>setup.py install</code> or <code>setup.py install --m... | 6 | 2009-12-11T12:05:13Z | 1,887,699 | <p>It seems you can... read <a href="http://tarekziade.wordpress.com/2007/09/30/extending-setuptools-adding-a-new-command/">this</a>.</p>
<p>Extract from article:</p>
<blockquote>
<p>Commands are simple class that derives from setuptools.Command, and define some minimum elements, which are:</p>
</blockquote>
<pre>... | 6 | 2009-12-11T12:16:52Z | [
"python",
"setup.py"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,887,701 | <p>You can of course use a dictionary, to look up the response:</p>
<pre><code>lst = ['one', 'two', 'three']
resp = { 'one': num1, 'two': num2, 'three': num3 }
for item in lst:
print resp[item]
</code></pre>
<p>This is still pretty static, though. Another approach would be object-orienting it, so you get to implem... | 5 | 2009-12-11T12:17:23Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,887,725 | <pre><code>>>> tups = ('one', 33), ('two', 45), ('three', 76)
>>> for i, j in tups:
print(j)
33
45
76
</code></pre>
| 5 | 2009-12-11T12:20:09Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,887,727 | <p>Is it intentional that your code ignores objects that are not mentioned in any if/elif clause? If so, use a dictionary with a default value of 'None' if the object is not found:</p>
<pre><code>lst = ['one', 'two', 'three']
d = { 'one': 33, 'two': 45, 'three': 76}
for item in lst:
x = d.get(item)
if x is ... | 4 | 2009-12-11T12:20:24Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,887,933 | <p>the whole logic of your if/else is equivalent to a dictionary's key and value pairs</p>
<pre><code>d = {"one":33, "two":44, "three":76}
</code></pre>
<p>this part of your code</p>
<pre><code>if item == 'one':
print num1
</code></pre>
<p>is the same as </p>
<pre><code>print d["one"]
</code></pre>
<p>like wi... | 2 | 2009-12-11T13:00:55Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,887,994 | <p>If you have dictionary like this:</p>
<pre><code>d = {"one":33, "two":44, "three":76}
</code></pre>
<p>You can print it like this:</p>
<pre><code>for k in d.keys():
print d[k]
</code></pre>
<p>This presumes that you do not care about the order.</p>
| 1 | 2009-12-11T13:13:53Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,888,078 | <p>For your simple example a dictinary lookup poposed in other answers is the best. But sometimes you need to run completely different code for each condition, so the following idiom might be useful too:</p>
<pre><code>class MyClass(object):
def process(self, item):
# Select the method to call based on it... | 1 | 2009-12-11T13:30:51Z | [
"python"
] |
Simplifying small code example | 1,887,690 | <p>Lets pretend I have the following code.</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three']
for item in lst:
if item == 'one':
print num1
elif item == 'two':
print num2
elif item == 'three':
print num3
</code></pre>
<p>Is there a way to make this more ele... | 0 | 2009-12-11T12:14:34Z | 1,888,651 | <p>When there's no correlation between the if clause and the prints, you can create a mapping dictionary to store the correlations. You need to be careful to map to the variable of numx, not the current value (thus the use of the eval function):</p>
<pre><code>num1 = 33
num2 = 45
num3 = 76
lst = ['one', 'two', 'three... | 1 | 2009-12-11T15:02:49Z | [
"python"
] |
python string as hex terminated with null | 1,887,707 | <p>I receive strings in their hex format, i.e. <code>s = "0x0ff"</code> or <code>s = "0fd"</code>
how can I check whether the above type of strings are null terminated or not? thanks!</p>
| 1 | 2009-12-11T12:18:22Z | 1,887,730 | <p>Try</p>
<pre><code>s[-2:] == "00"
</code></pre>
| 1 | 2009-12-11T12:20:58Z | [
"python",
"python-3.x"
] |
python string as hex terminated with null | 1,887,707 | <p>I receive strings in their hex format, i.e. <code>s = "0x0ff"</code> or <code>s = "0fd"</code>
how can I check whether the above type of strings are null terminated or not? thanks!</p>
| 1 | 2009-12-11T12:18:22Z | 1,887,734 | <p>Why would you care wether python strings are null terminated?</p>
<p>If you want to check wether the strings start by "0x" you can just use</p>
<pre><code>x.startswith("0x")
</code></pre>
<p>as x cannot be located anywhere else in a hexstring.</p>
| 0 | 2009-12-11T12:21:40Z | [
"python",
"python-3.x"
] |
Embed pickle (or arbitrary) data in python script | 1,887,968 | <p>In Perl, the interpreter kind of stops when it encounters a line with</p>
<pre><code>__END__
</code></pre>
<p>in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities.</p... | 4 | 2009-12-11T13:09:26Z | 1,887,979 | <p>In Python, you can use """ (triple-quoted strings) to embed long runs of text data in your program.</p>
<p>In your case, however, don't waste time on this.</p>
<p>If you have an object you've pickled, you'd be much, much happier dumping that object as Python source and simply including the source.</p>
<p>The <cod... | 1 | 2009-12-11T13:10:40Z | [
"python",
"embed"
] |
Embed pickle (or arbitrary) data in python script | 1,887,968 | <p>In Perl, the interpreter kind of stops when it encounters a line with</p>
<pre><code>__END__
</code></pre>
<p>in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities.</p... | 4 | 2009-12-11T13:09:26Z | 1,887,993 | <p>With pickle you can also work directly on strings.</p>
<pre><code>s = pickle.dumps(obj)
pickle.loads(s)
</code></pre>
<p>If you combine that with """ (triple-quoted strings) you can easily store any pickled data in your file.</p>
| 4 | 2009-12-11T13:13:49Z | [
"python",
"embed"
] |
Embed pickle (or arbitrary) data in python script | 1,887,968 | <p>In Perl, the interpreter kind of stops when it encounters a line with</p>
<pre><code>__END__
</code></pre>
<p>in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities.</p... | 4 | 2009-12-11T13:09:26Z | 1,891,877 | <p>If the data is not particularly large (many K) I would just .encode('base64') it and include that in a triple-quoted string, with .decode('base64') to get back the binary data, and a pickle.loads() call around it.</p>
| 3 | 2009-12-12T01:21:26Z | [
"python",
"embed"
] |
Embed pickle (or arbitrary) data in python script | 1,887,968 | <p>In Perl, the interpreter kind of stops when it encounters a line with</p>
<pre><code>__END__
</code></pre>
<p>in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities.</p... | 4 | 2009-12-11T13:09:26Z | 4,952,763 | <p>I made this code. You run something like python comp.py foofile.tar.gz, and it creates decomp.py, with foofile.tar.gz's contents embedded in it. I don't think this is really portable with windows because of the Popen though.</p>
<pre><code>import base64
import sys
import subprocess
inf = open(sys.argv[1],"r+b").... | 1 | 2011-02-10T02:20:36Z | [
"python",
"embed"
] |
Python hexadecimal comparison | 1,888,114 | <p>Hey guys, I got a problem I was hoping someone could help me figure out! </p>
<p>I have a string with a hexadecimal number = <code>'0x00000000'</code> which means: </p>
<pre><code>0x01000000 = apple
0x00010000 = orange
0x00000100 = banana
</code></pre>
<p>All combinations with those are possible. i.e., <code... | 3 | 2009-12-11T13:35:41Z | 1,888,133 | <p>convert your string to an integer, by using the <code>int()</code> built-in function and specifying a base:</p>
<pre><code>>>> int('0x01010000',16)
16842752
</code></pre>
<p>now you have a standard integer representing a bitset. use <code>&</code>, <code>|</code> and any other bitwise operator to test... | 9 | 2009-12-11T13:38:51Z | [
"python",
"hex"
] |
Python hexadecimal comparison | 1,888,114 | <p>Hey guys, I got a problem I was hoping someone could help me figure out! </p>
<p>I have a string with a hexadecimal number = <code>'0x00000000'</code> which means: </p>
<pre><code>0x01000000 = apple
0x00010000 = orange
0x00000100 = banana
</code></pre>
<p>All combinations with those are possible. i.e., <code... | 3 | 2009-12-11T13:35:41Z | 1,888,142 | <p>You could first of all convert your string to an integer:</p>
<pre><code>s = "0x01010000"
i = int(s, 16) #i = 269484032
</code></pre>
<p>then, you could set up a list for the fruits:</p>
<pre><code>fruits = [(0x01000000, "apple"), (0x00010000, "orange"), (0x00000100, "banana")]
</code></pre>
<p>for determing wha... | 2 | 2009-12-11T13:40:53Z | [
"python",
"hex"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.