Help with stale exception Python
I'm new to Python and programming. Been learning it for 3 weeks now but have
had lot of obstacles along the way. I found some of your insights very useful
as a starter but I have come across many more complicated challenges that
aren't very intuitive.
For example,I'm trying to scrap this web(via university library (fully access)
so it is a proxy) using selenium (because it is very heavily java script
driven). There is a button which allows user to navigate to the next page of
company and my script go and find the elements of interest from each page write
to a csv and then click to the next page and do it recursively. I have a couple
of problems need some help with. Firstly the element that I'm really interested
is only company website(which isn't always there) but when it is there the
location of the element can change all the time(see
http://pasteboard.co/2GOHkbAD.png and http://pasteboard.co/2GOK2NBT.png)
depending on the number of elements in the parent level. I'm using
driver.find_elements_by_xpath("//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position()
= 1 or position() = 2 or position() = 3]")
hoping to capture all information(e.g. phone,email,website) and then do some
cleansing later on However, it appears not all the web elements are captured
using this method and write to csv from each page. Some pages were written to
the file but some were missing. I couldn't figure it out why.
A second problem which is a more complicate and have been driving me nuts was
the the DOM changes as a result of web content changes and elements are
destroyed and/maybe being recreated after
driver.find_element_by_id('detail-pagination-next-btn').click()
I have tried uncountable number of methods (e.g. explicit, implicit wait) but
the stale error still persists as it seems to stays stale as long as it is
staled.
Have anyone come up with a solution to this and what is the best way to deal
with DOM tree changes.
Much appreciated for your help. My code is attached:
with open('C:/Python34/email.csv','w') as f:
z=csv.writer(f, delimiter='\t',lineterminator = '\n',)
while True:
row = []
for link in
driver.find_elements_by_xpath("//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position()
= 1 or position() = 2 or position() = 3]"):
try:
row.append(str(link.text))
z.writerow(link.text)
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]/span')))
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]')))
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.ID,'detail-pagination-next-btn')))
WebDriverWait(driver,
50).until(EC.element_to_be_clickable((By.ID,'detail-pagination-next-btn')))
WebDriverWait(driver,
50).until(EC.presence_of_all_elements_located((By.XPATH,"//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position()
= 1 or position() = 2 or position() = 3]")))
time.sleep(10)
c=driver.find_element_by_id('detail-pagination-next-btn')
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]/span')))
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]')))
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.ID,'detail-pagination-next-btn')))
WebDriverWait(driver,
50).until(EC.element_to_be_clickable((By.ID,'detail-pagination-next-btn')))
WebDriverWait(driver,
50).until(EC.presence_of_all_elements_located((By.XPATH,"//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position()
= 1 or position() = 2 or position() = 3]")))
c.click()
time.sleep(10)
continue
except StaleElementReferenceException as e:
c=driver.find_element_by_id('detail-pagination-next-btn')
for link in
driver.find_elements_by_xpath("//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position()
= 1 or position() = 2 or position() = 3]"):
row.append(str(link.text))
z.writerow(link.text)
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]/span')))
WebDriverWait(driver,
50).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="detail-pagination-next-btn"]')))
WebDriverWait(driver,
50).until(
Re: Unicode failure
On Sun, 6 Dec 2015 at 23:11 Quivis wrote:
> On Fri, 04 Dec 2015 13:07:38 -0500, D'Arcy J.M. Cain wrote:
>
> > I thought that going to Python 3.4 would solve my Unicode issues but it
> > seems I still don't understand this stuff. Here is my script.
> >
> > #! /usr/bin/python3 # -*- coding: UTF-8 -*-
> > import sys print(sys.getdefaultencoding())
> > print(u"\N{TRADE MARK SIGN}")
> >
> > And here is my output.
> >
> > utf-8 Traceback (most recent call last):
> > File "./g", line 5, in
> > print(u"\N{TRADE MARK SIGN}")
> > UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
> > position 0: ordinal not in range(128)
>
> H, interesting:
>
> Python 2.7.3 (default, Jun 22 2015, 19:43:34)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> print sys.getdefaultencoding()
> ascii
> >>> print u'\N{TRADE MARK SIGN}'
> ™
>
>
sys.getdefaultencoding() returns the default encoding used when opening a
file if an encoding is not explicitly given in the open call. What matters
here is the encoding associated with stdout which is sys.stdout.encoding.
$ python2.7 -c 'import sys; print(sys.stdout.encoding); print(u"\u2122")'
UTF-8
™
$ LANG=C python2.7 -c 'import sys; print(sys.stdout.encoding);
print(u"\u2122")'
ANSI_X3.4-1968
Traceback (most recent call last):
File "", line 1, in
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in
position 0: ordinal not in range(128)
--
Oscar
--
https://mail.python.org/mailman/listinfo/python-list
define the area plot
Hi, I'm changing from the IDL to PYTHON. I would like know how can I define the size of the window plot in Python. On IDL I define the size of area plot by: window,1,xsize=1000,ysize=1000 It is possible define the size of the window before my data is plotted. Conrado -- https://mail.python.org/mailman/listinfo/python-list
Twisted 15.4 was the last release to support Python 2.6; or: a HawkOwl Can't Words Situation
Hi everyone! It's been brought to my attention that I misworded something in the release notes and it slipped through the cracks. In the NEWS I said: > This is the last Twisted release where Python 2.6 is supported, on any > platform. However, I meant that this is the first Twisted release to drop 2.6 support wholesale, preventing import on this platform. Twisted 15.4 will still operate, so if you have Python 2.6 deployment requirements, bracket the maximum to 15.4 on that platform by using an if statement in your setup.py, and `Twisted >=*minreq*,<=15.4; python_version < '2.7'` under requires_dist in your setup.cfg, where minreq is the minimum required Twisted. Sorry for the inconvenience! - Amber "HawkOwl" Brown Twisted Release Manager signature.asc Description: Message signed with OpenPGP using GPGMail -- https://mail.python.org/mailman/listinfo/python-list
Packages installing problem
Dear sir. I was using Python2.7 and i move to Python3.5. I face big trouble with installing software packages, i need in my development. Lalith -- https://mail.python.org/mailman/listinfo/python-list
install issue
When I run the installer (python-3.5.0.exe [win32]) I see this: There seems to be "hidden" buttons as clicking in the middle of this dialog box does continue the process. However, when complete, and I start python, I get an error saying its not a valid win32 application. So, I uninstalled, and there is the same issue with the un-installer - I have to click random places in the dialog box to "find" the uninstall button, that is not shown. (plus AVG shows this as the Heri virus - and, yes, I did disable AVG while attempting the installation.) Win xp, SP3, AMD Phenom, 1100, 8gb (dual boot with win7) Rgds -- https://mail.python.org/mailman/listinfo/python-list
Re: getting fileinput to do errors='ignore' or 'replace'?
On 2015-12-04, Oscar Benjamin wrote: > Or you can use fileinput which is designed to be exactly this kind of > context manager and to be used in this way. Although fileinput is slightly > awkward in defaulting to reading stdin. That default is what I specifically like about fileinput --- it's a normal way for command-line tools to work: $ sort file0 file1 file2 >sorted.txt $ generate_junk | sort >sorted_junk.txt -- $2.95! PLATE O' SHRIMP Luncheon Special -- https://mail.python.org/mailman/listinfo/python-list
writing an email.message.Message in UTF-8
I'm trying to write an instance of email.message.Message, whose body
contains unicode characters, to a UTF-8 file. (Python 2.7.3 & 2.7.10
again.)
reply = email.message.Message()
reply.set_charset('utf-8')
... # set various headers
reply.set_payload('\n'.join(body_lines) + '\n')
...
outfile = codecs.open(outfilename, 'w', encoding='utf-8', errors='ignore')
outfile.write(reply.as_string())
outfile.close()
Then reply.as_string() barfs a UnicodeDecodeError. I look in the
documentation, which says the generator is better. So I replace the
outfile.write(...) line with the following:
g = email.generator.Generator(outfile, mangle_from_=False)
g.flatten(reply)
which still barfs a UnicodeDecodeError. Looking closer at the first
error, I see that the exception was in g.flatten(...) already & thrown
up to reply.as_string(). How can I force the thing to do UTF-8
output?
Thanks.
--
$2.95!
PLATE O' SHRIMP
Luncheon Special
--
https://mail.python.org/mailman/listinfo/python-list
Re: define the area plot
In a message of Mon, 07 Dec 2015 10:51:10 -0200, [email protected] wr ites: > > >Hi, > >I'm changing from the IDL to PYTHON. I would like know how can I define >the size of the window plot in Python. On IDL I define the size of area >plot by: > > >window,1,xsize=1000,ysize=1000 > > >It is possible define the size of the window before my data is plotted. > > >Conrado What package are you using to plot with? Laura -- https://mail.python.org/mailman/listinfo/python-list
Re: writing an email.message.Message in UTF-8
On 2015-12-07, Adam Funk wrote:
> I'm trying to write an instance of email.message.Message, whose body
> contains unicode characters, to a UTF-8 file. (Python 2.7.3 & 2.7.10
> again.)
>
> reply = email.message.Message()
> reply.set_charset('utf-8')
> ... # set various headers
> reply.set_payload('\n'.join(body_lines) + '\n')
I've also tried changing that to
reply.set_payload('\n'.join(body_lines) + '\n', 'utf-8')
but I get the same error on output.
> ...
> outfile = codecs.open(outfilename, 'w', encoding='utf-8', errors='ignore')
> outfile.write(reply.as_string())
> outfile.close()
>
> Then reply.as_string() barfs a UnicodeDecodeError. I look in the
> documentation, which says the generator is better. So I replace the
> outfile.write(...) line with the following:
>
> g = email.generator.Generator(outfile, mangle_from_=False)
> g.flatten(reply)
>
> which still barfs a UnicodeDecodeError. Looking closer at the first
> error, I see that the exception was in g.flatten(...) already & thrown
> up to reply.as_string(). How can I force the thing to do UTF-8
> output?
>
> Thanks.
>
>
--
Cats don't have friends. They have co-conspirators.
http://www.gocomics.com/getfuzzy/2015/05/31
--
https://mail.python.org/mailman/listinfo/python-list
Re: install issue
In a message of Sun, 06 Dec 2015 20:44:03 +, "John Robinson" writes: >When I run the installer (python-3.5.0.exe [win32]) I see this: > > > > > > > >There seems to be "hidden" buttons as clicking in the middle of this dialog >box does continue the process. > >However, when complete, and I start python, I get an error saying its not a >valid win32 application. > >So, I uninstalled, and there is the same issue with the un-installer - I >have to click random places in the dialog box to "find" the uninstall >button, that is not shown. > > > >(plus AVG shows this as the Heri virus - and, yes, I did disable AVG while >attempting the installation.) > >Win xp, SP3, AMD Phenom, 1100, 8gb (dual boot with win7) > > > >Rgds AVG, like most antivirus software, gets tons and tons of false positives. Regardless of weirdnesses in the installer, your real problem is that 3.5 and later does not run on XP, period. The new Python 3.5.1 released yesterday was supposed to be able to detect your XP, and tell you that you needed to upgrade your OS in order to run 3.5.1 Looks like things are still not working perfectly on that front, but that's the problem. You need to either stick with 3.4 or upgrade your OS. Laura Creighton -- https://mail.python.org/mailman/listinfo/python-list
Re: Packages installing problem
In a message of Mon, 07 Dec 2015 00:32:37 +, lalith writes: >Dear sir. > >I was using Python2.7 and i move to Python3.5. >I face big trouble with installing software packages, i need in my >development. > >Lalith >-- >https://mail.python.org/mailman/listinfo/python-list Do you want to install your packages system-wide? Try py -3.5 -m install Do you want to install your python packages in a virtual environment? Start reading here: https://docs.python.org/3/library/venv.html If you sometimes want a python 2.7 virtual env you cannot get it with venv. You will have to use the older virtualenv. https://virtualenv.readthedocs.org/en/latest/ Laura -- https://mail.python.org/mailman/listinfo/python-list
python 3.5: upgrading smart_open package
Hello, I have to use python 3.5 in my use case and one of the packages I use (gensim), in turn uses smart_open (https://github.com/piskvorky/smart_open/) smart_open depends on boto and boto fails on my machine running ubuntu 15.10. So in order for me to be able to run gensim, I need to be able to migrate smart_open from boto to boto3 (boto3 works fine with python 3 and ubuntu 15.10). There is an open github issue: https://github.com/piskvorky/smart_open/issues/43 I am not sure how to go about doing this migration. If someone has any directions or guidelines I will appreciate. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: writing an email.message.Message in UTF-8
On 12/7/2015 9:57 AM, Adam Funk wrote: I'm trying to write an instance of email.message.Message, whose body contains unicode characters, to a UTF-8 file. (Python 2.7.3 & 2.7.10 again.) The email package was rewritten for, I believe, 3.3. I believe it should handle unicode email encoded as utf-8 more easily. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5: upgrading smart_open package
In a message of Mon, 07 Dec 2015 22:45:44 +0530, Anupam Mediratta writes: >Hello, > >I have to use python 3.5 in my use case and one of the packages I use >(gensim), in turn uses smart_open (https://github.com/piskvorky/smart_open/) > >smart_open depends on boto and boto fails on my machine running ubuntu >15.10. > >So in order for me to be able to run gensim, I need to be able to migrate >smart_open from boto to boto3 (boto3 works fine with python 3 and ubuntu >15.10). > >There is an open github issue: >https://github.com/piskvorky/smart_open/issues/43 > >I am not sure how to go about doing this migration. If someone has any >directions or guidelines I will appreciate. > >Thanks You have a bad problem. https://github.com/piskvorky/smart_open/labels/help%20wanted is the other name of https://github.com/piskvorky/smart_open/issues/43 This issue is because the author of smart_open wishes very badly to move to boto3, but hasn't, because he wants help with something. Thus you cannot migrate until he does. You should send mail to the author, which I looked up for you and you can find here. http://radimrehurek.com/contact/ Tell him that you want smart_open to work with boto3, and that you want to help making that happen. Then see what he says is needed and see if that is something you can do. Laura -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5: upgrading smart_open package
On 12/7/2015 12:15 PM, Anupam Mediratta wrote: Hello, I have to use python 3.5 in my use case and one of the packages I use (gensim), in turn uses smart_open (https://github.com/piskvorky/smart_open/) smart_open depends on boto and boto fails on my machine running ubuntu 15.10. So in order for me to be able to run gensim, I need to be able to migrate smart_open from boto to boto3 (boto3 works fine with python 3 and ubuntu 15.10). There is an open github issue: https://github.com/piskvorky/smart_open/issues/43 I am not sure how to go about doing this migration. If someone has any directions or guidelines I will appreciate. Is boto3 installed and imported as 'boto' or 'boto3'? If the former, just install boto3 (into site-packages/boto) and 'import boto' in smart_open will just work. This would be the same in installing pillow as a drop in replacement for PIL, since pillow is installed in a directory named 'PIL'. If the latter, change the import statement. Any other changes would depend on boto3 having a different api from boto. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue
On Sun, Dec 6, 2015 at 5:16 PM, Steven D'Aprano wrote: > Oh, I can make one guess... if you're using Windows XP, I'm afraid that > Python 3.5 is not supported. You'll have to either downgrade to Python 3.4, > or upgrade to Windows 7 or higher, or another operating system. For the sake of accuracy, the requirement is actually Windows Vista or higher. -- https://mail.python.org/mailman/listinfo/python-list
Accessing container's methods
Hi,
I have a class A, containing embedded embedded classes, which need to
access methods from A.
.
A highly contrived example, where I'm setting up an outer class in a
Has-a relationship, containing a number of Actors. The inner class needs
to access a method of the outer class; here the method get_name.
I don't really want to make Actor a sub-class (is-a; it isn't) of Monty;
that would raise all sorts of other problems.
Can anyone please advise me on how to achieve this magic?
# define the outer class
class Monty:
def __init__( self, names ):
self.actors = []
i = 0
for n in names:
self.actors.append( Actor( n, i ) )
i += 1# here is a case for python supporting post-increment!
def count_actors( self ):
return len( self.actors )
def list_actors( self ):
h=[]
for n in self.actors:
h.append( n.get_name() )
return h
# define the inner class
class Actor:
def __init__ ( self, name, id ):
self.name = name
self.id = id
def get_name( self ):
# and here lies the problem;
# AttributeError: Actor instance has no attribute 'count_actors'
# how do I access the method in the enclosing class
txt = "I'm Actor {} Number {} of {}".\
format( self.name, self.id, self.count_actors() )
# this works, of course
#txt = "I'm Actor \"{}\"; Number {}. ".\
format( self.name, self.id )
return txt
if __name__ == '__main__':
o = Monty( ["Cleese", "Idle", "Palin" ] )
print "number: ",o.count_actors()
a = o.list_actors()
for l in a:
print l
Thanks, Tony
--
https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
Tony van der Hoff wrote:
> Hi,
>
> I have a class A, containing embedded embedded classes, which need to
> access methods from A.
> .
> A highly contrived example, where I'm setting up an outer class in a
> Has-a relationship, containing a number of Actors. The inner class needs
> to access a method of the outer class; here the method get_name.
>
> I don't really want to make Actor a sub-class (is-a; it isn't) of Monty;
> that would raise all sorts of other problems.
>
> Can anyone please advise me on how to achieve this magic?
>
> # define the outer class
> class Monty:
>def __init__( self, names ):
> self.actors = []
>
> i = 0
> for n in names:
>self.actors.append( Actor( n, i ) )
>i += 1 # here is a case for python supporting post-increment!
>
>def count_actors( self ):
> return len( self.actors )
>
>def list_actors( self ):
> h=[]
> for n in self.actors:
>h.append( n.get_name() )
> return h
>
> # define the inner class
> class Actor:
>def __init__ ( self, name, id ):
> self.name = name
> self.id = id
>
>def get_name( self ):
>
> # and here lies the problem;
> # AttributeError: Actor instance has no attribute 'count_actors'
> # how do I access the method in the enclosing class
> txt = "I'm Actor {} Number {} of {}".\
> format( self.name, self.id, self.count_actors() )
>
> # this works, of course
> #txt = "I'm Actor \"{}\"; Number {}. ".\
> format( self.name, self.id )
>
> return txt
>
> if __name__ == '__main__':
>o = Monty( ["Cleese", "Idle", "Palin" ] )
>print "number: ",o.count_actors()
>a = o.list_actors()
>for l in a:
> print l
>
> Thanks, Tony
Ideally, you wouldn't. The Actor's name is independent of the number of
them in the Monty, and all logic that needed to know about the full
Monty would be there in list_actors, not delegated down to the Actor.
That way all your dependencies are unidirectional: a Monty has a lot
of expectations about an Actor but an Actor knows nothing about a
Monty.
And that's GREAT when you can make it work. Sometimes, in non-contrived
examples, you get a situation where you can't avoid blending information
around in both directions. In that case, your Monty instance is already
acting as a factory for Actors. Keep going with that; have the Actor be
init'ed with a .monty that refers back to the Monty containing it. It's
a circular reference, but you can either a) break that using weakref or
b) not care and trust Python's garbage collection to sort it all out
eventually.
--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
On 12/07/2015 11:10 AM, Tony van der Hoff wrote: > Hi, > > I have a class A, containing embedded embedded classes, which need to > access methods from A. > . > A highly contrived example, where I'm setting up an outer class in a > Has-a relationship, containing a number of Actors. The inner class needs > to access a method of the outer class; here the method get_name. > > I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; > that would raise all sorts of other problems. > > Can anyone please advise me on how to achieve this magic? You could add an attribute to each embedded object that provides a reference back to the container object. All in all, this design has a kind of smell to it. Would it not be better to ask the container class for information about the children, rather than the other way around? If a piece of code holds a reference to the child object then it must also by definition hold a reference to the container object, so why can't it ask the container object directly? -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
Tony van der Hoff wrote: > I have a class A, containing embedded embedded classes, which need to > access methods from A. Let the name of the "embedded class" (which is not embedded at all in your example code) be E. You could either store the information in the E instance upon or after construction, or you could pass a reference to the A instance to the E instance’s method, so that you can call the A instance’s public methods from the E instance’s methods. But consider the Law of Demeter. > I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; > that would raise all sorts of other problems. It would simply be wrong. Monty (Python) is (literally) a group of Actors. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
Tony van der Hoff wrote:
> Hi,
>
> I have a class A, containing embedded embedded classes, which need to
> access methods from A.
> .
> A highly contrived example, where I'm setting up an outer class in a
> Has-a relationship, containing a number of Actors. The inner class needs
> to access a method of the outer class; here the method get_name.
>
> I don't really want to make Actor a sub-class (is-a; it isn't) of Monty;
> that would raise all sorts of other problems.
>
> Can anyone please advise me on how to achieve this magic?
>
> # define the outer class
> class Monty:
>def __init__( self, names ):
> self.actors = []
>
> i = 0
> for n in names:
>self.actors.append( Actor( n, i ) )
>i += 1 # here is a case for python supporting post-increment!
>
>def count_actors( self ):
> return len( self.actors )
>
>def list_actors( self ):
> h=[]
> for n in self.actors:
>h.append( n.get_name() )
> return h
>
> # define the inner class
> class Actor:
>def __init__ ( self, name, id ):
> self.name = name
> self.id = id
>
>def get_name( self ):
>
> # and here lies the problem;
> # AttributeError: Actor instance has no attribute 'count_actors'
> # how do I access the method in the enclosing class
> txt = "I'm Actor {} Number {} of {}".\
> format( self.name, self.id, self.count_actors() )
>
> # this works, of course
> #txt = "I'm Actor \"{}\"; Number {}. ".\
> format( self.name, self.id )
>
> return txt
>
> if __name__ == '__main__':
>o = Monty( ["Cleese", "Idle", "Palin" ] )
>print "number: ",o.count_actors()
>a = o.list_actors()
>for l in a:
> print l
I think I've seen the solution a long time a go in a Borland library -- a
Collection and a CollectionItem class, the latter with a reference to the
collection it belongs to. However, you are introducing a reference cycle,
and a simpler solution like putting a print_actor() method into the Monty
class is probably the way to go.
Anyway, here's a variant of your code using the back reference (and a few
cosmetical changes):
__metaclass__ = type # python 2 compatibility
class Monty:
def __init__(self, names):
self.actors = []
for id, name in enumerate(names, 1):
Actor(name, id, container=self)
def __len__(self):
return len(self.actors)
def __iter__(self):
for actor in self.actors:
yield actor
def remove(self, actor):
raise NotImplementedError
def add(self, actor):
self.actors.append(actor)
class Actor:
def __init__ (self, name, id, container=None):
self.name = name
self.id = id
self._container = None
self.container = container
def get_container(self):
return self._container
def set_container(self, container):
if self._container is not None:
self._container.remove(self)
if container is not None:
container.add(self)
self._container = container
container = property(get_container, set_container)
def __repr__(self):
return "I'm Actor {} Number {} of {}".format(
self.name, self.id, len(self.container))
if __name__ == '__main__':
o = Monty( ["Cleese", "Idle", "Palin" ])
print("number: {}".format(len(o)))
for l in o:
print(l)
--
https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
Michael Torrie wrote: > On 12/07/2015 11:10 AM, Tony van der Hoff wrote: >> I have a class A, containing embedded embedded classes, which need to >> access methods from A. >> . >> A highly contrived example, where I'm setting up an outer class in a >> Has-a relationship, containing a number of Actors. The inner class needs >> to access a method of the outer class; here the method get_name. >> >> I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; >> that would raise all sorts of other problems. >> >> Can anyone please advise me on how to achieve this magic? > > You could add an attribute to each embedded object that provides a > reference back to the container object. ACK. > All in all, this design has a kind of smell to it. Would it not be > better to ask the container class for information about the children, > rather than the other way around? If a piece of code holds a reference > to the child object then it must also by definition hold a reference to > the container object, so why can't it ask the container object directly? Indeed, in this example asking the parent would be the best approach because only the number of actors is requested. That number should not change, and if it can change, then there should be an interface for that (a public method), in which the parent must update it. So Monty.count_actors() does not have to count the actors each time it is called from Actor.get_name(); it just has to return the value of a private attribute containing the current count. In other cases asking the parent would avoid inconsistencies, but that could be at the cost of efficiency; for example, a search for each child object, which does not scale well. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
On Mon, Dec 7, 2015 at 11:10 AM, Tony van der Hoff wrote: > Hi, > > I have a class A, containing embedded embedded classes, which need to access > methods from A. > . > A highly contrived example, where I'm setting up an outer class in a Has-a > relationship, containing a number of Actors. The inner class needs to access > a method of the outer class; here the method get_name. > > I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; > that would raise all sorts of other problems. > > Can anyone please advise me on how to achieve this magic? I'm guessing that you're coming from Java. Java has two different types of nested classes: non-static, where an instance of the inner class is implicitly associated with an instance of the outer class; and static, where an instance of the inner class is unrelated to any instance of the outer class. It looks like you're trying to do the former, but Python only has the static type of nested classes. So how would you go about creating a nested class of the non-static type? Make the association explicit. When Monty creates an Actor, pass it self as one of the arguments. Actor can then save that instance of Monty in an attribute and call the method thusly. As others have noted, this does create a reference cycle. The Python garbage collector is fine with cleaning up reference cycles as long as there are no objects __del__ methods anywhere in the cycle, so it's up to you whether you consider that to be a problem or not. -- https://mail.python.org/mailman/listinfo/python-list
Re: Brief Code Review Please - Learning Python
Joel Goldstick wrote:
> On Sun, Dec 6, 2015 at 12:21 PM, wrote:
>> * Same question as right above but with the if tests on lines 5-8.
>> * I've also used ( ) around the if tests, but I'm not sure if this is
>> good Python style or not.
>>
>> 1 def measure_string(desired_text,
>> 2 desired_font_family,
>> 3 desired_font_size,
>> 4 is_multi_lines):
>> 5 if (desired_text != '') and \
>> 6 (desired_font_family != '') and \
>> 7 (desired_font_size != '') and \
>> 8 ((is_multi_lines == "True") or (is_multi_lines == "False")):
>>
>
> The above test will always be True.
How did you get that idea?
> Look up what is considered True in Python, and what is False.
ISTM that *you* should do that.
> I imagine you don't want quotes around True and False.
I imagine also that they do not want to write “== True” and “== False”.
Because you can omit the former and should replace “x == False” with
“not x”. This applies to many programming languages, even those that do
type conversion (and have “!” instead of “not”).
> Any string will == True
No, Python does not do type conversion on comparison by default.
$ python -c 'print("" == False)'
False
$ python -c 'print("True" == True)'
False
$ python -c 'print(True == True)'
True
(You have to define the __eq__() method of an object for that, and then you
should also define at least __ne__().)
>> 9 with Image(filename='wizard:') as temp_image:
>> 10 with Drawing() as measure:
>> 11 measure.font_family = desired_font_family
>> 12 measure.font_size = desired_font_size
>> 13 measures = measure.get_font_metrics(temp_image,
>> 14 desired_text,
>> 15
>> multiline=is_multi_lines)
>>
>
> No need to set multiline = ... when is_multiline is already defined
Watch for word wrap. He sets the argument for the “multiline” parameter in
the measure.get_font_metrics() method call from the “is_multi_lines”
parameter of the measure_string() call.
Please trim your quotes to the relevant minimum.
OP: Please do not post code line numbers or any other "decoration" if you
want a code review. Especially with Python it is important that you post
the code verbatim.
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.$
--
https://mail.python.org/mailman/listinfo/python-list
Understanding Python from a PHP coder's perspective
Hello all! Just started getting into Python, and am very excited about the prospect. I am struggling on some general concepts. My past experience with server-side code is mostly limited to PHP and websites. I have some file called "whatever.php", the browser accesses it, and PHP parses it and returns HTML, JSON, etc. Every now and then, I need to run some background PHP script, or have some PHP script initiated by a CRON job, or have some PHP script initiated by the command line running in an endless loop, and while it works, feel other languages might be more appropriate. So, my interest in Python... I've read up on Python, and while some aspects seem similar to PHP, some don't. I have learned how to create Python script, have run it from the command line, and have even accessed it with Apache by placing http://example.com/myscript.py in the browser. I am used to seeing .php extensions, but never .py extentions, and even visited multiple sites which I knew were written in Python, but never saw the browser expose the .py extensions. I am obviously missing something. Why don't I see the .py extension in my browser? Is Python event driven like PHP, or is it somehow different? How should I view Python differently than PHP? Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
Right after I sent my first post, I realized I did not include the following: Per https://wiki.python.org/moin/PythonVsPhp, "The vast majority of Python Web applications are run in a separate process. This has some important implications." >From a PHP background, guess I just don't understand the concept of "separate >processes". What does this mean, how is it implemented, and what are the >"important implications"? -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On 07Dec2015 13:07, [email protected] wrote: Hello all! Just started getting into Python, and am very excited about the prospect. I am struggling on some general concepts. My past experience with server-side code is mostly limited to PHP and websites. I have some file called "whatever.php", the browser accesses it, and PHP parses it and returns HTML, JSON, etc. Every now and then, I need to run some background PHP script, or have some PHP script initiated by a CRON job, or have some PHP script initiated by the command line running in an endless loop, and while it works, feel other languages might be more appropriate. So, my interest in Python... I've read up on Python, and while some aspects seem similar to PHP, some don't. I have learned how to create Python script, have run it from the command line, and have even accessed it with Apache by placing http://example.com/myscript.py in the browser. I am used to seeing .php extensions, but never .py extentions, and even visited multiple sites which I knew were written in Python, but never saw the browser expose the .py extensions. I am obviously missing something. The most obvious thing is: a well produced website does not expose its implementation language in the URL. All those .php URLs? Sloppy! Suppose you wanted to change the implementation and switched languages? All your users' bookmarks would break! Why don't I see the .py extension in my browser? Usually Python is used in a web server via a WSGI plugin and a framework of some kind (Django, Flask, CheryPy etc). All of these allow you to associate any URL with any piece of Python code. So you don't have that simple but also inflexible "map this URL to a specific .php file" model that you have with typical PHP. Is Python event driven like PHP, or is it somehow different? Depends what you mean by "event driven". I suspect you mean "accessing a web page runs a python program". A .php file is a kind of macro file: it is ostensibly HTML until one hits the the HTML stream going back to the browser. You very rarely see PHP "standalone", used to write command line tools or other things - it is almost only ever used to generate web page output. Python is a general purpose language and all manner of things are written in it. When used in a web server there is usually: - a WGSI hook into the web server configuration, which causes certain URLs to be passed to a running Python program for implementation - a Python program which maps those URLs to particular pieces of code, which return HTML or other suitable content How should I view Python differently than PHP? It is more useful and nicer to work in. For a quick and dirty web page PHP is a handy tool, but almost entirely for the web. Python is not targeted specificly at web output. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Mon, Dec 7, 2015 at 2:07 PM, wrote: > Hello all! Just started getting into Python, and am very excited about the > prospect. > > I am struggling on some general concepts. My past experience with > server-side code is mostly limited to PHP and websites. I have some file > called "whatever.php", the browser accesses it, and PHP parses it and returns > HTML, JSON, etc. Every now and then, I need to run some background PHP > script, or have some PHP script initiated by a CRON job, or have some PHP > script initiated by the command line running in an endless loop, and while it > works, feel other languages might be more appropriate. > > So, my interest in Python... > > I've read up on Python, and while some aspects seem similar to PHP, some > don't. I have learned how to create Python script, have run it from the > command line, and have even accessed it with Apache by placing > http://example.com/myscript.py in the browser. I am used to seeing .php > extensions, but never .py extentions, and even visited multiple sites which I > knew were written in Python, but never saw the browser expose the .py > extensions. I am obviously missing something. > > Why don't I see the .py extension in my browser? PHP tends to be served like CGI, which is that the URL mirrors the path to a script that lives under a particular directory, which the webserver locates and executes. This has some downsides: * It exposes details about the structure of your server to the public, which is considered a security weakness. * An improperly configured webserver might accidentally serve scripts or other files that are not intended to be served over the web, which is a much bigger security risk. * It's inflexible; if you move your script, then the URL must necessarily change as well. With Python, it's more usual to have a greater abstraction between URLs and code. A set of URLs are mapped to a single Python gateway, and the gateway dispatches the request to the appropriate request handler. With this scheme there is no reason to have .py extensions in the URLs because the URL structure is unrelated to how the actual scripts are organized in the file system. This also makes it easier to create meaningful URLs: see https://en.wikipedia.org/wiki/Semantic_URL. -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
On 12/7/2015 1:10 PM, Tony van der Hoff wrote: Hi, I have a class A, containing embedded embedded classes, which need to access methods from A. . A highly contrived example, where I'm setting up an outer class in a Has-a relationship, containing a number of Actors. The inner class needs to access a method of the outer class; here the method get_name. I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; that would raise all sorts of other problems. Can anyone please advise me on how to achieve this magic? # define the outer class class Monty: def __init__( self, names ): self.actors = [] i = 0 for n in names: self.actors.append( Actor( n, i ) ) i += 1# here is a case for python supporting post-increment! This is actually a case for using enumerate: for i, name in enumerate(names): def count_actors( self ): return len( self.actors ) def list_actors( self ): h=[] for n in self.actors: h.append( n.get_name() ) return h return list(self.actors) # or perhaps even faster return self.actors[:] # define the inner class class Actor: def __init__ ( self, name, id ): self.name = name self.id = id Tkinter, and I presume tk, works by creating circular references. widgets get a reference to a master widget, and container get a reference to widgets that are placed (or packed or gridded) therein. Widgets have an explicit .destroy method (inherited from tcl/tk) to undo the circularity. It works, but it is not completely without problems. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 8:07 AM, wrote:
> I am struggling on some general concepts. My past experience with
> server-side code is mostly limited to PHP and websites. I have some file
> called "whatever.php", the browser accesses it, and PHP parses it and returns
> HTML, JSON, etc. Every now and then, I need to run some background PHP
> script, or have some PHP script initiated by a CRON job, or have some PHP
> script initiated by the command line running in an endless loop, and while it
> works, feel other languages might be more appropriate.
>
> So, my interest in Python...
>
> I've read up on Python, and while some aspects seem similar to PHP, some
> don't. I have learned how to create Python script, have run it from the
> command line, and have even accessed it with Apache by placing
> http://example.com/myscript.py in the browser. I am used to seeing .php
> extensions, but never .py extentions, and even visited multiple sites which I
> knew were written in Python, but never saw the browser expose the .py
> extensions. I am obviously missing something.
>
> Why don't I see the .py extension in my browser?
>
> Is Python event driven like PHP, or is it somehow different?
>
> How should I view Python differently than PHP?
Hi there! Welcome to the list, and thank you for trying to understand
the differences, instead of simply expecting Python to behave like the
thing you already know.
To demonstrate what I'm talking about, I'm going to use a tiny
Python-based web site that I knocked together a little while ago. The
source code is on GitHub, and the site itself is public, so you can
see both:
https://github.com/Rosuav/MinstrelHall
http://minstrelhall.com/
The most important file is mh.py - click on that to see the code.
In PHP-based web sites, the structure of your source code exactly
matches the structure of your web site. The file system *is* your web
site; any time the web server (say, Apache) comes across something
that has the .php extension, it goes and runs it as code.
In Python-based web sites like this one, the layout of the source tree
follows a structure that keeps logically-similar things together, and
the structure of the web site is defined by code. As you read through
mh.py, you'll see a number of lines saying @app.route("...") - those
are creating URLs (sloppy terminology, but close enough). There's one
that says @app.route("/"), which handles http://minstrelhall.com/, and
another just underneath it that says @app.route("/campaign/"),
which handles http://minstrelhall.com/campaign/6 (and any other
integer ID that might be passed in).
One advantage of this kind of setup is that your URLs don't depend on
your back end. I could replace all this with a Ruby on Rails site, and
nobody would notice the difference. I could put together something
using Node.js to replace the Ruby site, and continue to provide
content at http://minstrelhall.com/campaign/6 - again, nobody would
care.
Another *massive* advantage is a complete separation of code and data.
You can't see it in Minstrel Hall, but here's a web site that has a
static-files collection:
https://github.com/Rosuav/Flask1
http://rosuav.com/1/
(Note that this Flask site isn't running the whole domain, but only
one subdirectory. What Python sees as @app.route("/foo") is actually
http://rosuav.com/1/foo.)
Any files in the static/ directory are made available by the framework
- but they are, by definition, static files. Any file that I put there
will be delivered as-is. Suppose I create a world-writable directory
called "uploads", and then have a script that accepts a form
submission with an attachment, saving the attachment into the uploads
directory. (This, by the way, is not an engineered example; it's a
real-world example from another web site that I host.) If the uploads
directory is inside static, anything saved to there will be viewable
as http://rosuav.com/1/uploads/some_file_name, but - and this is the
important part - no matter what its file extension, *it will not be
run*. You can upload a .py file, and it'll be visible as Python source
code. You can upload a .cgi script, and it'll be visible as plain
text. You can upload a .php file, and nothing will run it.
This is quite tricky to do with PHP, and requires some careful
management of Apache config files (or presumably the equivalent in
nginx or other servers). With Python+Flask (or any other code-based
server setup), it's automatic. This safety makes a *huge* difference
to the compromisability of your web site; you can accept uploads
without worrying about remote users running code and creating reverse
shells and stuff.
So that's a quick potted summary of why the URLs don't reflect the
language used. Python is event-driven, but instead of defining events
at the file level, the way PHP does, they're defined at the function
level. Of course, if you *want* to put ".py" on the end of all your
URLs, Python won't stop you... :)
ChrisA
--
https://mail.python.org/ma
Re: Understanding Python from a PHP coder's perspective
On 12/7/2015 4:07 PM, [email protected] wrote: Hello all! Just started getting into Python, and am very excited about the prospect. I am struggling on some general concepts. My past experience with server-side code is mostly limited to PHP and websites. I have some file called "whatever.php", the browser accesses it, and PHP parses it and returns HTML, JSON, etc. Every now and then, I need to run some background PHP script, or have some PHP script initiated by a CRON job, or have some PHP script initiated by the command line running in an endless loop, and while it works, feel other languages might be more appropriate. So, my interest in Python... I've read up on Python, and while some aspects seem similar to PHP, some don't. I have learned how to create Python script, have run it from the command line, and have even accessed it with Apache by placing http://example.com/myscript.py in the browser. I am used to seeing .php extensions, but never .py extentions, and even visited multiple sites which I knew were written in Python, but never saw the browser expose the .py extensions. I am obviously missing something. Why don't I see the .py extension in my browser? Better security, I believe. Is Python event driven like PHP, or is it somehow different? Python is not event-driven as you mean it, but you can do event-driven systems in Python. How should I view Python differently than PHP? You should formulate your own general view. For specific aspects, ask more specific quesitons, like you did above. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Mon, Dec 7, 2015 at 2:40 PM, Chris Angelico wrote: > So that's a quick potted summary of why the URLs don't reflect the > language used. Python is event-driven, but instead of defining events > at the file level, the way PHP does, they're defined at the function > level. Of course, if you *want* to put ".py" on the end of all your > URLs, Python won't stop you... :) Or, if it's a poorly implemented site, ".rb". ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 8:21 AM, wrote: > Per https://wiki.python.org/moin/PythonVsPhp, "The vast majority of Python > Web applications are run in a separate process. This has some important > implications." > > From a PHP background, guess I just don't understand the concept of "separate > processes". What does this mean, how is it implemented, and what are the > "important implications"? This is talking about how PHP code is generally run inside the server's process. I'll use Apache as my example, as that's what I have handy. There are a couple of ways that Apache can run PHP code. One is spawning a new process for every page request, and using CGI mode; and the other is a thing called mod_php, which runs PHP as a module inside the Apache process. The first one gives great isolation between pages, but it's horribly slow (every page request has to start a process - cheap on Unix, expensive on Windows - and fully initialize PHP, which is expensive everywhere); the second fixes the performance problem, at the cost of maintaining hidden state in the Apache process. Most PHP web sites out there are using (the equivalent of) mod_php. Coupled with features like persistent database connections (another way of improving performance at the cost of isolation), or changes to PHP error handling configs, this can create *very* debug-hard problems that depend on the exact distribution of work among Apache processes. This becomes even worse if you have multiple PHP web sites running in the same Apache instance; any one of them could interfere with any other. In contrast, Python code is run in a separate process from Apache. It openly and honestly does NOT reset its state between page requests (which you do need to be aware of; for instance, always commit/roll back your database transactions before returning), but there's no way for Python to affect Apache or vice versa. If you have two Python web sites on the same server, they are completely isolated from each other. As well as the massively-beneficial implications of this isolation, there are some less positive effects. One is that you need to have a complete Python process for each site you have active - or else pay a hefty startup penalty on first page load. This can cost a lot of memory, especially if you have a lot of isolated little web sites running. But the benefits of isolation are usually worth that cost; it's possible to run the web server as one user (say, www-data) and the application server as a different user (say, python-app), and then grant file system permissions differently to the different users. You could have two apps running in different versions of Python, or with different versions of third-party packages. You can put memory or CPU usage limits on the Python process, without worrying about the web server itself going down. It's a very different structure from the mod_php model, and worth understanding. Personally, I think the costs are worth it; but if you have a mostly-static web site with just a tiny amount of scripting in it, it might be better to follow a PHP model. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
On Tue, Dec 8, 2015 at 8:38 AM, Terry Reedy wrote: >>def list_actors( self ): >> h=[] >> for n in self.actors: >>h.append( n.get_name() ) >> return h > > > return list(self.actors) # or perhaps even faster > return self.actors[:] Not identical semantics. This is a use-case for a comprehension: return [n.get_name() for n in self.actors] (although I would eschew the getter in favour of just "n.name") ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 8:59 AM, Ian Kelly wrote: > On Mon, Dec 7, 2015 at 2:40 PM, Chris Angelico wrote: >> So that's a quick potted summary of why the URLs don't reflect the >> language used. Python is event-driven, but instead of defining events >> at the file level, the way PHP does, they're defined at the function >> level. Of course, if you *want* to put ".py" on the end of all your >> URLs, Python won't stop you... :) > > Or, if it's a poorly implemented site, ".rb". ;-) Well hey. Python won't stop me from adding ".rb" to the ends of my URLs either... ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
Thank you all! Okay, the concept of a WSGI along with a framework provides insight on my main questions. In regards to Chris's statement: "It openly and honestly does NOT reset its state between page requests" With PHP, I have sessions to preserve state. I have a feeling that this is significantly different. Yes? How? Does this somehow relate to how websockets are implemented? -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 9:27 AM, wrote: > In regards to Chris's statement: "It openly and honestly does NOT reset its > state between page requests" > > With PHP, I have sessions to preserve state. I have a feeling that this is > significantly different. Yes? How? Does this somehow relate to how > websockets are implemented? All three are very different. 1) Process state. You start up a Python program, and it sits there waiting for a request. You give it a request, and get back a response; it goes back to waiting for a request. If you change a global variable, or maintain persistent state, or anything, the next request will 'see' that change. This is completely global. 2) Sessions, cookies, and related concepts. A request comes in, and the response goes out "Hi! You're caller number 52635686412, and your call is important to us". Another request comes in from the same web browser, and the browser says "Hi! You said I was caller number 52635686412". The server looks up its information about that caller, which might be in a database, or on disk in the /tmp directory, or stored in process state (see above), or anything at all. This gives the appearance of per-client state, but it's all a simulation. 3) Websockets. A client makes a request saying "Hey, I want a websocket, please". The server says "Sure", and then they start maintaining true state. The socket would be broken if either the server or the client restarts (unlike sessions, although normally they're set up so a client restart will wipe the session). Websocket state is per-connection. Does that answer your question? The one I was talking about there was #1, process state. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Mon, Dec 7, 2015 at 3:27 PM, wrote: > Thank you all! > > Okay, the concept of a WSGI along with a framework provides insight on my > main questions. > > In regards to Chris's statement: "It openly and honestly does NOT reset its > state between page requests" > > With PHP, I have sessions to preserve state. I have a feeling that this is > significantly different. Yes? How? Does this somehow relate to how > websockets are implemented? Session state is different from process state. Whether you start a new process for each request or reuse an existing process has no bearing on whether you're storing and reading data related to the user's session (except that in the reuse case it's possible to cache the data in-process, whereas an in-memory cache for a restarted process must be in a separate process; but it's good practice to have your in-memory cache in a separate process anyway). I'm not sure where websockets come into this, as that's a completely separate technology that is not used by most web apps. -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On 07Dec2015 14:27, [email protected] wrote: In regards to Chris's statement: "It openly and honestly does NOT reset its state between page requests" With PHP, I have sessions to preserve state. I have a feeling that this is significantly different. Yes? How? Does this somehow relate to how websockets are implemented? Plenty of things use sessions, and I suspect most frameworks provide handy mechanisms to make using them easier. I expect that normally, as with PHP, you'd pass a cookie back to the user to track them and store the session state either in an in memory data structure (loses state over an app or web server restart) or in some persistent storage, typically a database of some kind (which also allows another process implementing your app/site to handle the state even if it did not issue the original session). Cannot address your websockets query. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Accessing container's methods
Hi Tony,
On 07/12/15 18:10, Tony van der Hoff wrote:
A highly contrived example, where I'm setting up an outer class in a
Has-a relationship, containing a number of Actors. The inner class needs
to access a method of the outer class; here the method get_name.
Generally, an object should not need to know which container it's in
(let alone its "index" or "key" in that container). Amongst other
things, you can't put the object into multiple containers which might be
organised differently and you are asking for bugs where the container
and the object get out of sync WRT just where the object is in the
container (in your example, if you found you wanted to add a method
where the 'actors' list is modified (say, calling "self.actors.insert(3,
...)", or sorting the list) then things get nasty quickly.
However, you've said this is a highly contrived example, so I'll bear
with you and assume what you're trying to do is not quite as nasty as
the example implies ;)
Can anyone please advise me on how to achieve this magic?
As you can't sensibly put the object into more than one container at a
time anyway, then you can pass the container object to the Actor object
as its "parent". It can then call its parent object for things that the
parent will know (such as, the total number of contained objects):
class Actor:
def __init__ ( self, name, id, parent ):
self.name = name
self.id = id
self.parent = parent
def get_name( self ):
txt = "I'm Actor {} Number {} of {}".\
format( self.name, self.id, self.parent.count_actors() )
return txt
Then you can add a new actor with:
self.actors.append( Actor( n, i, self ) )
Note that you are creating circular references here, too (the container
references the Actor object and the Actor object references the
container). Just another possible issue to bear in mind ...
Also, while I'm looking at this, you have this loop and comment:
>def __init__( self, names ):
> self.actors = []
>
> i = 0
> for n in names:
>self.actors.append( Actor( n, i ) )
>i += 1# here is a case for python supporting post-increment!
However, if you refactor that loop to use iterators, you don't need to
manually manipulate 'i' at all (you need to import the itertools module
first, and this is the "new" version where the container is passed in as
the parent):
def __init__( self, names ):
self.actors = [ Actor ( n, i, self ) for n, i in
itertools.izip(names, itertools.count()) ]
Or, if you don't like list comprehensions:
def __init__( self, names ):
self.actors = []
for n, i in itertools.izip(names, itertools.count()):
self.actors.append( Actor( n, i, self ) )
(I assume you're using Python 3 because of your print statement - in
Python 3, 'itertools.izip' should just be 'zip'.)
HTH,
E.
--
https://mail.python.org/mailman/listinfo/python-list
Re: increment/decrement operators
On Dec 5, 2015 10:21 AM, "BartC" wrote: > > > The latter is not the same. Some of the differences are: > > * ++ and -- are often inside inside expressions and return values (unlike x+=1 in Python) > > * x++ and x-- return the /current/ value of x, unlike x+=1 even if it were to return a value; it would be the new value Since x+=1 is not an expression, this is moot. > * x+=1 requires you to hard-code the value 1, but ++ is not necessarily stepping by 1. You can imagine ++ stepping something to its next value. Note that x+=1 does not necessarily have to mean stepping by 1 either. You could even do something like x+=5 to mean skip the next four values and step x to the value after that. > However, if ++ and -- are only used as statements, then why not simply map them to x+=1? In Python, that would need to be x++ and x-- as ++x or --x have existing meanings. I think a better question is if they're only going to be statements, then why bother adding them? x++ doesn't give you anything you can't get from x+=1, so why commit to supporting the extra syntax? -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
Ah, I was confused that process state somehow allowed Python to support per-connection state without using sessions (which lead me to ask about websockets). I guess Python could do it without storing the session ID in a file or database and instead in process state (i.e. application memory, right?), but the effect seems basically the same. Without being per-connection, what is the value of process state? -- https://mail.python.org/mailman/listinfo/python-list
Help on for loop understanding
Hi, When I learn for loop with below link: http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html it has such explanation: \ for loop under the hood First let's look at the for loop under the hood. When Python executes the for loop, it first invokes the __iter__() method of the container to get the iterator of the container. It then repeatedly calls the next() method (__next__() method in Python 3.x) of the iterator until the iterator raises a StopIteration exception. Once the exception is raised, the for loop ends. \ When I follow a list example from the above link, and one example of myself: // xx=[1,2,3,4,5] xx.next --- AttributeErrorTraceback (most recent call last) in () > 1 xx.next AttributeError: 'list' object has no attribute 'next' xx.__iter__ Out[77]: for c in xx: print c 1 2 3 4 5 // I am puzzled that the list examples have no next method, but it can run as a for loop. Could you explain it to me the difference? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
Am 08.12.2015 um 02:05 schrieb Robert: Hi, When I learn for loop with below link: http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html it has such explanation: \ for loop under the hood First let's look at the for loop under the hood. When Python executes the for loop, it first invokes the __iter__() method of the container to get the iterator of the container. It then repeatedly calls the next() method (__next__() method in Python 3.x) of the iterator until the iterator raises a StopIteration exception. Once the exception is raised, the for loop ends. \ When I follow a list example from the above link, and one example of myself: // xx=[1,2,3,4,5] xx.next --- AttributeErrorTraceback (most recent call last) in () > 1 xx.next AttributeError: 'list' object has no attribute 'next' xx.__iter__ Out[77]: for c in xx: print c 1 2 3 4 5 // I am puzzled that the list examples have no next method, but it can run as a for loop. Could you explain it to me the difference? Lists don't have a next method. Their iterators have: xx.__iter__().__next__() or xxIterator = xx.__iter__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() That's also what your quoted paragraph states: | It then repeatedly calls the next() method | (__next__() method in Python 3.x) of the iterator -- Robin Koch -- https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote: > Am 08.12.2015 um 02:05 schrieb Robert: > > Hi, > > When I learn for loop with below link: > > > > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html > > > > it has such explanation: > > > > \ > > for loop under the hood > > > > First let's look at the for loop under the hood. When Python executes the > > for loop, it first invokes the __iter__() method of the container to get > > the > > iterator of the container. It then repeatedly calls the next() method > > (__next__() method in Python 3.x) of the iterator until the iterator > > raises a > > StopIteration exception. Once the exception is raised, the for loop ends. > > \ > > > > When I follow a list example from the above link, and one example of myself: > > > > // > > xx=[1,2,3,4,5] > > > > xx.next > > --- > > AttributeErrorTraceback (most recent call last) > > in () > > > 1 xx.next > > > > AttributeError: 'list' object has no attribute 'next' > > > > xx.__iter__ > > Out[77]: > > > > for c in xx: print c > > 1 > > 2 > > 3 > > 4 > > 5 > > // > > > > I am puzzled that the list examples have no next method, but it can run as > > a for loop. Could you explain it to me the difference? > > Lists don't have a next method. Their iterators have: > > xx.__iter__().__next__() > > or > > xxIterator = xx.__iter__() > xxIterator.__next__() > xxIterator.__next__() > xxIterator.__next__() > xxIterator.__next__() > xxIterator.__next__() > > That's also what your quoted paragraph states: > > | It then repeatedly calls the next() method > | (__next__() method in Python 3.x) of the iterator > > -- > Robin Koch I use Python 2.7. I have tried these commands: xx=[1,2,3,4,5] xx.__iter__ Out[2]: xx.__iter__().__next__() --- AttributeErrorTraceback (most recent call last) in () > 1 xx.__iter__().__next__() AttributeError: 'listiterator' object has no attribute '__next__' xx.__iter__.__next__ --- AttributeErrorTraceback (most recent call last) in () > 1 xx.__iter__.__next__ AttributeError: 'method-wrapper' object has no attribute '__next__' xx.__iter__() Out[5]: xx.__iter__().__next__ --- AttributeErrorTraceback (most recent call last) in () > 1 xx.__iter__().__next__ AttributeError: 'listiterator' object has no attribute '__next__' xxIterator = xx.__iter__() xxIterator Out[8]: xxIterator.__next__() --- AttributeErrorTraceback (most recent call last) in () > 1 xxIterator.__next__() AttributeError: 'listiterator' object has no attribute '__next__' for c in xx: print c 1 2 3 4 5 I don't find a way to show __next__ yet. Can we explicitly get the iterator for a list? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
On Monday, December 7, 2015 at 8:32:30 PM UTC-5, Robert wrote: > On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote: > > Am 08.12.2015 um 02:05 schrieb Robert: > > > Hi, > > > When I learn for loop with below link: > > > > > > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html > > > > > > it has such explanation: > > > > > > \ > > > for loop under the hood > > > > > > First let's look at the for loop under the hood. When Python executes the > > > for loop, it first invokes the __iter__() method of the container to > > > get the > > > iterator of the container. It then repeatedly calls the next() method > > > (__next__() method in Python 3.x) of the iterator until the iterator > > > raises a > > > StopIteration exception. Once the exception is raised, the for loop > > > ends. > > > \ > > > > > > When I follow a list example from the above link, and one example of > > > myself: > > > > > > // > > > xx=[1,2,3,4,5] > > > > > > xx.next > > > --- > > > AttributeErrorTraceback (most recent call > > > last) > > > in () > > > > 1 xx.next > > > > > > AttributeError: 'list' object has no attribute 'next' > > > > > > xx.__iter__ > > > Out[77]: > > > > > > for c in xx: print c > > > 1 > > > 2 > > > 3 > > > 4 > > > 5 > > > // > > > > > > I am puzzled that the list examples have no next method, but it can run as > > > a for loop. Could you explain it to me the difference? > > > > Lists don't have a next method. Their iterators have: > > > > xx.__iter__().__next__() > > > > or > > > > xxIterator = xx.__iter__() > > xxIterator.__next__() > > xxIterator.__next__() > > xxIterator.__next__() > > xxIterator.__next__() > > xxIterator.__next__() > > > > That's also what your quoted paragraph states: > > > > | It then repeatedly calls the next() method > > | (__next__() method in Python 3.x) of the iterator > > > > -- > > Robin Koch > > I use Python 2.7. I have tried these commands: > > xx=[1,2,3,4,5] > > xx.__iter__ > Out[2]: > > xx.__iter__().__next__() > --- > AttributeErrorTraceback (most recent call last) > in () > > 1 xx.__iter__().__next__() > > AttributeError: 'listiterator' object has no attribute '__next__' > > xx.__iter__.__next__ > --- > AttributeErrorTraceback (most recent call last) > in () > > 1 xx.__iter__.__next__ > > AttributeError: 'method-wrapper' object has no attribute '__next__' > > xx.__iter__() > Out[5]: > > xx.__iter__().__next__ > --- > AttributeErrorTraceback (most recent call last) > in () > > 1 xx.__iter__().__next__ > > AttributeError: 'listiterator' object has no attribute '__next__' > > xxIterator = xx.__iter__() > > xxIterator > Out[8]: > > xxIterator.__next__() > --- > AttributeErrorTraceback (most recent call last) > in () > > 1 xxIterator.__next__() > > AttributeError: 'listiterator' object has no attribute '__next__' > > for c in xx: print c > 1 > 2 > 3 > 4 > 5 > > > I don't find a way to show __next__ yet. > Can we explicitly get the iterator for a list? > Thanks, Excuse me. I find it as the following: xx.__iter__().next Out[16]: xx.__iter__().next() Out[17]: 1 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
Hi Robert,
On 08/12/15 01:39, Robert wrote:
I don't find a way to show __next__ yet.
Can we explicitly get the iterator for a list?
Thanks,
Excuse me. I find it as the following:
xx.__iter__().next
Out[16]:
xx.__iter__().next()
Out[17]: 1
Robin has told you how things work under the hood for the particular
version of Python that he is running (Python 3). As you've seen, it
works a bit different under the hood in your version (Python 2).
This is why you should not be calling the __ ("dunder") methods
directly. Until you understand more and want to write your own classes
that support being iterated using the 'for' keyword, you should probably
ignore them.
Instead, the way to do this which works on all versions is:
x = [1, 2, 3, 4]
xit = iter(x)
next(xit)
next(xit)
next(xit)
next(xit)
next(xit)
This is what the 'for' keyword is doing for you - it first gets an
iterator for the list (using iter()) and then processes each element
that the iterator returns (from next()) until it raises the exception.
It then suppresses the exception (so you don't have to catch it
yourself) and exits the for loop.
Of course, it might actually do this using the __ methods and other
things as a shortcut internally, but that's just an implementation detail.
E.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
On Monday, December 7, 2015 at 8:39:48 PM UTC-5, Robert wrote: > On Monday, December 7, 2015 at 8:32:30 PM UTC-5, Robert wrote: > > On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote: > > > Am 08.12.2015 um 02:05 schrieb Robert: > > > > Hi, > > > > When I learn for loop with below link: > > > > > > > > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html > > > > > > > > it has such explanation: > > > > > > > > \ > > > > for loop under the hood > > > > > > > > First let's look at the for loop under the hood. When Python executes > > > > the > > > > for loop, it first invokes the __iter__() method of the container to > > > > get the > > > > iterator of the container. It then repeatedly calls the next() method > > > > (__next__() method in Python 3.x) of the iterator until the iterator > > > > raises a > > > > StopIteration exception. Once the exception is raised, the for loop > > > > ends. > > > > \ > > > > > > > > When I follow a list example from the above link, and one example of > > > > myself: > > > > > > > > // > > > > xx=[1,2,3,4,5] > > > > > > > > xx.next > > > > --- > > > > AttributeErrorTraceback (most recent call > > > > last) > > > > in () > > > > > 1 xx.next > > > > > > > > AttributeError: 'list' object has no attribute 'next' > > > > > > > > xx.__iter__ > > > > Out[77]: > > > 0x0A1ACE08> > > > > > > > > for c in xx: print c > > > > 1 > > > > 2 > > > > 3 > > > > 4 > > > > 5 > > > > // > > > > > > > > I am puzzled that the list examples have no next method, but it can run > > > > as > > > > a for loop. Could you explain it to me the difference? > > > > > > Lists don't have a next method. Their iterators have: > > > > > > xx.__iter__().__next__() > > > > > > or > > > > > > xxIterator = xx.__iter__() > > > xxIterator.__next__() > > > xxIterator.__next__() > > > xxIterator.__next__() > > > xxIterator.__next__() > > > xxIterator.__next__() > > > > > > That's also what your quoted paragraph states: > > > > > > | It then repeatedly calls the next() method > > > | (__next__() method in Python 3.x) of the iterator > > > > > > -- > > > Robin Koch > > > > I use Python 2.7. I have tried these commands: > > > > xx=[1,2,3,4,5] > > > > xx.__iter__ > > Out[2]: > > > > xx.__iter__().__next__() > > --- > > AttributeErrorTraceback (most recent call last) > > in () > > > 1 xx.__iter__().__next__() > > > > AttributeError: 'listiterator' object has no attribute '__next__' > > > > xx.__iter__.__next__ > > --- > > AttributeErrorTraceback (most recent call last) > > in () > > > 1 xx.__iter__.__next__ > > > > AttributeError: 'method-wrapper' object has no attribute '__next__' > > > > xx.__iter__() > > Out[5]: > > > > xx.__iter__().__next__ > > --- > > AttributeErrorTraceback (most recent call last) > > in () > > > 1 xx.__iter__().__next__ > > > > AttributeError: 'listiterator' object has no attribute '__next__' > > > > xxIterator = xx.__iter__() > > > > xxIterator > > Out[8]: > > > > xxIterator.__next__() > > --- > > AttributeErrorTraceback (most recent call last) > > in () > > > 1 xxIterator.__next__() > > > > AttributeError: 'listiterator' object has no attribute '__next__' > > > > for c in xx: print c > > 1 > > 2 > > 3 > > 4 > > 5 > > > > > > I don't find a way to show __next__ yet. > > Can we explicitly get the iterator for a list? > > Thanks, > > Excuse me. I find it as the following: > > xx.__iter__().next > Out[16]: > > xx.__iter__().next() > Out[17]: 1 One example, see below please, is in the above mentioned link. I don't see the purpose of the example. \\\ class MyList(list): def __iter__(self): return MyListIter(self) class MyListIter(object): """ A sample implementation of a list iterator. NOTE: This is just a demonstration of concept!!! YOU SHOULD NEVER IMPLEMENT SOMETHING LIKE THIS! Even if you have to (for any reason), there are many better ways to implement this.""" def __init__(self, lst): self.lst = lst self.i = -1 def __iter__(self): return self def next(self): if self.ihttps://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
On 08/12/15 01:50, Robert wrote:
One example, see below please, is in the above mentioned link. I don't see
the purpose of the example.
OK, so there are two parts to this.
The first, is "how do I iterate over something". The answer to that is
using "for" or using "iter()" followed by zero or more calls to
"next()". In this case, by using the correct syntax/function calls your
script can work under various versions of Python without change.
The second is "how do I make my own classes iterable". This is what the
example you pasted is trying to show. In this case, you are implementing
things which are "internal" to the way the version of Python you are
using does things (which is why the code Robin posted wasn't quite right
for you).
I have the following two same results. What do they tell me?
They tell you that the implementation does the same thing as the default
implementation for a list. That perhaps doesn't help much - especially
with the comment in the example telling you not to do it!
Instead, try the following (Python 2):
class MyList(list):
def __iter__(self):
return MyListIter(self)
class MyListIter(object):
def __init__(self, lst):
self.lst = lst
self.i = -1
def __iter__(self):
return self
def next(self):
if self.i >= -len(self.lst):
item = self.lst[self.i]
self.i -= 1
return item
raise StopIteration
if __name__ == "__main__":
a = MyList([1, 2, 3, 4])
ia = iter(a)
print 'type(a): %r, type(ia): %r' %(type(a), type(ia))
for i in a:
print i,
print
while True:
print next(ia)
What we have here is the same class that subclasses 'list'. It's just a
list. However, it has a custom iterator. In this implementation the
iterator works BACKWARDS through the list - the final element is
returned first, the penultimate element second, and so on. After the
first element has been returned, the iterator raises StopIteration. This
tells you not to call next() again, or if in a for loop, the loop is exited.
So, you can write your class's iterator to do anything that makes sense
when someone says "for i in myclassinstance:".
If your class is a subclass of a class ("is-a") that already has a
defined iterator (such as a list or a dict) and the behaviour of that is
correct for you, then you need to do nothing (you inherit that class's
__iter__() method).
If your class should iterate over an embedded object ("has-a") that
already has a defined iterator, then your __iter__() method can just
delegate to that object's iterator using something like:
def __iter__(self):
return iter(self.embedded_thing)
Does that make more sense?
E.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help on for loop understanding
On Tue, Dec 8, 2015 at 1:36 PM, Erik wrote:
> So, you can write your class's iterator to do anything that makes sense when
> someone says "for i in myclassinstance:".
>
> If your class is a subclass of a class ("is-a") that already has a defined
> iterator (such as a list or a dict) and the behaviour of that is correct for
> you, then you need to do nothing (you inherit that class's __iter__()
> method).
>
> If your class should iterate over an embedded object ("has-a") that already
> has a defined iterator, then your __iter__() method can just delegate to
> that object's iterator using something like:
>
> def __iter__(self):
> return iter(self.embedded_thing)
Another great way to write an __iter__ method is as a generator.
def __iter__(self):
yield "thing"
yield from self.things
yield "other thing"
Like returning an embedded object's iterator, this saves you having to
write a __next__ method. The less work you do, the less bugs you get.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 12:00 PM, wrote: > Ah, I was confused that process state somehow allowed Python to support > per-connection state without using sessions (which lead me to ask about > websockets). I guess Python could do it without storing the session ID in a > file or database and instead in process state (i.e. application memory, > right?), but the effect seems basically the same. Without being > per-connection, what is the value of process state? > Caches and stuff. For example, you might establish a database connection, and then hang onto it for all queries; or maybe you load up a bunch of user objects and keep them in memory for when you need them again. Anything that you store in a global (module-level) variable will hang around. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
manually build a unittest/doctest object.
If I have a string that is python code, for example
mycode = "print('hello world')"
myresult = "hello world"
How can a "manually" build a unittest (doctest) and test I get myresult
I have attempted to build a doctest but that is not working.
e = doctest.Example(source="print('hello world')/n", want="hello world\n")
t = doctest.DocTestRunner()
t.run(e)
Thanks
Vincent Davis
--
https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On 2015-12-08 10:09, Chris Angelico wrote: > All three are very different. > > 1) Process state. > > You start up a Python program, and it sits there waiting for a > request. You give it a request, and get back a response; it goes > back to waiting for a request. If you change a global variable, or > maintain persistent state, or anything, the next request will 'see' > that change. This is completely global. 1) This is completely global *to the process* (you can have multiple Python processes sitting around waiting, taking advantage of multiple cores) 2) This is almost always a bad idea for multiple reasons (it can get in the way of scaling, it can produce hard-to-track-down bugs, etc). Use a real session store (a database, a key/value store like memcached, a NoSQL store like Redis, store session info in cookies, etc.) instead. -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On Tue, Dec 8, 2015 at 2:11 PM, Tim Chase wrote: > On 2015-12-08 10:09, Chris Angelico wrote: >> All three are very different. >> >> 1) Process state. >> >> You start up a Python program, and it sits there waiting for a >> request. You give it a request, and get back a response; it goes >> back to waiting for a request. If you change a global variable, or >> maintain persistent state, or anything, the next request will 'see' >> that change. This is completely global. > > 1) This is completely global *to the process* (you can have multiple > Python processes sitting around waiting, taking advantage of multiple > cores) > > 2) This is almost always a bad idea for multiple reasons (it can get > in the way of scaling, it can produce hard-to-track-down bugs, etc). > Use a real session store (a database, a key/value store like > memcached, a NoSQL store like Redis, store session info in cookies, > etc.) instead. If your goal is session state, then yes - use something that actually persists past the process life. But for caches and stuff, where dropping them has performance implications but nothing else, it makes good sense to keep them in globals. Particularly if you simply populate the cache on process load and then never lose it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
On 2015-12-08 08:40, Chris Angelico wrote: > One advantage of this kind of setup is that your URLs don't depend > on your back end. I could replace all this with a Ruby on Rails > site, and nobody would notice the difference. I could put together > something using Node.js to replace the Ruby site, and continue to > provide content at http://minstrelhall.com/campaign/6 Of course, this assumes that your Ruby/Node.js skills allow you to create a site that's as robust as Python. Trust me...if I were to swap out my Python web code with Ruby or Node.js, it would be pretty noticeable. Not because of URL issues, but because I don't do Ruby/Node.js so it would fail pretty spectacularly. :-D -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding Python from a PHP coder's perspective
Based on your responses, it is my understanding that process-state might offer some efficiency benefits, but it is typically not a driving factor. If I misunderstand, please advise. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: manually build a unittest/doctest object.
On Tuesday 08 December 2015 14:30, Vincent Davis wrote:
> If I have a string that is python code, for example
> mycode = "print('hello world')"
> myresult = "hello world"
> How can a "manually" build a unittest (doctest) and test I get myresult
Not easily. Effectively, you would have to re-invent the doctest module and
re-engineer it to accept a completely different format.
But if you are willing to write your tests in doctest format, this might
help you:
import doctest
def rundoctests(text, name='', globs=None, verbose=None,
report=True, optionflags=0, extraglobs=None,
raise_on_error=False,
quiet=False,):
# Assemble the globals.
if globs is None:
globs = globals()
globs = globs.copy()
if extraglobs is not None:
globs.update(extraglobs)
if '__name__' not in globs:
globs['__name__'] = '__main__'
# Parse the text looking for doc tests.
parser = doctest.DocTestParser()
test = parser.get_doctest(text, globs, name, name, 0)
# Run the tests.
if raise_on_error:
runner = doctest.DebugRunner(
verbose=verbose, optionflags=optionflags)
else:
runner = doctest.DocTestRunner(
verbose=verbose, optionflags=optionflags)
if quiet:
runner.run(test, out=lambda s: None)
else:
runner.run(test)
if report:
runner.summarize()
# Return a (named, if possible) tuple (failed, attempted).
a, b = runner.failures, runner.tries
try:
TestResults = doctest.TestResults
except AttributeError:
return (a, b)
return TestResults(a, b)
Then call rundoctests(text) to run any doc tests in text. By default, if
there are no errors, it prints nothing. If there are errors, it prints the
failing tests. Either way, it returns a tuple
(number of failures, number of tests run)
Examples in use:
py> good_code = """
... >>> import math
... >>> print "Hello World!"
... Hello World!
... >>> math.sqrt(100)
... 10.0
...
... """
py> rundoctests(good_code)
TestResults(failed=0, attempted=3)
py> bad_code = """
... >>> print 10
... 11
... """
py> rundoctests(bad_code)
**
File "", line 2, in
Failed example:
print 10
Expected:
11
Got:
10
**
1 items had failures:
1 of 1 in
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help with stale exception Python
[email protected] writes: > ... > I have tried uncountable number of methods (e.g. explicit, implicit wait) but > the stale error still persists as it seems to stays stale as long as it is > staled. > > Have anyone come up with a solution to this and what is the best way to deal > with DOM tree changes. > ... > with open('C:/Python34/email.csv','w') as f: > z=csv.writer(f, delimiter='\t',lineterminator = '\n',) > while True: > row = [] > for link in > driver.find_elements_by_xpath("//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position() > = 1 or position() = 2 or position() = 3]"): The "find_elements_by_xpath" likely gives you a list of elements on the *initial* page. The "for" sets things up that this list is iterated over. > try: > ... > c=driver.find_element_by_id('detail-pagination-next-btn') > ... > c.click() > ... > except StaleElementReferenceException as e: This "click" may change the page which means that you would now be on a page different from the initial page. It is likely that your web access framework contains some form of automatic garbage collection: when you switch to a new page, references to the old page may become stale. This could explain that you sometimes observe a "StaleElementReferenceException". Read the documentation of your web access framework to find out whether you can control stalelifying of elements when you switch a page. If this is impossible, avoid accessing elements of a page once you have switched to a new page. To this end, extract (and store) all relevant information from the page before you switch to a new one and, if necessary, use the extracted information to later restore the previous page state. As you can see from my explanation: your question is much more related to your web access framework than to Python in general. Likely, there is a forum (dedicated to this framework) that can better help you with this question than this general Python list. -- https://mail.python.org/mailman/listinfo/python-list
Re: writing an email.message.Message in UTF-8
Adam Funk writes:
> I'm trying to write an instance of email.message.Message, whose body
> contains unicode characters, to a UTF-8 file. (Python 2.7.3 & 2.7.10
> again.)
>
> reply = email.message.Message()
> reply.set_charset('utf-8')
> ... # set various headers
> reply.set_payload('\n'.join(body_lines) + '\n')
> ...
> outfile = codecs.open(outfilename, 'w', encoding='utf-8', errors='ignore')
> outfile.write(reply.as_string())
> outfile.close()
>
> Then reply.as_string() barfs a UnicodeDecodeError. I look in the
> documentation, which says the generator is better. So I replace the
> outfile.write(...) line with the following:
>
> g = email.generator.Generator(outfile, mangle_from_=False)
> g.flatten(reply)
>
> which still barfs a UnicodeDecodeError. Looking closer at the first
> error, I see that the exception was in g.flatten(...) already & thrown
> up to reply.as_string(). How can I force the thing to do UTF-8
> output?
You could try replacing "reply.set_payload('\n'.join(body_lines) + '\n')"
by "reply.set_payload(('\n'.join(body_lines) + '\n').encode('utf-8'))",
i.e. you would not pass in a unicode payload but an "utf-8" encode
"str" payload.
--
https://mail.python.org/mailman/listinfo/python-list
