Re: [Tutor] error_connection_refused

2012-06-06 Thread BILAL Mustapha

Hello,

 Thank you for your reply.

 Please read underlines.

Le 05/06/2012 23:12, Alan Gauld a écrit :

On 05/06/12 12:53, BILAL Mustapha wrote:

Hello,


Hi, Its best not to hijack somebody else's thread coz it messes up all 
the threaded mail/news readers out there. (At least you changed the 
subject though!). Please start with a fresh mail for a new topic.




Oops, sorry I didn't pay attention to it..

I am trying to lunch a small python program but I am always getting this
error:


How are you "launching" it? From a batch file or shell script?
From another Python program? From the Python prompt?


s.connect((IP, PORT))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

What's weird that it doesn't even print the line "test sending data" so
how it could get to s.connect() without passing through this line!!


It sounds like you might be using  a Python >>> prompt and not 
reloading the module after making changes. importing a second time 
won't work as Python knows you already have it and ignores it. You 
must use reload() for changes to take effect.


If thats not it then we need more details about what you are doing.

I actually found the solution which was somehow trivial, I was not 
listening to an open port so I changed the port and it worked fine.


Thank you again

Regards

HTH,



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii?

2012-06-06 Thread Marc Tompkins
On Tue, Jun 5, 2012 at 11:22 PM, Stefan Behnel  wrote:

> You can do this:
>
>connection = urllib2.urlopen(url)
>tree = etree.parse(connection, my_html_parser)
>
> Alternatively, use fromstring() to parse from strings:
>
>page = urllib2.urlopen(url)
>pagecontents = page.read()
> html_root = etree.fromstring(pagecontents, my_html_parser)
>
>
Thank you!  fromstring() did the trick for me.

Interestingly, your first suggestion - parsing straight from the connection
without an intermediate read() - appears to create the tree successfully,
but my first strip_tags() fails, with the error "ValueError: Input object
has no document: lxml.etree._ElementTree".  Since fromstring() works just
fine, I will set this aside as a mystery for my copious free time (after
this project is done, for example.)



> See the lxml tutorial.

I did - I've been consulting it religiously - but I missed the fact that I
was mixing strings with file-like IO, and (as you mentioned) the error
message really wasn't helping me figure out my problem.  Perhaps I should
have figured it out from the fact that the character value and position
change, even though the webpage doesn't... but no.


> Also note that there's lxml.html, which provides an
> extended tool set for HTML processing.
>

I've been using lxml.etree because I'm used to the syntax, and because
(perhaps mistakenly) I was under the impression that its parser was more
resilient in the face of broken HTML - this page has unclosed tags all over
the place.  I'll try lxml.html, but (again) it'll have to be some time
later.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] connecting to USB

2012-06-06 Thread BILAL Mustapha

Hello,

 I am working to connect to a serial link (USB). In order to do that I 
have to write this command:


sudo ../../tools/stm32w/serialdump-linux -b115200 -d1 (keeping in 
mind dat*serialdump-linux*, which is in the path, is not a folder, it's 
an executable)


To get to the root, I have used: returncode = 
subprocess.call(["/usr/bin/sudo", "/usr/bin/id"])


Then to connect to the USB through the command above I tried the following:

usb = 
Popen(['../../tools/stm32w/serialdump-linux','-b115200','-d1'], 
stdout=PIPE, stdin=PIPE, stderr=STDOUT)


usb.communicate()[0]

 But I am getting an error, it's considering that serialdump-linux as a 
folder!!


 Any help how I can run the command?

 Many thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii?

2012-06-06 Thread Stefan Behnel
Marc Tompkins, 06.06.2012 10:21:
> On Tue, Jun 5, 2012 at 11:22 PM, Stefan Behnel wrote:
> 
>> You can do this:
>>
>>connection = urllib2.urlopen(url)
>>tree = etree.parse(connection, my_html_parser)
>>
>> Alternatively, use fromstring() to parse from strings:
>>
>>page = urllib2.urlopen(url)
>>pagecontents = page.read()
>> html_root = etree.fromstring(pagecontents, my_html_parser)
>>
>>
> Thank you!  fromstring() did the trick for me.
> 
> Interestingly, your first suggestion - parsing straight from the connection
> without an intermediate read() - appears to create the tree successfully,
> but my first strip_tags() fails, with the error "ValueError: Input object
> has no document: lxml.etree._ElementTree".

