Re: Exception Handling (C - extending python)
Lee, 23.10.2011 06:09: Where does PyExc_TypeError (and alike) points to? I can see its declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h but I cannot figure out what it is its value, where it is initialized. It gets initialised inside of the interpreter core and then points to a Python object. Any help is greatly appreciated. The question is: why do you ask? What exactly do you want to do? If you ask a more targeted question, you will get an answer that will help you further. Stefan -- http://mail.python.org/mailman/listinfo/python-list
What is wrong with my code?
import os nome = sys.argv[1] final = nome for i in nome: print i if nome[i] = "_": final[i] = " " os.rename(nome, final) -- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
On Sun, Oct 23, 2011 at 3:03 AM, apometron wrote: > import os > nome = sys.argv[1] You did not `import sys`, so you'll get a NameError there. > final = nome > for i in nome: > print i > if nome[i] = "_": > final[i] = " " Strings aren't mutable in Python; you can't assign to slices of them. So you'll get a TypeError on the previous line. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to isolate a constant?
Gnarlodious writes: > Thanks for all those explanations, I've already fixed it with a tuple. > Which is more reliable anyway. neither of lists or tuples are "more reliable" than the other. They both have perfectly well defined behaviour (which can be gleaned from reading the documentation) and reliably behave as documented. You just have to choose which fits better for the computation you're trying to implement. -- http://mail.python.org/mailman/listinfo/python-list
Deditor 0.3.1
Hey, I'm happy to announce a new release of Deditor version 0.3.1. What is Deditor? Deditor is a pythonic text-editor, written 100% in python and with the primary goal to ease python development. There is a python shell, codecompletion, code analyzing, instant code running and error checking and lots more. (Other languages are ofcourse also supported for syntax highlighting but don't have the other features) Deditor uses a very good plugin system DPlug which makes it easy to use a combination of plugins like a projects plugin to manage files, a network plugin to up-download stuff,... What's new in this version? The Projects plugin has been totally rewritten and is now a very usefull plugin which makes developing much easier if you have to work at multiple projects in short periods. Note that the network plugin is going to be totally rewritten for next release and is thus not that good atm, nevertheless enjoy! Download .deb/.tar.gz http://launchpad.net/deditor Download ppa: ppa:darragh-ssa/deditor and sudo apt-get install deditor Note: ppa is still in building proces but should be finished this afternoon pictures/videos of the new version are still in progress of creation but here is a short snapshot https://lh6.googleusercontent.com/-oopTygoo6o4/TqCwLY4EoRI/Abw/19J1jDq4yIU/deditor_project_0.3.1.png -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling (C - extending python)
Thanks Stefan, I am just interested to understand the mechanism inside python. If it points to an object that means I can defered it (through ob_type). >From there, how a function like PyErr_SetString knows what exception is? Where its value is kept? Lee On Oct 23, 10:06 pm, Stefan Behnel wrote: > Lee, 23.10.2011 06:09: > > > Where does PyExc_TypeError (and alike) points to? I can see its > > declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h > > but I cannot figure out what it is its value, where it is > > initialized. > > It gets initialised inside of the interpreter core and then points to a > Python object. > > > Any help is greatly appreciated. > > The question is: why do you ask? What exactly do you want to do? > > If you ask a more targeted question, you will get an answer that will help > you further. > > Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling (C - extending python)
Am 23.10.2011 06:09, schrieb Lee: > Hi all, > > Where does PyExc_TypeError (and alike) points to? I can see its > declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h > but I cannot figure out what it is its value, where it is > initialized. It's initialized in Objects/exceptions.c SimpleExtendsException(PyExc_StandardError, TypeError, "Inappropriate argument type."); SimpleExtendsException() is a macro that defines a PyTypeObject and stores a cast to PyObject in PyExc_TypeError. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling (C - extending python)
Hi, note that I reformatted your posting to get the replies back into order. Lee, 23.10.2011 13:32: On Oct 23, 10:06 pm, Stefan Behnel wrote: Lee, 23.10.2011 06:09: Where does PyExc_TypeError (and alike) points to? I can see its declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h but I cannot figure out what it is its value, where it is initialized. It gets initialised inside of the interpreter core and then points to a Python object. If it points to an object that means I can defered it (through ob_type). That will give you the "type" object, because PyExc_TypeError points to the type "TypeError". Note that types are also objects in Python. From there, how a function like PyErr_SetString knows what exception is? The type object that you pass in must inherit from the BaseException type. You can read the code in Python/errors.c, function PyErr_SetObject(). Any help is greatly appreciated. The question is: why do you ask? What exactly do you want to do? If you ask a more targeted question, you will get an answer that will help you further. I am just interested to understand the mechanism inside python. That's just fine. If you are interested in the inner mechanics of the CPython runtime, reading the source is a very good way to start getting involved with the project. However, many extension module authors don't care about these inner mechanics and just use Cython instead. That keeps them from having to learn the C-API of CPython, and from tying their code too deeply into the CPython runtime itself. Stefan -- http://mail.python.org/mailman/listinfo/python-list
python32 to write file
code 1 can run in python2.6
#coding:utf-8
import urllib
import lxml.html
down='http://frux.wikispaces.com/'
root=urllib.urlopen(down).read()
root=lxml.html.fromstring(root)
file=root.xpath('//a')
for i in file:
str1=i.text_content()
if str1.find('pdf') >-1 :
str2='http://frux.wikispaces.com/file/view/'+str1
myfile=urllib.urlopen(str2).read()
book=open('/tmp/'+str1,'w')
book.write(myfile)
book.close()
i usr command : 2to3-3.2 ~/xml.py -w to get code2
#coding:utf-8
import urllib.request, urllib.parse, urllib.error
import lxml.html
down='http://frux.wikispaces.com/'
root=urllib.request.urlopen(down).read()
root=lxml.html.fromstring(root)
file=root.xpath('//a')
for i in file:
str1=i.text_content()
if str1.find('pdf') >-1 :
str2='http://frux.wikispaces.com/file/view/'+str1
myfile=urllib.request.urlopen(str2).read()
book=open('c:\'+str1,'w') # i change it
book.write(myfile)
book.close()
when i run it in python32,the output is :
book=open('c:\'+str1,'w')
invalid syntax,what is wrong?--
http://mail.python.org/mailman/listinfo/python-list
Re: python32 to write file
2011/10/23 水静流深 <[email protected]>: > book=open('c:\'+str1,'w') # i change it > when i run it in python32,the output is : > book=open('c:\'+str1,'w') > invalid syntax,what is wrong? Your problem is not at all Python 3-specific. Backslashes are used for escape sequences in string literals (e.g. "\n" is newline, "\t" is tab). For example, the string "c:\new\tally" contains both a newline and a tab, but not an N, nor a T, nor any backslashes (a literal backslash is written using the escape sequence "\\"; i.e. two backslashes). Similarly, "\'" is an escape sequence for apostrophe, and thus does not terminate the string literal, leading to a not entirely obvious SyntaxError. Use forward slashes (/) instead; Windows accepts them instead of backslashes as directory separators in path strings, and they have no such escaping issues. Cheers, Chris -- Damn you, CP/M! http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
回复: python32 to write file
i change my code into :
import urllib.request, urllib.parse, urllib.error
import lxml.html
down='http://frux.wikispaces.com/'
root=urllib.request.urlopen(down).read()
root=lxml.html.fromstring(root)
file=root.xpath('//a')
for i in file:
str1=i.text_content()
if str1.find('pdf') >-1 :
str2='http://frux.wikispaces.com/file/view/'+str1
myfile=urllib.request.urlopen(str2).read()
book=open('c:/'+str1,'w')
book.write(myfile)
book.close()
the new problem is :
C:\Python32>python c:\xml.py
Traceback (most recent call last):
File "c:\xml.py", line 5, in
root=lxml.html.fromstring(root)
File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 630, in froms
tring
if start.startswith(';
发送时间: 2011年10月23日(星期天) 晚上9:48
收件人: "水静流深"<[email protected]>;
抄送: "python-list";
主题: Re: python32 to write file
2011/10/23 水静流深 <[email protected]>:
> book=open('c:\'+str1,'w') # i change it
> when i run it in python32,the output is :
> book=open('c:\'+str1,'w')
> invalid syntax,what is wrong?
Your problem is not at all Python 3-specific.
Backslashes are used for escape sequences in string literals (e.g.
"\n" is newline, "\t" is tab). For example, the string "c:\new\tally"
contains both a newline and a tab, but not an N, nor a T, nor any
backslashes (a literal backslash is written using the escape sequence
"\\"; i.e. two backslashes).
Similarly, "\'" is an escape sequence for apostrophe, and thus does
not terminate the string literal, leading to a not entirely obvious
SyntaxError.
Use forward slashes (/) instead; Windows accepts them instead of
backslashes as directory separators in path strings, and they have no
such escaping issues.
Cheers,
Chris
--
Damn you, CP/M!
http://rebertia.com--
http://mail.python.org/mailman/listinfo/python-list
File Association for Python on XP
Last night I thought I'd install Python on a friend's XP PC. I noticed he had declined to show extensions on any file. I thought the Control Panel might be the way to do it. I couldn't find anything that would do all files, doc, txt, py, etc. I was able to do it, I think, from a right-click on a py file. However, when I then clicked on the file, it seemed like the IDE was not going to come up. Instead it looked like it was headed directly for interpreter. His computer is quite slow. We went on to something else. Python doesn't yet need that much attention. So what should I have done, and how do I undo what I did? -- http://mail.python.org/mailman/listinfo/python-list
Job Offer: 3 Python Backend Developer and other Positions in Berlin
Hi Folks, hope it is okay to post job offers here. If not sorry for the spam and please let me know! One of our clients is looking for new and talented people to join their international, seven people strong, developers team in the heart of Berlin. You will join an awesome, nice and humble startup and you can enjoy the coffee and smoothie flatrate whilst playing a quick round of foosball in between. The team is working with Scrum and TDD so everybody stays involved and English is the language of choice. Furthermore they go out for a beer now and then and the company can even provide you with housing for the first couple of weeks in Berlin. Excited? Here are the job details: // Python Backend Developer You speak Python whilst dreaming and you do have experience with Django or RoR? You are not afraid of Javascript, HTML, CSS nor unit or functional testing (cucumber, lettuce) and github is your source codes living room? // Java Backend Developer You’ve worked on a professional Java (J2EE, J2SE 5/6) application at least for two years and you liked Spring and Hibernate on Facebook? Even the following technologies: Lucenel Solr, Tomcat, JBoss and GIT don’t make you sweat? // Technical QA Manager You mainly discuss about object oriented programming concepts with your friends? xUnit, TDD, BDD in combination with Scrum and XP raise your heartbeat? You call Ubuntu, Nginx, PostgreSQL as well as Python, Django and GIT your daily bread? // Frontend Developer (Javascript) You are a talent with Javascript in general and particularly with jQuery, Require.js and Backbone.js? You do testing and object orientated coding by heart and the user experience always comes in the first place for you? If so, request more details and apply: Tweet @webcrowdnet or email at [email protected] or just reply here for further information. -- http://mail.python.org/mailman/listinfo/python-list
Re: help
Dne 22.10.2011 17:02, Steven D'Aprano napsal(a): Rather than assume malice, we should give X1 the benefit of the doubt and assume he genuinely believed what he wrote but was merely mistaken. Sure, I didn't want to assume malice (sorry, English is my second language and sometimes it shows; would "libel" or "slander" fit the bill better?). I just wanted to slip in the information about repoquery which is an awesome tool, but not many people know about it. Matěj -- http://mail.python.org/mailman/listinfo/python-list
Re: help
"Libel" and "slander" also generally indicate malice. Perhaps just "That's incorrect" might have come off a little less harsh. :-) - Dave On Oct 23, 2011, at 12:05 PM, Matej Cepl wrote: > Dne 22.10.2011 17:02, Steven D'Aprano napsal(a): >> Rather than assume malice, we should give X1 the benefit of the doubt and >> assume he genuinely believed what he wrote but was merely mistaken. > > Sure, I didn't want to assume malice (sorry, English is my second language > and sometimes it shows; would "libel" or "slander" fit the bill better?). I > just wanted to slip in the information about repoquery which is an awesome > tool, but not many people know about it. > > Matěj > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: File Association for Python on XP
On 23/10/2011 15:57, W. eWatson wrote: Last night I thought I'd install Python on a friend's XP PC. I noticed he had declined to show extensions on any file. I thought the Control Panel might be the way to do it. I couldn't find anything that would do all files, doc, txt, py, etc. I was able to do it, I think, from a right-click on a py file. However, when I then clicked on the file, it seemed like the IDE was not going to come up. Instead it looked like it was headed directly for interpreter. His computer is quite slow. We went on to something else. Python doesn't yet need that much attention. So what should I have done, and how do I undo what I did? To show the extensions, in an Explorer window go to Tools->Folder Options... and look in the View tab. You can also look at the file associations in the File Types tab. -- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
On 10/23/2011 03:08 AM, Chris Rebert wrote: On Sun, Oct 23, 2011 at 3:03 AM, apometron wrote: import os nome = sys.argv[1] You did not `import sys`, so you'll get a NameError there. final = nome for i in nome: print i if nome[i] = "_": final[i] = " " Strings aren't mutable in Python; you can't assign to slices of them. So you'll get a TypeError on the previous line. Cheers, Chris Also, the statement for i in nome: does not loop through indices, but rather it loops through the actual characters of the string. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: File Association for Python on XP
On 10/23/2011 9:41 AM, MRAB wrote: To show the extensions, in an Explorer window go to Tools->Folder Options... and look in the View tab. You can also look at the file associations in the File Types tab. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Job Offer: 3 Python Backend Developer and other Positions in Berlin
On Sun, 23 Oct 2011 17:50:54 +0200, webcrowd.net wrote: > hope it is okay to post job offers here. If not sorry for the spam and > please let me know! Not really. It's a newsgroup on Python *language*, not on Python-everything. You might want to post here instead, though: http://www.python.org/community/jobs/ Best regards, Waldek -- http://mail.python.org/mailman/listinfo/python-list
Re: 回复: python32 to write file
On 10/23/2011 9:59 AM, 水静流深 wrote:
i change my code into :
Calling your file xml.py (as indicated below) is a potentially bad idea
since the Python stdlib has a package named 'xml'. If you write 'import
xml.xxx' in another file in the same directory, Python will try to find
'xxx' in your xml.py file.
import urllib.request, urllib.parse, urllib.error
import lxml.html
Are you sure you have a version of lxml that works with Python 3?
down='http://frux.wikispaces.com/'
root=urllib.request.urlopen(down).read()
What type of object is returned and bound to root? (print(type(root)) if
doc not clear.)
root=lxml.html.fromstring(root)
What type of object is root required to be (from lxml docs)?
[snip]
the new problem is :
C:\Python32>python c:\xml.py
Traceback (most recent call last):
File "c:\xml.py", line 5, in
root=lxml.html.fromstring(root)
File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 630, in
fromstring
if start.startswith('
This implies that the name 'start' is bound to bytes when it should be
(for 3.2) bound to unicode, which would most likely mean that 'root' is
the wrong type. Or that the above is the 2.x version of lxml where
'
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling (C - extending python)
For a moment, back to the basics... I am using the example provided by docs at 2.1.2 "Providing finer control...". Using say: mynoddy = noddy2.Noddy() mynoddy.first = "a" mynoddy.last = 0 the last line causes an ugly crash (on python 2.6.5 on winxp). No way to catch the exception. As I understand from the docs, all PyErr_SetString does is to set an exception, what one has to do to raise this exception (in C)? If I replace "return -1" in the Noddy_setlast() function with "return NULL" (well, the compiler will complain...) the program passes by without raising an exception. Any explanations?... Lee -- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
On 10/23/2011 06:03 AM, apometron wrote: import os nome = sys.argv[1] final = nome for i in nome: print i if nome[i] = "_": final[i] = " " os.rename(nome, final) What do you want to be wrong with it? There are so many things, it'd be fun to try to see who could come up with the most. 1) it's not a valid Fortran program. 2) it's missing a shebang line if we assume it's for Windows, or that you run it with an explicit bash line 3) if we pretend it's a python program, a few more 3a) It has a syntax error calling the print() function. (Python 3.2) If we assume it's a python 2.x program 4) it uses sys, without importing it 5) it uses second argument without checking if the user typed such an argument 6) it tries to change a character within a string, which is a non-mutable type 7) It creates two more references to the same string sys.argv[1], then tries to modify one of them, not realizing the others would change to. 8) it tries to subscript a string using a character. 9) it calls rename with two references to the same object. So nothing will ever actually happen, even if the other problems were fixed. Generally, you'll get the best answers here if you specify more of your environment (python version, OS), show what you tried (pasted from the command line), and the results you got (such as stack traces). HTH DaveA -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: logging: warn() methods and function to be deprecated.
On 22/10/2011 11:09 PM, Vinay Sajip wrote: In response to an issue (#13235) raised on the Python bug tracker, I'm going to deprecate the warn() methods in the Logger and LoggerAdapter classes in the stdlib logging package, as well the module-level warn() function. The warn() variants were synonyms for the warning() methods and function, and a holdover from before the time that logging was added to Python.They were not documented; it's probably time to retire them, so I've added a DeprecationWarning to appear in 3.3, and they'll be completely removed in 3.4 (along with the WARN synonym for WARNING). With this change, all the logging levels are adjectives which apply to the logged message: DEBUG, INFO, WARNING, ERROR and CRITICAL. I don't believe the WARN/warn variants were used much, if at all - but this is just a heads up for anyone who might have used them. I think that is a real shame - it seems to be gratuitous breakage for almost zero benefit. That issue shows that Trac makes heavy use of .warn, I've use .warn almost exclusively for many years, and code.google.com shows it is used extensively in the wild. Is there still a chance to reconsider? Mark -- http://mail.python.org/mailman/listinfo/python-list
getroot() problem
C:\Documents and Settings\peng>cd c:\python32 C:\Python32>python Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml.html >>> sfile='http://finance.yahoo.com/q/op?s=A+Options' >>> root=lxml.html.parse(sfile).getroot() there is no problem to parse : http://finance.yahoo.com/q/op?s=A+Options' why i can not parse http://frux.wikispaces.com/ ?? >>> import lxml.html >>> sfile='http://frux.wikispaces.com/' >>> root=lxml.html.parse(sfile).getroot() Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 692, in parse return etree.parse(filename_or_url, parser, base_url=base_url, **kw) File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:5 4187) File "parser.pxi", line 1528, in lxml.etree._parseDocument (src/lxml/lxml.etre e.c:79485) File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL (src/lxml/lx ml.etree.c:79768) File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile (src/lxml/lxml.e tree.c:78843) File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile (src/ lxml/lxml.etree.c:75698) File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDo c (src/lxml/lxml.etree.c:71739) File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.e tree.c:72614) File "parser.pxi", line 583, in lxml.etree._raiseParseError (src/lxml/lxml.etr ee.c:71927) IOError: Error reading file 'b'http://frux.wikispaces.com/'': b'failed to load e xternal entity "http://frux.wikispaces.com/";' >>>-- http://mail.python.org/mailman/listinfo/python-list
Re: getroot() problem
On 10/23/2011 09:06 PM, wrote: C:\Documents and Settings\peng>cd c:\python32 C:\Python32>python Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. import lxml.html sfile='http://finance.yahoo.com/q/op?s=A+Options' root=lxml.html.parse(sfile).getroot() there is no problem to parse : http://finance.yahoo.com/q/op?s=A+Options' why i can not parse http://frux.wikispaces.com/ ?? import lxml.html sfile='http://frux.wikispaces.com/' root=lxml.html.parse(sfile).getroot() Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 692, in parse return etree.parse(filename_or_url, parser, base_url=base_url, **kw) File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:5 4187) File "parser.pxi", line 1528, in lxml.etree._parseDocument (src/lxml/lxml.etre e.c:79485) File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL (src/lxml/lx ml.etree.c:79768) File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile (src/lxml/lxml.e tree.c:78843) File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile (src/ lxml/lxml.etree.c:75698) File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDo c (src/lxml/lxml.etree.c:71739) File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.e tree.c:72614) File "parser.pxi", line 583, in lxml.etree._raiseParseError (src/lxml/lxml.etr ee.c:71927) IOError: Error reading file 'b'http://frux.wikispaces.com/'': b'failed to load e xternal entity "http://frux.wikispaces.com/";' > Double-spacing makes your message much harder to read. I can only comment in a general way, in any case. most html is mal-formed, and not legal html. Although I don't have any experience with parsing it, I do with xml which has similar problems. The first thing I'd do is to separate the loading of the byte string from the website, from the parsing of those bytes. Further, I'd make a local copy of those bytes, so you can do testing repeatably. For example, you could run wget utility to copy the bytes locally and create a file. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: compare range objects
To compare two instances of objects defined by others in the same class or in derived classes from the same base class is an old problem in OOP. -- http://mail.python.org/mailman/listinfo/python-list
How to use shell return value like $? In python?
exp:
os.system('ls -al')
#I like to catch return value after this command. 0 or 1,2,3
does python support to get "$?"?
then I can use something like:
If $?==0:
TIA
david
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to use shell return value like $? In python?
On Oct 23, 7:44 pm, [email protected] wrote: > exp: > os.system('ls -al') > #I like to catch return value after this command. 0 or 1,2,3 > does python support to get "$?"? > then I can use something like: > If $?==0: > > > TIA > david So for what I do is: r_number =os.system('ls -al') if r_number == 0 . . any other way? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use shell return value like $? In python?
On Sun, Oct 23, 2011 at 7:51 PM, wrote: > On Oct 23, 7:44 pm, [email protected] wrote: >> exp: >> os.system('ls -al') >> #I like to catch return value after this command. 0 or 1,2,3 >> does python support to get "$?"? >> then I can use something like: >> If $?==0: > So for what I do is: > r_number =os.system('ls -al') > if r_number == 0 > . > . > any other way? I would recommend using the `subprocess` module instead: http://docs.python.org/library/subprocess.html#convenience-functions Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use shell return value like $? In python?
On Oct 23, 2011, at 10:44 PM, [email protected] wrote: > exp: > os.system('ls -al') > #I like to catch return value after this command. 0 or 1,2,3 > does python support to get "$?"? > then I can use something like: > If $?==0: > > From the manual (http://docs.python.org/library/os.html#os.system): "On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent." From the linked wait() documentation, the data returned is in a 16-bit integer, with the high byte indicating the exit status (the low byte is the signal that killed the process). So: status = os.system("foo") retval, sig = ((status >> 8) & 0xFF), (status & 0xFF) In the above example, your return status will end up in "retval". Of course, you probably ought to be using subprocess to run your subprocesses anyway; it's a lot more powerful and a lot harder to enable things like shell injection attacks. See: http://docs.python.org/library/subprocess.html#subprocess-replacements (which, of course, shows a direct replacement for os.system which is just as vulnerable to shell injection) - Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
Sorry to continue discussing my thread on this list, I already subbed on the Tutor list but I need to reply and if possible, some ideas of why it dont works. Now it is another thing, entirely. Rename1.py and Rename2.py works, but why Rename3.py dont works? http://pastebin.com/dExFtTkp Thanks by the gentle support. []s Apometron On 10/23/2011 8:56 PM, Dave Angel wrote: On 10/23/2011 06:03 AM, apometron wrote: import os nome = sys.argv[1] final = nome for i in nome: print i if nome[i] = "_": final[i] = " " os.rename(nome, final) What do you want to be wrong with it? There are so many things, it'd be fun to try to see who could come up with the most. 1) it's not a valid Fortran program. 2) it's missing a shebang line if we assume it's for Windows, or that you run it with an explicit bash line 3) if we pretend it's a python program, a few more 3a) It has a syntax error calling the print() function. (Python 3.2) If we assume it's a python 2.x program 4) it uses sys, without importing it 5) it uses second argument without checking if the user typed such an argument 6) it tries to change a character within a string, which is a non-mutable type 7) It creates two more references to the same string sys.argv[1], then tries to modify one of them, not realizing the others would change to. 8) it tries to subscript a string using a character. 9) it calls rename with two references to the same object. So nothing will ever actually happen, even if the other problems were fixed. Generally, you'll get the best answers here if you specify more of your environment (python version, OS), show what you tried (pasted from the command line), and the results you got (such as stack traces). HTH DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use shell return value like $? In python?
David Riley wrote: > On Oct 23, 2011, at 10:44 PM, [email protected] wrote: > > > exp: > > os.system('ls -al') > > #I like to catch return value after this command. 0 or 1,2,3 > > does python support to get "$?"? > > then I can use something like: > > If $?==0: > > > > > > From the manual (http://docs.python.org/library/os.html#os.system): > > "On Unix, the return value is the exit status of the process encoded in the > format specified for wait(). Note that POSIX does not specify the meaning of > the return value of the C system() function, so the return value of the > Python function is system-dependent." > > From the linked wait() documentation, the data returned is in a 16-bit > integer, with the high byte indicating the exit status (the low byte is the > signal that killed the process). So: > > > > > status = os.system("foo") > > retval, sig = ((status >> 8) & 0xFF), (status & 0xFF) > ... or retval, sig = os.WEXITSTATUS(status), os.WTERMSIG(status) for some insulation from low-level details. Nick > > > > In the above example, your return status will end up in "retval". > > Of course, you probably ought to be using subprocess to run your subprocesses > anyway; it's a lot more powerful and a lot harder to enable things like shell > injection attacks. See: > http://docs.python.org/library/subprocess.html#subprocess-replacements > (which, of course, shows a direct replacement for os.system which is just as > vulnerable to shell injection) > > > - Dave > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
On Sun, Oct 23, 2011 at 8:21 PM, apometron wrote: > Sorry to continue discussing my thread on this list, I already subbed on the > Tutor list > but I need to reply and if possible, some ideas of why it dont works. Now it > is another > thing, entirely. Rename1.py and Rename2.py works, but why Rename3.py dont > works? You haven't specified in what way it's not working. What error are you getting, or what undesired/unexpected behavior are you observing? - Chris -- http://mail.python.org/mailman/listinfo/python-list
install lxml
there are two python versions in my computer, python2.7 is the default,python3.2 is the second install. for python2.7 ~$python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml >>> for python3.2 ~$python3.2 Python 3.2.2 (default, Oct 24 2011, 10:33:35) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml Traceback (most recent call last): File "", line 1, in ImportError: No module named lxml i want to install lxml in python3.2. how can i do?-- http://mail.python.org/mailman/listinfo/python-list
Re: What is wrong with my code?
The problem is that it is not reporting any error. The do something and quits silently. No rename action is done and no error. Running Python 2.7 on Windows Seven. I know that it is a bit tricky to help someone so, but I dont have any info to pass that be good to understand the situation. Thanks by the effort in help me. I appreciate every character of it. []s Apometron On 10/24/2011 1:27 AM, Chris Rebert wrote: On Sun, Oct 23, 2011 at 8:21 PM, apometron wrote: Sorry to continue discussing my thread on this list, I already subbed on the Tutor list but I need to reply and if possible, some ideas of why it dont works. Now it is another thing, entirely. Rename1.py and Rename2.py works, but why Rename3.py dont works? You haven't specified in what way it's not working. What error are you getting, or what undesired/unexpected behavior are you observing? - Chris -- http://mail.python.org/mailman/listinfo/python-list
回复: getroot() problem
in my computer,there two os , 1.xp+python32 import lxml.html sfile='http://finance.yahoo.com/q/op?s=A+Options' root=lxml.html.parse(sfile).getroot() it is ok import lxml.html sfile='http://frux.wikispaces.com/' root=lxml.html.parse(sfile).getroot() there is problem Traceback (most recent call last): File "", line 1, in File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 692, in parse return etree.parse(filename_or_url, parser, base_url=base_url, **kw) File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:5 4187) File "parser.pxi", line 1528, in lxml.etree._parseDocument (src/lxml/lxml.etre e.c:79485) File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL (src/lxml/lx ml.etree.c:79768) File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile (src/lxml/lxml.e tree.c:78843) File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile (src/ lxml/lxml.etree.c:75698) File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDo c (src/lxml/lxml.etree.c:71739) File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.e tree.c:72614) File "parser.pxi", line 583, in lxml.etree._raiseParseError (src/lxml/lxml.etr ee.c:71927) IOError: Error reading file 'b'http://frux.wikispaces.com/'': b'failed to load e xternal entity "http://frux.wikispaces.com/";' 2. ubuntu11.04+python2.6 import lxml.html sfile='http://frux.wikispaces.com/' root=lxml.html.parse(sfile).getroot() it is ok it is so strange thing for me to understand -- 原始邮件 -- 发件人: "Dave Angel"; 发送时间: 2011年10月24日(星期一) 上午9:22 收件人: "1248283536"<[email protected]>; 抄送: "lxml"; "python-list"; 主题: Re: getroot() problem On 10/23/2011 09:06 PM, 水静流深 wrote: > C:\Documents and Settings\peng>cd c:\python32 > > > > C:\Python32>python > > Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on > win > > 32 > > Type "help", "copyright", "credits" or "license" for more information. > import lxml.html > sfile='http://finance.yahoo.com/q/op?s=A+Options' > root=lxml.html.parse(sfile).getroot() > there is no problem to parse : > > > http://finance.yahoo.com/q/op?s=A+Options' > > > > > why i can not parse > > http://frux.wikispaces.com/ ?? > import lxml.html > sfile='http://frux.wikispaces.com/' > root=lxml.html.parse(sfile).getroot() > > Traceback (most recent call last): > >File "", line 1, in > >File "C:\Python32\lib\site-packages\lxml\html\__init__.py", line 692, in > parse > > > > return etree.parse(filename_or_url, parser, base_url=base_url, **kw) > >File "lxml.etree.pyx", line 2942, in lxml.etree.parse > (src/lxml/lxml.etree.c:5 > > 4187) > >File "parser.pxi", line 1528, in lxml.etree._parseDocument > (src/lxml/lxml.etre > > e.c:79485) > >File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL > (src/lxml/lx > > ml.etree.c:79768) > >File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile > (src/lxml/lxml.e > > tree.c:78843) > >File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile > (src/ > > lxml/lxml.etree.c:75698) > >File "parser.pxi", line 564, in > lxml.etree._ParserContext._handleParseResultDo > > c (src/lxml/lxml.etree.c:71739) > >File "parser.pxi", line 645, in lxml.etree._handleParseResult > (src/lxml/lxml.e > > tree.c:72614) > >File "parser.pxi", line 583, in lxml.etree._raiseParseError > (src/lxml/lxml.etr > > ee.c:71927) > > IOError: Error reading file 'b'http://frux.wikispaces.com/'': b'failed to > load e > > xternal entity "http://frux.wikispaces.com/";' > >>> > Double-spacing makes your message much harder to read. I can only comment in a general way, in any case. most html is mal-formed, and not legal html. Although I don't have any experience with parsing it, I do with xml which has similar problems. The first thing I'd do is to separate the loading of the byte string from the website, from the parsing of those bytes. Further, I'd make a local copy of those bytes, so you can do testing repeatably. For example, you could run wget utility to copy the bytes locally and create a file. -- DaveA-- http://mail.python.org/mailman/listinfo/python-list
Re: install lxml
On 10/24/2011 12:02 AM, 水静流深 wrote: there are two python versions in my computer, python2.7 is the default,python3.2 is the second install. for python2.7 ~$python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml >>> for python3.2 ~$python3.2 Python 3.2.2 (default, Oct 24 2011, 10:33:35) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml Traceback (most recent call last): File "", line 1, in ImportError: No module named lxml i want to install lxml in python3.2. how can i do? Put a 3.2 version of lxml in the 3.2 Lib/site-packages directory. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
CSV writer question
Hello,
I have a question about a csv.writer instance. I have a utility that I want
to write a full CSV file from lots of data, but due to performance (and
memory) considerations, there's no way I can write the data sequentially.
Therefore, I write the data in chunks to temporary files, then combine them
all at the end. For convenience, I declare each writer instance via a
statement like
my_csv = csv.writer(open('temp.1.csv', 'wb'))
so the open file object isn't bound to any explicit reference, and I don't
know how to reference it inside the writer class (the documentation doesn't
say, unless I've missed the obvious). Thus, the only way I can think of to
make sure that all of the data is written before I start copying these files
sequentially into the final file is to unbuffer them so the above command is
changed to
my_csv = csv.writer(open('temp.1.csv', 'wb', 0))
unless, of course, I add an explicit reference to track the open file object
and manually close or flush it (but I'd like to avoid it if possible). My
question is 2-fold. Is there a way to do that directly via the CSV API, or
is the approach I'm taking the only way without binding the open file object
to another reference? Secondly, if these files are potentially very large
(anywhere from ~1KB to 20 GB depending on the amount of data present), what
kind of performance hit will I be looking at by disabling buffering on these
types of files?
Tips, answers, comments, and/or suggestions are all welcome.
Thanks a lot!
Jason
As an afterthought, I suppose I could always subclass the csv.writer class
and add the reference I want to that, which I may do if there's no other
convenient solution.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Books to lean Python 3 Web Programming?
On Oct 23, 3:18 am, Jonathan Loescher wrote: > Can anyone recommend a good book to learn the web programming aspects > of Python 3? Hi Try out "Dive into Python 3" for an introduction to HTTP services. regards Moses -- http://mail.python.org/mailman/listinfo/python-list
回复: install lxml
The latest version is lxml 2.3.1, released 2011-09-25, 1.download it 2.extract it ,put it in the /home/user/Python-3.2.2/libxml2-2.7.8 3.cd /home/user/Python-3.2.2/libxml2-2.7.8 4. ./configure 5.make 6.sudo make install when i finished ~$ python3.2 Python 3.2.2 (default, Oct 24 2011, 10:33:35) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import lxml Traceback (most recent call last): File "", line 1, in ImportError: No module named lxml there is still the problem!! -- 原始邮件 -- 发件人: "Terry Reedy"; 发送时间: 2011年10月24日(星期一) 中午12:58 收件人: "python-list"; 抄送: "lxml"; 主题: Re: install lxml On 10/24/2011 12:02 AM, 水静流深 wrote: > there are two python versions in my computer, > python2.7 is the default,python3.2 is the second install. > for python2.7 > ~$python > Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) > [GCC 4.5.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import lxml > >>> > > for python3.2 > ~$python3.2 > Python 3.2.2 (default, Oct 24 2011, 10:33:35) > [GCC 4.5.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import lxml > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named lxml > > i want to install lxml in python3.2. > how can i do? Put a 3.2 version of lxml in the 3.2 Lib/site-packages directory. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list-- http://mail.python.org/mailman/listinfo/python-list
Re: CSV writer question
On Sun, Oct 23, 2011 at 10:18 PM, Jason Swails wrote:
> Hello,
>
> I have a question about a csv.writer instance. I have a utility that I want
> to write a full CSV file from lots of data, but due to performance (and
> memory) considerations, there's no way I can write the data sequentially.
> Therefore, I write the data in chunks to temporary files, then combine them
> all at the end. For convenience, I declare each writer instance via a
> statement like
>
> my_csv = csv.writer(open('temp.1.csv', 'wb'))
>
> so the open file object isn't bound to any explicit reference, and I don't
> know how to reference it inside the writer class (the documentation doesn't
> say, unless I've missed the obvious). Thus, the only way I can think of to
> make sure that all of the data is written before I start copying these files
> sequentially into the final file is to unbuffer them so the above command is
> changed to
>
> my_csv = csv.writer(open('temp.1.csv', 'wb', 0))
>
> unless, of course, I add an explicit reference to track the open file object
> and manually close or flush it
> (but I'd like to avoid it if possible).
Why? Especially when the performance cost is likely to be nontrivial...
> Is there a way to do that directly via the CSV API,
Very doubtful; csv.writer (and reader for that matter) is implemented
in C, doesn't expose a ._file or similar attribute, and has no
.close() or .flush() methods.
Cheers,
Chris
--
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
