Pattern-match & Replace - help required
Hi, I am new to python and web2py framework. Need urgent help to match a pattern in an string and replace the matched text. I've this string (basically an sql statement): stmnt = 'SELECT taxpayer.id, taxpayer.enc_name, taxpayer.age, taxpayer.occupation FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' The requirement is to replace it with this one: r_stmnt = 'SELECT taxpayer.id, decrypt(taxpayer.enc_name), taxpayer.age, taxpayer.occupation FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' Can somebody please help? Thanks & Regards -- http://mail.python.org/mailman/listinfo/python-list
counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
Hi Iam just starting out with python...My code below changes the txt file into
a list and add them to an empty dictionary and print how often the word occurs,
but it only seems to recognise and print the last entry of the txt file. Any
help would be great.
tm =open('ask.txt', 'r')
dict = {}
for line in tm:
line = line.strip()
line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
line = line.lower()
list = line.split(' ')
for word in list:
if word in dict:
count = dict[word]
count += 1
dict[word] = count
else:
dict[word] = 1
for word, count in dict.iteritems():
print word + ":" + str(count)
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote: > Hi, > > I am new to python and web2py framework. Need urgent help to match a > pattern in an string and replace the matched text. > > I've this string (basically an sql statement): > > stmnt = 'SELECT taxpayer.id, > taxpayer.enc_name, > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > The requirement is to replace it with this one: > > r_stmnt = 'SELECT taxpayer.id, >decrypt(taxpayer.enc_name), >taxpayer.age, >taxpayer.occupation >FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > Can somebody please help? Can you do this? stmnt = r_stmnt That should do what you are asking. If that doesn't solve your problem, you will need to explain your problem in more detail. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
[email protected] writes: > Hi Iam just starting out with python...My code below changes the txt > file into a list and add them to an empty dictionary and print how > often the word occurs, but it only seems to recognise and print the > last entry of the txt file. Any help would be great. > > tm =open('ask.txt', 'r') > dict = {} > for line in tm: > line = line.strip() > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') > line = line.lower() > list = line.split(' ') > for word in list: > if word in dict: > count = dict[word] > count += 1 > dict[word] = count > else: > dict[word] = 1 > for word, count in dict.iteritems(): > print word + ":" + str(count) The "else" clause is mis-indented (rather, mis-unindented). Python's "for" statement does have an optional "else" clause. That's why you don't get a syntax error. The "else" clause is used after the loop finishes normally. That's why it catches the last word. -- http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:
> Hi Iam just starting out with python...My code below changes the txt
> file into a list and add them to an empty dictionary and print how often
> the word occurs, but it only seems to recognise and print the last entry
> of the txt file. Any help would be great.
>
> tm =open('ask.txt', 'r')
> dict = {}
> for line in tm:
> line = line.strip()
> line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
> line = line.lower()
> list = line.split(' ')
Note: you should use descriptive names. Since this is a list of WORDS, a
much better name would be "words" rather than list. Also, list is a built-
in function, and you may run into trouble when you accidentally re-use
that as a name. Same with using "dict" as you do.
Apart from that, so far so good. For each line, you generate a list of
words. But that's when it goes wrong, because you don't do anything with
the list of words! The next block of code is *outside* the for-loop, so
it only runs once the for-loop is done. So it only sees the last list of
words.
> for word in list:
The problem here is that you lost the indentation. You need to indent the
"for word in list" (better: "for word in words") so that it starts level
with the line above it.
> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count
This bit is fine.
> else:
> dict[word] = 1
But this fails for the same reason! You have lost the indentation.
A little-known fact: Python for-loops take an "else" block too! It's a
badly named statement, but sometimes useful. You can write:
for value in values:
do_something_with(value)
if condition:
break # skip to the end of the for...else
else:
print "We never reached the break statement"
So by pure accident, you lined up the "else" statement with the for loop,
instead of what you needed:
for line in tm:
... blah blah blah
for word in words:
if word in word_counts: # better name than "dict"
... blah blah blah
else:
...
> for word, count in dict.iteritems():
> print word + ":" + str(count)
And this bit is okay too.
Good luck!
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wednesday, 19 December 2012 15:51:22 UTC+5, Steven D'Aprano wrote: > On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote: > > > > > Hi, > > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > I've this string (basically an sql statement): > > > > > > stmnt = 'SELECT taxpayer.id, > > > taxpayer.enc_name, > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > The requirement is to replace it with this one: > > > > > > r_stmnt = 'SELECT taxpayer.id, > > >decrypt(taxpayer.enc_name), > > >taxpayer.age, > > >taxpayer.occupation > > >FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > Can somebody please help? > > > > Can you do this? > > > > stmnt = r_stmnt > > > > That should do what you are asking. > > > > If that doesn't solve your problem, you will need to explain your problem > > in more detail. > > > > > > > > -- > > Steven I just wanted to change taxpayer.enc_name in stmnt to decrypt(taxpayer.enc_name) hope it clarifies? thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wed, 19 Dec 2012 03:01:32 -0800, AT wrote:
> I just wanted to change taxpayer.enc_name in stmnt to
> decrypt(taxpayer.enc_name)
>
> hope it clarifies?
Maybe. Does this help?
lunch = "Bread, ham, cheese and tomato."
# replace ham with spam
offset = lunch.find('ham')
if offset != -1:
lunch = lunch[:offset] + 'spam' + lunch[offset + len('ham'):]
print(lunch)
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
Hi, just as a side-note On Wed, Dec 19, 2012 at 02:45:13AM -0800, [email protected] wrote: > for word in list: > if word in dict: > count = dict[word] > count += 1 > dict[word] = count > else: > dict[word] = 1 When you got the indentation and names right, you can restate this as import collections counter = collections.Counter(words) in Python 2.7 or as import collections counter = collections.defaultdict(int) for word in words: counter[word] += 1 in Python 2.6 Regards, Thomas. -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote:
> Hi,
>
> I am new to python and web2py framework. Need urgent help to match a
> pattern in an string and replace the matched text.
>
Well, what about str.replace then?
>>> 'egg, ham, tomato'.replace('ham', 'spam, ham, spam')
'egg, spam, ham, spam, tomato'
If the pattern you want to match is more complicated, have a look at
the re module!
Regards,
Thomas.
--
http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
On Wednesday, December 19, 2012 11:55:28 PM UTC+13, Jussi Piitulainen wrote:
>
>
>
>
> > Hi Iam just starting out with python...My code below changes the txt
>
> > file into a list and add them to an empty dictionary and print how
>
> > often the word occurs, but it only seems to recognise and print the
>
> > last entry of the txt file. Any help would be great.
>
> >
>
> > tm =open('ask.txt', 'r')
>
> > dict = {}
>
> > for line in tm:
>
> > line = line.strip()
>
> > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
>
> > line = line.lower()
>
> > list = line.split(' ')
>
> > for word in list:
>
> > if word in dict:
>
> > count = dict[word]
>
> > count += 1
>
> > dict[word] = count
>
> > else:
>
> > dict[word] = 1
>
> > for word, count in dict.iteritems():
>
> > print word + ":" + str(count)
>
>
>
> The "else" clause is mis-indented (rather, mis-unindented).
>
>
>
> Python's "for" statement does have an optional "else" clause. That's
>
> why you don't get a syntax error. The "else" clause is used after the
>
> loop finishes normally. That's why it catches the last word.
Thanks for quick reply Jussi...indentation fixed the problem :-)
--
http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
On Thursday, December 20, 2012 12:03:21 AM UTC+13, Steven D'Aprano wrote:
> On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote:
>
>
>
> > Hi Iam just starting out with python...My code below changes the txt
>
> > file into a list and add them to an empty dictionary and print how often
>
> > the word occurs, but it only seems to recognise and print the last entry
>
> > of the txt file. Any help would be great.
>
> >
>
> > tm =open('ask.txt', 'r')
>
> > dict = {}
>
> > for line in tm:
>
> > line = line.strip()
>
> > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
>
> > line = line.lower()
>
> > list = line.split(' ')
>
>
>
> Note: you should use descriptive names. Since this is a list of WORDS, a
>
> much better name would be "words" rather than list. Also, list is a built-
>
> in function, and you may run into trouble when you accidentally re-use
>
> that as a name. Same with using "dict" as you do.
>
>
>
> Apart from that, so far so good. For each line, you generate a list of
>
> words. But that's when it goes wrong, because you don't do anything with
>
> the list of words! The next block of code is *outside* the for-loop, so
>
> it only runs once the for-loop is done. So it only sees the last list of
>
> words.
>
>
>
> > for word in list:
>
>
>
> The problem here is that you lost the indentation. You need to indent the
>
> "for word in list" (better: "for word in words") so that it starts level
>
> with the line above it.
>
>
>
> > if word in dict:
>
> > count = dict[word]
>
> > count += 1
>
> > dict[word] = count
>
>
>
> This bit is fine.
>
>
>
> > else:
>
> > dict[word] = 1
>
>
>
> But this fails for the same reason! You have lost the indentation.
>
>
>
> A little-known fact: Python for-loops take an "else" block too! It's a
>
> badly named statement, but sometimes useful. You can write:
>
>
>
>
>
> for value in values:
>
> do_something_with(value)
>
> if condition:
>
> break # skip to the end of the for...else
>
> else:
>
> print "We never reached the break statement"
>
>
>
> So by pure accident, you lined up the "else" statement with the for loop,
>
> instead of what you needed:
>
>
>
> for line in tm:
>
> ... blah blah blah
>
> for word in words:
>
> if word in word_counts: # better name than "dict"
>
> ... blah blah blah
>
> else:
>
> ...
>
>
>
>
>
> > for word, count in dict.iteritems():
>
> > print word + ":" + str(count)
>
>
>
> And this bit is okay too.
>
>
>
>
>
> Good luck!
>
>
>
>
>
> --
>
> Steven
Thanks Steven appreciate great info for future coding. i have change names to
be more decriptive and corrected the indentation... all works! cheers
--
http://mail.python.org/mailman/listinfo/python-list
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file
On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote: > Hi, > > > > just as a side-note > > > > On Wed, Dec 19, 2012 at 02:45:13AM -0800, : > > > for word in list: > > > if word in dict: > > > count = dict[word] > > > count += 1 > > > dict[word] = count > > > else: > > > dict[word] = 1 > > > > When you got the indentation and names right, you can restate this as > > > > import collections > > counter = collections.Counter(words) > > > > in Python 2.7 or as > > > > import collections > > counter = collections.defaultdict(int) > > for word in words: > > counter[word] += 1 > > > > in Python 2.6 > > > > Regards, > > Thomas. Thanks Thomas for your time... using 2.7 great! -- http://mail.python.org/mailman/listinfo/python-list
YOU CAN EARN $200 Day.
PART TIME JOBS You can join get free for $10 http://www.profitclicking.com/?r=J6pLs7V4sU Simple Work,No Experience Necessary, Excellent Rates of Pay,STEP by STEP INTRODUTION, http://www.profitclicking.com/?r=J6pLs7V4sU -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wednesday, 19 December 2012 16:27:19 UTC+5, Thomas Bach wrote:
> On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote:
>
> > Hi,
>
> >
>
> > I am new to python and web2py framework. Need urgent help to match a
>
> > pattern in an string and replace the matched text.
>
> >
>
>
>
> Well, what about str.replace then?
>
>
>
> >>> 'egg, ham, tomato'.replace('ham', 'spam, ham, spam')
>
> 'egg, spam, ham, spam, tomato'
>
>
>
>
>
> If the pattern you want to match is more complicated, have a look at
>
> the re module!
>
>
>
> Regards,
>
> Thomas.
The pattern is '%s.enc_%s', and after matching this pattern want to change it
to 'decrypt(%s.enc_%s)'
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
calculation on lists
hi, I have a question, is there a tool to calculate on list ? something like : >a= [1,1,1,1] >b = [5,9,8,4] >c = a+b*a >print c >[6,10,9,5] Thx -- http://mail.python.org/mailman/listinfo/python-list
Re: calculation on lists
2012/12/19 loïc Lauréote : hi, I have a question, is there a tool to calculate on list ? something like : >a= [1,1,1,1] >b = [5,9,8,4] >c = a+b*a >print c >[6,10,9,5] Thx == Hi, for such simpler cases, you may try list comprehensions and probably the zip(...) function >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] [6, 10, 9, 5] >>> hth, vbr -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
AT wrote: > I am new to python and web2py framework. Need urgent help to match a > pattern in an string and replace the matched text. > > I've this string (basically an sql statement): > stmnt = 'SELECT taxpayer.id, > taxpayer.enc_name, > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > The requirement is to replace it with this one: > r_stmnt = 'SELECT taxpayer.id, >decrypt(taxpayer.enc_name), >taxpayer.age, >taxpayer.occupation >FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > Can somebody please help? > The pattern is '%s.enc_%s', and after matching this pattern want to change > it to 'decrypt(%s.enc_%s)' after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why Doesn't This MySQL Statement Execute?
On Tue, 18 Dec 2012 17:34:08 -0400, Tom Borkin wrote: > Hi; > I have this test code: > > if i_id == "1186": > sql = 'insert into interactions values(Null, %s, "Call Back","% s")' % (i_id, date_plus_2) > cursor.execute(sql) Please don't build your sql strings like this but pass the data as paramaters something like sql="Insert into table (`field1`,`field2`) Values %s,%s" cursor.execute(sql,(data1,data2)) And Goolge SQL injection -- I can hire one half of the working class to kill the other half. -- Jay Gould -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On Wednesday, 19 December 2012 18:16:18 UTC+5, Peter Otten wrote: > AT wrote: > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > I've this string (basically an sql statement): > > > stmnt = 'SELECT taxpayer.id, > > > taxpayer.enc_name, > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > The requirement is to replace it with this one: > > > r_stmnt = 'SELECT taxpayer.id, > > >decrypt(taxpayer.enc_name), > > >taxpayer.age, > > >taxpayer.occupation > > >FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > Can somebody please help? > > > > > The pattern is '%s.enc_%s', and after matching this pattern want to change > > > it to 'decrypt(%s.enc_%s)' > > > > after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before) Thanks a million Can you recommend a good online book/tutorial on regular expr. in python? Regards -- http://mail.python.org/mailman/listinfo/python-list
[newbie] plotting pairs of data
I have a data set for which x and y-values are presented as pairs of floating point numbers: e.g. 0.0364771 0.55569 is the first pair . 0.132688 0.808496 is the second pair . . The data is available in Python in this format: ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', '0.332702 0.849128', '0.432695 0.862158'] I suppose it is possible to plot x versus y using matplotlib, maybe separating the data first in two arrays, but I'm not sure whether this is necessary. Can anyone here help me further thanks hugo -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
Am 19.12.2012 14:41, schrieb AT: > Thanks a million > Can you recommend a good online book/tutorial on regular expr. in python? http://docs.python.org/3/howto/regex.html -- http://mail.python.org/mailman/listinfo/python-list
Virtualenv loses context
I am somewhat new to Python and virtualenv. I have setup a virtualenv with no site packages under the assumption that all 3rd party packages will be installed into the site-packages directory for the virtualenv. Another assumption I have about virtualenv is that it's completely self contained. When I first setup the virtualenv, install my packages and then install my application both of those assumptions seem to be correct. However, at some point I start getting ImportErrors. The be clear, I tested the application and it was all working correctly, the ImportErrors seem to show up at some random point that I cannot reproduce. I have a requirements file that I made with 'pip freeze'. I try to use this to reinstall the packages but I get errors saying that I cannot install to the location. The location is the global site-packages directory on my system. I have the virtualenv activated while installing so I dont know why this is happening. Due to something about Centos6 I cant install M2Crypto using pip and have to do it manually. This gives me the same types of errors: (botnet_etl)[swright@localhost M2Crypto]$ ./fedora_setup.sh install running install error: can't create or remove files in install directory The following error occurred while trying to add or remove files in the installation directory: [Errno 13] Permission denied: '/usr/lib64/python2.6/site-packages/test-easy-inst all-29133.write-test' The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was: /usr/lib64/python2.6/site-packages/ Perhaps your account does not have write access to this directory? If the installation directory is a system-owned directory, you may need to sign in as the administrator or "root" account. If you do not have administrative access to this machine, you may wish to choose a different installation directory, preferably one that is listed in your PYTHONPATH environment variable. For information on other options, you may wish to consult the documentation at: http://peak.telecommunity.com/EasyInstall.html Please make the appropriate changes for your system and try again. The only fix I have managed for this problem so far is to reinstall the entire virtualenv from scratch. This works fine for a while and I have to repeat the process. I would think that I was doing something wrong if the ImportErrors showed up every time I attempt to run the app but the fact that they show up later leads me to believe something funky is going on. Os = Centos6 Python = 2.6.6 Thanks Steve -- http://mail.python.org/mailman/listinfo/python-list
Fuzzy Logic Library for Python 3
Hi all, I'm trying to build an fuzzy expert system in Python 3 and I need a good recommendation for a library/package for this, but working in Python 3.2. Some options are pyfuzzy and pyfuzzylib, but I was not succeed to install these packages with Python 3. Thanks in advance for any answer. Cheers, Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: Pattern-match & Replace - help required
On 2012-12-19 14:11, Alexander Blinne wrote: Am 19.12.2012 14:41, schrieb AT: Thanks a million Can you recommend a good online book/tutorial on regular expr. in python? http://docs.python.org/3/howto/regex.html Another good resource is: http://www.regular-expressions.info/ -- http://mail.python.org/mailman/listinfo/python-list
Py 3.3, unicode / upper()
I was using the German word "Straße" (Strasse) — German
translation from "street" — to illustrate the catastrophic and
completely wrong-by-design Unicode handling in Py3.3, this
time from a memory point of view (not speed):
>>> sys.getsizeof('Straße')
43
>>> sys.getsizeof('STRAẞE')
50
instead of a sane (Py3.2)
>>> sys.getsizeof('Straße')
42
>>> sys.getsizeof('STRAẞE')
42
But, this is not the problem.
I was suprised to discover this:
>>> 'Straße'.upper()
'STRASSE'
I really, really do not know what I should think about that.
(It is a complex subject.) And the real question is why?
jmf
--
http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
This may have something to do with it. I create the virtualenv in: /home/swright/workspace/botnet_etl/ After I install my app the directory structure looks like this: (botnet_etl)[swright@localhost botnet_etl]$ ll total 32 drwxrwxr-x 4 swright swright 4096 Dec 19 09:21 app drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 bin drwxrwxr-x 9 swright swright 4096 Dec 18 10:24 build drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 include drwxrwxr-x 4 swright swright 4096 Dec 18 10:34 lib -rw-rw-r-- 1 swright swright 1973 Dec 17 15:35 README -rw-rw-r-- 1 swright swright 109 Dec 17 15:35 stable-req.txt drwxrwxr-x 3 swright swright 4096 Dec 17 15:35 utils When I run the following command the path is not showing up which is probably what is causing the problem. I have to reinstall from scratch to see if this is different at install time. But, am I supposed to create virtualenv in specific location in the file system? That seems a little odd. -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
I wrote this little test script to prove my virtualenv is reading from global site packages: Script Content from distutils.sysconfig import get_python_lib print (get_python_lib()) Script Output (botnet_etl)[swright@localhost app]$ python test.py /usr/lib/python2.6/site-packages (botnet_etl)[swright@localhost app]$ Please note that (botnet_etl) is the name of the virtualenv -- http://mail.python.org/mailman/listinfo/python-list
Telnetlib and special quit characters with Ctrl, oh my!
Greetings all, I am using telnetlib and the box that I'm connecting to has a special escape sequence--^]--to leave the prompt and go back to the regular telnet prompt. For example, from teh command line I do this: telnet 123.12.123.0 Login:> xx Password:> xxx Welcome. Connected to '123.12.123.0'. Escape character is '^]'. #> (some commands) (responses) When I pressing and hold Ctrl and then ], I go back to my normal telnet prompt and can quit. Using telnetlib, I can send commands and get responses. But I'm stuck on sending the ctrl+]! Is there some escape character--\c?--or a keycode like \^ to send through telnetlib? Telnetlib is doing what I need it to, but the read_until command that I'm familiar appears to be getting stuck on the fact that I never leave the connection properly--like I would normally do with an "exit" or "quit". As always, I'm glad for comments and pointers in the right direction! Thank you all in advance, newbo -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
This may have something to do with it. I create the virtualenv in: /home/swright/workspace/botnet_etl/ After I install my app the directory structure looks like this: (botnet_etl)[swright@localhost botnet_etl]$ ll total 32 drwxrwxr-x 4 swright swright 4096 Dec 19 09:21 app drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 bin drwxrwxr-x 9 swright swright 4096 Dec 18 10:24 build drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 include drwxrwxr-x 4 swright swright 4096 Dec 18 10:34 lib -rw-rw-r-- 1 swright swright 1973 Dec 17 15:35 README -rw-rw-r-- 1 swright swright 109 Dec 17 15:35 stable-req.txt drwxrwxr-x 3 swright swright 4096 Dec 17 15:35 utils When I run the following command the path is not showing up which is probably what is causing the problem. Clearly the site packages for my virtualenv are not listed. I have to reinstall from scratch to see if this is different at install time. (botnet_etl)[swright@localhost app]$ python -m site sys.path = [ '/home/swright/workspace/botnet_etl/app', '/usr/lib/python2.6/site-packages/pymongo-2.3-py2.6-linux-x86_64.egg', '/usr/lib/python2.6/site-packages/pip-1.2.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', ] USER_BASE: '/home/swright/.local' (exists) USER_SITE: '/home/swright/.local/lib/python2.6/site-packages' (doesn't exist) ENABLE_USER_SITE: True But, am I supposed to create virtualenv in specific location in the file system? That seems a little odd -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 06:23:00AM -0800, [email protected] wrote: > I was suprised to discover this: > > >>> 'Straße'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Because there is no definition for upper-case 'ß'. 'SS' is used as the common replacement in this case. I think it's pretty smart! :) Regards, Thomas. -- http://mail.python.org/mailman/listinfo/python-list
Re: [newbie] plotting pairs of data
On Wed, Dec 19, 2012 at 05:47:30AM -0800, hugocoolens wrote: > The data is available in Python in this format: > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > '0.332702 0.849128', '0.432695 0.862158'] > > I suppose it is possible to plot x versus y using matplotlib, maybe > separating the data first in two arrays, but I'm not sure whether this > is necessary. I think, yes it is necessary to split the data in separate lists/arrays. Although I find it annoying, too. In case that not only the if, but also the how is the matter of your question something like xs = [ float(x) for x, _ in map(str.split, l) ] ys = [ float(y) for _, y in map(str.split, l) ] should do the trick. Regards, Thomas Bach. -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
Just installed a brand new virtualenv along with two packages. Ran this and I got nothing: (venvtest)[swright@localhost venvtest]$ python -m site (venvtest)[swright@localhost venvtest]$ I expected to have at least one path in sys.path [swright@localhost workspace]$ virtualenv --no-site-packages venvtest New python executable in venvtest/bin/python Installing setuptoolsdone. Installing pip...done. [swright@localhost venvtest]$ source bin/activate (venvtest)[swright@localhost venvtest]$ python -m site (venvtest)[swright@localhost venvtest]$ (venvtest)[swright@localhost venvtest]$ pip install -r stable-req.txt Downloading/unpacking pycrypto==2.6 (from -r stable-req.txt (line 1)) Downloading pycrypto-2.6.tar.gz (443kB): 443kB downloaded Running setup.py egg_info for package pycrypto Downloading/unpacking pymongo==2.4 (from -r stable-req.txt (line 2)) Downloading pymongo-2.4.tar.gz (273kB): 273kB downloaded Running setup.py egg_info for package pymongo Downloading/unpacking yolk==0.4.3 (from -r stable-req.txt (line 3)) Downloading yolk-0.4.3.tar.gz (86kB): 86kB downloaded Running setup.py egg_info for package yolk warning: no files found matching '*.txt' under directory 'tests' warning: no files found matching '*.conf' under directory 'docs' warning: no files found matching '*.css_t' under directory 'docs' warning: no files found matching 'indexsidebar.html' under directory 'docs' warning: no files found matching 'tests/test_cli.py' Requirement already satisfied (use --upgrade to upgrade): setuptools in ./lib/python 2.6/site-packages/setuptools-0.6c11-py2.6.egg (from yolk==0.4.3->-r stable-req.txt ( line 3)) ... lots more text... (venvtest)[swright@localhost venvtest]$ pwd /home/swright/workspace/venvtest (venvtest)[swright@localhost venvtest]$ ll lib/python2.6/site-packages/ total 372 drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 bson drwxrwxr-x 10 swright swright 4096 Dec 19 09:34 Crypto -rw-rw-r-- 1 swright swright237 Dec 19 09:32 easy-install.pth drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 gridfs drwxrwxr-x 4 swright swright 4096 Dec 19 09:32 pip-1.2.1-py2.6.egg drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pycrypto-2.6-py2.6.egg-info drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pymongo drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pymongo-2.4-py2.6.egg-info -rw-r--r-- 1 swright swright 333447 Dec 5 09:13 setuptools-0.6c11-py2.6.egg -rw-rw-r-- 1 swright swright 30 Dec 19 09:32 setuptools.pth drwxrwxr-x 3 swright swright 4096 Dec 19 09:34 yolk drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 yolk-0.4.3-py2.6.egg-info (venvtest)[swright@localhost venvtest]$ -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
Am 19.12.2012 15:23, schrieb [email protected]: > But, this is not the problem. > I was suprised to discover this: > 'Straße'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? It's correct. LATIN SMALL LETTER SHARP S doesn't have an upper case form. However the unicode database specifies an upper case mapping from ß to SS. http://codepoints.net/U+00DF Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Telnetlib and special quit characters with Ctrl, oh my!
On Thu, Dec 20, 2012 at 1:28 AM, wrote: > I am using telnetlib and the box that I'm connecting to has a special escape > sequence--^]--to leave the prompt and go back to the regular telnet prompt. > For example, from teh command line I do this: > ... > When I pressing and hold Ctrl and then ], I go back to my normal telnet > prompt and can quit. > > Using telnetlib, I can send commands and get responses. But I'm stuck on > sending the ctrl+]! Is there some escape character--\c?--or a keycode like > \^ to send through telnetlib? The ctrl-] keystroke doesn't get sent down the wire, it's commands to the _local_ telnet. For instance, if your session has stalled, you can enter "^]close" to immediately disconnect. There won't be a direct way to drop to "command mode" inside telnetlib, but you can do similar actions with methods on the connection object (in that instance, close() will do the job). ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
[email protected] wrote: > But, this is not the problem. > I was suprised to discover this: > > >>> 'Straße'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? http://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F#Versalsatz_ohne_gro.C3.9Fes_.C3.9F "Die gegenwärtigen amtlichen Regeln[6] zur neuen deutschen Rechtschreibung kennen keinen Großbuchstaben zum ß: Jeder Buchstabe existiert als Kleinbuchstabe und als Großbuchstabe (Ausnahme ß). Im Versalsatz empfehlen die Regeln, das ß durch SS zu ersetzen: Bei Schreibung mit Großbuchstaben schreibt man SS, zum Beispiel: Straße -- STRASSE." According to the new official spelling rules the uppercase ß does not exist. The recommendation is to use "SS" when writing in all-caps. As to why: It has always been acceptable to replace ß with "ss" when ß wasn't part of a character set. In the new spelling rules, ß has been officially replaced with "ss" in some cases: http://en.wiktionary.org/wiki/da%C3%9F The uppercase ß isn't really needed, since ß does not occur at the beginning of a word. As far as I know, most Germans wouldn't even know that it has existed at some point or how to write it. Stefan Krah -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
So I reinstalled the virtualenv for my project from scratch. Application runs as expected. Here are the notes that I took while installing. Interestingly the command 'python -m site' produces no output now. Notice that before I reinstalled the virtualenv I got a bunch of paths from that command. I am going to reboot my machine to see if that does something. (must be used to windows) [swright@localhost workspace]$ virtualenv botnet_etl New python executable in botnet_etl/bin/python Installing setuptoolsdone. Installing pip...done. [swright@localhost workspace]$ cd botnet_etl/ [swright@localhost botnet_etl]$ source bin/activate (botnet_etl)[swright@localhost botnet_etl]$ (botnet_etl)[swright@localhost botnet_etl]$ python -m site (botnet_etl)[swright@localhost botnet_etl]$ (botnet_etl)[swright@localhost botnet_etl]$ pip install -r stable-req.txt --- lots of text follows this for compiling and installing Install M2Crypto manually: Installed /home/swright/workspace/botnet_etl/lib/python2.6/site-packages/M2Crypto-0.21.1-py2.6-linux-x86_64.egg Processing dependencies for M2Crypto==0.21.1 Finished processing dependencies for M2Crypto==0.21.1 Install PySVN manually: (botnet_etl)[swright@localhost M2Crypto]$ cp -r ~/.virtualenvs/myvirtualenv/lib/python2.6/site-packages/pysvn ~/workspace/botnet_etl/lib/python2.6/site-packages/. (botnet_etl)[swright@localhost botnet_etl]$ python -m site (botnet_etl)[swright@localhost botnet_etl]$ -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, Dec 20, 2012 at 1:23 AM, wrote: > But, this is not the problem. > I was suprised to discover this: > 'Straße'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Not all strings can be uppercased and lowercased cleanly. Please stop trotting out the old Box Hill-to-Camberwell arguments[1] yet again. For comparison, try this string: '𝐇𝐞𝐥𝐥𝐨, 𝐰𝐨𝐫𝐥𝐝!'.upper() And while you're at it, check out sys.getsizeof() on that sort of string, compare your beloved 3.2 on that. Oh, and also check out len() on it. [1] Melbourne's current ticketing system is based on zones, and Camberwell is in zone 1, and Box Hill in zone 2. Detractors of public transport point out that it costs far more to take the train from Box Hill to Camberwell than it does to drive a car the same distance. It's the same contrived example that keeps on getting trotted out time and time again. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On 19.12.2012 15:23, [email protected] wrote: > I was using the German word "Straße" (Strasse) — German > translation from "street" — to illustrate the catastrophic and > completely wrong-by-design Unicode handling in Py3.3, this > time from a memory point of view (not speed): > sys.getsizeof('Straße') > 43 sys.getsizeof('STRAẞE') > 50 > > instead of a sane (Py3.2) > sys.getsizeof('Straße') > 42 sys.getsizeof('STRAẞE') > 42 How do those arbitrary numbers prove anything at all? Why do you draw the conclusion that it's broken by design? What do you expect? You're very vague here. Just to show how ridiculously pointless your numers are, your example gives 84 on Python3.2 for any input of yours. > But, this is not the problem. > I was suprised to discover this: > 'Straße'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Because in the German language the uppercase "ß" is virtually dead. Regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht öffentlich! Ah, der neueste und bis heute genialste Streich unsere großen Kosmologen: Die Geheim-Vorhersage. - Karl Kaos über Rüdiger Thomas in dsa -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On 19.12.2012 16:18, Johannes Bauer wrote: > How do those arbitrary numbers prove anything at all? Why do you draw > the conclusion that it's broken by design? What do you expect? You're > very vague here. Just to show how ridiculously pointless your numers > are, your example gives 84 on Python3.2 for any input of yours. ...on Python3.2 on MY system is what I meant to say (x86_64 Linux). Sorry. Also, further reading: http://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F http://en.wikipedia.org/wiki/Capital_%E1%BA%9E Regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht öffentlich! Ah, der neueste und bis heute genialste Streich unsere großen Kosmologen: Die Geheim-Vorhersage. - Karl Kaos über Rüdiger Thomas in dsa -- http://mail.python.org/mailman/listinfo/python-list
calculation on lists
Thank for your answer, I found something allowing to avoid loops. I use operator overloading. import math class Vector: def __init__(self, x=0, y=0): self.x=x self.y=y def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y) def __add__(self, vB): return Vector(self.x+vB.x,self.y+vB.y) def __sub__(self, vB): return Vector(self.x-vB.x,self.y-vB.y) def __mul__(self, c): if isinstance(c,Vector): return Vector(self.x*c.x,self.y*c.y) else: return Vector(c*self.x,c*self.y) def __div__(self, c): if isinstance(c,Vector): return Vector(self.x/c.x,self.y/c.y) else: return Vector(c*self.x,c*self.y) a = Vector(4,5) b = Vector(6,7) print a,b print b*b+a thx > Date: Wed, 19 Dec 2012 13:38:28 +0100 > Subject: Re: calculation on lists > From: [email protected] > To: [email protected] > CC: [email protected] > > 2012/12/19 loïc Lauréote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > > >a= [1,1,1,1] > >b = [5,9,8,4] > >c = a+b*a > >print c > >[6,10,9,5] > > Thx > > == > > Hi, > for such simpler cases, you may try list comprehensions and probably > the zip(...) function > > >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] > [6, 10, 9, 5] > >>> > > hth, > vbr -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
Am 19.12.2012 16:01, schrieb Stefan Krah: > The uppercase ß isn't really needed, since ß does not occur at the beginning > of a word. As far as I know, most Germans wouldn't even know that it has > existed at some point or how to write it. I think Python 3.3+ is using uppercase mapping (uc) instead of simple upper case (suc). Some background: The old German Fractur has three variants of the letter S: capital s: S long s: ſ round s: s. ß is a ligature of ſs. ſ is usually used at the beginning or middle of a syllable while s is used at the end of a syllable. Compare Wachſtube (Wach-Stube == guard room) to Wachstube (Wachs-Tube == tube of wax). :) Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, Dec 20, 2012 at 2:18 AM, Johannes Bauer wrote: > On 19.12.2012 15:23, [email protected] wrote: >> I was using the German word "Straße" (Strasse) — German >> translation from "street" — to illustrate the catastrophic and >> completely wrong-by-design Unicode handling in Py3.3, this >> time from a memory point of view (not speed): >> > sys.getsizeof('Straße') >> 43 > sys.getsizeof('STRAẞE') >> 50 >> >> instead of a sane (Py3.2) >> > sys.getsizeof('Straße') >> 42 > sys.getsizeof('STRAẞE') >> 42 > > How do those arbitrary numbers prove anything at all? Why do you draw > the conclusion that it's broken by design? What do you expect? You're > very vague here. Just to show how ridiculously pointless your numers > are, your example gives 84 on Python3.2 for any input of yours. You may not be familiar with jmf. He's one of our resident trolls, and he has a bee in his bonnet about PEP 393 strings, on the basis that they take up more space in memory than a narrow build of Python 3.2 would, for a string with lots of BMP characters and one non-BMP. In 3.2 narrow builds, strings were stored in UTF-16, with *surrogate pairs* for non-BMP characters. This means that len() counts them twice, as does string indexing/slicing. That's a major bug, especially as your Python code will do different things on different platforms - most Linux builds of 3.2 are "wide" builds, storing characters in four bytes each. PEP 393 brings wide build semantics to all Pythons, while achieving memory savings better than a narrow build can (with PEP 393 strings, any all-ASCII or all-Latin-1 strings will be stored one byte per character). Every now and then, though, jmf points out *yet again* that his beloved and buggy narrow build consumes less memory and runs faster than the oh so terrible 3.3 on some contrived example. It gets rather tiresome. Interestingly, IDLE on my Windows box can't handle the bolded characters very well... >>> s="\U0001d407\U0001d41e\U0001d425\U0001d425\U0001d428, >>> \U0001d430\U0001d428\U0001d42b\U0001d425\U0001d41d!" >>> print(s) Traceback (most recent call last): File "", line 1, in print(s) UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001d407' in position 0: Non-BMP character not supported in Tk I think this is most likely a case of "yeah, Windows XP just sucks". But I have no reason or inclination to get myself a newer Windows to find out if it's any different. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
context aware execution
Hi All ! Is is possible and if yes, is it more easily possible (i am thinking f_back maybe) to get the context of the caller when in a function ? Like to which variable name is this object assigned ? Or whatever of the callers context that might be of interest. I want in a function or method determine the context of my caller and adapt the functionality accordingly. thnx, Bart -- http://mail.python.org/mailman/listinfo/python-list
Re: context aware execution
On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: > Hi All ! > > Is is possible and if yes, is it more easily possible (i am thinking f_back > maybe) to get the context of the caller when in a function ? > > Like to which variable name is this object assigned ? > > Or whatever of the callers context that might be of interest. > > I want in a function or method determine the context of my caller and adapt > the functionality accordingly. First off, please don't! Your code will be *extremely* confusing. You can find out what function is calling you, eg by throwing an exception and checking the traceback. My 3AM brain can't think of a way to get the current thread's traceback without throwing, but there may well be a way. (I also don't know of a way to get any other thread's traceback, but that doesn't matter to this discussion.) But finding out what variable name your return value is to be assigned to isn't possible at all. For one thing, it might not be assigned anywhere - it might be being ignored, or it might be used in some other expression, or anything. Usually, the best way to adapt to your caller's environment is to be passed a parameter that specifies the change. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: [newbie] plotting pairs of data
On Wednesday, December 19, 2012 6:38:30 AM UTC-8, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 05:47:30AM -0800, hugocoolens wrote: > > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > > '0.332702 0.849128', '0.432695 0.862158'] > xs = [ float(x) for x, _ in map(str.split, l) ] > ys = [ float(y) for _, y in map(str.split, l) ] Let's play golf :) xs, ys = zip(*(map(float, s.split()) for s in l)) -- http://mail.python.org/mailman/listinfo/python-list
Re: context aware execution
On Thu, 20 Dec 2012, Chris Angelico wrote: On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: I want in a function or method determine the context of my caller and adapt the functionality accordingly. First off, please don't! Your code will be *extremely* confusing. Usually, the best way to adapt to your caller's environment is to be passed a parameter that specifies the change. Or assume that the caller is smart enough to determine which one of the functions to call, and provide them, with good names. -W -- http://mail.python.org/mailman/listinfo/python-list
Problem with Threads
I have this code for Prime Numbers and i want to do it with Threads.. Any idea.?? # prime numbers are only divisible by unity and themselves # (1 is not considered a prime number by convention) import time def isprime(n): if n == 2: return 1 if n % 2 == 0: return 0 max = n**0.5+1 i = 3 while i <= max: if n % i == 0: return 0 i+=2 return 1 print "Please give the maximum number" endnum = input() start = time.time() for i in range(endnum): if isprime(i) == 1: print "Number %d is PRIME" % i print "Elapsed Time: %s" % (time.time() - start) -- http://mail.python.org/mailman/listinfo/python-list
Re: context aware execution
Thanks for your response Chris !
Ha ! the job of the mad man is todo the things the are not "advisable" and
see what gives. Like why it is not advisable and, if possible, their are
ways to achieve things that are previously overseen.
i already do a lot of travelling of the callstack to see from where a
function is called. mostely for logging purposes, like what plugin
registered this callback etc.
lately i also had the need to log the variable name of a object, and the
thought of be able to "break out of the namespace" concept got me thinking
what i am thinking of is code that can examine the context it is run in.
The object, when called can figure out in what kind of space it is living
in and discover what kind of API other objects in the space offer.
This is a pre function that gets called before the actual function/method
and can thus determine if a certain request can be fullfilled.
I bet a decorator could be made, in which i can assign certain caller
context references into variables in the function/method ?
I use 1 generic parameter object, in which i can stuff lots of things, but
i rather have the function be able to see what is around ;]
Think of sending JSON over the wire, reconstruct an object with it and then
let the object figure out what it can and cannot do in this external
environment.
Code i use now is this:
# life/plugs/context.py
#
#
""" show context. """
## basic import
import sys
## context command
def context(event):
result = []
frame = sys._getframe()
code = frame.f_back.f_code
for i in dir(code): print("%s => %s" % (i, getattr(code, i)))
del frame
context.cmnd = "context"
So much to explore ;]
--
http://mail.python.org/mailman/listinfo/python-list
Re: calculation on lists
2012/12/19 loïc Lauréote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > >>a= [1,1,1,1] >>b = [5,9,8,4] >>c = a+b*a >>print c >>[6,10,9,5] > > Thx > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, I guess, if you are interested in effective and more complex matrix computations, you might have a look at numpy: http://www.numpy.org/ Is the following the result you expect? (it seems so, based on the (added) output of your posted code, but its incompatible with the example in the first post). >>> import numpy >>> a=numpy.array([4,5]) >>> b=numpy.array([6,7]) >>> b*b+a array([40, 54]) >>> hth, vbr -- http://mail.python.org/mailman/listinfo/python-list
Brython - Python in the browser
Hi, The objective of Brython is to replace Javascript by Python as the scripting language for web browsers, making it usable on all terminals including smartphones, tablets, connected TVs, etc. Please forgive the lack of ambition ;-) The best introduction is to visit the Brython site (http://www.brython.info). Right click on the page with a clock and take a look at the source code : no Javascript, only Python code inside a tag
Re: calculation on lists
On 12/19/12 09:24, loïc Lauréote wrote: >> is there a tool to calculate on list ? >> >> something like : >> >>> a= [1,1,1,1] >>> b = [5,9,8,4] >>> c = a+b*a >>> print c >>> [6,10,9,5] >> >> Thx >> >> == >> >> Hi, >> for such simpler cases, you may try list comprehensions and probably >> the zip(...) function >> > [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] >> [6, 10, 9, 5] > Thank for your answer, > > I found something allowing to avoid loops. I use operator > overloading. This is a pretty obvious case of "don't duplicate others' effort". With stock python, Vlastimil's list-comprehension using zip() is the canonical answer. I'm pretty sure it's far faster than your class, in addition to feeling far more pythonic. If you happen to have numpy installed, Vlastimil's answer there already does what you want, but has the added benefits of 1) being heavily tested 2) having core parts written in C for efficiency 3) having a complete intuitive interface, rather than yours which may have issues (such as not having left-vs-right issues: a * 5 5 * a should produce the same results) 4) both the list-comprehension version and the numpy version aren't limited to 2-element vectors like your code, but can handle N-element vectors. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 8:40 AM, Chris Angelico wrote: > You may not be familiar with jmf. He's one of our resident trolls, and > he has a bee in his bonnet about PEP 393 strings, on the basis that > they take up more space in memory than a narrow build of Python 3.2 > would, for a string with lots of BMP characters and one non-BMP. In > 3.2 narrow builds, strings were stored in UTF-16, with *surrogate > pairs* for non-BMP characters. This means that len() counts them > twice, as does string indexing/slicing. That's a major bug, especially > as your Python code will do different things on different platforms - > most Linux builds of 3.2 are "wide" builds, storing characters in four > bytes each. >From what I've been able to discern, his actual complaint about PEP 393 stems from misguided moral concerns. With PEP-393, strings that can be fully represented in Latin-1 can be stored in half the space (ignoring fixed overhead) compared to strings containing at least one non-Latin-1 character. jmf thinks this optimization is unfair to non-English users and immoral; he wants Latin-1 strings to be treated exactly like non-Latin-1 strings (I don't think he actually cares about non-BMP strings at all; if narrow-build Unicode is good enough for him, then it must be good enough for everybody). Unfortunately for him, the Latin-1 optimization is rather trivial in the wider context of PEP-393, and simply removing that part alone clearly wouldn't be doing anybody any favors. So for him to get what he wants, the entire PEP has to go. It's rather like trying to solve the problem of wealth disparity by forcing everyone to dump their excess wealth into the ocean. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with Threads
On 12/19/2012 12:11 PM, Kwnstantinos Euaggelidis wrote: > I have this code for Prime Numbers and i want to do it with Threads.. Any > idea.?? Why do you want to do it with threads? Is it to speed up the processing? (Guessing that partly because of your printing "elapsed time." Chances are running this code in multiple threads will be substantially slower. If you want speed, you could speed up the algorithm by a few orders of magnitude. Ask for suggestions. On the other hand, you might be interested in deciding/discussing the tradeoffs of implementing an arbitrary algorithm serially or in parallel, using multiple threads, and/or multiple processes, and/or multiple networked computers, and/or different possible languages. So, if you give us some ideas about your goals, I'm sure many people have ideas to share. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with Threads
On 19/12/12 18:11:37, Kwnstantinos Euaggelidis wrote: > I have this code for Prime Numbers and i want to do it with Threads.. > Any idea.?? Why would you want to do that? It's not going to be any faster, since your code is CPU-bound. You may have several CPUs, but CPython is going to use only one of them. If you want to make it faster, read http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes > # prime numbers are only divisible by unity and themselves > # (1 is not considered a prime number by convention) > import time > def isprime(n): > if n == 2: > return 1 > if n % 2 == 0: > return 0 > max = n**0.5+1 > i = 3 > while i <= max: > if n % i == 0: > return 0 > i+=2 > return 1 > print "Please give the maximum number" > endnum = input() > start = time.time() > for i in range(endnum): > if isprime(i) == 1: > print "Number %d is PRIME" % i > print "Elapsed Time: %s" % (time.time() - start) Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
On 19/12/12 15:38:01, [email protected] wrote: > Just installed a brand new virtualenv along with two packages. Ran this and I > got nothing: > > (venvtest)[swright@localhost venvtest]$ python -m site > (venvtest)[swright@localhost venvtest]$ > > I expected to have at least one path in sys.path For some reason, that doesn't work with python2.7. I guess that's a bug in 2.7. One variant that does work, is: python -c 'import site; site._script();' Another would be: python /usr/lib/python2.7/site.py If you think you may have accidentally deactivated your virtualenv, you can do: python -c 'import sys; print sys.executable;' Your prompt suggests that "venvtest" is active. However, it's possible to deactivate a virtualenv without resetting the prompt; you may have unintentionally done that. If that's the problem, you can solve it by sourcing the "activate" script again. Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtualenv loses context
Thanks for the information Hans, I will double check that stuff. I am positive however that the environment was active. In addition that fact that I get different responses from python -m site from the botnet_etl virtual environment before I blew it away and after I rebuilt it really concerns me. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
gmail.com> writes: > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Because that's what the Unicode spec says to do. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy Logic Library for Python 3
I also pondered this, though not intensely or urgently. I felt like Jython and Jess with FuzzyJess might be interesting http://www.jessrules.com/FAQ.shtml#Q13 though I didn't dig any further. On Wed, Dec 19, 2012 at 8:22 AM, Alexsandro Soares wrote: > Hi all, > >I'm trying to build an fuzzy expert system in Python 3 and I need a good > recommendation for a library/package for this, but working in Python 3.2. > Some options are pyfuzzy and pyfuzzylib, but I was not succeed to install > these packages with Python 3. > Thanks in advance for any answer. > > Cheers, > Alex > -- > http://mail.python.org/mailman/listinfo/python-list > -- Grant Rettke | ACM, AMA, COG, IEEE [email protected] | http://www.wisdomandwonder.com/ Wisdom begins in wonder. ((λ (x) (x x)) (λ (x) (x x))) -- http://mail.python.org/mailman/listinfo/python-list
Re: plotting pairs of data
On 19 dec, 15:38, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 05:47:30AM -0800,hugocoolenswrote: > > The data is available in Python in this format: > > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > > '0.332702 0.849128', '0.432695 0.862158'] > > > I suppose it is possible to plot x versus y using matplotlib, maybe > > separating the data first in two arrays, but I'm not sure whether this > > is necessary. > > I think, yes it is necessary to split the data in separate > lists/arrays. Although I find it annoying, too. > > In case that not only the if, but also the how is the matter of your > question something like > > xs = [ float(x) for x, _ in map(str.split, l) ] > ys = [ float(y) for _, y in map(str.split, l) ] > > should do the trick. > > Regards, > Thomas Bach. thanks Thomas this works nicely hugo -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
Le mercredi 19 décembre 2012 15:52:23 UTC+1, Christian Heimes a écrit : > Am 19.12.2012 15:23, schrieb [email protected]: > > > But, this is not the problem. > > > I was suprised to discover this: > > > > > 'Straße'.upper() > > > 'STRASSE' > > > > > > I really, really do not know what I should think about that. > > > (It is a complex subject.) And the real question is why? > > > > It's correct. LATIN SMALL LETTER SHARP S doesn't have an upper case > > form. However the unicode database specifies an upper case mapping from > > ß to SS. http://codepoints.net/U+00DF > > > > Christian - Yes, it is correct (or can be considered as correct). I do not wish to discuss the typographical problematic of "Das Grosse Eszett". The web is full of pages on the subject. However, I never succeeded to find an "official position" from Unicode. The best information I found seem to indicate (to converge), U+1E9E is now the "supported" uppercase form of U+00DF. (see DIN). What is bothering me, is more the implementation. The Unicode documentation says roughly this: if something can not be honoured, there is no harm, but do not implement a workaroud. In that case, I'm not sure Python is doing the best. If "wrong", this can be considered as programmatically correct or logically acceptable (Py3.2) >>> 'Straße'.upper().lower().capitalize() == 'Straße' True while this will *always* be problematic (Py3.3) >>> 'Straße'.upper().lower().capitalize() == 'Straße' False jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
Le mercredi 19 décembre 2012 19:27:38 UTC+1, Ian a écrit :
> On Wed, Dec 19, 2012 at 8:40 AM, Chris Angelico wrote:
>
> > You may not be familiar with jmf. He's one of our resident trolls, and
>
> > he has a bee in his bonnet about PEP 393 strings, on the basis that
>
> > they take up more space in memory than a narrow build of Python 3.2
>
> > would, for a string with lots of BMP characters and one non-BMP. In
>
> > 3.2 narrow builds, strings were stored in UTF-16, with *surrogate
>
> > pairs* for non-BMP characters. This means that len() counts them
>
> > twice, as does string indexing/slicing. That's a major bug, especially
>
> > as your Python code will do different things on different platforms -
>
> > most Linux builds of 3.2 are "wide" builds, storing characters in four
>
> > bytes each.
>
>
>
> >From what I've been able to discern, his actual complaint about PEP
>
> 393 stems from misguided moral concerns. With PEP-393, strings that
>
> can be fully represented in Latin-1 can be stored in half the space
>
> (ignoring fixed overhead) compared to strings containing at least one
>
> non-Latin-1 character. jmf thinks this optimization is unfair to
>
> non-English users and immoral; he wants Latin-1 strings to be treated
>
> exactly like non-Latin-1 strings (I don't think he actually cares
>
> about non-BMP strings at all; if narrow-build Unicode is good enough
>
> for him, then it must be good enough for everybody). Unfortunately
>
> for him, the Latin-1 optimization is rather trivial in the wider
>
> context of PEP-393, and simply removing that part alone clearly
>
> wouldn't be doing anybody any favors. So for him to get what he
>
> wants, the entire PEP has to go.
>
>
>
> It's rather like trying to solve the problem of wealth disparity by
>
> forcing everyone to dump their excess wealth into the ocean.
latin-1 (iso-8859-1) ? are you sure ?
>>> sys.getsizeof('a')
26
>>> sys.getsizeof('ab')
27
>>> sys.getsizeof('aé')
39
Time to go to bed. More complete answer tomorrow.
jmf
--
http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 1:55 PM, wrote: > Yes, it is correct (or can be considered as correct). > I do not wish to discuss the typographical problematic > of "Das Grosse Eszett". The web is full of pages on the > subject. However, I never succeeded to find an "official > position" from Unicode. The best information I found seem > to indicate (to converge), U+1E9E is now the "supported" > uppercase form of U+00DF. (see DIN). Is this link not official? http://unicode.org/cldr/utility/character.jsp?a=00DF That defines a full uppercase mapping to SS and a simple uppercase mapping to U+00DF itself, not U+1E9E. My understanding of the simple mapping is that it is not allowed to map to multiple characters, whereas the full mapping is so allowed. > What is bothering me, is more the implementation. The Unicode > documentation says roughly this: if something can not be > honoured, there is no harm, but do not implement a workaroud. > In that case, I'm not sure Python is doing the best. But this behavior is per the specification, not a workaround. I think the worst thing we could do in this regard would be to start diverging from the specification because we think we know better than the Unicode Consortium. > If "wrong", this can be considered as programmatically correct > or logically acceptable (Py3.2) > 'Straße'.upper().lower().capitalize() == 'Straße' > True > > while this will *always* be problematic (Py3.3) > 'Straße'.upper().lower().capitalize() == 'Straße' > False On the other hand (Py3.2): >>> 'Straße'.upper().isupper() False vs. Py3.3: >>> 'Straße'.upper().isupper() True There is probably no one clearly correct way to handle the problem, but personally this contradiction bothers me more than the example that you posted. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 2:18 PM, wrote:
> latin-1 (iso-8859-1) ? are you sure ?
Yes.
sys.getsizeof('a')
> 26
sys.getsizeof('ab')
> 27
sys.getsizeof('aé')
> 39
Compare to:
>>> sys.getsizeof('a\u0100')
42
The reason for the difference you posted is that pure ASCII strings
have a further optimization, which I glossed over and which is purely
a savings in overhead:
>>> sys.getsizeof('abcde') - sys.getsizeof('a')
4
>>> sys.getsizeof('ábçdê') - sys.getsizeof('á')
4
--
http://mail.python.org/mailman/listinfo/python-list
Why does os.stat() tell me that my file-group has no members?
I'm using python 2.6.4 on Solaris 5-10.
I have a file named "myFile". It is owned by someone else, by I ("myuser") am
in the file's group ("mygrp"). Below is my python code. Why does it tell me
that mygrp has no members???
>>> import os, pwd, grp
>>> stat_info = os.stat("myFile")
>>> fileUID = stat_info.st_uid
>>> fileGID = stat_info.st_gid
>>> fileGroup = grp.getgrgid(fileGID)[0]
>>> fileUser = pwd.getpwuid(fileUID)[0]
>>> print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID)
grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='',
gr_gid=100, gr_mem=[])
--
http://mail.python.org/mailman/listinfo/python-list
Re: calculation on lists
Chris On Wed, Dec 19, 2012 at 7:24 AM, loïc Lauréote wrote: > Thank for your answer, > > I found something allowing to avoid loops. > I use operator overloading. > > > import math > > class Vector: > def __init__(self, x=0, y=0): > self.x=x > self.y=y > def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y) > def __add__(self, vB): return Vector(self.x+vB.x,self.y+vB.y) > def __sub__(self, vB): return Vector(self.x-vB.x,self.y-vB.y) > def __mul__(self, c): > if isinstance(c,Vector): return Vector(self.x*c.x,self.y*c.y) > else: return Vector(c*self.x,c*self.y) > > > def __div__(self, c): > if isinstance(c,Vector): return Vector(self.x/c.x,self.y/c.y) > else: return Vector(c*self.x,c*self.y) > > > > a = Vector(4,5) > b = Vector(6,7) > print a,b > print b*b+a > > > thx > > Depending on exactly what you are doing, you may also want to look at using NumPy. It is a highly optimized math library for Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Brython - Python in the browser
Hi Pierre this looks very interesting, thanks. But I wonder ... do you know of pyjs (pyjamas as-was)? http://pyjs.org/ I would be interested in a comparison between (the aims of) Brython and pyjs. Either way, thanks for the info. Regards Jon N -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does os.stat() tell me that my file-group has no members?
On 19/12/12 22:40:00, [email protected] wrote: > > > I'm using python 2.6.4 on Solaris 5-10. > > I have a file named "myFile". It is owned by someone else, by > I ("myuser") am in the file's group ("mygrp"). Below is my python > code. Why does it tell me that mygrp has no members??? > > import os, pwd, grp stat_info = os.stat("myFile") fileUID = stat_info.st_uid fileGID = stat_info.st_gid fileGroup = grp.getgrgid(fileGID)[0] fileUser = pwd.getpwuid(fileUID)[0] print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID) > > grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', > gr_gid=100, gr_mem=[]) It doesn't say that your group has no members. Every account has a primary group, and some accounts also have addtional groups. The primary group is the one in the .pw_gid attribute in the pwd entry. The additional groups are those that mention the account in the .gr_mem attribute in their grp entry. Your experiment shows that nobody has "mygrp" as an additional group. So if you're a member of mygrp, then it must be your primary group, i.e. os.getgid() should return 100 for you. You can get a complete list of members of group by adding two lists: def all_members(gid): primary_members = [ user.pw_name for user in pwd.getpwall() if user.pw_gid == gid ] additional_members = grp.getgrgid(gid).gr_mem return primary_members + additional_members Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: Brython - Python in the browser
On 12/19/2012 1:19 PM, Pierre Quentel wrote:
The objective of Brython is to replace Javascript by Python as the
scripting language for web browsers, making it usable on all
terminals including smartphones, tablets, connected TVs, etc. Please
forgive the lack of ambition ;-)
This sounds similar to pyjs, but the latter has two big problems: a)
personality conflicts splits among the developers; b) last I knew, it
was stuck on Python 2.
I think your home page/doc/announcement should specify Python 3 at the
top, so it is not a mystery until one reads down to
"Brython supports most keywords and functions of Python 3 : "
"lists are created with [] or list(), tuples with () or tuple(),
dictionaries with {} or dict() and sets with set()"
non-empty sets are also created with {} and you should support that.
The best introduction is to visit the Brython site
(http://www.brython.info).
That says that my browser, Firefox 17, does not support HTML5. Golly
gee. I don't think any browser support5 all of that moving target, and
Gecko apparently supports about as large a subset as most.
https://en.wikipedia.org/wiki/Comparison_of_layout_engines_%28HTML5%29
It is possible the FF still does not support the particular feature
needed for the clock, but then the page should say just that. Has the
latest FF (17) actually been tested?
To create an element, for instance an HTML anchor :
doc <= A('Python',href="http://www.python.org";)
To me, that is a awful choice and I urge you to change it.
'<=' is not just an operator, it is a comparison operator. It normally
return False or True. Numpy array comparison returns arrays of booleans,
so the meaning is extended, not completely changed. People will often be
using it with its normal mean in conditionals elsewhere, so this usage
creates strong cognitive dissonance. Also, using an expression as a
statement is allowed, but except in the interactive interpreter, it only
makes sense with an expression that obviously has side-effects or could
have side-effects (like the expression 'mylist.sort()'. It just looks
wrong to an experienced Python programmer like me.
It also is unnecessary. Use '+=' or '|='. The former means just what you
want the statement to do and the latter is at least somewhat related
(bit or-addition) and is rarely used and is very unlikely to be used in
code intended for a browser.
It still lacks important features of Python, mostly list
comprehensions and classes ;
Since Python 3 has 4 types of comprehensions, while Python 2 only has
list comprehensions, I took this to mean that Brython was Python 2.
And yes, I am all in favor of being able to use a subset of Py3 instead
of javascript. A full Python interpreter in a browser is too dangerous.
(Actually, I think javascript is too, but that is a different issue.)
Python translated to javascript cannot be worse than javascript. I
presume the same would be true if the javascript step were omitted and
Python were directly compiled to the virtual machines defined by current
javascript engines.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list
Re: calculation on lists
On Wed, Dec 19, 2012 at 4:38 AM, Vlastimil Brom wrote: > 2012/12/19 loïc Lauréote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > > >a= [1,1,1,1] > >b = [5,9,8,4] > >c = a+b*a > >print c > >[6,10,9,5] > > Thx > > == > > Hi, > for such simpler cases, you may try list comprehensions and probably > the zip(...) function > > >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] > [6, 10, 9, 5] > >>> > You can also use map (Python 2.6): map(lambda a,b: a+b*a, [1,1,1,1], [5,9,8,4]) Note that the lambda can be replaced by any callable which takes 2 arguments. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy Logic Library for Python 3
On 12/19/2012 9:22 AM, Alexsandro Soares wrote: Hi all, I'm trying to build an fuzzy expert system in Python 3 and I need a good recommendation for a library/package for this, but working in Python 3.2. Some options are pyfuzzy and pyfuzzylib, Please tell the authors that you would like to use with Py3 but can't. One reason people have had for not upgrading is because there is 'no demand'. Of course, happy 2.x users are mostly happy to continue with 2.x, whereas potential 3.x users usually silently move on. > but I was not succeed to install these packages with Python 3. I do not know about installation issues and how there are related to Py3. I would expect that most of the actual code should run with little or no change. The only change in arithmetic is int / int, and that can be given the Py3 meaning in 2.6/7 with a future import. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: context aware execution
On 12/19/2012 09:51 AM, Bart Thate wrote: > Think of sending JSON over the wire, reconstruct an object with it and then > let the object figure out what it can and cannot do in this external > environment. Probably the better way to do it is to formally define an API that lets an object discover things about its environment and then broker calls through it. This is sort of what COM does on windows, or Corba. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On 12/19/2012 10:40 AM, Chris Angelico wrote: Interestingly, IDLE on my Windows box can't handle the bolded characters very well... s="\U0001d407\U0001d41e\U0001d425\U0001d425\U0001d428, \U0001d430\U0001d428\U0001d42b\U0001d425\U0001d41d!" print(s) Traceback (most recent call last): File "", line 1, in print(s) UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001d407' in position 0: Non-BMP character not supported in Tk On 3.3.0 on Win7 , the expressions 's', 'repr(s)', and 'str(s)' (without the quotes) echo the input as entered (with \U escapes) while 'print(s)' gets the same traceback you did. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Brython - Python in the browser
On Wed, Dec 19, 2012 at 5:07 PM, Terry Reedy wrote:
> That says that my browser, Firefox 17, does not support HTML5. Golly gee. I
> don't think any browser support5 all of that moving target, and Gecko
> apparently supports about as large a subset as most.
> https://en.wikipedia.org/wiki/Comparison_of_layout_engines_%28HTML5%29
> It is possible the FF still does not support the particular feature needed
> for the clock, but then the page should say just that. Has the latest FF
> (17) actually been tested?
It works for me using FF 17.0.1.
>> To create an element, for instance an HTML anchor :
>> doc <= A('Python',href="http://www.python.org";)
>
>
> To me, that is a awful choice and I urge you to change it.
+1. The DOM already has a well-established API. The following may
require more typing:
link = document.createElement('a')
link.setAttribute("href", "http://www.python.org/";)
link.appendChild(document.createTextNode('Python'))
document.body.appendChild(link)
But it is much clearer in intent. Since these methods map directly to
DOM methods, I know exactly what I expect them to do, and I can look
them up in the browser documentation if I have any doubts. With the
one-liner above, I don't know exactly what that maps to in actual DOM
calls, and so I'm a lot less clear on what exactly it is supposed to
do. I'm not even entirely certain whether it's actually equivalent to
my code above.
I suggest that Brython should have a "low-level" DOM API that matches
up to the actual DOM in as close to a 1:1 correspondence as possible.
Then if you want to have a higher-level API that allows whiz-bang
one-liners like the above, build it as an abstraction on top of the
low-level API and include it as an optional library. This has the
added benefit that if the user runs into an obscure bug where the
fancy API breaks on some particular operation on some specific
browser, they will still have the option of falling back to the
low-level API to work around it. It would also make the conversion
barrier much lower for web programmers looking to switch to Brython,
if they can continue to use the constructs that they're already
familiar with but just write them in Python instead of JavaScript.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Why does os.stat() tell me that my file-group has no members?
Thanks!! This was very helpful. It worked perfectly. I had no clue about the intricacies of how python represents the group data from the underlying OS. This page doesn't go into to detailed explanation like you did: http://docs.python.org/2/library/grp.html On Wednesday, December 19, 2012 6:17:16 PM UTC-5, Hans Mulder wrote: > On 19/12/12 22:40:00, [email protected] wrote: > > > > > > > > > I'm using python 2.6.4 on Solaris 5-10. > > > > > > I have a file named "myFile". It is owned by someone else, by > > > I ("myuser") am in the file's group ("mygrp"). Below is my python > > > code. Why does it tell me that mygrp has no members??? > > > > > > > > import os, pwd, grp > > stat_info = os.stat("myFile") > > fileUID = stat_info.st_uid > > fileGID = stat_info.st_gid > > fileGroup = grp.getgrgid(fileGID)[0] > > fileUser = pwd.getpwuid(fileUID)[0] > > print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID) > > > > > > grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', > > gr_gid=100, gr_mem=[]) > > > > It doesn't say that your group has no members. > > > > Every account has a primary group, and some accounts also > > have addtional groups. The primary group is the one in the > > .pw_gid attribute in the pwd entry. The additional groups > > are those that mention the account in the .gr_mem attribute > > in their grp entry. > > > > Your experiment shows that nobody has "mygrp" as an additional > > group. So if you're a member of mygrp, then it must be your > > primary group, i.e. os.getgid() should return 100 for you. > > > > You can get a complete list of members of group by adding > > two lists: > > > > def all_members(gid): > > primary_members = [ user.pw_name > > for user in pwd.getpwall() if user.pw_gid == gid ] > > additional_members = grp.getgrgid(gid).gr_mem > > return primary_members + additional_members > > > > > > Hope this helps, > > > > -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, Dec 20, 2012 at 8:23 AM, Ian Kelly wrote:
> On Wed, Dec 19, 2012 at 1:55 PM, wrote:
>> Yes, it is correct (or can be considered as correct).
>> I do not wish to discuss the typographical problematic
>> of "Das Grosse Eszett". The web is full of pages on the
>> subject. However, I never succeeded to find an "official
>> position" from Unicode. The best information I found seem
>> to indicate (to converge), U+1E9E is now the "supported"
>> uppercase form of U+00DF. (see DIN).
>
> Is this link not official?
>
> http://unicode.org/cldr/utility/character.jsp?a=00DF
>
> That defines a full uppercase mapping to SS and a simple uppercase
> mapping to U+00DF itself, not U+1E9E. My understanding of the simple
> mapping is that it is not allowed to map to multiple characters,
> whereas the full mapping is so allowed.
Ahh, thanks, that explains why the other Unicode-aware language I
tried behaved differently.
Pike v7.9 release 5 running Hilfe v3.5 (Incremental Pike Frontend)
> string s="Stra\u00dfe";
> upper_case(s);
(1) Result: "STRA\337E"
> lower_case(upper_case(s));
(2) Result: "stra\337e"
> String.capitalize(lower_case(s));
(3) Result: "Stra\337e"
The output is the equivalent of repr(), and it uses octal escapes
where possible (for brevity), so \337 is its representation of U+00DF
(decimal 223, octal 337). Upper-casing and lower-casing this character
result in the same thing.
> write("Original: %s\nLower: %s\nUpper: %s\n",s,lower_case(s),upper_case(s));
Original: Straße
Lower: straße
Upper: STRAßE
It's worth noting, incidentally, that the unusual upper-case form of
the letter (U+1E9E) does lower-case to U+00DF in both Python 3.3 and
Pike 7.9.5:
> lower_case("Stra\u1E9Ee");
(9) Result: "stra\337e"
>>> ord("\u1e9e".lower())
223
So both of them are behaving in a compliant manner, even though
they're not quite identical.
ChrisA
--
http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, Dec 20, 2012 at 5:27 AM, Ian Kelly wrote: > From what I've been able to discern, [jmf's] actual complaint about PEP > 393 stems from misguided moral concerns. With PEP-393, strings that > can be fully represented in Latin-1 can be stored in half the space > (ignoring fixed overhead) compared to strings containing at least one > non-Latin-1 character. jmf thinks this optimization is unfair to > non-English users and immoral; he wants Latin-1 strings to be treated > exactly like non-Latin-1 strings (I don't think he actually cares > about non-BMP strings at all; if narrow-build Unicode is good enough > for him, then it must be good enough for everybody). Not entirely; most of his complaints are based on performance (speed and/or memory) of 3.3 compared to a narrow build of 3.2, using silly edge cases to prove how much worse 3.3 is, while utterly ignoring the fact that, in those self-same edge cases, 3.2 is buggy. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does os.stat() tell me that my file-group has no members?
On Thu, Dec 20, 2012 at 12:23 PM, wrote: > > Thanks!! This was very helpful. It worked perfectly. > I had no clue about the intricacies of how python represents the group data > from the underlying OS. > > This page doesn't go into to detailed explanation like you did: > http://docs.python.org/2/library/grp.html Want to show your appreciation in a lasting way? Write up a patch, or a new paragraph or so of text for the page, and open a tracker issue to improve the docs! http://bugs.python.org/ ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Brython - Python in the browser
On 12/19/2012 7:54 PM, Ian Kelly wrote:
On Wed, Dec 19, 2012 at 5:07 PM, Terry Reedy wrote:
That says that my browser, Firefox 17, does not support HTML5. Golly gee. I
don't think any browser support5 all of that moving target, and Gecko
apparently supports about as large a subset as most.
https://en.wikipedia.org/wiki/Comparison_of_layout_engines_%28HTML5%29
It is possible the FF still does not support the particular feature needed
for the clock, but then the page should say just that. Has the latest FF
(17) actually been tested?
It works for me using FF 17.0.1.
It works for me too when ignore the mistaken and misleading error
message and just turn on javascript for the page. Some sites say things
like "You have javascript turned off. Turn it on to see all features."
To create an element, for instance an HTML anchor :
doc <= A('Python',href="http://www.python.org";)
To me, that is a awful choice and I urge you to change it.
+1. The DOM already has a well-established API. The following may
require more typing:
link = document.createElement('a')
link.setAttribute("href", "http://www.python.org/";)
link.appendChild(document.createTextNode('Python'))
document.body.appendChild(link)
But it is much clearer in intent.
I agree with the rest of your suggestion.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 02:23:15PM -0700, Ian Kelly wrote: > On Wed, Dec 19, 2012 at 1:55 PM, wrote: > > If "wrong", this can be considered as programmatically correct > > or logically acceptable (Py3.2) > > > 'Straße'.upper().lower().capitalize() == 'Straße' > > True > > > > while this will *always* be problematic (Py3.3) > > > 'Straße'.upper().lower().capitalize() == 'Straße' > > False > > On the other hand (Py3.2): > > >>> 'Straße'.upper().isupper() > False > > vs. Py3.3: > > >>> 'Straße'.upper().isupper() > True > > There is probably no one clearly correct way to handle the problem, > but personally this contradiction bothers me more than the example > that you posted. Why would it ever be wrong for 'Straße' to not equal 'Strasse'? Python is not intended to do any sort of advanced linguistic processing. It is comparing strings not words. It is not problematic. It makes sense. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On 12/19/2012 9:03 PM, Chris Angelico wrote: On Thu, Dec 20, 2012 at 5:27 AM, Ian Kelly wrote: From what I've been able to discern, [jmf's] actual complaint about PEP 393 stems from misguided moral concerns. With PEP-393, strings that can be fully represented in Latin-1 can be stored in half the space (ignoring fixed overhead) compared to strings containing at least one non-Latin-1 character. jmf thinks this optimization is unfair to non-English users and immoral; he wants Latin-1 strings to be treated exactly like non-Latin-1 strings (I don't think he actually cares about non-BMP strings at all; if narrow-build Unicode is good enough for him, then it must be good enough for everybody). Not entirely; most of his complaints are based on performance (speed and/or memory) of 3.3 compared to a narrow build of 3.2, using silly edge cases to prove how much worse 3.3 is, while utterly ignoring the fact that, in those self-same edge cases, 3.2 is buggy. And the fact that stringbench.py is overall about as fast with 3.3 as with 3.2 *on the same Windows 7 machine* (which uses narrow build in 3.2), and that unicode operations are not far from bytes operations when the same thing can be done with both. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Wed, Dec 19, 2012 at 09:54:20PM -0500, Terry Reedy wrote: > On 12/19/2012 9:03 PM, Chris Angelico wrote: > >On Thu, Dec 20, 2012 at 5:27 AM, Ian Kelly wrote: > >> From what I've been able to discern, [jmf's] actual complaint about PEP > >>393 stems from misguided moral concerns. With PEP-393, strings that > >>can be fully represented in Latin-1 can be stored in half the space > >>(ignoring fixed overhead) compared to strings containing at least one > >>non-Latin-1 character. jmf thinks this optimization is unfair to > >>non-English users and immoral; he wants Latin-1 strings to be treated > >>exactly like non-Latin-1 strings (I don't think he actually cares > >>about non-BMP strings at all; if narrow-build Unicode is good enough > >>for him, then it must be good enough for everybody). > > > >Not entirely; most of his complaints are based on performance (speed > >and/or memory) of 3.3 compared to a narrow build of 3.2, using silly > >edge cases to prove how much worse 3.3 is, while utterly ignoring the > >fact that, in those self-same edge cases, 3.2 is buggy. > > And the fact that stringbench.py is overall about as fast with 3.3 > as with 3.2 *on the same Windows 7 machine* (which uses narrow build > in 3.2), and that unicode operations are not far from bytes > operations when the same thing can be done with both. > > -- > Terry Jan Reedy Really, why should we be so obsessed with speed anyways? Isn't improving the language and fixing bugs far more important? -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, Dec 20, 2012 at 2:12 PM, Westley Martínez wrote: > Really, why should we be so obsessed with speed anyways? Isn't > improving the language and fixing bugs far more important? Because speed is very important in certain areas. Python can be used in many ways: * Command-line calculator with awesome precision and variable handling * Proglets, written once and run once, doing one simple job and then moving on * Applications that do heaps of work and are run multiple times a day * Internet services (eg web server), contacted many times a second * Etcetera * Etcetera * And quite a few other ways too For the first two, performance isn't very important. No matter how slow the language, it's still going to respond "3" instantly when you enter "1+2", and unless you're writing something hopelessly inefficient or brute-force, the time spent writing a proglet usually dwarfs its execution time. But performance is very important for something like Mercurial, which is invoked many times and always with the user waiting for it. You want to get back to work, not sit there for X seconds while your source control engine fires up and does something. And with a web server, language performance translates fairly directly into latency AND potential requests per second on any given hardware. To be sure, a lot of Python performance hits the level of "sufficient" and doesn't need to go further, but it's still worth considering. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On 12/19/2012 10:12 PM, Westley Martínez wrote: On Wed, Dec 19, 2012 at 09:54:20PM -0500, Terry Reedy wrote: On 12/19/2012 9:03 PM, Chris Angelico wrote: On Thu, Dec 20, 2012 at 5:27 AM, Ian Kelly wrote: From what I've been able to discern, [jmf's] actual complaint about PEP 393 stems from misguided moral concerns. With PEP-393, strings that can be fully represented in Latin-1 can be stored in half the space (ignoring fixed overhead) compared to strings containing at least one non-Latin-1 character. jmf thinks this optimization is unfair to non-English users and immoral; he wants Latin-1 strings to be treated exactly like non-Latin-1 strings (I don't think he actually cares about non-BMP strings at all; if narrow-build Unicode is good enough for him, then it must be good enough for everybody). Not entirely; most of his complaints are based on performance (speed and/or memory) of 3.3 compared to a narrow build of 3.2, using silly edge cases to prove how much worse 3.3 is, while utterly ignoring the fact that, in those self-same edge cases, 3.2 is buggy. And the fact that stringbench.py is overall about as fast with 3.3 as with 3.2 *on the same Windows 7 machine* (which uses narrow build in 3.2), and that unicode operations are not far from bytes operations when the same thing can be done with both. -- Terry Jan Reedy Really, why should we be so obsessed with speed anyways? Isn't improving the language and fixing bugs far more important? Being conservative, there are probably at least 10 enhancement patches and 30 bug fix patches for every performance patch. Performance patches are considered enhancements and only go in new versions with enhancements, where they go through the extended alpha, beta, candidate test and evaluation process. In the unicode case, Jim discovered that find was several times slower in 3.3 than 3.2 and claimed that that was a reason to not use 3.2. I ran the complete stringbency.py and discovered that find (and consequently find and replace) are the only operations with such a slowdown. I also discovered that another at least as common operation, encoding strings that only contain ascii characters to ascii bytes for transmission, is several times as fast in 3.3. So I reported that unless one is only finding substrings in long strings, there is no reason to not upgrade to 3.3. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Py 3.3, unicode / upper()
On Thu, 20 Dec 2012 00:32:42 -0500, Terry Reedy wrote:
> In the unicode case, Jim discovered that find was several times slower
> in 3.3 than 3.2 and claimed that that was a reason to not use 3.2. I ran
> the complete stringbency.py and discovered that find (and consequently
> find and replace) are the only operations with such a slowdown. I also
> discovered that another at least as common operation, encoding strings
> that only contain ascii characters to ascii bytes for transmission, is
> several times as fast in 3.3. So I reported that unless one is only
> finding substrings in long strings, there is no reason to not upgrade to
> 3.3.
Yes, and if you remember, Jim (jfm) based his complaints on very possibly
the worst edge-case for the new Unicode implementation:
- generate a large string of characters
- replace every character in that string with another character
By memory:
s = "a"*10
s = s.replace("a", "b")
or equivalent. Hardly representative of normal string processing, and
likely to be the worst-performing operation on new Unicode strings. And
yet even so, many people reported either a mild slow down or, in a few
cases, a small speed up.
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Determining if any threads are waiting for GIL
I'm working on a package that can compile CPython byte-code into native machine code (so far: x86 and x86_64 are supported). I have support for almost every byte-code instruction implemented already, but to fully emulate the interpreter, I need an efficient way to determine two things: when to give up the global interpreter lock (GIL) so another thread can use it, and if there are any asynchronous calls pending (a la Py_AddPendingCall). The interpreter loop does this by polling a pair of boolean values (technically there is a third value it polls first as an optimization, but this is not important), but these values are not exposed by the API in any way. Even if I just implement context switching between compiled code only, I would still need a way to determine if there are pending asynchronous calls. The whole point of this package is to speed up Python programs, so unconditionally calling Py_MakePendingCalls every once in a while is unacceptable. So far the only solution I have come up with is to implement a basic machine code interpreter that would parse PyEval_AcquireLock and Py_AddPendingCall at run-time to determine the addresses of the values the Python interpreter uses. I wouldn't need to fully parse the machine code since for most instructions, I just need to figure out how long it is so I can skip over it, but with varying compilers and build settings, this would be opening up a can of worms. Any suggestions are welcome. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does os.stat() tell me that my file-group has no members?
[email protected] writes: > Thanks!! This was very helpful. It worked perfectly. > I had no clue about the intricacies of how python represents the group data > from the underlying OS. > > This page doesn't go into to detailed explanation like you did: > http://docs.python.org/2/library/grp.html > That's mostly because it represents the group data almost exactly as the underlying OS, including using the same structure member names as defined in pwd.h. The unix-specific modules often assume the user is familiar with the underlying library calls and data structures, and is looking for a way to use them from python. > -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list