Weird. You may want to check the parser error log to see if it has any hint.


>> See the lxml tutorial.
> 
> I did - I've been consulting it religiously - but I missed the fact that I
> was mixing strings with file-like IO, and (as you mentioned) the error
> message really wasn't helping me figure out my problem.

Yes, I think it could do better here. Reporting a parser error with an
"unprintable error message" would at least make it less likely that users
are being diverted from the actual cause of the problem.


>> Also note that there's lxml.html, which provides an
>> extended tool set for HTML processing.
> 
> I've been using lxml.etree because I'm used to the syntax, and because
> (perhaps mistakenly) I was under the impression that its parser was more
> resilient in the face of broken HTML - this page has unclosed tags all over
> the place.

Both are using the same parser and share most of their API. lxml.html is
mostly just an extension to lxml.etree with special HTML tools.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] connecting to USB

2012-06-06 Thread BILAL Mustapha

Le 06/06/2012 11:02, BILAL Mustapha a écrit :

Hello,

 I am working to connect to a serial link (USB). In order to do that I 
have to write this command:


sudo ../../tools/stm32w/serialdump-linux -b115200 -d1 (keeping in 
mind dat*serialdump-linux*, which is in the path, is not a folder, 
it's an executable)


To get to the root, I have used: returncode = 
subprocess.call(["/usr/bin/sudo", "/usr/bin/id"])


Then to connect to the USB through the command above I tried the 
following:


usb = 
Popen(['../../tools/stm32w/serialdump-linux','-b115200','-d1'], 
stdout=PIPE, stdin=PIPE, stderr=STDOUT)


usb.communicate()[0]

 But I am getting an error, it's considering that serialdump-linux as 
a folder!!


 Any help how I can run the command?

 Many thanks


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

I resolved the issue, you can do the following:

First method:
args = ("sudo", "../../tools/stm32w/serialdump-linux", "-b115200", 
"-d1")

usb = Popen(args, stdout=PIPE, stdin=PIPE)
usb.wait()
usb.communicate()[0]

Second method:
cmd = 'sudo ../../tools/stm32w/serialdump-linux -b115200 -d1'
os.system(cmd)

you can run it using cmd and os or using Popen if you need to use stdout 
and stdin in your application (which is my case)


PS. -b115200 -d1 are two options related to USB so you can change 
the whole command and use any option you would like to.


Regards

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 Static Version and Postgresql

2012-06-06 Thread Opher Lubzens
On Tue, May 29, 2012 at 9:26 PM, Opher Lubzens  wrote:
>
> That actually might be workable- I'll have to look into it. I'll have
> to understand them better then I do after a brief look in their
> webpages and check whether they're compatible with our specific flavor
> of OS, since both of these are Windows oriented according to their
> pages, but this may be a good answer for what I need.
>
> Thank you for your help,
> Opher Lubzens

Just reporting how I solved this: Turns out that a static bundle will
identify a built library if you copy the library dir to the same
directory as the bundle. So I used this to copy a pg8000 library I
built in Ubuntu into the same directory after disabling the SSL
dependency, since our OS doesn't have some of the .so files that the
ssl library needs, and my script won't use SSL connection anyway since
it operates from within the server.

Thanks for the help,
Opher Lubzens
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] special attributes naming confusion

2012-06-06 Thread Dave
I was reading some tutorial material on creating iterators. It shows the
following example implementation of an iterator:

class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def next(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]


My question is how was I supposed to kinow that the function I call using
the name iter() is implemented using the name __iter__()?

Is there a rule that describes when I would implement an attribute name
with leading and trailing double underscores, and then call it without
those underscores? How many names like this exist in Python? Are these
special cases or is there a general rule that leading and trailing double
underscores get dropped when calling functions that were implemented with
these names? I'm trying to understand the big picture as far as how Python
works when it comes to this situation. Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sqlite3 Question

2012-06-06 Thread Khalid Al-Ghamdi
Hi All,

hi Joel,

Regarding sqlite3 practice code below:


   1. import sqlite3
   2. conn=sqlite3.connect("mydb2.db")
   3. c=conn.cursor()
   4. c.execute('create table family (Id integer primary key, name text,
   age integer)')
   5. c.execute("insert into family values (1,'elham',32)")
   6. c.execute('select * from family')
   7. conn.commit()
   8. for i in c:
   9. print(i)


