Detect character encoding
Hello, is there any way how to detect string encoding in Python? I need to proccess several files. Each of them could be encoded in different charset (iso-8859-2, cp1250, etc). I want to detect it, and encode it to utf-8 (with string function encode). Thank you for any answer Regards Michal -- http://mail.python.org/mailman/listinfo/python-list
Re: Detect character encoding
Thanks everybody for helpfull advices. Michal -- http://mail.python.org/mailman/listinfo/python-list
how to check compile options
hello, how i could check, if my python was compiled with option WANT_HUGE_STACK_SIZE=yes (or option CFLAG: -DTHREAD_STACK_SIZE=0x10) i have already installed python 2.3.5 on my freebsd 5.4 from ports, but i dont remember, if i used this options. thank you for answer regards michal -- http://mail.python.org/mailman/listinfo/python-list
SnakeScript? (CoffeeScript for Python)
Hello, I've been looking for something similar to CoffeeScript, but for python. Does anyone know of such project? So far I haven't found any attempt to do this, so I took few regular expressions and hacked this: https://plus.google.com/116702779841286800811/posts/56sBdwiZ4fT Any advice on what parses to use for the CoffeeScript-like syntaxe? I would like to use parser written in Python so I don't introduce dependencies. Any advice from Python gurus / language experimentators? -- http://mail.python.org/mailman/listinfo/python-list
Re: SnakeScript? (CoffeeScript for Python)
See the link I attached.
Ruby-like blocks would be nice too.
Implicit returns.
Better strings like """My name is #{name}""".
--
http://mail.python.org/mailman/listinfo/python-list
Moving to Python from PHP - 3 questions
Hi everyone, I've been planning to move to Python from PHP for some time now. I use PHP extensively for web scripting, with mod_php, Apache, and a DB (I would characterize my knowledge of PHP as advanced). Here are three stumbling blocks I've experienced, for which I couldn't seem to find any helpful information via Google: 1) Mod_python on OSX. I use a Mac as my primary development environment, so I need mod_python to get anything done. Compiling mod_python from source fails under OSX 10.3, and mod_python does not appear to be included in Fink. I've found plenty of other questions about this on other mailing lists, but so far no conclusive answers - is mod_python on OSX currently a dead-end? 2) Session management. Cookie-based sessions in PHP are pretty transparent, with a small library of basic functions that do 95% of what anyone may need to store session data in serialized files and associate them with cookies. I've seen python code that accomplishes this, but so far no pre-built server-side session management modules. 3) Structured request variables. PHP has a really handy feature where request variables with name like "foo[]", "foo[bar]", or "foo[bar][baz]" are automatically structured into nested associative arrays. I can see that the python cgi module will make a list of MiniFieldStorage objects when more than one variable with the same name is provided, but that's about the extent of it that I've seen. 2 & 3 are problems I can solve on my own, but they're so transparently automatic in PHP that it's a real pleasure to use them. I can probably implement my own fixes, but I'd rather not have to. Alternatively, are these just "PHP-isms" for which python has a more native, more appropriate response? An "RTFM" answer with a URL for the M would be great. :) Thank you, -mike. -- michal migurski- contact info, blog, and pgp key: sf/cahttp://mike.teczno.com/contact.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving to Python from PHP - 3 questions
The python-based zope application server has session management. Togther with a built-in user and access rights management. ... This can be done in zope if you name a variable :list. That then will give you the variable as list regardless of the number of occurences. Thank you. My impression of Zope in the past has been that it does what I need, along with 10,000 other things I don't (built in WebDAV server?!), but clearly I owe it another chance. I've been initially attracted to mod_python because of its raw simplicity and its apparent similarity to mod_php and mod_perl, which I am familiar with. I'll give Zope a try. -- michal migurski- contact info, blog, and pgp key: sf/cahttp://mike.teczno.com/contact.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving to Python from PHP - 3 questions
Thank you. My impression of Zope in the past has been that it does what I need, along with 10,000 other things I don't (built in WebDAV server?!), but clearly I owe it another chance. I've been initially attracted to mod_python because of its raw simplicity and its apparent similarity to mod_php and mod_perl, which I am familiar with. I'll give Zope a try. I am moving my band's website from an antiquated and horrible PHPNuke site to a Python-based site, and so have done quite a bit of research about this (though I never considered myself a PHP expert). Here's what I've come up with... [snip] This is awesome, quote a bit to chew on. Thank you Joe! ------ michal migurski- contact info, blog, and pgp key: sf/cahttp://mike.teczno.com/contact.html -- http://mail.python.org/mailman/listinfo/python-list
wxPtyhon 2.4 non trivial problems :D
Hi to everybody :)
I use Boa-constructor 0.3.1, wx 2.4 and python 2.3.
I'have 2 big problems in my application :
1. My EVT_KEY_UP on wxTextCtrl works only when mouse pointer is on the
active window. If i'll move it outside the window (with no clicking)
it doesn't work. And if i move mouse pointer back on the window - it
works.
Anyone know why???
2. My application is divided into few sections. I want to create each
window only once - on first visit, because creating a window takes a
80% of time needed to open it (about 6 seconds on a middle-sized
window). I did a construction which allows to do this and which
destroys all windows on exiting to another section of program. Here is
my solution :
from wxPython.wx import *
from wxPython.lib.maskededit import *
from wxPython.lib.maskednumctrl import *
...
window = None // variable which has a object inside (its not
destroyed
after a closing a window
oldparent = None // it helps me to check do i have to recreate a
window with
new parent
def ShowMyWindow(parent): //this function shows my window. And
creates
it if needed
global window,oldparent
if oldparent<>parent:
oldparent = parent
window = create(parent)
window.Inicialize(parent)// it does some things to window -
sets
accelerator table, sets
fonts,loads data and
such
window.SetTitle(' My window')
window.MakeModal(True)
window.Show()
And i close my window with :
def OnMyWindowClose(self, event):
self.Hide()
self.MakeModal(FALSE)
It's destroyed when a parent is destroyed.
It looks simple.
Now my problem :D
I can create a chains of windows. They are all destroyed when the
parent of the first of them is being destroyed.
I want to create tree :
Parent
|
Child1
|
Child2
Where parent is created and destroyed on open/close and children are
created once and destroyed with parent.
When i open a child1 and close it - it stays created, and if i open it
second time it opens faster and works properly. It happens with second
child too. Problem appears when i : open first child, open second
child and close both. Then they both exist and child1 is parent of
child2. After that if i open child1 it works properly, but opening of
child2 results with some kind of inactivity of it. It's losing focus
on all application and is inactive in strange way. My tests told me
that both windows exist.
Can anyone can help me? It's really important to me...
Thanks for answers
Michal
--
http://mail.python.org/mailman/listinfo/python-list
Data reading problem
I'm a newbie. I have some problems with bits data reading. I have binary data file where data is written as 12bits pack. I need to read it becouse there are saved values which have to processed later by my program. I was wandering about reading 3bytes=24bits and split it by bits moving. If anyone know how to do that please tell me or give me some code. My data example: 10100100010010100101 etc i need to split it: x=101001000101 y=111010100101 and then do: mydata=x*a+bwhere a and b are a float point numbers mydata2=y*a+b Sorry for my poor english. I wish You understand what I mean. THX for any answer. -- Mike Szpadzik -- http://mail.python.org/mailman/listinfo/python-list
Strange behavior
Hello, I've just installed Python 3.5.1 on my computer and came across somewhat strange behavior. This is a snapshot of a short code that I wrote: https://goo.gl/izYbD0 Quite simple, you might say. Obviously, the program should create a new text file, nothing else. But do you see the numbers it had added to the output? Is this behavior normal or am I overlooking something? Thank you very much for your help. Michal, Slovakia -- https://mail.python.org/mailman/listinfo/python-list
Re: [Pylint-dev] [code-quality] [ANN] Pylint 1.4 released
Hello, Just submitted pull request with a fix: https://bitbucket.org/logilab/pylint/pull-request/205/fixed-reading-list-of-ignored-words-for/diff Regards, Godfryd On Thu, Nov 27, 2014 at 7:00 PM, Ned Batchelder wrote: > On 11/27/14 11:59 AM, Julien Cristau wrote: > >> On Thu, Nov 27, 2014 at 10:19:28 -0500, Ned Batchelder wrote: >> >> I found a problem with the new spell feature, but the issue tracker >>> (http://www.bytebucket.org/logilab/pylint/issues) seems broken: >>> everything I tried ended at a 403 CSRF validation failure page. >>> >>> Not sure where that url came from, afaik the tracker is >> https://bitbucket.org/logilab/pylint/issues >> >> > Hmm, not sure where bytebucket came from either! Sorry for the noise... > > Cheers, >> Julien >> >> > > -- > Ned Batchelder, http://nedbatchelder.com > > ___ > Pylint-dev mailing list > [email protected] > http://lists.logilab.org/mailman/listinfo/pylint-dev > -- https://mail.python.org/mailman/listinfo/python-list
Re: list of lists of lists ....
faulkner wrote: > ok, so, recursion is just functional programming sugar for a loop. And a loop is a procedural programming sugar for tail recursion. 8-) Cheers, mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
FTP HANGS on very slow connection
Hi,
I am trying to download data from remote ftp server connected through
GSM modem. The speed is slow 1kB/s. Despite the classic linux ftp
client works fine the ftp.retrbinary(),ftpretrlines() function from
ftplib hangs. I am able succesfuly login but a can not download. It has
probably something common with sockets.
Code:
***
#Open ftp connection and downloads file
ftpDLoad = FTP(hostAdress)
ftpDLoad.set_debuglevel(2)
#ftpDLoad.sendcmd('USER x')
#ftpDLoad.sendcmd('PASS x')
ftpDLoad.login(userName,userPass)
#print ftpDLoad.login()
#Prints start time
print '###Starting sync.: ',datetime.today()
#Opens file for writing
fileObj = open(tmpDir + rFileName,'wb')
#Downloads file
#!!!
#ftpDLoad.retrbinary('RETR ' + rFileName, fileObj.write)
#!!!
ftpDLoad.retrlines('RETR' + rFileName)
#Closes file
fileObj.close()
#Checks the file size
ftpDLoad.cwd('modem')
Ouput:
**
[EMAIL PROTECTED]:~/python$ ./jezeri_new_ftp.py
*cmd* 'USER telis'
*put* 'USER telis\r\n'
*get* '331 Password required for telis\r\n'
*resp* '331 Password required for telis'
*cmd* 'PASS **'
*put* 'PASS **\r\n'
*get* '230 Logged on\r\n'
*resp* '230 Logged on'
###Starting sync.: 2006-08-10 22:17:06.634695
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A\r\n'
*resp* '200 Type set to A'
*cmd* 'PASV'
*put* 'PASV\r\n'
Traceback (most recent call last):
File "./jezeri_new_ftp.py", line 43, in ?
ftpDLoad.retrlines('RETR' + rFileName)
File "/usr/lib/python2.4/ftplib.py", line 396, in retrlines
conn = self.transfercmd(cmd)
File "/usr/lib/python2.4/ftplib.py", line 345, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.4/ftplib.py", line 321, in ntransfercmd
host, port = self.makepasv()
File "/usr/lib/python2.4/ftplib.py", line 299, in makepasv
host, port = parse227(self.sendcmd('PASV'))
File "/usr/lib/python2.4/ftplib.py", line 241, in sendcmd
return self.getresp()
File "/usr/lib/python2.4/ftplib.py", line 207, in getresp
resp = self.getmultiline()
File "/usr/lib/python2.4/ftplib.py", line 193, in getmultiline
line = self.getline()
File "/usr/lib/python2.4/ftplib.py", line 180, in getline
line = self.file.readline()
File "/usr/lib/python2.4/socket.py", line 332, in readline
data = self._sock.recv(self._rbufsize)
socket.error: (104, 'Connection reset by peer')
__
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--
http://mail.python.org/mailman/listinfo/python-list
quoting strings
Hi, is any equivalent to perl "quotemeta" function in python? I know that I can use this on string: r'\\test' but I need to do this on the variable Thanks in advance Michal -- http://mail.python.org/mailman/listinfo/python-list
Local variables initialization
Hi! I'm building a class that most of methods have similar intro, something like this: def method(self): var_one = self.attr_one var_two = self.attr_two.another_attr empty_list = [] # significant code goes here # ... It's done for clarity reasons, aliasing most used variables to shorted names and initializing some other common variables. I'm wondering if I can generalize it in any way. So far I couldn't come up with any solution. It smells like macros (which won't be implemented in Python, I know, I know ;), but maybe there is a way? I would like to code it that way: @init_local_variables def method(self): # significant code goes here # ... If only such a decorator exists... Thanks for any hints. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Local variables initialization
Alex Martelli wrote: > Michal Kwiatkowski <[EMAIL PROTECTED]> wrote: >... >> def method(self): >> var_one = self.attr_one >> var_two = self.attr_two.another_attr >> empty_list = [] >> # significant code goes here >... > Personally, I would keep pushing back against this approach even after > I'd gone to the trouble of implementing it more solidly -- in no way is > clarity served by having magic local variables appear out of the blue > (or, rather, the black of black magic). However, whether something CAN > be done, and whether it SHOULD be done, are separate issues. I agree with your approach to local variables appearing out of nowhere, but still, it's a real pain to copy-and-paste few lines of this standard initialization to each new method I add. And then imagine another local variable will be needed and I have to change every single method... That's even more bug prone than magic-variables approach, if you ask me. The problem wouldn't be such a problem if Python had implicit self... but on the other side, it's another ambiguity. Well, maybe you can help me in refactoring this code, so that it won't be such a pain to easily modify groups of methods? I'm thinking about this: def init_arguments(fun): def new_f(self): var_one = self.attr_one var_two = self.attr_two.another_attr empty_list = [] fun(self, var_one, var_two, empty_list) return new_f @init_arguments def method(self, var_one, var_two, empty_list): # significant code goes here # ... But the bad thing about this approach is that actual method doesn't really look like its definition, because of different number of arguments. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Local variables initialization
Alex Martelli wrote: > But of course, then the method's body would have to use _.one rather > than var_one, _.two rather than var_two, and _.empty_list rather than > empty_list (what a strange name -- does it STAY empty throughout the > method's execution?!). To me this looks like a small price to pay, and empty_list name was chosen only for sake of an example. > the 'def method(_, self, ...)' signature a clear, strong indication that > something weird, but not TOO weird, is going on. YMMV... Thanks for your reply, I'll consider your suggestion. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Local variables initialization
Michal Kwiatkowski wrote: > def init_arguments(fun): > def new_f(self): > var_one = self.attr_one > var_two = self.attr_two.another_attr > empty_list = [] > > fun(self, var_one, var_two, empty_list) > > return new_f > > @init_arguments > def method(self, var_one, var_two, empty_list): > # significant code goes here > # ... > > But the bad thing about this approach is that actual method doesn't > really look like its definition, because of different number of arguments. I've come up with a solution that don't have this disadvantage. Here we go: def wrap_method(self, do): var_one = self.attr_one var_two = self.attr_two.another_attr empty_list = [] return do(self, var_one, var_two, empty_list) def method(self): def do(self, var_one, var_two, empty_list): # significant code goes here # ... return self.wrap_method(do) All is implicit and have an advantage of being clear in < 2.4, where there are no nice @ decorator syntax. Again, simple things proved to be better. :) mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Modernization of Emacs: terminology buffer and keybinding
Twisted <[EMAIL PROTECTED]> writes: > On Jun 20, 5:03 pm, Kaldrenon <[EMAIL PROTECTED]> wrote: >> I still have a good deal to learn, even of the basics, but I've toyed >> with it casually for a little bit (a total of two hours at most, but >> almost certainly less) and I already know enough that finding out how >> to do anything else IS trivial. It's not a program whose controls >> throw themselves at you, exactly, but with a touch of patience and a >> genuine interest in learning, it's not too bad. > > I don't know what software you're describing, but it's obviously not > emacs, unless there have been some HUGE changes to (at minimum) the > help and pane-navigation (er, excuse me, "window"-navigation) > controls... I don't know about *your* version of Emacs but in *my* version one can switch windows using mouse. I think that's pretty easy especially for beginners who are used to Windows. There was also a Help menu on menu bar but I disabled menu bar since keybindings are more convenient for me. -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +---ooO--(_)--Ooo-- -- http://mail.python.org/mailman/listinfo/python-list
problem with eval while using PythonCard
Hi its my first post. I have a problem, I want to user eval() function in a for loop to set labels to staticText so i done something like this: dzien=self.components.Calendar.GetDate().GetDay() for i in range(1,8): act=dzien+i -1 eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') but there is a problem, the program returned error: File "/home/lipton/Projekty/kami-organizer/grafik.py", line 27, in __init__ eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') File "", line 1 self.components.d1.text = "25" SyntaxError: invalid syntax dont know what is wrong, becouse when i copy and paste in to code : self.components.d1.text = "25" it work great, but when Im trying to do it using eval() it give me a SyntaxError Please help. ps. sorry for my english ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with eval while using PythonCard
now it's working just fine. but still I dont know why eval dont work ? and thx for help 25 May 2007 15:05:03 -0700, 7stud <[EMAIL PROTECTED]>: > Here's a complete example: > > ### > #create object 's': > > class S(object):pass > class X(object):pass > class Y(object):pass > > s = S() > s.components = X() > s.components.d1 = Y() > s.components.d2 = Y() > s.components.d3 = Y() > ## > > ## > set some initial values: > for i in range(1, 4): > obj = getattr(s.components, "d" + str(i)) > obj.text = "hello world" > # > > # > #change the values: > for i in range(1, 4): > getattr(s.components, "d" + str(i)).text = "goodybe" > # > > # > #print out the new values: > for i in range(1, 4): > print getattr(s.components, "d" + str(i)).text > # > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Pozdrawiam Michał Lipiński http://lipton.kom.pl -- http://mail.python.org/mailman/listinfo/python-list
Re: read part of jpeg file by pure python
On 13 Wrz, 10:48, Pacino <[EMAIL PROTECTED]> wrote: > On 9 13 , 4 43 , Laurent Pointal <[EMAIL PROTECTED]> wrote: > > > Pacino a écrit : > > > > Hi, everyone, > > > > I am wondering whether it's possible to read part (e.g. 1000*1000) of > > > a huge jpeg file (e.g. 3*3) and save it to another jpeg file > > > by pure python. I failed to read the whole file and split it, because > > > it would cost 2GB memory. > > > > Can anyone help me? Any comments would be appreciated. > > > > Just reading parts of the *file* is easy (see tell() seek() and read() > > methods on files). > > But to extract a part of the *picture*, you must uncompress the picture > > in memory, grab the sub-picture, and save it back - generally with > > compression. I can't see how you can bypass the uncompress/compress > > phases and the corresponding memory use. > > > The most difficult part is the uncompress part. I don't want the whole > picture to be uncompressed in the memory, because it will consume a > lot of memory (2GB, as I mentioned). My goal is to uncompress part of > the picture into the memory. > > I just read some article on this subject (http://mail.python.org/ > pipermail/image-sig/1999-April/000713.html) , but haven't test it out > yet. > I have no idea what it does. Anyway - jpeg: 1. RGB -> HLV 2. divide data into 8x8 - blocks of data. 3. blocks are treated with discrete cosine transform. 4. Result is filtered to remove "fast changes". 5. Then result is compressed with Huffman alg. So to get part of image - you can take smaller image before step 4. As far as I understand code presented at: http://mail.python.org/pipermail/image-sig/1999-April/000713.html - full image will be loaded, and cutted. -- Regards, Michał Zaborowski (TeXXaS) -- http://mail.python.org/mailman/listinfo/python-list
Tkinter / Tk 8.5
Today has been released a first beta of Tk 8.5, including a Ttk (tile) style engine, which makes possible the native look of widgets on MS platform, without having to install any extension. http://wiki.tcl.tk/11075 http://sourceforge.net/mailarchive/message.php?msg_name=1190813039.46fa5d6f6a06b%40webmail.nist.gov Is there a chance it will be included in 2.5.x, 2.6 or 3.0 ? -m. -- http://mail.python.org/mailman/listinfo/python-list
Re: ~ bit-wise unary operator
cau, maybe int is represented internally as a signed integer you can use numpy types: >>> import numpy >>> ~ numpy.uint16(7978) 57557 -m. On Thu, 27 Sep 2007 00:14:49 +0200, Ladislav Andel wrote: > Hello, > why ~ bit-wise unary operator returns -(x+1) and not bit inversion of > the given integer? > > example: > a = 7978 > a = ~a > python returns -7979 > > but I need to get back 57557 as in C language. > > which is also in binary > 000100101010 > and inverted > 111011010101 > > Is here any other operator or do I have to write it on my own? > > Thank you, > Lada -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter question
On Thu, 04 Oct 2007 20:16:14 -0700, goldtech wrote:
> This works OK. But I notice that if I enlarge the window after the
> script has run, the white listbox only gets "so" big while the grey
> background enlarges.
>
> Is there a way to have it all white when I enlarge a window - like
> what normally happens?
>
>
>
> from Tkinter import *
> root = Tk()
> root.title("Dirty Words")
> scrollbar = Scrollbar(root)
> scrollbar.pack(side=RIGHT, fill=Y)
> listbox = Listbox(root, bg='white', font = "Courier 16", width=60)
> listbox.pack()
> i='123456789abcdefghijklmnopqrstuvwxyz'
> for x in range(10):
> listbox.insert(END, i)
> listbox.config(yscrollcommand=scrollbar.set)
> scrollbar.config(command=listbox.yview)
> mainloop()
try to change listbox.pack() to listbox.pack(expand=True, fill=BOTH)
.. is it that you want ?
-mykhal
--
http://mail.python.org/mailman/listinfo/python-list
Re: Putting a line from a text file into a variable, then moving to next line
On Sun, 07 Oct 2007 12:00:44 +, Vernon Wenberg III wrote:
> I'm not really sure how readline() works. Is there a way to iterate
> through a file with multiple lines and then putting each line in a
> variable in a loop?
There are always more ways how to do it.. one of them is:
f = open(filename)
for line in f:
# you may then strip the newline:
line = line.strip('\n')
# do anything you want with the line
f.close()
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pil image module, "mode" bug..
On Sun, 07 Oct 2007 06:03:06 -0700, Abandoned wrote:
> Hi..
> I find the picture color with:
> im=Image.open("/%s" %name)
> color=im.mode #p=black & beyaz rgb=color L=grey
>
> This usually work true but in these pictures:
> http://malatya.meb.gov.tr/images/alt/ilsis_logo.gif
> http://malatya.meb.gov.tr/images/meb.gif
>
> Say me P (black&white) but these pictures are color..
>
> What is the reason of this ?
>
> I'm sorry my bad english
P does mean palette, black&white is a special case of palette, with two
colors.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pil image module, "mode" bug..
On Sun, 07 Oct 2007 09:02:09 -0700, Abandoned wrote:
> On Oct 7, 4:47 pm, Michal Bozon <[EMAIL PROTECTED]> wrote:
>> On Sun, 07 Oct 2007 06:03:06 -0700, Abandoned wrote:
>> > Hi..
>> > I find the picture color with:
>> > im=Image.open("/%s" %name)
>> > color=im.mode #p=black & beyaz rgb=color L=grey
>>
>> > This usually work true but in these pictures:
>> >http://malatya.meb.gov.tr/images/alt/ilsis_logo.gif
>> >http://malatya.meb.gov.tr/images/meb.gif
>>
>> > Say me P (black&white) but these pictures are color..
>>
>> > What is the reason of this ?
>>
>> > I'm sorry my bad english
>>
>> P does mean palette, black&white is a special case of palette, with two
>> colors.
>
> How can i understand the picture color ? (black &white or color or
> grey)
If you know how to work with RGB images, you can convert the image
from the palette mode easily:
img2 = img.convert(mode='RGB')
Anyway, always is helpful to read the tutorial or basic documentation.. ;)
MB
--
http://mail.python.org/mailman/listinfo/python-list
Re: struct.unpack less than 1 byte
You are able to read single bits from file in C ? You'll have to read the bytes and than perform some bitwise operations on them to extract the bits > hello all, > > i need to read from a file a struct like this [1byte, 12bits, 12bits] > reading 1 byte or more is not a problem ... but the 12 bits values > are ... > > thanks -- http://mail.python.org/mailman/listinfo/python-list
[0..9] list (range) syntax
many Python newcomers are confused why range(10), does not include 10. If there was a proposal for the new syntax for ranges, which is known e.g. from Pascal or Ruby... >>> [0..10] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ...is there a chance to be approved ? We have had a short discussion on it at the irc, I hope that the fact that nobody agreed it is a good idea was just an accident :) -m. PS: to dream further.. >>> (0..10) or >>> (0..10) (0..10) or >>> (0..10) range(0, 11) -- http://mail.python.org/mailman/listinfo/python-list
Re: [0..9] list (range) syntax
On Thu, 25 Oct 2007 01:16:57 +0200, Wildemar Wildenburger wrote: > Michal Bozon wrote: >> many Python newcomers are confused why >> range(10), does not include 10. >> > It produces a list of ten elements. Also the documentation is quite > clear on the topic. And lastly: This will probably really bother you for > a week, then no more. > > >> If there was a proposal for the new >> syntax for ranges, which is known >> e.g. from Pascal or Ruby... >> >>>>> [0..10] >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> >> ...is there a chance to be approved ? >> > Quite close to none. It is too much cosmetics and too little > enhancement. What would that make possible that is impossible by now or > what would that make easier that is really hard? > > And if you really need something like that, write a function: > > def fullrange(start, end): > r = range(start, end) > r.append(end) > return r > > /W This is something completely different. The .. syntax was not meant only as something which would include the last item, but also/rather a range list syntactic shortcut: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] --> [0, 1, ... 9, 10] --> [0..10] -m. -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all not allowed characters..
On Thu, 25 Oct 2007 07:52:36 -0700, Abandoned wrote: > Hi.. > I want to delete all now allowed characters in my text. > I use this function: > > def clear(s1=""): > if s1: > allowed = > [u'+',u'0',u'1',u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9',u' ', u'Ş', > u'ş', u'Ö', u'ö', u'Ü', u'ü', u'Ç', u'ç', u'İ', u'ı', u'Ğ', u'ğ', 'A', > 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'K', 'J', 'M', 'L', 'O', 'N', > 'Q', 'P', 'S', 'R', 'U', 'T', 'W', 'V', 'Y', 'X', 'Z', 'a', 'c', 'b', > 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'm', 'l', 'o', 'n', 'q', 'p', > 's', 'r', 'u', 't', 'w', 'v', 'y', 'x', 'z'] > s1 = "".join(ch for ch in s1 if ch in allowed) > return s1 > > And my problem this function replace the character to "" but i > want to " " > for example: > input: Exam%^^ple > output: Exam ple > I want to this output but in my code output "Example" > How can i do quickly because the text is very long.. the list comprehension does not allow "else", but it can be used in a similar form: s2 = "" for ch in s1: s2 += ch if ch in allowed else " " (maybe this could be written more nicely) -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all not allowed characters..
>
>> the list comprehension does not allow "else", but it can be used in a
>> similar form:
>>
( I was wrong, as Tim Chase have shown )
>> s2 = ""
>> for ch in s1:
>> s2 += ch if ch in allowed else " "
>>
>> (maybe this could be written more nicely)
>
> Repeatedly adding strings together in this way is about the most
> inefficient, slow way of building up a long string. (Although I'm sure
> somebody can come up with a worse way if they try hard enough.)
>
> Even though recent versions of CPython have a local optimization that
> improves the performance hit of string concatenation somewhat, it is
> better to use ''.join() rather than add many strings together:
>
String appending is not tragically slower,
for strings long tens of MB, the speed
makes me a difference in few tens of percents,
so it is not several times slower, or so
> s2 = []
> for ch in s1:
> s2.append(ch if (ch in allowed) else " ")
> s2 = ''.join(s2)
>
> Although even that doesn't come close to the efficiency and speed of
> string.translate() and string.maketrans(). Try to find a way to use them.
>
> Here is one way, for ASCII characters.
>
> allowed = "abcdef"
> all = string.maketrans('', '')
> not_allowed = ''.join(c for c in all if c not in allowed)
> table = string.maketrans(not_allowed, ' '*len(not_allowed))
> new_string = string.translate(old_string, table)
Nice, I did not know that string translation exists, but
Abandoned have defined allowed characters, so making
a translation table for the unallowed characters,
which would take nearly complete unicode character table
would be inefficient.
--
http://mail.python.org/mailman/listinfo/python-list
Performance of Python 2.3 and 2.4
Hi!
I was just wondering...
Python 2.3.5 (#2, Mar 6 2006, 10:12:24)
[GCC 4.0.3 20060304 (prerelease) (Debian 4.0.2-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> a = timeit.Timer('2**1')
>>> b = timeit.Timer('112233445566778899 * 112233445566778899')
>>> a.timeit(1)
5.3986599445343018
>>> b.timeit()
0.59309601783752441
Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
[GCC 4.0.3 2005 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> a = timeit.Timer('2**1')
>>> b = timeit.Timer('112233445566778899 * 112233445566778899')
>>> a.timeit(1)
13.129707813262939
>>> b.timeit()
0.72854804992675781
Why long numbers operations are slower in 2.4?
mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
--
http://mail.python.org/mailman/listinfo/python-list
Re: calling upper() on a string, not working?
John Salerno wrote: > def encrypt_quote(original): > original_letters = filter_letters(original) You call filter_letters() which makes upper() on all letters, so original_letters contain only uppercase letters. > new_letters = list(string.ascii_uppercase) > while True: > random.shuffle(new_letters) > trans_letters = ''.join(new_letters)[:len(original_letters)] > if test_code(original_letters, trans_letters): > trans_table = string.maketrans(original_letters, > trans_letters) break > return original.translate(trans_table) And here you're translating 'original' (which contains a lot of lowercase letters) with use of trans_table that maps only uppercase characters. This return should be: return original.upper().translate(trans_table) mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
File names, character sets and Unicode
Hi all,
is there any way to determine what's the charset of filenames returned
by os.walk()?
The trouble is, if I pass argument to os.walk() I get the
filenames as byte-strings. Possibly UTF-8 encoded Unicode, who knows.
OTOH If I pass to os.walk() all the filenames I get in
the loop are already unicode()d.
However with some locales settings os.walk() dies with for example:
Traceback (most recent call last):
File "tst.py", line 10, in
for root, dirs, files in filelist:
File "/usr/lib/python2.5/os.py", line 303, in walk
for x in walk(path, topdown, onerror):
File "/usr/lib/python2.5/os.py", line 293, in walk
if isdir(join(top, name)):
File "/usr/lib/python2.5/posixpath.py", line 65, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1:
ordinal not in range(128)
I can't even skip over these files with 'os.walk(..., onerror=handler)'
the handler() is never called.
That happens for instance when the file names have some non-ascii
characters and locales are set to ascii, but reportedly in some other
cases as well.
What's the right and safe way to walk the filesystem and get some
meaningful filenames?
Related question - if the directory is given name on a command line
what's the right way to preprocess the argument before passing it down
to os.walk()?
For instance with LANG=en_NZ.UTF-8 (i.e. UTF-8 system):
* directory is called 'smile☺'
* sys.argv[1] will be 'smile\xe2\x98\xba' (type str)
* after .decode("utf-8") I get u'smile\u263a' (type unicode)
But how should I decode() it when running on a system where $LANG
doesn't end with "UTF-8"? Apparently some locales have non-ascii default
charsets. For instance zh_TW is BIG5 charset by default, ru_RU is
ISO-8850-5, etc. How do I detect that to get the right charset for decode()?
I tend to have everything internally in Unicode but it's often unclear
how to convert some inputs to Unicode in the first place. What are the
best practices for dealing with these chraset issues in Python?
Thanks!
Michal
--
* Amazon S3 backup tool -- http://s3tools.logix.cz/s3cmd
--
http://mail.python.org/mailman/listinfo/python-list
setup.py installs modules to a wrong place
Hi, I have recently seen some reports from users of my s3cmd script [1] who installed the package using the provided distutils-based setup.py and immediately after installation the script failed to run because it couldn't find its modules. Here's an example session from Mac OS X, but similar behaviour has been observed on Ubuntu as well. Needless to say it works for me just fine so I can't easily debug and fix it :-( First is the reported setup.py output: - ~/bin/s3cmd $ sudo python setup.py install Password: ... running install_lib creating /usr/lib/python2.5/site-packages creating /usr/lib/python2.5/site-packages/S3 copying build/lib/S3/PkgInfo.py -> /usr/lib/python2.5/site-packages/S3 ... more modules, etc ... - It decided to put the modules to /usr/lib/python2.5/site-packages/S3 Now, s3cmd should import from there, but fails: ~/bin/s3cmd $ s3cmd Traceback (most recent call last): File "/usr/bin/s3cmd", line 1207, in from S3 import PkgInfo ImportError: No module named S3 sys.path at the time the script died had these entries: /usr/bin /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5 /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.5/...other_subdirs... /Library/Python/2.5/site-packages There is nothing special in setup.py. After all have a look yourself: http://s3tools.svn.sourceforge.net/viewvc/s3tools/s3cmd/trunk/setup.py?view=markup What could be the reason for distutils / setup.py to install the modules to a directory that's not in sys.path? Can I detect it in setup.py and prevent or workaround it somehow? Thanks! [1] http://s3tools.logix.cz/s3cmd -- Amazon S3 command line client Michal -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py installs modules to a wrong place
Ping! Does anyone know what the problem could be? The bug reported provided some more information recently... The modules have been installed to /usr/lib/python2.5/site-packages as show below. Apparently on his system /usr/lib/python2.5 is a symlink to /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5 While this directory *is* in sys.path, the .../site-packages one isn't, see below in my original post. So why did Python decide to put the modules there? Why not one of the directories that are in sys.path? Is this configurable somewhere? Thanks! Michal Michal Ludvig wrote: > Hi, > > I have recently seen some reports from users of my s3cmd script [1] who > installed the package using the provided distutils-based setup.py and > immediately after installation the script failed to run because it > couldn't find its modules. > > Here's an example session from Mac OS X, but similar behaviour has been > observed on Ubuntu as well. Needless to say it works for me just fine so > I can't easily debug and fix it :-( > > First is the reported setup.py output: > > - > ~/bin/s3cmd $ sudo python setup.py install > Password: > ... > running install_lib > creating /usr/lib/python2.5/site-packages > creating /usr/lib/python2.5/site-packages/S3 > copying build/lib/S3/PkgInfo.py -> /usr/lib/python2.5/site-packages/S3 > ... more modules, etc ... > - > > It decided to put the modules to /usr/lib/python2.5/site-packages/S3 > > Now, s3cmd should import from there, but fails: > > ~/bin/s3cmd $ s3cmd > Traceback (most recent call last): >File "/usr/bin/s3cmd", line 1207, in > from S3 import PkgInfo > ImportError: No module named S3 > > sys.path at the time the script died had these entries: > /usr/bin > /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip > /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5 > /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin > /System/Library/Frameworks/Python.framework/Versions/2.5/...other_subdirs... > /Library/Python/2.5/site-packages > > There is nothing special in setup.py. After all have a look yourself: > http://s3tools.svn.sourceforge.net/viewvc/s3tools/s3cmd/trunk/setup.py?view=markup > > What could be the reason for distutils / setup.py to install the modules > to a directory that's not in sys.path? Can I detect it in setup.py and > prevent or workaround it somehow? > > Thanks! > > [1] http://s3tools.logix.cz/s3cmd -- Amazon S3 command line client > > Michal > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Unicode encoding - ignoring errors
Hi, in my script I have sys.stdout and sys.stderr redefined to output unicode strings in the current system encoding: encoding = locale.getpreferredencoding() sys.stdout = codecs.getwriter(encoding)(sys.stdout) However on some systems the locale doesn't let all the unicode chars be displayed and I eventually end up with UnicodeEncodeError exception. I know I could explicitly "sanitize" all output with: whatever.encode(encoding, "replace") but it's quite inconvenient. I'd much prefer to embed this "replace" operation into the sys.stdout writer. Is there any way to set a conversion error handler in codecs.getwriter() or perhaps chain it with some other filter somehow? I prefer to have questionmarks in the output instead of experiencing crashes with UnicodeEncodeErrors ;-) Thanks! Michal -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py installs modules to a wrong place
Hi Omer, > I'm seeing this on fc8 with a custom built python2.6. Not happening > with any other packages (e.g. boto). Workaround of course was just to > copy the S3 dir to /usr/local/lib/python2.6/site-packages. I've found it. The culprit was a pre-set install prefix in setup.cfg - I can't remember why I put it there ages ago. Anyway, now it's removed ;-) > I poked around a bit but nothing obvious jumped out. Happy to do any > debugging if you have tests you'd like to me to run. Thanks for the offer. I believe the current SVN trunk [1] of s3cmd should install just fine everywhere. Michal [1] .. http://s3tools.logix.cz/download -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode encoding - ignoring errors
Chris Rebert wrote: > On Mon, Dec 29, 2008 at 4:06 AM, Michal Ludvig wrote: >> Hi, >> >> in my script I have sys.stdout and sys.stderr redefined to output >> unicode strings in the current system encoding: >> >>encoding = locale.getpreferredencoding() >>sys.stdout = codecs.getwriter(encoding)(sys.stdout) >> >> However on some systems the locale doesn't let all the unicode chars be >> displayed and I eventually end up with UnicodeEncodeError exception. >> >> I know I could explicitly "sanitize" all output with: >> >>whatever.encode(encoding, "replace") >> >> but it's quite inconvenient. I'd much prefer to embed this "replace" >> operation into the sys.stdout writer. >> [...] > codecs.getwriter() returns a StreamWriter subclass (basically). > The constructor of said subclass has the signature: > StreamWriter(stream[, errors]) > You want the 'errors' argument. Thanks! (and I'm going to read the module docs, really ;-) Michal -- http://mail.python.org/mailman/listinfo/python-list
Re: NEWB: dividing numbers
Lo pisze: I just tried python first time. 2/3 the result is zero Float type must be specified explicitly: 2/3.0 or 2.0/3 In Python 3.x operators behave differently and '2/3' would give float number as a result. I want the result to be .333... Than try: 1/3.0 because 2/3.0 will never show you that ;) Michal -- http://mail.python.org/mailman/listinfo/python-list
Ending data exchange through multiprocessing pipe
Hi, I am adding support for parallel processing to an existing program which fetches some data and then performs some computation with results saved to a database. Everything went just fine until I wanted to gather all of the results from the subprocesses. First idea, which came to my mind, was using a queue. I've got many producers (all of the workers) and one consumer. Seams quite simple, but it isn't, at least for me. I presumed that each worker will put() its results to the queue, and finally will close() it, while the parent process will get() them as long as there is an active subprocess. So I did this: >>> from multiprocessing import Process, Queue, active_children >>> >>> def f(q): ... q.put(1) ... q.close() ... >>> queue = Queue() >>> Process(target=f, args=(queue,)).start() >>> while len(active_children()) > 0: ... print queue.get() ... 1 This (of course?) hangs after first iteration of the loop. Delaying second iteration by putting a sleep() call fixes the problem, since the result of active_children is being some kind of refreshed, but it's not the solution. One could say to iterate the exact number of subprocesses I have, but let's presume such information isn't available. Due to my failure with queues I decided to have a try with pipes, and again I found a behavior, which is at least surprising and not obvious. When I use a pipe within a process everything works perfectly: >>> from multiprocessing import Pipe >>> parent, child = Pipe() >>> child.send(1) >>> child.close() >>> parent.recv() 1 >>> child.closed True >>> parent.recv() Traceback (most recent call last): File "", line 1, in EOFError The problems appear in subprocess communication using pipes, though. >>> from multiprocessing import Process, Pipe >>> def f(child): ... child.send(1) ... child.close() ... >>> parent, child = Pipe() >>> Process(target=f, args=(child,)).start() >>> parent.recv() 1 >>> child.closed False >>> parent.recv() ... and hangs. No idea of fixing this, not even of a workaround, which would solve my problem. Most possibly I'm missing something in philosophy of multiprocessing, but I couldn't find anything covering such a situation. I'd appreciate any kind of hint on this topic, as it became a riddle I just have to solve. :-) Best regards, Michal Chruszcz -- http://mail.python.org/mailman/listinfo/python-list
Re: Ending data exchange through multiprocessing pipe
On Apr 22, 6:33 pm, MRAB wrote: > You could do this: > > from multiprocessing.queues import Empty > > queue = Queue() > Process(target=f, args=(queue,)).start() > while active_children(): > try: > print queue.get(timeout=1) > except Empty: > pass This one isn't generic. When I have tasks that all finish within 0.1 seconds the above gives 10x overhead. On the other hand, if I know the results will be available after 10 hours there's no use in checking every second. Best regards, Michal Chruszcz -- http://mail.python.org/mailman/listinfo/python-list
Re: Ending data exchange through multiprocessing pipe
On Apr 22, 10:30 pm, Scott David Daniels wrote: > Michal Chruszcz wrote: > > ... First idea, which came to my mind, was using a queue. I've got many > > producers (all of the workers) and one consumer. Seams quite simple, > > but it isn't, at least for me. I presumed that each worker will put() > > its results to the queue, and finally will close() it, while the > > parent process will get() them as long as there is an active > > subprocess > > Well, if the protocol for a worker is: > : > > queue.put(result) > queue.put() > queue.close() > > Then you can keep count of how many have finished in the consumer. Yes, I could, but I don't like the idea of using a sentinel, if I still need to close the queue. I mean, if I mark queue closed or close a connection through a pipe why do I still have to "mark" it closed using a sentinel? From my point of view it's a duplication. Thus I dare to say multiprocessing module misses something quite important. Probably it is possible to retain a pipe state using a multiprocessing manager, thus omitting the state exchange duplication, but I haven't tried it yet. Best regards, Michal Chruszcz -- http://mail.python.org/mailman/listinfo/python-list
sys.settrace 'call' event behavior
I'm building a tool to trace all function calls using sys.settrace
function from the standard library. One of the awkward behaviors of
this facility is that the class definitions are reported as 'call'
events.[1] Since I don't want to catch class definitions, only
function calls, I'm looking for a way to differentiate between those
two. So far I have only vague clues about how to do that.
At the bottom of this mail is a simple script that prints all
attributes (except for the bytecode) of the traced code. In the sample
code Bar class is defined and foo function is called after that. The
following trace output is reported:
Bar, 0, 0, (), (), (), (None,), ('__name__', '__module__', 'None'),
foo.py, 21, , 1, 66
foo, 0, 0, (), (), (), (None,), (), foo.py, 25, , 1, 67
Class definition and function call differs on four attributes. Two of
them, co_name and co_firstlineno are not very helpful. Other two are
co_names and co_flags. The latter differs only by the CO_OPTIMIZED
flag, which is for "internal use only"[2]. So we're left with
co_names, which "is a tuple containing the names used by the
bytecode". Is that helpful in distinguishing between class definitions
and function calls? Do you have any other ideas on how to tell them
apart?
Source of the sample script I used follows.
def trace(frame, event, arg):
if event == 'call':
print ', '.join(map(str, [frame.f_code.co_name,
frame.f_code.co_argcount,
frame.f_code.co_nlocals,
frame.f_code.co_varnames,
frame.f_code.co_cellvars,
frame.f_code.co_freevars,
frame.f_code.co_consts,
frame.f_code.co_names,
frame.f_code.co_filename,
frame.f_code.co_firstlineno,
frame.f_code.co_lnotab,
frame.f_code.co_stacksize,
frame.f_code.co_flags]))
return trace
import sys
sys.settrace(trace)
class Bar(object):
None
pass
def foo():
pass
foo()
[1] It is strange for me, but documented properly.
http://docs.python.org/lib/debugger-hooks.html says that call event
happens when "a function is called (or some other code block
entered)."
[2] http://docs.python.org/ref/types.html#l2h-145
Cheers,
mk
--
http://mail.python.org/mailman/listinfo/python-list
Python AST preserving whitespace and comments
Hi, I'm working on Pythoscope[1], a unit test generator for Python and stumbled into the following problem. I need a way to analyze and modify Python AST tree, but without loosing source code formatting and comments. Standard library ast module discards those, so I started looking for other solutions. I found a library used by the 2to3 script[2], which does different kinds of source code refactorings and preserves the formatting of the original file. Sadly AST generated by this library is not compatible with the standard one, so I can't use a nice interface of compiler.visitor[3]. It isn't also as well documented. I kind of liked the clean and descriptive list of standard AST nodes[4]. So here are my questions to the list. Is there any library built on top of lib2to3 which makes traversing it easier? The pattern matcher is nice, but not always feasible for the higher level view of the source code. Are there any tutorials/documentation about usage of lib2to3 which I'm not aware of? I basically read through HACKING, README and a lot of source code, but would appreciate some more examples and insights. Is the lib2to3 library meant to be used outside of the 2to3 script or rather not? Are there any projects that will incorporate its features (enriched AST in particular) in a more "official" library-like package? Will lib2to3 be put on PyPI anytime? [1] http://pythoscope.org/ [2] http://svn.python.org/view/sandbox/trunk/2to3/ [3] http://docs.python.org/lib/module-compiler.visitor.html [4] http://docs.python.org/lib/module-compiler.ast.html Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Why property works only for objects?
Hi, Code below shows that property() works only if you use it within a class. class A(object): pass a = A() a.y = 7 def method_get(self): return self.y a.x = property(method_get) print a.x # => A.x = property(method_get) print a.x # => 7 Is there any method of making descriptors on per-object basis? I would like to customize x access in different objects of class A. Is this possible and how? mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Steven Bethard napisał(a):
>> Is there any method of making descriptors on per-object basis?
>
> I'm still not convinced that you actually want to, but you can write
> your own descriptor to dispatch to the instance object (instead of the
> type):
Ok, this works for attributes I know a name of at class definition. What
about other situations? I could possibly want to have different sets of
attributes for instances of a class. Is it still possible to do? I don't
also like current solution, as it wraps things around, creating another
attribute ('_x') in the process. Aren't there any cleaner solutions?
The problem is I have an instance of a given class (say BaseClass) and I
want it to implement some attribute accesses as method calls. I'm not a
creator of this object, so changing definition of BaseClass or
subclassing it is not an option. Code below doesn't work, but shows my
intention:
# obj is instance of BaseClass
def get_x(self):
# ...
def set_x(self, value):
# ...
obj.x = property(get_x, set_x)
Changing __setattr__/__getattr__ of an object also doesn't work for the
same reason: dictionary lookup is made only in class attributes,
ommiting object attributes. It's confusing, because writting obj.x and
obj.__getattribute__('x') gives a different results. There also seems
not to be any clear way to define methods for objects. Something like:
class C(object):
pass
def method(self):
return self.x
c = c.x
c.method = method
c.method() # => AttributeError
So another question arise. Is it possible to make function a method (so
it will receive calling object as first argument)?
mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a): > Wrong! Of _course_ it's an option -- why do you think it matters at all > whether you're the creator of this object?! Statically typed languages background. Sorry. ;) >> Code below doesn't work, but shows my >> intention: >> >> # obj is instance of BaseClass >> def get_x(self): >> # ... >> def set_x(self, value): >> # ... >> obj.x = property(get_x, set_x) > > def insert_property(obj, name, getter, setter): > class sub(obj.__class__): pass > setattr(sub, name, property(getter, setter)) > obj.__class__ = sub > > See? Of COURSE you can subclass -- not hard at all, really. Let me understand it clearly. If I change __class__ of an object, existing attributes (so methods as well) of an object are still accessible the same way and don't change its values. Only resolution of attributes/methods not found in object is changed, as it uses new version of __class__ to lookup names. Is this right? mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a): >> So another question arise. Is it possible to make function a method (so >> it will receive calling object as first argument)? > > Sure, impor types then call types.MethodType: > > f = types.MethodType(f, obj, someclass) > > (f.__get__ is also fine for Python-coded functions) -- make sure that > someclass is obj's class or some ancestor of it, of course. I wasn't aware of types module. Thanks for your reply. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why python doesn't use syntax like function(,,x) for default parameters?
Dmitry Anikin napisał(a):
> Some example (from real life).
> def ChooseItems(StartDate, EndDate, Filter):
> #function returns a set of some items in chronological order
> #from required interval possibly using filter
>
> ChooseItems() #get everything
> ChooseItems('01.01.2000', ,SomeFilter) #get everything after a date
> using filter
> ChooseItems(, '01.01.2000') #get everything before a date
> ChooseItems(, , SomeFilter) #get everything using filter
>
> Now compare this to something which (I hope) is rather pythonian
>
> Seq[:] #get everything
> Seq[2::3] #get everything after an index using filter (filter every
> third value)
> Seq[:3] #get everythin before an index
> Seq[::4] #get everything using a filter
>
> Do you see any significant difference?
You're not comparing what you should. Range has only 3 parameters and is
a standard part of a language. Even if it's not obvious for someone
which parameters mean what in different combinations, it's not that hard
to look up in a manual. But user-defined functions are allowed to have
any number of arguments, with any possible meaning. That means it's
impossible to learn to recognize arguments exclusively by position
(which can be done for range), you have to look up function definition
*each time*.
And please remember that when writing a function, you define defaults as
values that user will mostly consider as appropriate. So if he doesn't
define them, that means he doesn't care. And Python syntax shows exactly
this intention. Your proposed syntax doesn't, as it suggest something
that user should know about is going on.
Now look at your example rewritten with standard Python keyword syntax.
If you know nothing about ChooseItems function, which version in your
opinion is more informative?
# get everything
ChooseItems()
# get everything after a date using filter
ChooseItems(after='01.01.2000', filter_with=SomeFilter)
# get everything before a date
ChooseItems(before='01.01.2000')
# get everything using filter
ChooseItems(filter_with=SomeFilter)
> I understand that many do not need such a syntax, I don't understand
> why someone would be AGAINST it. I don't propose to CHANGE anything
> in python (right now this syntax is error anyway). What I propose is just
> ADD another way of calling a function with keyword parameters but using
> POSITIONS instead of NAMES. And sometimes position is easier to
> remember than name. Anyway, who wants names let them use names.
> Who wants positions let them use positions. But to have a CHOICE is
> always good. As far as the choice itself doesn't damage anything,
> and I don't think that my does.
With this attitude Python will end up being Perl. Current semantics of
calling functions are already good enough to write clean and
understandable code.
> I think that if we compare
> ChooseItems('01.01.2000', ,SomeFilter)
> and
> ChooseItems(StartDate='01.01.2000', Filter=SomeFilter)
> the first one is more readable, 'cos you see
> what is meant right away. In second one you have to
> actually READ the keyword names to understand.
> It's not the common case, of course, but still, why
> not have a choice to use it?
I still think reading is better than guessing. :) Choosing good names
for arguments is another important factor (as shown in last example) in
readability.
> Some other examples which might benefit
> SetDate(year, month, day)
> SetDate(, month+1) # set next month, leaving year and day
> SetDate(, , 31) # set to end of month, not changing year
> #(wrong date adjusted automatically, of course)
In Poland we usually write dates in day-month-year notation. Having
function calls like this:
SetDate(year=y, month=m, day=d)
SetDate(month=m+1)
SetDate(day=31)
will be understandable by anyone.
> Please, don't try to scare me with 25-parameter functions.
> This is not for them. But to remember positions of two to
> five parameters is actually easier (if their order has some
> logic) then what are their names: startDate ? beginDate?
> firstDate? openDate? Date1?
I must disagree. If you choose argument names wisely you won't have any
trouble remembering which is which.
> The same approach can be used with tuples:
> (, , z) = func() # returning three element tuple()
> You think
> z = func()[2]
> is actually more clear? - By the way, I want THIRD value,
> not SECOND. And tuples don't have keyword names, do they?
It's not cleaner. It's error-prone, as you may lost one comma somewhere.
You also have to literally count what is the index of returned tuple
value that will be binded to "z".
> Finally, if syntax
> func (None, None, 10)
> seems natural to you, I propose to make it even more
> natural: I don't want some "None" passed as argument,
> I don't want anything at all passed, so I just use empty space
> func ( , , 10)
> And the called func don't have to bother with checking
> None for EACH argument but will happily use defaults instead.
If you would write it as func(third=10) it would be clearer that
func(, , 10) and call
Re: Why property works only for objects?
Bruno Desthuilliers napisał(a): >> Let me understand it clearly. If I change __class__ of an object, >> existing attributes (so methods as well) of an object are still >> accessible the same way and don't change its values. Only resolution of >> attributes/methods not found in object is changed, as it uses new >> version of __class__ to lookup names. Is this right? > > Attributes, yes. Not methods. Methods are looked up in the class. My experience shows exactly the opposite. Any attribute/method you try to access is first looked up in object dictionary, then inside class definition. import types class C(object): def f(self): print "old method f()" obj = C() def f(self): print "new method f()" obj.f = types.MethodType(f, C) obj.f() # => "new method f()" Since that works, intuitively for me would be to assign object's descriptors like that: obj.x = property(types.MethodType(lambda self: 42, C)) But I just get a property object. So, it seems descriptors have little bit of magic, as they don't work identically for classes and objects. The same goes for special methods and attributes (written as __*__). So I cannot change __getattr__/__setattr__/__metaclass__ or any other attribute that starts with __ for a single object. It's not so bad except for situations were class of an object defines its own __getattribute__ method, which takes control of an object from us. To get/set any attribute of an object we must use object type methods: class C(object): def __getattribute__(self, name): return 42 obj = C() obj.a = 5 print obj.a # => 42 print object.__getattribute__(obj, 'a') # => 5 I gets even more strange when you try to modify, say __len__ or __repr__. Docstring for object.__repr__ says: "x.__repr__() <==> repr(x)" which doesn't seem to be always true: class C(object): def __repr__(self): return "class repr" obj = C() obj.__repr__ = types.MethodType(lambda self: "instance repr", C) print repr(obj) # => class repr print obj.__repr__() # => instance repr Maybe the manual should say "x.__class__.__repr__() <==> repr(x)" instead? I'm trying to understand attributes lookups made by Python, having properties and special methods in mind. So far I've come up with kind of reasoning I've coded below. I would appreciate any comments and suggestions. def lookup_name(obj, name): get = lambda obj, name: object.__getattribute__(obj, name) has = lambda obj, name: name in get(obj, '__dict__') # assume C is a new style class C = get(obj, '__class__') # 1) use class' __getattribute__ method try: if has(C, '__getattribute__'): return get(C, '__getattribute__')(obj, name) except AttributeError: pass # 2) lookup in object's dictionary try: if has(obj, name): return get(obj, name) except AttributeError: pass # 3) lookup in classes for c in obj.__class__.mro(): try: if has(c, name): desc = get(c, name) # 3a) handle descriptors try: return get(desc, '__get__')(obj) except: pass # 3b) no descriptors -> use value return desc except AttributeError: pass raise AttributeError, "Not found!" mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a):
> First, let's forget legacy-style classes, existing only for backwards
> compatibility, and focus on new-style ones exclusively -- never use
> legacy classes if you can avoid that.
Ok, let's cover only new-style classes in our discussion.
I've read your comments and am on a way of reading your articles. Still,
with my current knowledge I'm trying to write pure python attributes
lookup function. I've failed for example given below:
class C(object):
__dict__ = {}
obj = C()
obj.a = 7
obj.__dict__ = {}
print object.__getattribute__(obj, '__dict__')
print object.__getattribute__(C, '__dict__')
print obj.a # => 7 !!!
First print returns "{}" and the second returns
{'__dict__': {},
'__module__': '__main__',
'__weakref__': ,
'__doc__': None}
Neither of them have "a" attribute. How come obj.a doesn't raise an
exception? Where obj.a is kept?
mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a):
obj.__dict__
> {}
>
> ...the presence of '__dict__' as an entry in C is confusing the issue,
> because that's what you get in this case as obj.__dict__.
It still bugs me. What's the actual procedure when doing attribute
assignment? I understand it like this:
obj.attr = value
* if instance class has __setattr__, call it
* else: if class has an attribute with name "attr" check if it's a
descriptor; when it's overriding descriptor, call its __set__
method, otherwise raise AttributeError
* else: bind "attr" as object attribute
I've initialized C.__dict__ as plain dictionary, so it's not a
descriptor. C also doesn't have __setattr__ method. I'm probably wrong,
as __dict__ is considered somewhat special, but I can't really
understand to which degree. Any other attribute I try to bind to object
will be stored inside object dictionary, without looking up class
dictionary. __dict__ is special, because it's *the* dictionary that
stores all references to object's attributes. So, when assigning to
__dict__ I would expect old dictionary to be simply replaced with this
one. I don't understand why class attributes has any factor in this.
> C.__dict__ gives you a dictproxy, not a real dict, by the way:
>
obj.__dict__ is C.__dict__['__dict__']
> True
Why is that? I've initialized it as dictionary. When the type was
changed and why? Is this used anywhere?
> The funny thing is that builtins like var, which should know better,
> also get fooled...:
>
vars(obj)
> {}
vars(obj) is C.__dict__['__dict__']
> True
Docstring for vars says:
With an argument, equivalent to object.__dict__.
So this behavior simply conforms to the doc.
> I think a fair case can be made that you've found a bug in Python here:
> the existence of that __dict__ in C's class body is clearly causing
> unintended anomalies. Fortunately, getattr and friends don't in fact
> get confused, but vars does, as does assignment to obj.__dict__...
What solution do you propose?
mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It's easier to get forgiveness for being wrong
o o o than forgiveness for being right.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Shalabh Chaturvedi napisał(a): > Here is a step-by-step description of what happens when you set or get > an attribute on an object: > > http://cafepy.com/article/python_attributes_and_methods/ch01s05.html This description doesn't take __getattr__/__getattribute__/__setattr__ in count. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a): >> It still bugs me. What's the actual procedure when doing attribute >> assignment? I understand it like this: >> >> obj.attr = value >> * if instance class has __setattr__, call it >>* else: if class has an attribute with name "attr" check if it's a >> descriptor; when it's overriding descriptor, call its __set__ >> method, otherwise raise AttributeError >> * else: bind "attr" as object attribute > > Yes, this is correct. Can you also check my reasoning for getting attributes? value = obj.attr * if instance class has __getattribute__, call it * else: lookup "attr" in all parent classes using class __mro__; if it's a descriptor call its __get__ method, return its value otherwise (when descriptor doesn't have __get__, it's unreadable and AttributeError is raised) * else: check instance __dict__ for "attr", return it when found * else: lookup __getattr__ in instance class and call it when found * else: raise AttributeError > Opening a bug report on the Python bugtracker would maximize the > likelihood that something gets done about this bug. Bug submitted: http://sourceforge.net/tracker/index.php?func=detail&aid=1448042&group_id=5470&atid=105470 mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a): >> Can you also check my reasoning for getting attributes? >> >> value = obj.attr >> * if instance class has __getattribute__, call it >> * else: lookup "attr" in all parent classes using class __mro__; >> if it's a descriptor call its __get__ method, return its value >> otherwise (when descriptor doesn't have __get__, it's unreadable >> and AttributeError is raised) >> * else: check instance __dict__ for "attr", return it when found >> * else: lookup __getattr__ in instance class and call it when found >> * else: raise AttributeError > > No, the value found in the instance (your second 'else' here) takes > precedence if the descriptor found in the first 'else' is > non-overriding. Oh, right. My mistake comes from the subtle difference between defining descriptor as a class and by property() builtin (I've tested only second option and assumed that descriptor without __set__ cannot be rebinded): class non_overriding(object): def __get__(*a): return 12 class C(object): x = non_overriding() y = property(lambda s:23) c = C() c.x = 4 print c.x # => 4 c.y = 5 # => AttributeError: can't set attribute IMHO that's not very consistent. Well, probably some code rely on this, so I just have to live with it. Thanks for your time and patience in explaining my doubts. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why property works only for objects?
Alex Martelli napisał(a): >> IMHO that's not very consistent. > > How so? Given the lower-level semantics of descriptors (and the > distinction between overriding and non), are you suggesting that > property should not be a type but a factory function able to return > instances of either overriding or non-overriding types? I'm not sure how > that complication would make things "consistent" in your view. Nothing in property documentation suggest that if you don't define __set__ method, you won't be able to set an attribute. It just states "Return a property attribute for new-style classes". After reading a bit about descriptors I've just assumed that property is a handy way to create (any) descriptors. Learning that it only creates overriding descriptors was a bit shocking. I'm not suggesting it's bad behavior, only that it seemed unexpected for an unaware programmer. What at first glance looked like a shortcut, turned out to be designed for a specific use case. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
Re: My Generator Paradox!
vbgunz wrote: > def generatorFunction(sequence=['item1', 'item2', 'item3']): > for item in sequence: > yield item > > yieldedValue = generatorFunction() You're creating an iterator here and binding it to name yieldedValue (which is bogus, it should be named valueGenerator or sth like that). > '''this seems to work perfectly.''' > print '-' * 32 > print yieldedValue # > print yieldedValue.next() # item1 > print yieldedValue.next() # item2 > print yieldedValue.next() # item3 You're calling your iterator's next() method getting all of values, as expected. > '''this is where things don't make any sense!''' > print '-' * 32 > print generatorFunction() # You're creating a new iterator here. > print generatorFunction().next()# item1 Another anonymous iterator gets created here. Instantly its next() method is called, yielding first value. > print generatorFunction().next()# item1 And so on... > generatorFunction() is a call (obvious) when calling the second set, I > am resetting the iteration and this explains why I only and always get > item1. > > ok. *but* why in the world does the first set of calls work? > technically, isn't yieldedValue == generatorFunction() on a name > basis? I mean isn't the following technically the same? > > generatorFunction() > yieldedValue = generatorFunction() Well, first statement creates new iterator which is garbage collected right away (as it has no bindings). Second statement creates an iterator and binds it to name yieldedValue. Then it can be used as typical iterator. Calling yieldedValue.next() just calls method of the same iterator you've created a moment ago. It's still the same object. The difference is like the difference between following two lines: list() # creating a new list new_list = list() # creating a new list and binding its name So, rewriting your example from generator to dictionary objects: alist = [1, 2, 3, 4, 5] print alist # [1, 2, 3, 4, 5] print alist.pop() # 5 print alist.pop() # 4 print alist.pop() # 3 print [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5] print [1, 2, 3, 4, 5].pop() # 5 print [1, 2, 3, 4, 5].pop() # 5 print [1, 2, 3, 4, 5].pop() # 5 Remember that generator is an object and you'll be fine. mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. -- http://mail.python.org/mailman/listinfo/python-list
lambda forms within a loop
The snippet of code below uses two functions to dynamically create functions using lambda. Both of these uses should produce the same result, but they don't. The expected output of this code is 11 12 11 12 However, what we get instead is 12 12 11 12 The problem is that the two functions returned by MakeLambdaBad() are apparently the same, but the functions returned by MakeLambdaGood() are different. Can anyone explain why this would/should be the case? -- Michal Ostrowski [email protected] def MakeLambdaGood(): def DoLambda(x): return lambda q: x + q a = [] for x in [1,2]: a.append(DoLambda(x)) return a def MakeLambdaBad(): a = [] for x in [1,2]: a.append(lambda q: x + q) return a [a,b] = MakeLambdaBad() print a(10) print b(10) [a,b] = MakeLambdaGood() print a(10) print b(10) -- http://mail.python.org/mailman/listinfo/python-list
ignoring some placeholders in string formatting
Hi all,
when I've got a string, say:
URL="http://xyz/blah?session=%(session)s&message=%(message)s"
is it possible to fill in only 'session' and leave "%(message)s" as is
when it isn't present in the values dict?
For example:
URL % { 'session' : 123 }
raises KeyError because of missing 'message' in the dict.
I could indeed replace '%(session)s' with a string replace or regexp but
that's not very elegant ;-)
Is there any way to tell the formatter to use only what's available and
ignore the rest?
Thanks
Michal
--
http://mail.python.org/mailman/listinfo/python-list
Arguments and a return value of a C function call when using setprofile
Hi,
I'm trying to write code that will trace arguments and return values
of all function calls. Using sys.settrace with 'call' and 'return'
events works great for Python functions, but now I want to extend that
to C functions as well. Using sys.setprofile instead in theory gives
me what I need ('c_call' and 'c_return'), but I'm stuck on getting
actual arguments and return values.
A snippet of code is worth a thousand words, so here it is:
import inspect
import sys
def trace(frame, event, arg):
if event == 'call':
print "CALL", frame.f_code.co_name, "arguments:",
inspect.getargvalues(frame)
elif event == 'return':
print "RETURN", frame.f_code.co_name, "return value:", arg
elif event == 'c_call':
print "C_CALL", arg.__name__, "???"
elif event == 'c_return':
print "C_RETURN", arg.__name__, "???"
def func(x):
return x+1
sys.setprofile(trace)
func(1) # Python function
repr(2) # C function
sys.setprofile(None)
For a 'c_call' event the trace function is called *before* the call,
so frame is still in the context of the caller. 'c_return' event is
invoked *after* the function has returned, so I don't get the return
value here either. That's why I can't use inspect.getargvalues in
those cases. Is there any other way I could get arguments and a return
value of a C function call?
Cheers,
mk
--
http://mail.python.org/mailman/listinfo/python-list
How to check what is holding reference to object
Hi I've just found out that one of objects is not destroyed when it should be. This means that something was holding reference to this object or part of it (i.e. method). Is there any way to check what holds that reference? I am unable to do that just looking to the code or debugging it because it is pretty complicated, but I am able to invoke this situation again. Regards Michal M. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check what is holding reference to object
On 27 Kwi, 23:21, Duncan Booth wrote: > Michal M wrote: > > Hi > > > I've just found out that one of objects is not destroyed when it > > should be. This means that something was holding reference to this > > object or part of it (i.e. method). Is there any way to check what > > holds that reference? I am unable to do that just looking to the code > > or debugging it because it is pretty complicated, but I am able to > > invoke this situation again. > > See if this code helps: > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/... > > It's pretty old so it may need some adjustment, but I wrote it > to figure out exactly that sort of problem. Thanks you for answers. I tried to use gc.get_referrers(self) in point in code when according to me object should be destroyed earlier but I did not see anything interesting. Then just realised that gc will not destroy object even when itself holds reference to his own method. For example class A(object): def a(self): pass def b(self): self.method = self.a def __del__(self): print "A object deleted" >> a = A() >> del a A object delted >> a = A() >> a.b() >> del a ... nothing ... I thought gc would discover such circle loops but apparently it did not. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check what is holding reference to object
On Apr 28, 12:02 am, Chris Rebert wrote: > On Tue, Apr 27, 2010 at 2:58 PM, Chris Rebert wrote: > > On Tue, Apr 27, 2010 at 2:42 PM, Michal M > > wrote: > >> On 27 Kwi, 23:21, Duncan Booth wrote: > >>> Michal M wrote: > >>> > I've just found out that one of objects is not destroyed when it > >>> > should be. This means that something was holding reference to this > >>> > object or part of it (i.e. method). Is there any way to check what > >>> > holds that reference? I am unable to do that just looking to the code > >>> > or debugging it because it is pretty complicated, but I am able to > >>> > invoke this situation again. > > >>> See if this code helps: > > >>>http://groups.google.com/group/comp.lang.python/browse_thread/thread/... > > >>> It's pretty old so it may need some adjustment, but I wrote it > >>> to figure out exactly that sort of problem. > > >> Thanks you for answers. > >> I tried to use gc.get_referrers(self) in point in code when according > >> to me object should be destroyed earlier but I did not see anything > >> interesting. Then just realised that gc will not destroy object even > >> when itself holds reference to his own method. > > >> For example > > >> class A(object): > >> def a(self): > >> pass > >> def b(self): > >> self.method = self.a > >> def __del__(self): > >> print "A object deleted" > > >>>> a = A() > >>>> del a > >> A object delted > >>>> a = A() > >>>> a.b() > >>>> del a > >> ... nothing ... > > >> I thought gc would discover such circle loops but apparently it did > >> not. > > > No, it does, you just didn't give it long enough; for performance > > reasons, cyclical GC is only done every so often: > > Addendum: > Also, defining __del__ in your example was likely problematic: > Seehttp://docs.python.org/library/gc.html#gc.garbage > > - Chris Thanks, specially for pointing the last issue with __del__ - I missed that! -- http://mail.python.org/mailman/listinfo/python-list
Distinguishing active generators from exhausted ones
Hi, Is there a way to tell if a generator has been exhausted using pure Python code? I've looked at CPython sources and it seems that something like "active"/"exhausted" attribute on genobject is missing from the API. For the time being I am using a simple C extension to look at f_stacktop pointer of the generator frame, which seems to differentiate active generators from exhausted ones. See http://bazaar.launchpad.net/~ruby/pythoscope/support-python2.3/annotate/286/pythoscope/_util.c#L16 for complete source code. I may be missing something obvious here. Is there a better way to tell if a given generator object is still active or not? Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Distinguishing active generators from exhausted ones
On Jul 25, 10:00 pm, Jason Tackaberry wrote: > On Sat, 2009-07-25 at 11:30 -0700, Michal Kwiatkowski wrote: > > Is there a way to tell if a generator has been exhausted using pure > > Python code? I've looked at CPython sources and it seems that > > Upon a cursory look, after a generator 'gen' is exhausted (meaning > gen.next() has raised StopIteration), it seems that gen.gi_frame will be > None. Only in Python 2.5 or higher though. I need to support Python 2.3 and 2.4 as well, sorry for not making that clear in the original post. Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Distinguishing active generators from exhausted ones
On Jul 26, 1:10 am, Ben Finney wrote: > Michal Kwiatkowski writes: > > I may be missing something obvious here. Is there a better way to tell > > if a given generator object is still active or not? > > foo = the_generator_object > try: > do_interesting_thing_that_needs(foo.next()) > except StopIteration: > generator_is_exhausted() > > In other words, don't LBYL, because it's EAFP. Whatever you need to do > that requires the next item from the generator, do that; you'll get a > specific exception if the generator is exhausted. The thing is I don't need the next item. I need to know if the generator has stopped without invoking it. Why - you may ask. Well, the answer needs some explaining. I'm working on the Pythoscope project (http://pythoscope.org) and I use tracing mechanisms of CPython (sys.settrace) to capture function calls and values passed to and from them. Now, the problem with generators is that when they are ending (i.e. returning instead of yielding) they return a None, which is in fact indistinguishable from "yield None". That means I can't tell if the last captured None was in fact yielded or is a bogus value which should be rejected. Let me show you on an example. import sys def trace(frame, event, arg): if event != 'line': print frame, event, arg return trace def gen1(): yield 1 yield None def gen2(): yield 1 sys.settrace(trace) print "gen1" g1 = gen1() g1.next() g1.next() print "gen2" g2 = gen2() [x for x in g2] sys.settrace(None) The first generator isn't finished, it yielded 1 and None. Second one is exhausted after yielding a single value (1). The problem is that, under Python 2.4 or 2.3 both invocations will generate the same trace output. So, to know whether the last None was actually a yielded value I need to know if a generator is active or not. Your solution, while gives me an answer, is not acceptable because generators can cause side effects (imagine call to launch_rockets() before the next yield statement ;). Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Distinguishing active generators from exhausted ones
On Jul 27, 1:56 am, [email protected] (Aahz) wrote: > >> Upon a cursory look, after a generator 'gen' is exhausted (meaning > >> gen.next() has raised StopIteration), it seems that gen.gi_frame will be > >> None. > > >Only in Python 2.5 or higher though. I need to support Python 2.3 and > >2.4 as well, sorry for not making that clear in the original post. > > Are you sure? It appears to work in Python 2.4; I don't have time to > check 2.3. No, it does not work in Python 2.4. gi_frame can be None only in Python 2.5 and higher. Via "What’s New in Python 2.5" (http://docs.python.org/whatsnew/ 2.5.html): """ Another even more esoteric effect of this change: previously, the gi_frame attribute of a generator was always a frame object. It’s now possible for gi_frame to be None once the generator has been exhausted. """ Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Distinguishing active generators from exhausted ones
On Jul 27, 10:47 pm, Terry Reedy wrote: > There are two possible definition of 'exhausted': 1) will raise > StopIteration on the next next() call; 2) has raised StopIteration at > least once. The wrapper converts 2) to 1), which is to say, it obeys > definition 1 once the underlying iteration has obeyed definition 2. > > Since it is trivial to set 'exhausted=True' in the generator user code > once StopIteration has been raised (meaning 2), I presume the OP wants > the predictive meaning 1). No, I meant the second meaning (i.e. generator is exhausted when it has returned instead of yielding). While, as you showed, it is trivial to create a generator that will have the "exhausted" flag, in my specific case I have no control over the user code. I have to use what the Python genobject API gives me plus the context of the trace function. Cheers, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: programmatically manipulation environment variables of the calling shell
Hello, On Mon, Mar 12, 2007 at 11:40:11AM +0100, Diez B. Roggisch wrote: > Maxim Veksler wrote: > > I'm trying to write a python script that would allow me to manipulate > > shell variables of the calling shell. I'm trying to write some logic > > that would know to add LD_LIBRARY_PATH to the users environment. > > It's not possible, OS restrictions - you are not allowed to alter the > environment of a parent process. Actually, you are able to modify only your own environment (and call a new process with any possible environment). And the restrictions seem to me rather technical - the process would have to change its behaviour each time it changes. With regards -- grep me no patterns and I'll tell you no lines. Michal 'vorner' Vaner pgpiWqB4iIas1.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending ECHO_REQUEST (pinging) with python
On Mon, Mar 26, 2007 at 08:30:16AM +0200, Thomas Dybdahl Ahle wrote: > Hi, I've writing a python application in which I'd like to have a small > "ping label", to always tell the current ping time to the server. > > It seems however that I have to be root to send those imcp packages, but > I guess there must be a workaround since I can easily use the "ping" > command as ordinary user. > > Do anybody know how to do this in python? You need root for that and the ping command is allowed to have them by suid bit. You can execute ping from inside python and use ping as is, if you need. -- This is a terroristic email. It will explode in 10 minutes, if you do not close it in the meantime. Michal "vorner" Vaner pgpGCsSHtsxLl.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
