[Tutor] Tutor Digest, Vol 146, Issue 9

Jason Willis chaoticslacker at gmail.com
Fri Apr 8 12:44:06 EDT 2016


My apologies for the word wrap. It seemed to look ok in my web client
(gmail). Thank you for the pointer to other instances in the program where
there are hard-coded content dependant entries. I solved this by changing
all instances of the word "elite" and changing them to "standard" and the
program works! I agree with you that taking a few hours to learn python
would go a long way. I believe by doing things like this that this is
exactly what I am doing. I don't know if it's the materials I'm using or
what but learning from a book is not helping me much. Maybe you all have
better ideas about sources that would be helpful in moving me along into
learning python.

Thanks again!

On Fri, Apr 8, 2016 at 12:00 PM, <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: Declaring variables (Mark Lawrence)
>    2. Re: customizing dark_harvest problems (Alan Gauld)
>    3. Re: customizing dark_harvest problems (Ben Finney)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 7 Apr 2016 22:51:05 +0100
> From: Mark Lawrence <breamoreboy at yahoo.co.uk>
> To: tutor at python.org
> Subject: Re: [Tutor] Declaring variables
> Message-ID: <ne6kog$gp4$1 at ger.gmane.org>
> Content-Type: text/plain; charset=windows-1252; format=flowed
>
> On 07/04/2016 18:49, Dimitar Ivanov wrote:
> > Hello everyone,
> >
> > I have a (hopefully) quick and easy to explain question. I'm currently
> > using MySQLdb module to retrieve some information from a database. In my
> > case, the result that's being yield back is a single line.
> >
> > As far as my understanding goes, MySQLdb function called 'fetchone()'
> > returns the result as a tuple. Problem is, the tuple has some unnecessary
> > characters, such as an additional comma being returned. In my case:
> >
> >>>>   idquery = 'select id from table;'
> >>>>   cur = mysql.cursor()
> >>>>   cur.execute(idquery)
> >>>>   id = cur.fetchone()
>
> Note that using 'id' is frowned upon as you're overriding the builtin of
> the same name.  I'll use id_ below.
>
> >>>>   print id
> > ('idinhere',)
>
> No, it isn't an additional comma, it's a tuple that only has one field.
>
> >
> > I stumbled across an example given like this:
> >
> >>>>   (id,) = cur.fetchone()
> >
> > So I decided to give it a try and the result is exactly what I need:
> >
> >>>>   (id,) = cur.fetchone()
> >>>>   print id
> > idinhere
> >
> > My question is - can I reliably use this method? Is it always going to
> > return the string between the brackets as long as I define the variable
> > with '(,)'? I'm planning to use another query that will be using the
> result
> > from this one and then update another database with this result but I
> must
> > be sure that the variable will always be the string in and between the
> > brackets otherwise I'm risking to mess up a lot of things big time.
>
> I'd write it as:-
>
> id_ = cur.fetchone()[0]
>
> >
> > A backup plan I had was to use the following:
> >
> >>>>   id = cur.fetchone()
> >>>>   for x in id:
> >>>>     id = x
>
> Yuck :)
>
> >
> > But if the method above is certain to always return only the value I
> need,
> > I find it to be a far more elegant solution.
> >
> > Also, just to clarify things for myself - what does this method of
> > declaring variables do exactly? I'm sorry if this isn't the right place
> the
> > ask and if this has been documented clearly already, I'm not sure what to
> > use as a search term in order to find an answer.
>
> In Python nothing is declared as in C or Java. A name is bound to an
> object.  So from the above the name 'id_' is bound to the string object
> that happens to be 'idinhere'.  Once this has been done there is nothing
> to stop you from writing:-
>
> id_ = 1
> id_ = 1.0
> id_ = Point(1, 2)
> id_ = Complicated(lots, of, parameters, here)
>
> >
> > Thanks a lot in advance! I hope I posted all the details needed and my
> > question is easy to comprehend.
>
> The only things that are sometimes needed are your OS and Python
> version.  The latter can be deduced from your 'print id' rather than
> 'print(id)', indicating that it is 2.x, not 3.y.
>
> >
> > Regards,
> > Dimitar
> >
>
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 8 Apr 2016 02:10:55 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] customizing dark_harvest problems
> Message-ID: <ne70ev$1q1$1 at ger.gmane.org>
> Content-Type: text/plain; charset=utf-8
>
> On 08/04/16 01:51, Jason Willis wrote:
>
> > Though, I do know some things and can figure a little bit out when
> looking
> > at source code I'm usually at a loss when understanding the entire
> workings
> > of a program.
>
> And thats the problem here. The code is very specific to the page it is
> parsing. Simply substituting a different file will never work.
>
> For example...
>
> > DOC_ROOT          = 'http://freeproxylists.com'
> > ELITE_PAGE        = 'elite.html'
>
>
> >     def _extract_ajax_endpoints(self):
> >
> >         ''' make a GET request to freeproxylists.com/elite.html '''
> >         url = '/'.join([DOC_ROOT, ELITE_PAGE])
> >         response = requests.get(url)
> >
> >         ''' extract the raw HTML doc from the response '''
> >         raw_html = response.text
> >
> >         ''' convert raw html into BeautifulSoup object '''
> >         soup = BeautifulSoup(raw_html)
> >
> >         for url in soup.select('table tr td table tr td a'):
> >             if 'elite #' in url.text:
> >                 yield '%s/load_elite_d%s' % (DOC_ROOT,
> > url['href'].lstrip('elite/'))
>
>
> Notice that last 'if' section has 'elite #' hard coded in.
> But the standard page doesn't use 'elite #'...
>
> There are probably a lot more similar content-dependant things
> in the code, I just happened to spot that one.
>
> It would be better if you took the time(only a few hours really) to
> learn how to program in Python so that you can actually understand
> the code rather than making "poke 'n hope" changes.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
> ------------------------------
>
> Message: 3
> Date: Fri, 08 Apr 2016 11:15:07 +1000
> From: Ben Finney <ben+python at benfinney.id.au>
> To: tutor at python.org
> Subject: Re: [Tutor] customizing dark_harvest problems
> Message-ID: <85oa9k6gv8.fsf at benfinney.id.au>
> Content-Type: text/plain; charset=utf-8
>
> Jason Willis <chaoticslacker at gmail.com> writes:
>
> > I am a complete noob when it comes to python and programming in
> > general.
>
> Welcome! Congratulations on choosing to learn Python.
>
> > What i understand from the code so far
>
> Please set your mail client to send plain text messages, and to not
> automatically wrap lines of text when you paste in examples of program
> code.
>
> Only then can we see the exact same program code you see.
>
> > Even though, from my understanding, the both web pages are structured
> > identically the program does not work when I change the variable. What
> > am I missing here? I haven't the slightest idea.
>
> Nor do we. When reporting confusing behaviour, you need to say not only
> that ?the program does not work?; you need to say in quite close detail
> what it *does*.
>
> --
>  \       ?It is forbidden to steal hotel towels. Please if you are not |
>   `\          person to do such is please not to read notice.? ?hotel, |
> _o__)                                               Kowloon, Hong Kong |
> Ben Finney
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 146, Issue 9
> *************************************
>


More information about the Tutor mailing list