Why is it that if I call the for statement in the shell it doesn't print
the information more than once? I've found this to be true in other
situations where cursor carries the data only once. So, how can i
manipulate/use the data if I can only access it once? I have tried applying
useing x=list(c), but it doesn't seem to work!

Any guidance?

Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> My question is how was I supposed to kinow that the function I call using the
> name iter() is implemented using the name __iter__()?
> 
> Is there a rule that describes when I would implement an attribute name with
> leading and trailing double underscores, and then call it without those
> underscores? How many names like this exist in Python? Are these special cases
> or is there a general rule that leading and trailing double underscores get
> dropped when calling functions that were implemented with these names? I'm
> trying to understand the big picture as far as how Python works when it comes
> to this situation. Thanks.

They are listed here 
http://docs.python.org/reference/datamodel.html#specialnames 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Searching 2 Strings in A File

2012-06-06 Thread dohoang4093
i am writing a python script that will be invoked as follows: 

myScript.py  

and the script is as follwos: 

x = "Device " + sys.argv[1] + " restored the database" 
y = "Created connection to " + sys.argv[1] 

cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, 
stderr=STDOUT) 

the above code does not work because it will look for x and y in aLogFile 
instead of 
looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created 
connection to " + sys.argv[1]> 
Any hint is appreciated. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 Question

2012-06-06 Thread Prasad, Ramit
> 
> 1. import sqlite3
> 2. conn=sqlite3.connect("mydb2.db")
> 3. c=conn.cursor()
> 4. c.execute('create table family (Id integer primary key, name text, age
> integer)')
> 5. c.execute("insert into family values (1,'elham',32)")
> 6. c.execute('select * from family')
> 7. conn.commit()
> 8. for i in c:
> 9. print(i)
> 
> Why is it that if I call the for statement in the shell it doesn't print the
> information more than once? I've found this to be true in other situations
> where cursor carries the data only once. So, how can i manipulate/use the data
> if I can only access it once? I have tried applying useing x=list(c), but it
> doesn't seem to work!

The cursor only remembers the last action so you need to get the results 
before reusing the cursor. If you want to keep the data in memory you can use 
data = c.fetchall()# or c.fetchmany(num) or c.fetchone()


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Mark Lawrence

On 06/06/2012 20:19, Dave wrote:

I was reading some tutorial material on creating iterators. It shows the
following example implementation of an iterator:

class Reverse:
 """Iterator for looping over a sequence backwards."""
 def __init__(self, data):
 self.data = data
 self.index = len(data)
 def __iter__(self):
 return self
 def next(self):
 if self.index == 0:
 raise StopIteration
 self.index = self.index - 1
 return self.data[self.index]


My question is how was I supposed to kinow that the function I call using
the name iter() is implemented using the name __iter__()?

Is there a rule that describes when I would implement an attribute name
with leading and trailing double underscores, and then call it without
those underscores? How many names like this exist in Python? Are these
special cases or is there a general rule that leading and trailing double
underscores get dropped when calling functions that were implemented with
these names? I'm trying to understand the big picture as far as how Python
works when it comes to this situation. Thanks.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Try this to start with 
http://docs.python.org/reference/datamodel.html#special-method-names. 
Note this is for Python 2.7.3, there may be differences in Python 3.x.


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching 2 Strings in A File

2012-06-06 Thread Dave Angel
On 06/06/2012 03:33 PM, dohoang4...@comcast.net wrote:
> i am writing a python script that will be invoked as follows: 
>
> myScript.py  
>
> and the script is as follwos: 
>
> x = "Device " + sys.argv[1] + " restored the database" 
> y = "Created connection to " + sys.argv[1] 
>
> cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, 
> stderr=STDOUT) 
>
> the above code does not work because it will look for x and y in aLogFile 
> instead of 
> looking for <"Device " + sys.argv[1] + " restored the database"> and 
> <"Created connection to " + sys.argv[1]> 
> Any hint is appreciated. 
>
I'm not commenting on egrep and its parsing rules, but when you are
passing a list to Popen(), each item is a string.  Your second item is
"(x,y)"  when you want something like <"Device...
You have the word  "and" in there but I have no idea what you mean by
it.  Are there two arguments to egrep there, or one?  I'll assume one
for now.

