Re: Ideas for a module to process command line arguments
On Jan 11, 8:25 am, Alice Bevan–McGregor wrote: explicit callbacks or typecasting functions, etc. > > I got tired of using PasteScript and OptParse. Mostly OptParse, actually. :/ It's a pity that the argument parsing modules in the standard library are so verbose that everybody is reinventing the same thing :-( It looks like you have reinvented plac: http://pypi.python.org/pypi/plac -- http://mail.python.org/mailman/listinfo/python-list
Re: Python use growing fast
From: "Gerry Reno" On 01/10/2011 08:31 PM, Katie T wrote: On Mon, Jan 10, 2011 at 10:29 PM, John Nagle wrote: On 1/10/2011 1:02 PM, MRAB wrote: On 10/01/2011 20:29, Dan Stromberg wrote: I invite folks to check out Tiobe's Language Popularity Rankings: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html That's somehow derived from web searches, not from any real data source. Look how far down JavaScript is. Any measure is arbitrary and subject to biases, what methodology would you prefer ? Katie Measuring the "Buzz" about a language is actually a pretty good way to gauge its popularity. Well, not exactly. C and C++ are older than many other languages and probably many of the web pages that contain "programming C" are very old and don't reflect their current popularity. On the other hand, newer languages are more attractive for book publishers because they can sell more books about Ruby than about C, because for C there are already very many books written so there is a bigger intrest to promote the newer languages, not just because they are better, but because there are interests involved. Talking about interests, Java and DotNet are more popular than many other languages, but we all know why, and we also know why PHP has such a big success although it is a bad language, as we all know why Window has a bigger success than other operating systems... so the popularity contest is good, but for something else than we want to prove. A programming language popularity contest is like a beauty contest for finding the most intelligent girl. Octavian -- http://mail.python.org/mailman/listinfo/python-list
ctypes and cygwin
Hello, I was trying to use the libusb 1.0 with cygwin environments and noticed that this library uses stdcall calling convention, but ctypes does not have WinDLL object for cygwin. As far as I know, libusb builds with stdcall calling convention on cygwin by default. My question is if ctypes should have WinDLL in cygwin or cygwin, as a kind of emulation of Unix, considers everything to be cdecl and libusb guys should change their default build behavior in cygwin. Not sure if this is the right place to ask this question, but if ctypes guy(s) took out WinDLL from cygwin, I believe he (they) had a good reason to do so... -- Best Regards, Wander Lairson Costa LCoN - Laboratório de Computação Natural - Natural Computing Laboratory (http://www.mackenzie.com.br/lcon.html) Programa de Pós-Graduação em Engenharia Elétrica (PPGEE) Faculdade de Computação e Informática (FCI) Universidade Presbiteriana Mackenzie - SP - Brazil -- http://mail.python.org/mailman/listinfo/python-list
Parsing string for " "
Hi, I have come across a task where I would like to scan a short 20-80 character line of text for instances of " ". Ideally could be of any tense. I know quite a bit of programming and computer science, but computational linguistics is relatively new to me. If anyone can point me in the right direction, I would be very thankful! Cheers, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: String to char and decimal number conversion
On Jan 11, 4:02 am, Chris Rebert wrote:
> On Mon, Jan 10, 2011 at 2:44 PM, SANKAR . wrote:
> > Hello There,
>
> > I am from non IT field also new to python programming.Could you
> > please help me to solve the following problem?
>
> > I have a list T1 with following format:
>
> > T1 = [ ' "Field" ' , ' "12.5" ', ' "2.5" ']
>
> > How do get the list elements without double quote in my output (T2).
>
> > T2 =[ ' Field ' , ' 12.5 ', ' 2.5 ']
>
> How are you obtaining T1 in the first place?
>
> Cheers,
> Chris
> --http://blog.rebertia.com
You can also do it like :
T1 = [ ' "Field" ' , ' "12.5" ', ' "2.5" ']
T2 = [t.replace('"', '') for t in T1]
This seems to me a more better and fast code ;)
Davish
--
http://mail.python.org/mailman/listinfo/python-list
Re: importing modules dynamicly
dubux wrote:
I am trying to import modules dynamicly from a directory (modules/) in
which i have __init__.py with the __all__ variable set. Everything
imports correctly and I have verified this however I am stuck on
actually using my classes in the dynamicly imported modules.
this bit is in my main.py (or base script) to import the modules in
the modules/ directory:
loaded_modules = []
for item in modules:
if item == '__init__.py': pass
else:
if item.endswith('.py'):
__import__('modules.' + item[0:len(item) - 3])
loaded_modules.append(item[0:len(item) - 3])
else: pass
After loading all the modules, i try to do something like:
instance = modules.modulename.class()
And I get an AttributeError. What am I doing wrong here? Help please!!
Your code is rather strange, 'modules' looks to be a list or some
iterable, and then you expect to have a 'modulename' attribute or
something...
My guess is that you pasted an approximative translation of your code
which makes it impossible de debug.
Here is a possible way of importing a bunch of python files:
loaded_modules = {}
fileNames = os.listdir('./modules')
pyFiles = [os.path.basename(name).replace('.py', '') for name in
fileNames if name.endswith('.py')]
for pyFile in pyFiles:
loaded_modules[pyFile] = __import__('modules.%s' % pyFile)
# how to get a class named 'AClassName' defined in then modules
for module in loaded_modules:
myClass = getattr(loaded_modules[module], 'AClassName', None)
print myClass
if myClass:
myInstance = myClass()
JM
--
http://mail.python.org/mailman/listinfo/python-list
"socket operation on non socket" on Windows
Hi I have a problem on Windows with the module urllib2 with python 2.5 when i use the "urlopen" function, i have some time the following error : error I don't understand why suddenly this error arrives The urlopen function is called from a thread Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
os.path.realpath() and os.path.abspath()
Hi all. os.path.realpath() documentation states that it returns a 'canonical' path. Does that infer that it returns an absolute path? I have not found seen any implementation that does not return an absolute path, but can this be counted on? Or should we use os.path.abspath(os.path.realpath(x)) when we want to convert x to its full/canonical name? Best regards, Jurko Gospodnetić -- http://mail.python.org/mailman/listinfo/python-list
Re: PJL
Thank you. I was able to send the following PJL to the printer and it worked. @PJL STMSG DISPLAY = "Hello from John" Do you have any experience handling PJL responses from the printer?...What I really want to do is get PJL information back from the printer and read it in python(or some other Unix scripting tool) -- http://mail.python.org/mailman/listinfo/python-list
Asking for help with code? Post a minimal working example. (was: importing modules dynamicly)
Jean-Michel Pichavant writes: > Your code is rather strange, 'modules' looks to be a list or some > iterable, and then you expect to have a 'modulename' attribute or > something... > My guess is that you pasted an approximative translation of your code > which makes it impossible de debug. To the OP: It is often a good idea to simplify one's code when asking for help. That will make it easier to understand. But don't simplify to the point where the code doesn't actually run, or doesn't demonstrate the behaviour you're reporting! Instead, post a minimal working example of the thing you want explained. To discuss the code and present advice, people will want to run your code for themselves to see what you're seeing, or as close as they can get. Make that easier for them by ensuring your example actually does what you say it does. -- \ “I prayed for twenty years but received no answer until I | `\ prayed with my legs.” —Frederick Douglass, escaped slave | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: String to char and decimal number conversion
SANKAR ., 11.01.2011 01:00:
I am reading a Test.txt (see atatchment) file using following code to get
the T2:
F =open('C:\Test.txt','r')
T1 = F.readlines()
for i in range(len(T1)):
T2 = T1[i].split(',')
print(T2)
Take a look at the "csv" module in the standard library.
Stefan
--
http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On 2011-01-11 00:32:32 -0800, Michele Simionato said: On Jan 11, 8:25 am, Alice Bevan–McGregor wrote: I got tired of using PasteScript and OptParse. Mostly OptParse, actually. :/ It's a pity that the argument parsing modules in the standard library are so verbose that everybody is reinventing the same thing :-( It looks like you have reinvented plac: http://pypi.python.org/pypi/plac And a package called `commandline`. There are many command line parsing modules, many of which are unmaintained, few have reached 1.0. My criteria for 1.0? 100% unit test coverage, complete documentation, compatibility with 2.6+ and 3.1+ within a single package. marrow.script meets that criteria, do the others? :) - Alice. -- http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On 2011-01-11 00:32:32 -0800, Michele Simionato said:
It's a pity that the argument parsing modules in the standard library
are so verbose that everybody is reinventing the same thing :-( It
looks like you have reinvented plac: http://pypi.python.org/pypi/plac
After looking into it, Plac's default help display isn't very helpful;
you need to massage your application a fair amount before generating
nice, complete-looking argument lists and such. For example:
def main(verbose: ('prints more info', 'flag', 'v'), dsn: 'connection
string'):
@annotate(dsn="connection string", verbose="prints more info")
def main(dsn, verbose=False):
The latter is marrow.script, and even without the annotation a more
complete help text is generated. The -v and flag nature are assumed
from the first unique character and default value. (Flags, when
present on the command line, invert the default value.) Py3k
annotations haven't been implemented yet, though.
Also included is an easy way to simulte command-line execution (i.e. by
reading arguments passed by hand and by returning the exit code, vs.
reading sys.argv and calling sys.exit()) for unit testing purposes.
Plac appears (from the documentation) to be written on top of argparse.
:(
- Alice.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On Jan 11, 4:06 pm, Alice Bevan–McGregor wrote: > Plac appears (from the documentation) to be written on top of argparse. > :( And the problem with that being what? -- http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
Michele Simionato wrote: On Jan 11, 4:06 pm, Alice Bevan–McGregor wrote: Plac appears (from the documentation) to be written on top of argparse. :( And the problem with that being what? ... not available to python 2.5 / 2.6 users :) JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On Jan 11, 5:22 pm, Jean-Michel Pichavant wrote: > Michele Simionato wrote: > > On Jan 11, 4:06 pm, Alice Bevan McGregor wrote: > > >> Plac appears (from the documentation) to be written on top of argparse. > >> :( > > > And the problem with that being what? > > ... not available to python 2.5 / 2.6 users :) > > JM In that case easy_install/pip/whatever will install the dependency automatically (who is installing dependencies by hand nowadays?). More seriously I thought being based on a solid module which is also part of the standard library (for Python 2.7+) was an asset of plac. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.system and loggers
On Jan 10, 1:01 pm, Carl Banks wrote:
> On Jan 10, 8:29 am, Tim wrote:
>
>
>
>
>
>
>
>
>
> > On Jan 7, 11:24 am, Tim wrote:
>
> > > hi, I'm using a 3rd-party python program that uses the python logging
> > > facility and also makes calls to os.system. I'm trying to capture its
> > > output to a file.
>
> > > In my own code, I've taken control of the loggers that are setup in
> > > the other program by removing its StreamHandler and replacing with
> > > FileHander. But when it comes to the call to os.system I'm at a loss.
>
> > > I want to capture the stdout from that os.system call in my
> > > FileHandler. I thought this might work, before I call the other
> > > program's class/method:
> > > sys.stdout = getLogger('status').handlers[0].stream
>
> > > but no dice. Is there any clean way to get what I want? If not, I
> > > guess I'll override the other method with my own, but it will
> > > basically be a bunch of code copied with os.sytem replaced with
> > > subprocess, using getLogger('status').handlers[0].stream for stdout/
> > > stderr.
>
> > > thanks,
> > > --Tim Arnold
>
> > Replying to my own post
>
> > I think I may have included too much fluff in my original question.
> > The main thing I wonder is whether I can attach a log handler to
> > stdout in such a way that os.system calls will write to that handler
> > instead of the console.
>
> No, but you could replace os.system with something that does work.
> (It would replace it globally for all uses, so you may need some logic
> to decide whether to leave the call alone, or to modify it, perhaps by
> inspecting the call stack.)
>
> The simplest thing to do is to append a shell redirection to the
> command (>/your/log/file), so something like this:
>
> _real_os_system = os.system
>
> def my_os_system(cmd):
> if test_log_condition:
> return _real_os_system(cmd + "2> /my/log/file")
> return _real_os_system(cmd)
>
> os.system = my_os_system
>
> That could backfire for any number of reasons so you probably should
> only do this if you know that it works with all the commands it
> issues.
>
> The better way might be to call the subprocess module instead, where
> you can dispatch the command with redirection to any stream. I doubt
> there's a foolproof way to do that given an arbitrary os.system
> command, but the subprocess way is probably safer.
>
> Carl Banks
Thanks Carl. I will use subprocess. I made this little toy example to
prove to myself that subprocess does handle a filehandler stream, so I
include it here for completeness' sake:
import subprocess,logging,shlex
# create the logger, filehandler and get the stream
log = logging.getLogger('mytest')
fh = logging.FileHandler('mytest.log')
log.addHandler(fh)
log.setLevel(logging.INFO)
mylog = logging.getLogger('mytest').handlers[0].stream
# write a test line to the log
log.info('my first line')
# execute the subprocess using the stream as stdout
cmd = 'ls -l'
p = subprocess.Popen(shlex.split(cmd),stdout=mylog)
# if you don't wait(), the output won't be necessarily in
chronological order.
r = p.wait()
# write a last test line to the log
log.info('done %s'% r)
and it works as expected.
thanks,
--Tim Arnold
--
http://mail.python.org/mailman/listinfo/python-list
Re: "socket operation on non socket" on Windows
On 1/11/2011 6:18 AM, wiz1024 wiz1024 wrote: Hi I have a problem on Windows with the module urllib2 with python 2.5 when i use the "urlopen" function, i have some time the following error : error I don't understand why suddenly this error arrives The urlopen function is called from a thread Give us both the failing call and the complete copy-and-pasted traceback. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: "socket operation on non socket" on Windows
2011/1/11 Terry Reedy
> On 1/11/2011 6:18 AM, wiz1024 wiz1024 wrote:
>
>> Hi
>>
>> I have a problem on Windows with the module urllib2 with python 2.5
>>
>> when i use the "urlopen" function, i have some time the following error :
>> error
>>
>> I don't understand why suddenly this error arrives
>> The urlopen function is called from a thread
>>
>
> Give us both the failing call and the complete copy-and-pasted traceback.
>
> the failing call is urlopen
I investigate a little and it seams that the problem is cause by the
function connect of httplib
I have the following traceback with log level debug
connect: (10.42.1.116, )
connect fail: ('10.42.1.116', )
File "W:\david\OvdServer\ovd\SMRequestManager.py", line 145, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "C:\Python25\lib\httplib.py", line 866, in request
self._send_request(method, url, body, headers)
File "C:\Python25\lib\httplib.py", line 889, in _send_request
self.endheaders()
File "C:\Python25\lib\httplib.py", line 860, in endheaders
self._send_output()
File "C:\Python25\lib\httplib.py", line 732, in _send_output
self.send(msg)
File "C:\Python25\lib\httplib.py", line 699, in send
self.connect()
File "C:\Python25\lib\httplib.py", line 683, in connect
raise socket.error, msg
(10038, 'Socket operation on non-socket')
best regard.
David.
> --
> Terry Jan Reedy
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On Jan 11, 11:26 am, Michele Simionato wrote: > > In that case easy_install/pip/whatever will install the dependency > automatically (who is installing > dependencies by hand nowadays?). More seriously I thought being based I do. Is this bad? :} -- http://mail.python.org/mailman/listinfo/python-list
Convert unicode escape sequences to unicode in a file
I have a file that has unicode escape sequences, i.e.,
J\u00e9r\u00f4me
and I want to replace all of them in a file and write the results to a new
file. The simple script I've created is copied below. However, I am getting
the following error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position
947: ordinal not in range(128)
It appears that the data isn't being converted when writing to the file. Can
someone please help?
Thanks,
Jeremy
if __name__ == "__main__":
f = codecs.open(filename, 'r', 'unicode-escape')
lines = f.readlines()
line = ''.join(lines)
f.close()
utFound = re.sub('STRINGDECODE\((.+?)\)', r'\1', line)
print(utFound[:1000])
o = open('newDice.sql', 'w')
o.write(utFound.decode('utf-8'))
o.close()
--
http://mail.python.org/mailman/listinfo/python-list
Re: Integrating doctest with unittest
On Jan 9, 6:14 pm, Steven D'Aprano wrote: > >>Is there a way to have unittest.main() find and run doc_test_suite > >>together with the other test suites? I only recently began using unittest, so I only know a little about it. There are almost certainly more clever ways to what you want, but what I have done may satisfy you. allTests.py: import unittest import PalmDS.test.test_tree_node as test_tree_node import PalmDS.test.test_plugin_manager as test_plugin_manager import PalmDS.test.test_ds_utils as test_ds_utils import PalmDS.test.test_main as test_main import PalmDS.test.test_root as test_root all = unittest.TestSuite() for module in [test_tree_node, test_plugin_manager, test_ds_utils, test_root, ]: all.addTest(module.suite()) if __name__ == '__main__': unittest.main() Note: This requires me to put a suite() function in every unittest module, such as this one from my test_tree_node.py module: def suite(): return unittest.TestLoader().loadTestsFromTestCase(TstTreeNode) Note: I must change TstTreeNode appropriately when I copy suite() to a new module. Terminal contents after a run: b...@bobbuilt01:~/svnMyWork/PalmDS/test$ ./all_tests.py -v all testDs2tree01 (PalmDS.test.test_tree_node.TstTreeNode) ... ok testDs2tree02 (PalmDS.test.test_tree_node.TstTreeNode) ... ok testPlug01 (PalmDS.test.test_plugin_manager.TstPluginManager) ... ok testPlug02 (PalmDS.test.test_plugin_manager.TstPluginManager) ... ok testBitstringBytes (PalmDS.test.test_ds_utils.TstDsUtils) ... ok testComputeLoadDir (PalmDS.test.test_ds_utils.TstDsUtils) ... ok testDs2fmtStr (PalmDS.test.test_ds_utils.TstDsUtils) ... ok testPalmDateDecode (PalmDS.test.test_root.TstRoot) ... ok testPalmDateEncode (PalmDS.test.test_root.TstRoot) ... ok -- Ran 9 tests in 0.016s OK b...@bobbuilt01:~/svnMyWork/PalmDS/test$ My guess at an answer to your specific question: At the end of allTests.py add all.addTest(doctest.DocTestSuite(module=module_to_test))) Then I think your DocTest suite will be run with the unittest suites when you specify "all" on the command line. -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert unicode escape sequences to unicode in a file
On Jan 11, 8:53 pm, Jeremy wrote:
> I have a file that has unicode escape sequences, i.e.,
>
> J\u00e9r\u00f4me
>
> and I want to replace all of them in a file and write the results to a new
> file. The simple script I've created is copied below. However, I am getting
> the following error:
>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position
> 947: ordinal not in range(128)
>
> It appears that the data isn't being converted when writing to the file. Can
> someone please help?
Are you _sure_ that your file contains the characters '\', 'u', '0',
'0', 'e' and '9'? I expect that actually your file contains a byte
with value 0xe9 and you have inspected the file using Python, which
has printed the byte using a Unicode escape sequence. Open the file
using a text editor or hex editor and look at the value at offset 947
to be sure.
If so, you need to replace 'unicode-escape' with the actual encoding
of the file.
> if __name__ == "__main__":
> f = codecs.open(filename, 'r', 'unicode-escape')
> lines = f.readlines()
> line = ''.join(lines)
> f.close()
>
> utFound = re.sub('STRINGDECODE\((.+?)\)', r'\1', line)
> print(utFound[:1000])
>
> o = open('newDice.sql', 'w')
> o.write(utFound.decode('utf-8'))
> o.close()
--
http://mail.python.org/mailman/listinfo/python-list
Req: Python Developer : Direct CLient
Hi, Please send me your resume if you are interested in it. Python Developer Location: Sebastapol, CA Duration: 3 Months Python web application development. Systems integration. RESTful architectures and Web standards. Familiar with the following: JVM and java tools. Creating documentation. Workable knowledge of relational databases and NoSQL solutions Thanks Nitin Singhal | RJT Compuquest Inc. 23440 Hawthorne Blvd., Suite 210, Torrance, CA 90505 [email protected] www.rjtcompuquest.com Direct: 310 961 5807 Voice: 866-978-0384 Ext- 46 Fax: 310-378-6867 -- http://mail.python.org/mailman/listinfo/python-list
How to dump a Python 2.6 dictionary with UTF-8 strings?
Hi,
naively, I thought the following code:
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
import codecs
d = { u'key': u'我爱中国人' }
if __name__ == "__main__":
with codecs.open("ilike.txt", "w", "utf-8") as f:
print >>f, d
would produce a file ilike.txt like this:
{u'key': u'我爱中国人'}
But unfortunately, it results in:
{u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
What's the right way to get the strings in UTF-8?
Thanks in advance!
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to dump a Python 2.6 dictionary with UTF-8 strings?
> What's the right way to get the strings in UTF-8?
This will work. I doubt you can get it much simpler
in 2.x; in 3.x, your code will work out of the box
(with proper syntactical adjustments).
import pprint, cStringIO
class UniPrinter(pprint.PrettyPrinter):
def format(self, obj, context, maxlevels, level):
if not isinstance(obj, unicode):
return pprint.PrettyPrinter.format(self, obj,
context,
maxlevels,
level)
out = cStringIO.StringIO()
out.write('u"')
for c in obj:
if ord(c)<32 or c in u'"\\':
out.write('\\x%.2x' % ord(c))
else:
out.write(c.encode("utf-8"))
out.write('"')
# result, readable, recursive
return out.getvalue(), True, False
UniPrinter().pprint({ u'k"e\\y': u'我爱中国人' })
--
http://mail.python.org/mailman/listinfo/python-list
Please use the Python Job Board for recruitment (was: Req: Python Developer : Direct CLient)
nitin writes: > Please send me your resume if you are interested in it. Please don't use this forum for recruitment purposes. Instead, please use the Python Job Board http://www.python.org/community/jobs/>. -- \ “For every complex problem, there is a solution that is simple, | `\ neat, and wrong.” —Henry L. Mencken | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: How to dump a Python 2.6 dictionary with UTF-8 strings?
On Jan 11, 10:40 pm, "W. Martin Borgert" wrote:
> Hi,
>
> naively, I thought the following code:
>
> #!/usr/bin/env python2.6
> # -*- coding: utf-8 -*-
> import codecs
> d = { u'key': u'我爱中国人' }
> if __name__ == "__main__":
> with codecs.open("ilike.txt", "w", "utf-8") as f:
> print >>f, d
>
> would produce a file ilike.txt like this:
>
> {u'key': u'我爱中国人'}
>
> But unfortunately, it results in:
>
> {u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>
> What's the right way to get the strings in UTF-8?
>
> Thanks in advance!
It has worked, you're just seeing how python presents unicode
characters in the interactive interpreter:
Python 2.7.1+ (r271:86832, Dec 24 2010, 10:04:43)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> x
{u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> print x
{u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> print x['key']
我爱中国人
That last line only works if your terminal uses an suitable encoding
(e.g. utf-8).
Regards, Alex
--
http://mail.python.org/mailman/listinfo/python-list
Re: Convert unicode escape sequences to unicode in a file
On Tuesday, January 11, 2011 3:36:26 PM UTC-7, Alex wrote:
>
> Are you _sure_ that your file contains the characters '\', 'u', '0',
> '0', 'e' and '9'? I expect that actually your file contains a byte
> with value 0xe9 and you have inspected the file using Python, which
> has printed the byte using a Unicode escape sequence. Open the file
> using a text editor or hex editor and look at the value at offset 947
> to be sure.
>
> If so, you need to replace 'unicode-escape' with the actual encoding
> of the file.
Yeah, I'm sure that's what the file contains. In fact, I solved my own problem
while waiting for an answer. When writing to the file I need to *en*code
instead of *de*code; i.e.,
o = open('newDice.sql', 'w')
o.write(utFound.encode('utf-8'))
o.close()
That works!
--
http://mail.python.org/mailman/listinfo/python-list
order of importing modules
In what order does python import modules on a Linux system? I have a package that is both installed in /usr/lib64/python2.5/site-packages, and a newer version of the same module in a working directory. I want to import the version from the working directory, but when I print module.__file__ in the interpreter after importing the module, I get the version that's in site-packages. I've played with the PYTHONPATH environmental variable by setting it to just the path of the working directory, but when I import the module I still pick up the version in site-packages. /usr/lib64 is in my PATH variable, but doesn't appear anywhere else. I don't want to remove /usr/lib64 from my PATH because that will break a lot of stuff. Can I force python to import from my PYTHONPATH first, before looking in the system directory? Catherine -- http://mail.python.org/mailman/listinfo/python-list
Re: How to dump a Python 2.6 dictionary with UTF-8 strings?
On 2011-01-12 00:27, Martin v. Loewis wrote: > This will work. I doubt you can get it much simpler > in 2.x; in 3.x, your code will work out of the box > (with proper syntactical adjustments). Thanks, this works like a charm. I tried pprint before for this task and failed. Now I know why :~) -- http://mail.python.org/mailman/listinfo/python-list
Re: order of importing modules
On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney wrote: > In what order does python import modules on a Linux system? I have a > package that is both installed in /usr/lib64/python2.5/site-packages, > and a newer version of the same module in a working directory. > > I want to import the version from the working directory, but when I > print module.__file__ in the interpreter after importing the module, > I get the version that's in site-packages. > > I've played with the PYTHONPATH environmental variable by setting it > to just the path of the working directory, but when I import the module > I still pick up the version in site-packages. > > /usr/lib64 is in my PATH variable, but doesn't appear anywhere else. I > don't want to remove /usr/lib64 from my PATH because that will break > a lot of stuff. > > Can I force python to import from my PYTHONPATH first, before looking > in the system directory? > > Catherine > -- > http://mail.python.org/mailman/listinfo/python-list Please import sys and inspect sys.path; this defines the search path for imports. By looking at sys.path, you can see where in the search order your $PYTHONPATH is going. It might actually be better to give your script a command line option that says "Throw the following directory at the beginning of sys.path". -- http://mail.python.org/mailman/listinfo/python-list
Re: order of importing modules
Catherine Moroney writes: > In what order does python import modules on a Linux system? An import caused by a statement is done when that statement is executed. So the answer to that question is: in the order the statements occur in the execution flow. > I have a package that is both installed in > /usr/lib64/python2.5/site-packages, and a newer version of the same > module in a working directory. Ah, you're asking about the search path for importing modules. You would do well to work through the entire Python tutorial http://docs.python.org/tutorial/> to cover the basics like that. Don't merely read it; actually *do* it, exploring each example and experimenting to understand before proceeding. The module import search path is part of that coverage http://docs.python.org/tutorial/modules.html#the-module-search-path>. > I want to import the version from the working directory, but when I > print module.__file__ in the interpreter after importing the module, > I get the version that's in site-packages. You will be interested in the features enabled by PEP 328 http://www.python.org/dev/peps/pep-0328/>, specifically the feature of relative imports. -- \ “When I turned two I was really anxious, because I'd doubled my | `\ age in a year. I thought, if this keeps up, by the time I'm six | _o__) I'll be ninety.” —Steven Wright | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Parsing string for " "
Hi, I have come across a task where I would like to scan a short 20-80 character line of text for instances of " ". Ideally could be of any tense. I know quite a bit of programming and computer science, but computational linguistics is relatively new to me. If anyone can point me in the right direction, I would be very thankful! Cheers, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Syntactic structure for 'until :' loop
I'm still quite new to Python and I'm probably going about this entirely the wrong way, but it recently struck me that there might be the need for a control flow loop based on exception handling. First let me give the proposed syntax: until : do_something() This would be exactly equivalent to (but much more compact than): while True: try: do_something() except Exception: break Now, why would anyone want this structure? In my case, I'm using it (well, the latter form of it, obviously) to loop over an iterator object that was not created via the 'for obj in collection:' syntax. Here's the actual code snippet: headers = self.reader.next() ... intermediate code while True: try: line = self.reader.next() except StopIteration: return data data.append(line) I'm sure I'm doing this in a very backward and wrong way, and would appreciate tips on a better way to accomplish the same task. Obviously there is an existing syntax which handles the same situations, and I don't suspect that this will be an embraced proposal, I'm more hoping to spark some conversation. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
RE: Python use growing fast
Since this discussion is going on about the popularity of a programming language. I would like to know views regarding the best language for scientific programming especially in terms of user friendliness, resources available, graphics and robustness to handle large numerical and simulation problems. Thanks & regards Sachin Sachin Kumar Sharma Senior Geomodeler -Original Message- From: [email protected] [mailto:[email protected]] On Behalf Of Octavian Rasnita Sent: Tuesday, January 11, 2011 3:38 PM To: [email protected] Subject: Re: Python use growing fast From: "Gerry Reno" > On 01/10/2011 08:31 PM, Katie T wrote: >> On Mon, Jan 10, 2011 at 10:29 PM, John Nagle wrote: >> >>> On 1/10/2011 1:02 PM, MRAB wrote: >>> On 10/01/2011 20:29, Dan Stromberg wrote: > I invite folks to check out Tiobe's Language Popularity Rankings: > > http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html > >>> That's somehow derived from web searches, not from any real data >>> source. Look how far down JavaScript is. >>> >> Any measure is arbitrary and subject to biases, what methodology would >> you prefer ? >> >> >> Katie >> > > Measuring the "Buzz" about a language is actually a pretty good way to > gauge its popularity. Well, not exactly. C and C++ are older than many other languages and probably many of the web pages that contain "programming C" are very old and don't reflect their current popularity. On the other hand, newer languages are more attractive for book publishers because they can sell more books about Ruby than about C, because for C there are already very many books written so there is a bigger intrest to promote the newer languages, not just because they are better, but because there are interests involved. Talking about interests, Java and DotNet are more popular than many other languages, but we all know why, and we also know why PHP has such a big success although it is a bad language, as we all know why Window has a bigger success than other operating systems... so the popularity contest is good, but for something else than we want to prove. A programming language popularity contest is like a beauty contest for finding the most intelligent girl. Octavian -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntactic structure for 'until :' loop
On 1/11/2011 7:22 PM, eblume wrote: This would be exactly equivalent to (but much more compact than): while True: try: do_something() except Exception: break Or perhaps: try: while True: do_something() except Exception: pass Now, why would anyone want this structure? In my case, I'm using it (well, the latter form of it, obviously) to loop over an iterator object that was not created via the 'for obj in collection:' syntax. Here's the actual code snippet: headers = self.reader.next() ... intermediate code while True: try: line = self.reader.next() except StopIteration: return data data.append(line) I'm sure I'm doing this in a very backward and wrong way, and would appreciate tips on a better way to accomplish the same task. Obviously there is an existing syntax which handles the same situations, and I don't suspect that this will be an embraced proposal, I'm more hoping to spark some conversation. reader_iter = iter(self.reader) headers = reader_iter.next() # intermediate code for line in reader_iter: data.append(line) return data Also note that recommended best practice is to wrap the "headers = reader_iter.next()" line in a try-except in case it raises a StopIteration. Otherwise it could get propagated silently up to some unrelated for loop higher in the stack, resulting in unexpected behavior. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing string for " "
On 12/01/2011 01:50, Daniel da Silva wrote: Hi, I have come across a task where I would like to scan a short 20-80 character line of text for instances of " ". Ideally could be of any tense. I know quite a bit of programming and computer science, but computational linguistics is relatively new to me. If anyone can point me in the right direction, I would be very thankful! Have a look at the Natural Language Toolkit: http://www.nltk.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: os.path.realpath() and os.path.abspath()
On Jan 11, 6:35 am, Jurko Gospodnetić wrote: > Hi all. > > os.path.realpath() documentation states that it returns a 'canonical' > path. Does that infer that it returns an absolute path? > A canonical path is supposed to be absolute and at least Python 2.7.1 ensures that is the case. Historically, some versions of the UNIX syscall (Solaris in particular) have not always returned absolute paths, but I believe this is no longer the case and was a very long standing bug (though I may be mistaken). Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Python use growing fast
On Tue, Jan 11, 2011 at 6:38 PM, Sachin Kumar Sharma wrote: > Since this discussion is going on about the popularity of a programming > language. > > I would like to know views regarding the best language for scientific > programming especially in terms of user friendliness, resources available, > graphics and robustness to handle large numerical and simulation problems. > > Thanks & regards > > Sachin > > > Sachin Kumar Sharma > Senior Geomodeler > > According to this article ... http://neopythonic.blogspot.com/2009/11/python-in-scientific-world.html ... the answer is python. Josh Benner -- http://mail.python.org/mailman/listinfo/python-list
the C header file when extending CPython
Hi, I am wondering when extending Python (CPython), what should be put into the C header file? Any guidelines? Thanks, Yingjie -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntactic structure for 'until :' loop
On Jan 11, 6:53 pm, Ian Kelly wrote: > On 1/11/2011 7:22 PM, eblume wrote: > > > > > reader_iter = iter(self.reader) > headers = reader_iter.next() > # intermediate code > for line in reader_iter: > data.append(line) > return data > > Also note that recommended best practice is to wrap the "headers = > reader_iter.next()" line in a try-except in case it raises a > StopIteration. Otherwise it could get propagated silently up to some > unrelated for loop higher in the stack, resulting in unexpected behavior. > > Cheers, > Ian That's brilliant, exactly the code I was looking for. Thanks very much Ian! Erich -- http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On Jan 11, 4:06 pm, Alice Bevan–McGregor wrote:
> After looking into it, Plac's default help display isn't very helpful;
> you need to massage your application a fair amount before generating
> nice, complete-looking argument lists and such. For example:
>
> def main(verbose: ('prints more info', 'flag', 'v'), dsn: 'connection
> string'):
>
> @annotate(dsn="connection string", verbose="prints more info")
> def main(dsn, verbose=False):
>
> The latter is marrow.script, and even without the annotation a more
> complete help text is generated. The -v and flag nature are assumed
> from the first unique character and default value. (Flags, when
> present on the command line, invert the default value.)
Honestly I do not see any significant difference both in the
level of verbosity for the annotations and in the quality of the help
message provided in the absence of annotations. Originally plac too
was able to recognize flags automatically by looking at the default
value (if the default value is a boolean then the option is a flag);
however I removed that functionality because I wanted to be able to
differentiate between flag and (smart) options (see
http://micheles.googlecode.com/hg/plac/doc/plac.html#scripts-with-options-and-smart-options).
--
http://mail.python.org/mailman/listinfo/python-list
Re: Ideas for a module to process command line arguments
On Jan 11, 6:57 pm, Mike wrote: > On Jan 11, 11:26 am, Michele Simionato > wrote: > > In that case easy_install/pip/whatever will install the dependency > > automatically (who is installing > > dependencies by hand nowadays?). > > I do. Is this bad? :} You are simply spending more time than needed, since there are already tools available to perform the task for you. -- http://mail.python.org/mailman/listinfo/python-list
Re: the C header file when extending CPython
Yingjie Lan wrote: > I am wondering when extending Python (CPython), what should be put into > the C header file? Any guidelines? You don't even need to write a header file at all. There are no Python- specific requirements to put anything into a header file, though you might want to do so for reasons internal to your project. Uli -- http://mail.python.org/mailman/listinfo/python-list
