[Tutor] string indexing

rahmad akbar matbioinfo at gmail.com
Mon Jan 20 08:37:03 CET 2014


Spir and Peter, thanks for the specifics, super helpful. Alan, super thanks
for the general advice, you guys are awesome!!




On Mon, Jan 20, 2014 at 5:34 AM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: Understanding Classes (Alan Gauld)
>    2. Re: string indexing (Keith Winston)
>    3. Re: Question on os.popen (Alan Gauld)
>    4. Re: Question on os.popen (SM)
>    5. Re: Question on os.popen (eryksun)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 19 Jan 2014 23:50:59 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Understanding Classes
> Message-ID: <lbhocn$vg3$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 19/01/14 21:59, Christian Alexander wrote:
> > Looked all over the net for class tutorials
> > Unable to understand the "self" argument
> > Attempting to visual classes
>
> If you read my OOP tutorial there is a section there specifically about
> self.
>
> And the v3 tutor includes an introduction to the formal visualisation
> technique for OOP called UML. The diagrams illustrating the designs may
> help.
>
> http://www.alan-g.me.uk/l2p/tutclass.htm
>
> > I have searched high and low, for easy to follow tutorials regarding
> > classes.  Although I grok the general concept of classes,
>
> Do you also grok the concept of objects?
> Classes on their own are fairly useless (unless you are using Java)
> it is only when you create a universe of objects from those classes that
> they become useful.
>
> If you can repeat to us your understanding of classes and their
> relationship with objects that will help us understand your
> level and shape our responses accordingly.
>
> > to visually understand what exactly "self" does, or why it is even
> > necessary.  It seems very "magic" to me.
>
> When you define a class you define the data (attributes) that
> the class instances will have. Each instance will have a copy of the
> data defined in the __init__() method.
> You also define a set of operations or methods that are associated
> with the class. Those methods are shared by the instances.
>
> Note the difference. Instances get a copy of the attributes
> but they all share the methods.
>
> Thus when you invoke a method on an instance the instance relays that
> call to the class. For the class to know which instance is being
> operated on, and for the method to be able to access the correct
> instance's data it needs a reference to the instance. That reference
> is typically called 'self' or 'this'. (In some languages it's fixed
> but in Python self is only a convention, you can use any name you like).
>
> You can make the call to the class explicit and it will still work.
> See below:
>
> # define a class
> class MyClass:
>      def __init__(self,x): self.x = x
>      def myMethod(self): print(self.x)
>
> # create some instances
> ObjA = MyClass(2)
> ObjB = MyClass(4)
> ObjC = MyClass(6)
>
> # send some messages/call methods
> objA.myMethod()       # call from the instance
> MyClass.myMethod(ObjB)  # call explicitly to the class
> objC.myMethod()      # direct again
>
> All 3 calls do the same thing except the middle one
> passes the object identifier directly to the class
> whereas the first and last both do that internally
> within the object structure.
>
> > difficult with the "__init__()" method in classes,
>  > and why that is also required.
>
> It is not *required* as such. You can create a class
> without an init but it's unusual.
>
> When you create an instance of a class it creates a
> data structure in memory referenced by the name of
> the instance. But that structure is empty, it has
> no data. So to populate the data for the instances
> you must initialize it. That's what __init__() does.
> It takes the arguments you provide and applies them
> to the instance along with any static data definitions
> you may define.
>
> In the example we create an instance variable, x,
> within the instances and assign the value of the
> argument passed to init. Like any other method the
> actual code lives in the class so we could initialize
> it by calling init like so:
>
> MyClass.__init__(objC, 66)
>
> which is almost the same as doing:
>
> objC = MyClass(66)
>
> The difference is that the first case requires the object ObjC
> to already exist, the second example creates a new instance and
> then calls init on that instance.
>
> > Keep in mind that I am a visual person (maybe I should have
> > been a graphic designer), therefore most programming concepts flow
> > irritatingly slow for me.
>
> Most programming concepts have visual representations,
> its just that program code being text tends to lead programmers
> to be verbally based. But algorithms, state machines, logic, data
> structures, GUIs, formal requirements and OOP all have well
> established visual representations, and in many cases they
> are formalized so that, with the right tools, you can
> create code purely visually.
>
> If the above doesn't clarify things, and I suspect it won't
> entirely, then please do express your understanding so far
> in your own words and we'll try to clarify things from there.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 19 Jan 2014 19:19:46 -0500
> From: Keith Winston <keithwins at gmail.com>
> Cc: Python Tutor Mailing List <tutor at python.org>
> Subject: Re: [Tutor] string indexing
> Message-ID:
>         <CAO5ffbZfZmJA3Ex5j6aDJ9K_qGRxu=p6tw3FxDZAa4R242=
> hbw at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Sun, Jan 19, 2014 at 3:50 PM, Alan Gauld <alan.gauld at btinternet.com>
> wrote:
> >> How would Python know whether you want find for gettext, mmap, str,
> >> xml.etree.ElementTree.Element or xml.etree.ElementTree.ElementTree?
> >
> >
> > Absolutely, but a newbie doesn't even guess that more than one find would
> > exist. Or even that there would need to be more than one.
>
> That's exactly it. I'm just getting to the point of being able to
> understand how much I don't know, and (I'm only a little embarrassed
> to admit) Alan's empty-string example was an "ah-ha" moment for me. I
> expect Help will be a great deal more useful now (of course, as I type
> that I realize I could have used the class name, help(str.find),
> instead of an impromptu instance. Another little ah-ha). And of
> course, the instant I understood all that, the point that Mark made
> became obvious. But I didn't see it before.
>
>
> --
> Keith
>
>
> ------------------------------
>
> Message: 3
> Date: Mon, 20 Jan 2014 01:20:20 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Question on os.popen
> Message-ID: <lbhtk8$fjt$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 19/01/14 23:36, SM wrote:
>
> > I read about os.popen in
> > http://docs.python.org/2/library/subprocess.html#subprocess.Popen
>
> This doesn't answer the question but I'm curious.
> If you read about os.popen in the subprocess module docs why did
> you use it? The subprocess module replaces all the os.popen calls
> with the generally superior subprocess.Popen class.
>
> Now, I don't know if using subprocess instead of os would fix
> things in this case but it seems an odd choice? I'm curious why
> you chose to go with os.popen?
>
> BTW you don't tell us which OS you are using (or which Python
> version), that may be pertinent to the answer. I'm assuming
> it's some variant of *nix but which one?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 19 Jan 2014 23:00:14 -0500
> From: SM <sunithanc at gmail.com>
> To: Alan Gauld <alan.gauld at btinternet.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] Question on os.popen
> Message-ID:
>         <CAOeFuMSKTcfJ2t1PwM=b4m=yJfhby7snGGDCrmA=
> qrOkwB634Q at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi Alan,
> Thanks for your reply.
> My answer to why I am using os.popen could be a lame one - I have used it
> extensively in various places before and it has been working well and so
> was hung up on using it. Now I replaced it by subprocess.check_output with
> appropriate parameters and it seems to be working. Does that mean this is
> one of the limitations of os.popen? I am not sure.
> Sorry for not giving details on the OS and python version I am using:
> Ubuntu and Python3.
> Thanks,
> SM
>
>
> On Sun, Jan 19, 2014 at 8:20 PM, Alan Gauld <alan.gauld at btinternet.com
> >wrote:
>
> > On 19/01/14 23:36, SM wrote:
> >
> >  I read about os.popen in
> >> http://docs.python.org/2/library/subprocess.html#subprocess.Popen
> >>
> >
> > This doesn't answer the question but I'm curious.
> > If you read about os.popen in the subprocess module docs why did
> > you use it? The subprocess module replaces all the os.popen calls
> > with the generally superior subprocess.Popen class.
> >
> > Now, I don't know if using subprocess instead of os would fix
> > things in this case but it seems an odd choice? I'm curious why
> > you chose to go with os.popen?
> >
> > BTW you don't tell us which OS you are using (or which Python
> > version), that may be pertinent to the answer. I'm assuming
> > it's some variant of *nix but which one?
> >
> > --
> > Alan G
> > Author of the Learn to Program web site
> > http://www.alan-g.me.uk/
> > http://www.flickr.com/photos/alangauldphotos
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20140119/5b5bb634/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 5
> Date: Sun, 19 Jan 2014 23:33:46 -0500
> From: eryksun <eryksun at gmail.com>
> To: SM <sunithanc at gmail.com>
> Cc: "tutor at python.org" <tutor at python.org>
> Subject: Re: [Tutor] Question on os.popen
> Message-ID:
>         <CACL+1avKNFbE+TuAwUpkz=kZMSSDngJZX_Wp7cpWCaq1=
> tBjpA at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Sun, Jan 19, 2014 at 6:36 PM, SM <sunithanc at gmail.com> wrote:
> >
> > This time it probably ran for a few more iterations than before and
> stopped
> > with the same error message. This time it also output the following
> > messages:
> >
> > IOError: [Errno 4] Interrupted system call
> > Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not
> found)
> > Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not
> found)
> > Attribute not found in file (tsk_fs_attrlist_get: Attribute 128 not
> found)
>
> I can't help with these (NTFS?) Attribute errors from "The Sleuth Kit"
> digital forensics tools.
>
> In Python 3.3, `IOError` is an alias for `OSError`, and EINTR (i.e.
> errno.errorcode[4]) is exposed directly as `InterruptedError`. So you
> must be running a previous version. I see you're using `print` as a
> function, so I'll guess you're using 3.2.
>
> In 3.2, `os.popen` is implemented via `subprocess.Popen`:
>
> http://hg.python.org/cpython/file/cef745775b65/Lib/os.py#l776
>
> For example, it uses the following for 'r' mode:
>
>     proc = subprocess.Popen(cmd,
>                             shell=True,
>                             stdout=subprocess.PIPE,
>                             bufsize=buffering)
>    return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
>
> If you're sticking to `os.popen`, you'll need to retry the read in
> case of an interrupted system call.
>
> I recommend you switch to `Popen` directly and call `communicate`.
> This retries reading `stdout` using the helper function
> `_eintr_retry_call`:
>
> http://hg.python.org/cpython/file/cef745775b65/Lib/subprocess.py#l452
>
>     def _eintr_retry_call(func, *args):
>         while True:
>             try:
>                 return func(*args)
>             except (OSError, IOError) as e:
>                 if e.errno == errno.EINTR:
>                     continue
>                 raise
>
> More simply, use `subprocess.check_output`, which calls `communicate`
> for you. You can pass `shell=True` if you really need it.
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 119, Issue 91
> **************************************
>



-- 
many thanks
mat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140120/d820cfe0/attachment-0001.html>


More information about the Tutor mailing list