So build another variable called arg1, and make it right.  The cmd_line
will then just be

  cmd_line = Popen(["egrep", arg1, aLogFile], stdout=PIPE, stdin=PIPE, 
stderr=STDOUT) 


arg1 *might* just be something like
arg1 = x+y

but I seriously doubt it.  You'll probably be adding quotes, and maybe
even backslashes and other stuff to the mix.

If you showed us exactly what you're expecting the commandline of egrep
to look like, we might be able to come up with an answer.  But you
definitely don't want to try to do it all in one line.

Pick something simple and get that working, then make it more complex
till it meets your requirements.

-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
On Wed, Jun 6, 2012 at 3:30 PM, Prasad, Ramit wrote:

> > My question is how was I supposed to kinow that the function I call
> using the
> > name iter() is implemented using the name __iter__()?
> >
> > Is there a rule that describes when I would implement an attribute name
> with
> > leading and trailing double underscores, and then call it without those
> > underscores? How many names like this exist in Python? Are these special
> cases
> > or is there a general rule that leading and trailing double underscores
> get
> > dropped when calling functions that were implemented with these names?
> I'm
> > trying to understand the big picture as far as how Python works when it
> comes
> > to this situation. Thanks.
>
> They are listed here
> http://docs.python.org/reference/datamodel.html#specialnames
>
> Ramit
>
>
Thank you. That's a good start. It appears to answer half my question. It
tells me about special names like __new__, __init__, etc. And there is also
mention of __iter__(self) on that page too. But I don't see any discussion
of the convention regarding mappings from those names to the typical names
used to call the functions in code. Unless I'm overlooking it, that page
doesn't explain how to generalize the above example where calling the
function by the name iter() actually calls the implementation named
__iter__(). Are the leading and trailing double underscores simply dropped
always? (It doesn't seem that simple because functions like __init__ are
called behind the scenes.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence wrote:

> On 06/06/2012 20:19, Dave wrote:
>
>> I was reading some tutorial material on creating iterators. It shows the
>> following example implementation of an iterator:
>>
>> class Reverse:
>> """Iterator for looping over a sequence backwards."""
>> def __init__(self, data):
>> self.data = data
>> self.index = len(data)
>> def __iter__(self):
>> return self
>> def next(self):
>> if self.index == 0:
>> raise StopIteration
>> self.index = self.index - 1
>> return self.data[self.index]
>>
>>
>> My question is how was I supposed to kinow that the function I call using
>> the name iter() is implemented using the name __iter__()?
>>
>> Is there a rule that describes when I would implement an attribute name
>> with leading and trailing double underscores, and then call it without
>> those underscores? How many names like this exist in Python? Are these
>> special cases or is there a general rule that leading and trailing double
>> underscores get dropped when calling functions that were implemented with
>> these names? I'm trying to understand the big picture as far as how Python
>> works when it comes to this situation. Thanks.
>>
>>
> Try this to start with http://docs.python.org/**reference/datamodel.html#*
> *special-method-names.
> Note this is for Python 2.7.3, there may be differences in Python 3.x.
>
> --
>

Actually, I think I'm getting it now... as I read more of this page I see
that there is no single generalization. These are indeed all special cases.

But the documentation does appear to be incomplete. It leaves out the
mapping to the name or symbol that should be used to call the special
function in some cases. In particular, in the case of __iter(self), which
is one of the first ones I looked at, it doesn't mention that this is
usually called via iter(). It does mention how it would be called for
mapping containers, however (i.e., iterkeys()).
 object.__iter__(*self*)

This method is called when an iterator is required for a container. This
method should return a new iterator object that can iterate over all the
objects in the container. For mappings, it should iterate over the keys of
the container, and should also be made available as the method iterkeys().

But as I read more, I see that much of the documentation does mention how
these special method names are called. For example:

object.__lt__(*self*, *other*) object.__le__(*self*,
*other*)¶
object.__eq__(*self*, *other*) object.__ne__(*self*, *other*) object.__gt__(
*self*, *other*) object.__ge__(*self*, *other*)

New in version 2.1.

These are the so-called “rich comparison” methods, and are called for
comparison operators in preference to
__cmp__()below.
The correspondence between operator symbols and method names is as
follows: xy call x.__ne__(y), x>y calls x.__gt__(y),
and x>=ycalls
x.__ge__(y).
I think there is enough info at this page to answer my question as well as
I need it answered right now. Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching 2 Strings in A File

2012-06-06 Thread Prasad, Ramit
> x = "Device " + sys.argv[1] + " restored the database"
> y = "Created connection to " + sys.argv[1]
> 
> cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE,
> stderr=STDOUT)
> 
> the above code does not work because it will look for x and y in aLogFile
> instead of
> looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created
> connection to " + sys.argv[1]>
> Any hint is appreciated.

Try

pattern = "({0},{1})".format(x, y)
cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE, 
stderr=STDOUT)


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

 

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching 2 Strings in A File

2012-06-06 Thread Prasad, Ramit
> > x = "Device " + sys.argv[1] + " restored the database"
> > y = "Created connection to " + sys.argv[1]
> >
> > cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE,
> > stderr=STDOUT)
> >
> > the above code does not work because it will look for x and y in aLogFile
> > instead of
> > looking for <"Device " + sys.argv[1] + " restored the database"> and
> <"Created
> > connection to " + sys.argv[1]>
> > Any hint is appreciated.
> 
> Try
> 
> pattern = "({0},{1})".format(x, y)
> cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE,
> stderr=STDOUT)

Also, I think it should be '|' in pattern and not ','. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Dave
I'm not sure where this comment belongs, but I want to share my perspective
on the documentation of these special method names. In the following
section there is an inconsistency which could be confusing to someone just
learning Python (e.g., me).

In the sentence on implementing custom mapping objects, the recommended
method names are listed as the short calling names: eys(), values(), items(),
etc.

But, in contrast, in the sentence on implementing sequence types, the
recommended method names are listed as the double underscore internal
implementation names: __add__(), __radd__(), __iadd__(), __mul__(), etc.

Here's the section of the documentation with this inconsistency. I think it
would help to use one or the other of these pairs (calling name vs.
internal implementation name) consistently in this section.

3.4.6. Emulating container
types¶

The following methods can be defined to implement container objects.
Containers usually are sequences (such as lists or tuples) or mappings
(like dictionaries), but can represent other containers as well. The first
set of methods is used either to emulate a sequence or to emulate a
mapping; the difference is that for a sequence, the allowable keys should
be the integers *k* for which 0 <= k < N where *N* is the length of the
sequence, or slice objects, which define a range of items. (For backwards
compatibility, the method
__getslice__()(see
below) can also be defined to handle simple, but not extended slices.)
It is also recommended that mappings provide the methods keys(), values(),
items(), has_key(), get(), clear(), setdefault(), iterkeys(), itervalues(),
iteritems(), pop(), popitem(),
copy(),
and update() behaving similar to those for Python’s standard dictionary
objects. The 
UserDictmodule
provides a
DictMixin class to help create those methods from a base set of
__getitem__(),
__setitem__(),
__delitem__(),
and keys(). Mutable sequences should provide methods append(), count(),
index(), extend(), insert(), pop(), remove(), reverse() and sort(), like
Python standard list objects. Finally, sequence types should implement
addition (meaning concatenation) and multiplication (meaning repetition) by
defining the methods
__add__(),
__radd__() ,
__iadd__() ,
__mul__() ,
__rmul__() and
__imul__() 
described
below; they should not define
__coerce__()or
other numerical operators. It is recommended that both mappings and
sequences implement the
__contains__()method
to allow efficient use of the
in operator; for mappings, in should be equivalent of has_key(); for
sequences, it should search through the values. It is further recommended
that both mappings and sequences implement the
__iter__()method
to allow efficient iteration through the container; for mappings,
__iter__() 
should
be the same as
iterkeys(); for sequences, it should iterate through the values.
This is in addition to some missing documentation regarding mention how
some of the special method names should be conveniently called in code.
(See below.)


On Wed, Jun 6, 2012 at 3:59 PM, Dave  wrote:

>
>
> On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence wrote:
>
>> On 06/06/2012 20:19, Dave wrote:
>>
>>> I was reading some tutorial material on creating iterators. It shows the
>>> following example implementation of an iterator:
>>>
>>> class Reverse:
>>> """Iterator for looping over a sequence backwards."""
>>> def __init__(self, data):
>>> self.data = data
>>> self.index = len(data)
>>> def __iter__(self):
>>> return self
>>> def next(self):
>>> if self.index == 0:
>>> raise StopIteration
>>> self.index = self.index - 1
>>> return self.data[self.index]
>>>
>>>
>>> My question is how was I supposed to kinow that the function I call using
>>> the name iter() is implemented using the name __iter__()?
>>>
>>> Is there a rule that describes whe

Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Jerry Hill
On Wed, Jun 6, 2012 at 4:32 PM, Dave  wrote:
> I'm not sure where this comment belongs, but I want to share my perspective
> on the documentation of these special method names. In the following section
> there is an inconsistency which could be confusing to someone just learning
> Python (e.g., me).
>
> In the sentence on implementing custom mapping objects, the recommended
> method names are listed as the short calling names: eys(), values(),
> items(), etc.
>
> But, in contrast, in the sentence on implementing sequence types, the
> recommended method names are listed as the double underscore internal
> implementation names: __add__(), __radd__(), __iadd__(), __mul__(), etc.
>
> Here's the section of the documentation with this inconsistency. I think it
> would help to use one or the other of these pairs (calling name vs. internal
> implementation name) consistently in this section.

Those aren't pairs, where you can implement either one.  You have to
implement ALL of those methods to correctly emulate one of the built
in container classes.  The difference is that the "normal" method
names (the ones without double underscores) are meant to be called
directly on an instance of your class.  The double underscored names
are called by the internals of python, often in a variety of different
places.

For instance, the __iter__() method is called by the iter() built-in.
It can also called by any other piece of code that needs to acquire an
iterator from a generic container object.  There is no list of all
such pieces of code that could ever call the __iter__() method of your
object.

Jerry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching 2 Strings in A File

2012-06-06 Thread Prasad, Ramit
Please respond back to the list (so others know what worked 
and that the problem is solved).  

:)

> 
> hi all,
> 
> thanks a lot for your quick response.
> 
> Dave, actually it's a 2 arguments.  Sorry, i did not make it clear in my
> question.  I used Ramit's hint and it worked.  The code should be as follows:
> 
> pattern = "({0}|{1})".format(x,y)
> cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE,
> stderr=STDOUT)
> 
> Regards,
> 
> Do Nguyen



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> For instance, the __iter__() method is called by the iter() built-in.
> It can also called by any other piece of code that needs to acquire an
> iterator from a generic container object.  There is no list of all
> such pieces of code that could ever call the __iter__() method of your

A lot of times iter()/__iter__() are used implicitly and not explicitly.
So knowing that iter() specifically calls __iter__ is not as useful as
knowing *when* to implement __iter__(). For example,

for x in obj: 
# this is an implicit call; works for collections or instance that 
# implements __iter__()
pass


Knowing when the special methods are used is more important than 
knowing the "mapping" or what built-in calls it. Most of the times
I do not even use built-ins like iter() explicitly--the exception being
len().

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano

Prasad, Ramit wrote:

For instance, the __iter__() method is called by the iter() built-in.
It can also called by any other piece of code that needs to acquire an
iterator from a generic container object.  There is no list of all
such pieces of code that could ever call the __iter__() method of your


A lot of times iter()/__iter__() are used implicitly and not explicitly.
So knowing that iter() specifically calls __iter__ is not as useful as
knowing *when* to implement __iter__(). For example,

for x in obj: 
# this is an implicit call; works for collections or instance that 
# implements __iter__()

pass


Not just __iter__. For loops work over anything that implements the iterator 
protocol or the sequence protocol.


That is, for loops first try to build an iterator by calling __iter__, and if 
that fails they try the sequence protocol obj[0], obj[1], obj[2], ...


As a general rule, except when you are writing your own classes and defining 
__DoubleUNDERscore__ methods, you should (almost) never need to explicitly use 
such dunder methods.





--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Prasad, Ramit
> That is, for loops first try to build an iterator by calling __iter__, and if
> that fails they try the sequence protocol obj[0], obj[1], obj[2], ...

So...I could instead write __getitem__ for the same effect?

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Joining all strings in stringList into one string

2012-06-06 Thread Steven D'Aprano

Prasad, Ramit wrote:

But more importantly, some years ago (Python 2.4, about 8 years ago?) the
Python developers found a really neat trick that they can do to optimize
string concatenation so it doesn't need to repeatedly copy characters over
and
over and over again. I won't go into details, but the thing is, this trick
works well enough that repeated concatenation is about as fast as the join
method MOST of the time.


I would like to learn a bit more about the trick if you have a 
reference handy. I have no intention of using it, but it sounds 
interesting and might teach me more about Python internals.


In a nutshell, CPython identifies cases like:

mystr = mystr + otherstr
mystr += otherstr

where mystr is not used in any other place, and if possible, resizes mystr in 
place and appends otherstr, rather than copying both to a new string object.


The "if possible" hides a lot of technical detail, which is why the 
optimization can fail on some platforms while working on others. See this 
painful discussion trying to debug httplib slowness:


http://mail.python.org/pipermail/python-dev/2009-August/091125.html

After many dead-ends and red herrings, somebody spotted the problem:

http://mail.python.org/pipermail/python-dev/2009-September/091582.html

ending with GvR admitting that it was an embarrassment that repeated string 
concatenation had survived in the standard library for so long. The author 
even knew it was slow because he put a comment warning about it!


Here is the middle of the discussion adding the optimization back in 2004:

http://mail.python.org/pipermail/python-dev/2004-August/046695.html

which talks about the possibility of other implementations doing something 
similar. You can find the beginning of the discussion yourself :)


And here is a good description of the optimization itself:

http://utcc.utoronto.ca/~cks/space/blog/python/ExaminingStringConcatOpt




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano

Prasad, Ramit wrote:

That is, for loops first try to build an iterator by calling __iter__, and if
that fails they try the sequence protocol obj[0], obj[1], obj[2], ...


So...I could instead write __getitem__ for the same effect?



Er, no... __getitem__ and __iter__ do very different things. __getitem__ 
returns individual items, and __iter__ returns an iterator object which, when 
passed to the next() builtin, returns the next item.


I trust you aren't calling __iter__ explicitly! Always call it from the 
built-in function iter(), or better still, don't call it at all and let the 
for loop do so.


For the record, here is pseudo-code emulating how Python for-loops work.
"for x in obj: do_something(x)" becomes something similar to this:

try:
# Try the iterator protocol first.
it = iter(x)
except TypeError:
# Fall back on the old-fashioned sequence protocol.
counter = 0
while True:
try:
x = obj[counter]
except IndexError:
# We must be past the end of the sequence.
del counter
break
do_something(x)
counter += 1
else:
# Iterator protocol.
while True:
try:
x = next(it)
except StopIteration:
# We're done.
del it
break
do_something(x)



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Steven D'Aprano

Dave wrote:

I was reading some tutorial material on creating iterators. It shows the
following example implementation of an iterator:

class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def next(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]


My question is how was I supposed to kinow that the function I call using
the name iter() is implemented using the name __iter__()?


By reading the Fine Manual :)


Unless you are defining your own classes, you (almost) never need to care 
about __DoubleUNDERscore__ (dunder) methods and attributes. The main exception 
I can think of is the idiom for writing Python scripts:


if __name__ == '__main__':
# Running as a script.
main()
# Otherwise we're imported as a library.


Even when defining your own classes, simply define the methods you care about, 
such as __init__ for instance initialisation, but don't call __init__ 
directly. Python will call it for you. (Why keep a dog and bark yourself?)




Is there a rule that describes when I would implement an attribute name
with leading and trailing double underscores, and then call it without
those underscores? How many names like this exist in Python? Are these
special cases or is there a general rule that leading and trailing double
underscores get dropped when calling functions that were implemented with
these names? I'm trying to understand the big picture as far as how Python
works when it comes to this situation. Thanks.


The big picture is that dunder methods are used for customizing operators and 
functions, but you really need to check the manual to see which operator maps 
to which dunder method.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] special attributes naming confusion

2012-06-06 Thread Alan Gauld

On 06/06/12 21:32, Dave wrote:

I'm not sure where this comment belongs, but I want to share my
perspective on the documentation of these special method names. In the
following section there is an inconsistency which could be confusing to
someone just learning Python (e.g., me).


In general someone "just learning python" doesn't need to know about 
special names, they just use the operations they define.

The only exception being __init__() in class definitions.

These methods are really only of interest to someone who wants to define 
their own abstract data type and make it look like a built in type(*). 
In the vast majority of beginner scenarios that is not

needed or can be faked without use of them.

Even where it is needed the special names can be discovered on
a "need to know" basis. By the time that need arises the programmer
is hopefully so familiar with Python implementation style(use of 
dictionaries for namespaces etc) that the special name trick will be no 
great surprise and in fact come as an "aha!" moment of understanding.


So mostly I'd say just ignore them for now, you almost certainly don;t 
need them. And if you do you probably aren't "just learning" any more!


:-)

(*) Someone from a Lisp, Smalltalk or C++ background might stumble on 
them earlier because these languages support operator overloading and so 
the beginner may wonder about how to do it in Python. Most other 
languages don't use the concept so it won't even occur to ask!


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching 2 Strings in A File

2012-06-06 Thread Alan Gauld

On 06/06/12 20:33, dohoang4...@comcast.net wrote:

i am writing a python script that will be invoked as follows:



cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE,
stderr=STDOUT)


Assuming you really want to use egrep, why?
You could process the file directly in Python searching for your strings 
(using the re module if need be).


Using Popen makes sense if the command is doing a lot of work or is very 
CPU intensive. But for file processing its probably as easy to just use 
Python to scan the log files directly.


But if you are really doing something more complex I see that others 
have answered the Popen() question...



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Notepad++ question

2012-06-06 Thread Alexander Quest
Hey all; my question is regarding editing Python code in Notepad++. When I
run this piece of code in Notepad++:

def fix_start(s):
  var1 = s[0]
var2 = "*"
  var3 = s.replace(var1, var2)

  return var3


I get an indentation error, which reads:


  File "C:\google-python-exercises\google-python-exercises\basic>string1.py
line 56
var2 = "*"
^
IndentationError: unexpected indent


The thing is that in Notepad++, that code does not appear with an
indentation where var2 is. It appears like this:

def fix_start(s):
  var1 = s[0]
  var2 = "*"
  var3 = s.replace(var1, var2)

  return var3

but when I copy and paste it, it pastes with an indentation where var2 is,
which is what I think is causing the error. The code runs fine if I just
use IDLE. I am doing Google's python exercises, and they recommended I edit
the settings on Notepad++ to indent 2 spaces upon a tab, this being the
convention at Google. Does anyone know what the deal is here? Also, I am
wondering why use Notepad++ or other such programs when IDLE seems to be
fine for writing code. Thanks.

-Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Notepad++ question

2012-06-06 Thread Dave Angel
On 06/07/2012 12:29 AM, Alexander Quest wrote:
> Hey all; my question is regarding editing Python code in Notepad++. When I
> run this piece of code in Notepad++:
>
> def fix_start(s):
>   var1 = s[0]
> var2 = "*"
>   var3 = s.replace(var1, var2)
>
>   return var3
>
>
> I get an indentation error, which reads:
>
>
>   File "C:\google-python-exercises\google-python-exercises\basic>string1.py
> line 56
> var2 = "*"
> ^
> IndentationError: unexpected indent
>
>
> The thing is that in Notepad++, that code does not appear with an
> indentation where var2 is. It appears like this:
>
> def fix_start(s):
>   var1 = s[0]
>   var2 = "*"
>   var3 = s.replace(var1, var2)
>
>   return var3
>
> but when I copy and paste it, it pastes with an indentation where var2 is,
> which is what I think is causing the error. The code runs fine if I just
> use IDLE. I am doing Google's python exercises, and they recommended I edit
> the settings on Notepad++ to indent 2 spaces upon a tab, this being the
> convention at Google. Does anyone know what the deal is here? Also, I am
> wondering why use Notepad++ or other such programs when IDLE seems to be
> fine for writing code. Thanks.
>
> -Alex
>
>
I can't tell you what settings to use in Notepad++, but what you want is
either for tabs or spaces to be used for all indenting in a source file,
and never a mixture.  I think if you view your file in a hex viewer,
you'll see an invisible tab character tucked on that one line.

My opinion is to stick entirely to spaces.  30 years ago, when i first
faced this problem, I came up with what seems to be the best answer. 
The editor should not make any connection between the keyboard button
called tab and the file character hex(09).  When you use the tab key to
line up columns, the editor should insert an appropriate number of
spaces to make it come out right.  And if you subsequently want to
convert all the leading characters into one or more tabs followed by one
or more spaces, that should be done to the whole file at once.

Others think one should use tab characters exclusively for indenting. 
While there are some conceptual advantages to that, the biggest downside
is that very few editor/viewers make the tab characters visible.  I used
such an editor decades ago, and it solved most of the problems.

As for IDLE, I've never used it, so I don't know it's limitations.


-- 
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor