Re: [Tutor] 2 vs 3

2015-09-07 Thread Sithembewena Lloyd Dube
This has turned into a very interesting discussion. Thank you to everyone
who's participating and sharing nuggets of information on 2 vs 3.

On Mon, Sep 7, 2015 at 1:58 PM, Steven D'Aprano  wrote:

> On Mon, Sep 07, 2015 at 11:01:00AM +0100, Alan Gauld wrote:
> > On 07/09/15 10:32, Laura Creighton wrote:
> > >In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes:
> > >
> > >>And of course if you are really moving to the v3 way of
> > >>doing things you should move to using the new string
> > >>formatting style.
> > >
> > >I don't think there is any great push to prefer the new style over
> > >the old style 'just because' when you are doing simple things.
> >
> > No 'big push' because there's too much historic code out there.
> > But I thought I'd read somewhere when it first came out that
> > the old style was deprecated? But I may be hallucinating! :-)
>
> When the format() method first came out, there was talk about
> deprecating % formatting, but it was only talk and never became
> official.
>
> > OTOH the new style brings enough benefits, especially in the
> > areas of dynamic formatting,  that I'd strongly suggest moving
> > to it if you are making the jump to v3 anyway!
>
> It's worth learning both formatting systems, even if you're just using
> Python 2. The format method started in 2.6, and 2.7 added the
> "autonumbering" feature that makes it much easier to use:
>
> "{0} {1} {2}".format(a, b, c)  # needed in 2.6
> "{} {} {}".format(a, b, c)  # 2.7 and up
>
>
> Having said that, I still don't know how to use format other than the
> basics, as most of the code I write has to be compatible with 2.4
> onwards.
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Kind regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2 vs 3

2015-09-05 Thread Sithembewena Lloyd Dube
@Steven, @Mark,

Thanks for the feedback.

On Sat, Sep 5, 2015 at 5:37 PM, Mark Lawrence 
wrote:

> On 05/09/2015 14:25, Steven D'Aprano wrote:
>
>> On Sat, Sep 05, 2015 at 03:06:25PM +0200, Sithembewena Lloyd Dube wrote:
>>
>>> Hi all,
>>>
>>> A colleague and I are embarking on a project for a client. We have agreed
>>> to implement a REST API in Falcon (www.falconframework.org) and
>>> colleague
>>> wants to implement it in Python 3.
>>>
>>> Are there any advantages/ disadvantages of using Python 3? I have always
>>> stuck to Python 2.
>>>
>>
>> If you decide to use Python 3, 3.3 should be the oldest version you
>> consider. 3.4 has many improvements and you should consider using that.
>>
>>
> People should be aware that Python 3.5 is scheduled for full release on
> September 13, 2015, see https://www.python.org/dev/peps/pep-0478/
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Kind regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] 2 vs 3

2015-09-05 Thread Sithembewena Lloyd Dube
Hi all,

A colleague and I are embarking on a project for a client. We have agreed
to implement a REST API in Falcon (www.falconframework.org) and colleague
wants to implement it in Python 3.

Are there any advantages/ disadvantages of using Python 3? I have always
stuck to Python 2.

-- 
Kind regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Preparing virtualenvwrapper for Django 1.6 development

2014-08-22 Thread Sithembewena Lloyd Dube
Hi everyone,

I am developing on a Windows 8.1 machine and wold like to setup
virtualenvironment via virtualenvwrapper so as to have a properly set up
Python development environment.

I am referring to Jeff Knupp's guide at
http://www.jeffknupp.com/blog/2013/12/18/starting-a-django-16-project-the-right-way/

After installation of virtualenvwrapper via pip, the guide says:
"
After it's installed, add the following lines to your shell's start-up file
(.zshrc, .bashrc, .profile, etc).

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/directory-you-do-development-in
source /usr/local/bin/virtualenvwrapper.sh
"

My issue is, I do not understand what these lines are doing (I have a vague
idea but assumptions can be deadly). Moreover, it is clear that the
instructions were authored with a UNIX environment in mind.

Would anyone be so kind as to translate this to Windows speak, as well as
perhaps pointing out what file this would go into in a Windows environment?

Thanks in advance :)

Kind regards,
Sithu.
-- 
Regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PyCountry currency formatting woes

2014-05-08 Thread Sithembewena Lloyd Dube
Thank you all, babel works just fine. I also tried ccy, which isn't bad
either - except that it returns non-unicode currency letters for countries
in the Eurozone.


On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__pete...@web.de> wrote:

> Sithembewena Lloyd Dube wrote:
>
> > Thanks, i was actually getting the error information to update the post.
> > Apoligies to waste your time posting here - I could not find an
> > appropriate PyCountry discussion list and my next best bet seemed to be a
> > Python users' list.
> >
> > For those who care to look, the error is as follows (a concise example
> > from an interactive shell:
> >
> > import pycountry
> > country = pycountry.countries.get(alpha2='DE')
> > currency = pycountry.currencies.get(numeric=country.numeric)
> > Traceback (most recent call last):
> > File "", line 1, in
> > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
> > return self.indices[field][value]
> > KeyError: '276'
> >
> > The obvious issue here is that the pycountry.countries collection does
> not
> > contain a currency with a numeric of 276 (Germany's numeric) - yet it
> does
> > contain the Euro. Any ideas as to what the way around this may be?
>
> It looks like the development version of babel
>
>
> http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies
>
> can do what you want:
>
> $ LANG=en_US.UTF-8 python
> Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
> [GCC 4.8.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import babel.numbers as bn
> >>> bn.get_territory_currencies("DE")
> ['EUR']
> >>> print bn.format_currency(1.234, "EUR")
> €1.23
> >>> print bn.format_currency(1.234, "EUR", locale="DE")
> 1,23 €
> >>> import babel
> >>> babel.__version__
> '2.0-dev'
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PyCountry currency formatting woes

2014-05-06 Thread Sithembewena Lloyd Dube
Thanks for this response, this is exactly what I needed to know.


On Mon, May 5, 2014 at 6:26 AM, Marc Tompkins wrote:

> On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube  > wrote:
>
>> Thanks, i was actually getting the error information to update the post.
>> Apoligies to waste your time posting here - I could not find an appropriate
>> PyCountry discussion list and my next best bet seemed to be a Python users'
>> list.
>>
>>
> You also posted on StackOverflow; I just answered you there.  In short:
> the currency numeric is not guaranteed to be the same as the country
> numeric (in the case of the Euro, how could it possibly be?)  The numeric
> for DE is 276; the numeric for the Euro is 978.  Obviously they don't match.
>
> PyCountry is a wrapper around some tables provided by Debian; those tables
> don't include a country/currency mapping.  You can find those mapping
> tables at
>  http://www.currency-iso.org/en/home/tables/table-a1.html
> and roll your own wrapper; I'm sure it's been done a thousand times
> before, but I'm not aware of a Python package that does this.
>



-- 
Regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PyCountry currency formatting woes

2014-05-04 Thread Sithembewena Lloyd Dube
Thanks, i was actually getting the error information to update the post.
Apoligies to waste your time posting here - I could not find an appropriate
PyCountry discussion list and my next best bet seemed to be a Python users'
list.

For those who care to look, the error is as follows (a concise example from
an interactive shell:

import pycountry
country = pycountry.countries.get(alpha2='DE')
currency = pycountry.currencies.get(numeric=country.numeric)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
return self.indices[field][value]
KeyError: '276'

The obvious issue here is that the pycountry.countries collection does not
contain a currency with a numeric of 276 (Germany's numeric) - yet it does
contain the Euro. Any ideas as to what the way around this may be?


On Sun, May 4, 2014 at 10:25 PM, Sithembewena Lloyd Dube
wrote:

> Hi everyone,
>
> I have a function which accepts an alpha2 country code and a price string,
> where the aim is to get the country's currency and use the currency.letter
> property of that currency to format the supplied price string.
>
> The above works fine so far - yet it falls over when called with Germany
> as the country as follows:
>
> currency = pycountry.currencies.get(numeric=country.numeric)
>
> The function implementation is as follows:
>
> def formatPrice(self, alpha2CountryCode, price):
> """
> @param alpha2CountryCode: The 2-character country code for which
> to format the price value
> @param price: The price value as a string
> @return: A string representing the formatted monetary value for
> this country for this price.
>
> #Get country by alpha2 code
> country = pc.countries.get(alpha2=alpha2CountryCode.upper())
>
> #Get currency by country's numeric and format price
> currency = pc.currencies.get(numeric=country.numeric)
> letter = currency.letter
> formattedCurrency = "%s %s" % (letter, price)
>
> return formattedCurrency
>
> Any ideas as to what the issue may be?
>
> Thanks :)
>
> --
> Regards,
> Sithu Lloyd Dube
>



-- 
Regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] PyCountry currency formatting woes

2014-05-04 Thread Sithembewena Lloyd Dube
Hi everyone,

I have a function which accepts an alpha2 country code and a price string,
where the aim is to get the country's currency and use the currency.letter
property of that currency to format the supplied price string.

The above works fine so far - yet it falls over when called with Germany as
the country as follows:

currency = pycountry.currencies.get(numeric=country.numeric)

The function implementation is as follows:

def formatPrice(self, alpha2CountryCode, price):
"""
@param alpha2CountryCode: The 2-character country code for which to
format the price value
@param price: The price value as a string
@return: A string representing the formatted monetary value for
this country for this price.

#Get country by alpha2 code
country = pc.countries.get(alpha2=alpha2CountryCode.upper())

#Get currency by country's numeric and format price
currency = pc.currencies.get(numeric=country.numeric)
letter = currency.letter
formattedCurrency = "%s %s" % (letter, price)

return formattedCurrency

Any ideas as to what the issue may be?

Thanks :)

-- 
Regards,
Sithu Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Generating random alphanumeric codes

2012-06-26 Thread Sithembewena Lloyd Dube
HI,

Would anyone have tips on how to generate random 4-digit alphanumeric codes
in python? Also, how does one calculate the number of possible combinations?

Thanks in advance.

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


[Tutor] Barcode decoder in Python

2012-06-26 Thread Sithembewena Lloyd Dube
Hi everyone,

Is there any decent barcode decoder software which one could use to read
image barcodes and return a result to a calling function/ app? I wish to
implement a Python server backend which would import and use such a module
(if it were that, for instance).

Thanks.

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


Re: [Tutor] Error handling

2012-03-24 Thread Sithembewena Lloyd Dube
That is because 'errno' is a property on the exception object called 'e',
not the other way around.

On Sun, Mar 25, 2012 at 7:12 AM, Michael Lewis  wrote:

>
>
> On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers wrote:
>
>>  I am having a bit of trouble understanding what is going on below. What
>> does the "e" in "except OSError, e:" do?
>> Any other help you can provide regarding errno would be extremely
>> appreciated. I've done help() and dir() on it, but I am not really
>> understanding what's going on with "e.errno != errno.EEXIST:"
>>
>> Basically, that `except` block is catching all exceptions of type
>> OSError, and storing the exception in variable `e`.  This variable does not
>> have to be called `e`, but that's the most commonly-used variable name.
>>
>> Once you have the exception stored (in this case in the variable `e`),
>> you can then see what type of exception, using the `errno` property of the
>> exception.  You can read about the different types here:
>>
>> http://docs.python.org/library/errno.html
>>
>>  import os, errnotry:
>> os.makedirs('a/b/c')except OSError, e:
>> if e.errno != errno.EEXIST:
>> raise
>>
>> In this particular section, it's catching any OSError, and then if it
>> turns out that the error was "File Exists", it is raising that exception
>> again, to be either caught by an encapsulating try block, or which will
>> bring the program to a halt with an exception shown by the interpreter.
>>
>> Is that the behavior you are going for?  Any more confusion?
>>
>
> Why wouldn't it be errno.e instead of e.errno?
>
>>
>> --
>> Colton Myers
>>
>>
>
>
> --
> Michael J. Lewis
>
> mjole...@gmail.com
> 415.815.7257
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] Parsing an XML document using ElementTree

2011-06-10 Thread Sithembewena Lloyd Dube
Hi Stefan,

Thanks for the code review :) Only just noticed this.

On Wed, May 25, 2011 at 3:10 PM, Stefan Behnel  wrote:

> Sithembewena Lloyd Dube, 25.05.2011 14:40:
>
>  Thanks for all your suggestions. I read up on gzip and urllib and also
>> learned in the process that I could use urllib2 as its the latest form of
>> that library.
>>
>> Herewith my solution: I don't know how elegant it is, but it works just
>> fine.
>>
>> def get_contests():
>>  url = '
>>
>> http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po
>> '
>>  req = urllib2.Request(url)
>>  req.add_header('accept-encoding','gzip/deflate')
>>  opener = urllib2.build_opener()
>>  response = opener.open(req)
>>
>
> This is ok.
>
>
>
>   compressed_data = response.read()
>>  compressed_stream = StringIO.StringIO(compressed_data)
>>  gzipper = gzip.GzipFile(fileobj=compressed_stream)
>>  data = gzipper.read()
>>
>
> This should be simplifiable to
>
>   uncompressed_stream = gzip.GzipFile(fileobj=response)
>
>
>
>   current_path = os.path.realpath(MEDIA_ROOT + '/xml-files/d.xml')
>>  data_file = open(current_path, 'w')
>>  data_file.write(data)
>>  data_file.close()
>>  xml_data = ET.parse(open(current_path, 'r'))
>>
>
> And this subsequently becomes
>
>   xml_data = ET.parse(uncompressed_stream)
>
>
>
>   contest_list = []
>>  for contest_parent_node in xml_data.getiterator('contest'):
>>
>
> Take a look at ET.iterparse().
>
>
>
>contest = Contest()
>>   for contest_child_node in contest_parent_node:
>>if (contest_child_node.tag == "name" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>> contest.name = contest_child_node.text
>>if (contest_child_node.tag == "league" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>>contest.league = contest_child_node.text
>>if (contest_child_node.tag == "acro" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>>contest.acro = contest_child_node.text
>>if (contest_child_node.tag == "time" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>>contest.time = contest_child_node.text
>>if (contest_child_node.tag == "home" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>>contest.home = contest_child_node.text
>>if (contest_child_node.tag == "away" and
>> contest_child_node.text is not None and contest_child_node.text != ""):
>>contest.away = contest_child_node.text
>>
>
> This is screaming for a simplification, such as
>
>   for child in contest_parent_node:
>   if child.tag in ('name', 'league', ...): # etc.
>   if child.text:
>   setattr(context, child.tag, child.text)
>
>
>
> Stefan
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Parsing an XML document using ElementTree

2011-05-25 Thread Sithembewena Lloyd Dube
Hi Everyone,

Thanks for all your suggestions. I read up on gzip and urllib and also
learned in the process that I could use urllib2 as its the latest form of
that library.

Herewith my solution: I don't know how elegant it is, but it works just
fine.

def get_contests():
 url = '
http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po
'
 req = urllib2.Request(url)
 req.add_header('accept-encoding','gzip/deflate')
 opener = urllib2.build_opener()
 response = opener.open(req)
 compressed_data = response.read()
 compressed_stream = StringIO.StringIO(compressed_data)
 gzipper = gzip.GzipFile(fileobj=compressed_stream)
 data = gzipper.read()
 current_path = os.path.realpath(MEDIA_ROOT + '/xml-files/d.xml')
 data_file = open(current_path, 'w')
 data_file.write(data)
 data_file.close()
 xml_data = ET.parse(open(current_path, 'r'))
 contest_list = []
 for contest_parent_node in xml_data.getiterator('contest'):
  contest = Contest()
  for contest_child_node in contest_parent_node:
   if (contest_child_node.tag == "name" and
contest_child_node.text is not None and contest_child_node.text != ""):
contest.name = contest_child_node.text
   if (contest_child_node.tag == "league" and
contest_child_node.text is not None and contest_child_node.text != ""):
   contest.league = contest_child_node.text
   if (contest_child_node.tag == "acro" and
contest_child_node.text is not None and contest_child_node.text != ""):
   contest.acro = contest_child_node.text
   if (contest_child_node.tag == "time" and
contest_child_node.text is not None and contest_child_node.text != ""):
   contest.time = contest_child_node.text
   if (contest_child_node.tag == "home" and
contest_child_node.text is not None and contest_child_node.text != ""):
   contest.home = contest_child_node.text
   if (contest_child_node.tag == "away" and
contest_child_node.text is not None and contest_child_node.text != ""):
   contest.away = contest_child_node.text
      contest_list.append(contest)
 try:
  os.remove(current_path)
 except:
  pass
 return contest_list

Many thanks!

On Tue, May 24, 2011 at 12:35 PM, Stefan Behnel  wrote:

> Sithembewena Lloyd Dube, 24.05.2011 11:59:
>
>  I am trying to parse an XML feed and display the text of each child node
>> without any success. My code in the python shell is as follows:
>>
>> >>> import urllib
>> >>> from xml.etree import ElementTree as ET
>>
>> >>> content = urllib.urlopen('
>>
>> http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po
>> ')
>> >>> xml_content = ET.parse(content)
>>
>> I then check the xml_content object as follows:
>>
>> >>> xml_content
>> 
>>
>
> Well, yes, it does return an XML document, but not what you expect:
>
>  >>> urllib.urlopen('URL see above').read()
>  "\r\n  you must add 'accept-encoding' as
>  'gzip,deflate' to the header of your request\r
>  \n"
>
> Meaning, the server forces you to pass an HTTP header to the request in
> order to receive gzip compressed data. Once you have that, you must
> decompress it before passing it into ElementTree's parser. See the
> documentation on the gzip and urllib modules in the standard library.
>
> Stefan
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


[Tutor] Parsing an XML document using ElementTree

2011-05-24 Thread Sithembewena Lloyd Dube
Hi Everyone,

I am trying to parse an XML feed and display the text of each child node
without any success. My code in the python shell is as follows:

>>>import urllib
>>>from xml.etree import ElementTree as ET

>>>content = urllib.urlopen('
http://xml.matchbook.com/xmlfeed/feed?sport-id=&vendor=TEST&sport-name=&short-name=Po
')
>>>xml_content = ET.parse(content)

I then check the xml_content object as follows:

>>>xml_content


And now, to iterate through its child nodes and print out the text of each
node:

>>>for node in xml_content.getiterator('contest'):
...name = node.attrib.get('text')
...print name
...
>>>

Nothing is printed, even though the document does have 'contest' tags with
text in them. If I try to count the contest tags and increment an integer
(to see that the document is traversed) I get the same result - the int
remains at 0.

>>> i = 0
>>> for node in xml_content.getiterator('contest'):
... i += 1
...
>>> i
0

What am I getting wrong? Any hints would be appreciated.

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


Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python

2010-12-20 Thread Sithembewena Lloyd Dube
[?] Brett, that was very mischievous.

I wish I could help - am watching this thread with great curiosity, I could
learn something from it myself.

On Mon, Dec 20, 2010 at 11:40 PM, Brett Ritter wrote:

> On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano 
> wrote:
> >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file.
> >
> > I sympathize with you. I wonder who thought that building a 1GB XML file
> was
> > a good thing.
>
> XML is like violence: if it isn't working, try more.
>
> --
> Brett Ritter / SwiftOne
> swift...@swiftone.org
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
<<338.gif>>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Moving my C++ code to Python?

2010-10-17 Thread Sithembewena Lloyd Dube
Tis a pleasure, Paul. have no experience with Cython vs Boost.Python
benchmarking, but a real world user of the latter had something to say at
http://www.python.org/about/quotes/ (last review).

On Sun, Oct 17, 2010 at 11:29 PM, Paul  wrote:

> Thank you all for your kind suggestions.  It seem that cython and
> boost.python both look like a good suggestions; SWIG seems like a good
> tool, but may not meet my requirements.
> Quick google seems to suggest that boost.python is 7 times slower [1],
> however.  Any thoughts about speed performance between the two?
>
> [1]
> http://blog.chrischou.org/2010/02/28/simple-benchmark-between-cython-and-boost-python/
>
> Thank you,
> Paul
>
> And a link I found today.
> [2]
> http://koichitamura.blogspot.com/2008/06/various-ways-to-integrate-python-and-c.html
>
> On Sun, Oct 17, 2010 at 1:56 PM, Sithembewena Lloyd Dube
>  wrote:
> > Also have a look at Boost.Python:
> >
> > http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html
> >
> > On Fri, Oct 15, 2010 at 11:29 PM, Paul  wrote:
> >>
> >> Hi there,
> >>
> >> I've been using C/C++ for many years (python, just reading about it).
> >>
> >> I have a software written in C/C++ but considering porting most of it
> >> to python, as it seems like it's a better choice for decision making
> >> portion of the code.  I'm also thinking about having a 'matlab' like
> >> interface for reading, processing, and writing.
> >>
> >> In my current C++ code, I would read data into a vector of structs
> >> (which contains other simple vectors, strings, and structs) and it can
> >> be as large as 500MB to 2GB.  The data gets processed (requires random
> >> access).  The result is then written out.
> >>
> >> I would like to make modules for python.  The problem is that the
> >> vector of structs that is very large.  First, is it possible to pass
> >> such structures around to and from python and C/C++?  What would be
> >> the overhead cost of having a large structure that needs to be passed
> >> to and from the C/C++ modules?
> >>
> >> # I imagine I'd use the newly written software this way:
> >> >>> import c_stuff  # my C/C++ module
> >> >>> largestuff = c_stuff.read(file)  # read from disk
> >> >>> c_stuff.process(largestuff, someparams1)  # processing, requires
> >> >>> random access to the vector
> >> >>> c_stuff.process(largestuff, someparams2)
> >> ...
> >> >>> c_stuff.process(largestuff, someparams1) # the whole thing may
> >> >>> take a few minutes to days
> >> >>>
> >> >>> import python_stuff # some module written in python to process the
> >> >>> data as well
> >> >>>
> >> >>> python_stuff.process(largestuff, otherparams1)  # It's important
> that
> >> >>> this data can be read (and I hope written) by python code
> >> >>> python_stuff.process(largestuff, otherparams2)
> >> >>>
> >> >>> c_stuff.write(largestuff) #write result
> >>
> >> Thank you in advance,
> >> Paul
> >> ___
> >> Tutor maillist  -  Tutor@python.org
> >> To unsubscribe or change subscription options:
> >> http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> > --
> > Regards,
> > Sithembewena Lloyd Dube
> > http://www.lloyddube.com
> >
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Moving my C++ code to Python?

2010-10-17 Thread Sithembewena Lloyd Dube
Also have a look at Boost.Python:

http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html

On Fri, Oct 15, 2010 at 11:29 PM, Paul  wrote:

> Hi there,
>
> I've been using C/C++ for many years (python, just reading about it).
>
> I have a software written in C/C++ but considering porting most of it
> to python, as it seems like it's a better choice for decision making
> portion of the code.  I'm also thinking about having a 'matlab' like
> interface for reading, processing, and writing.
>
> In my current C++ code, I would read data into a vector of structs
> (which contains other simple vectors, strings, and structs) and it can
> be as large as 500MB to 2GB.  The data gets processed (requires random
> access).  The result is then written out.
>
> I would like to make modules for python.  The problem is that the
> vector of structs that is very large.  First, is it possible to pass
> such structures around to and from python and C/C++?  What would be
> the overhead cost of having a large structure that needs to be passed
> to and from the C/C++ modules?
>
> # I imagine I'd use the newly written software this way:
> >>> import c_stuff  # my C/C++ module
> >>> largestuff = c_stuff.read(file)  # read from disk
> >>> c_stuff.process(largestuff, someparams1)  # processing, requires random
> access to the vector
> >>> c_stuff.process(largestuff, someparams2)
> ...
> >>> c_stuff.process(largestuff, someparams1) # the whole thing may take
> a few minutes to days
> >>>
> >>> import python_stuff # some module written in python to process the data
> as well
> >>>
> >>> python_stuff.process(largestuff, otherparams1)  # It's important that
> this data can be read (and I hope written) by python code
> >>> python_stuff.process(largestuff, otherparams2)
> >>>
> >>> c_stuff.write(largestuff) #write result
>
> Thank you in advance,
> Paul
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-11 Thread Sithembewena Lloyd Dube
Thanks Shantanoo, finally got the hang of this.

2010/10/11 शंतनू (Shantanoo) 

> ===
> round(...)
> round(number[, ndigits]) -> floating point number
>
> Round a number to a given precision in decimal digits (default 0
> digits).
> This always returns a floating point number.  Precision may be
> negative.
> ===
>
> >>> round(1.23423,2)
> 1.23
> >>> round(1.23623,2)
> 1.24
>
>
> HTH.
>
> On Mon, Oct 11, 2010 at 13:51, Sithembewena Lloyd Dube 
> wrote:
>
>> Thanks everyone.
>>
>>
>> On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote:
>>
>>> On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube <
>>> zebr...@gmail.com> wrote:
>>>
>>>> I realise that one cannot have a half integer :) I meant how would one
>>>> round off to the first decimal nearest to either 0.5, or a whole number.
>>>>
>>>> Ugh...does anyone get what I'm trying to articulate? :)
>>>
>>>
>>> sample input/output cases are always useful.
>>>
>>> -Wayne
>>>
>>
>>
>>
>> --
>> Regards,
>> Sithembewena Lloyd Dube
>> http://www.lloyddube.com
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>


-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-11 Thread Sithembewena Lloyd Dube
Thanks everyone.

On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote:

> On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube  > wrote:
>
>> I realise that one cannot have a half integer :) I meant how would one
>> round off to the first decimal nearest to either 0.5, or a whole number.
>>
>> Ugh...does anyone get what I'm trying to articulate? :)
>
>
> sample input/output cases are always useful.
>
> -Wayne
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
@Evert, I didn't figure out that your response was a solution, thought it
was a question. Must be coffee time :P

I tried it and, for instance, the rounded value (9) / 2 gave me 4.0 Couldn't
get it until I noticed that @Joel divided the roudned figure by a decimal
2.0. That gave 4.5, which is what I was looking for.

Thanks to all.

On Fri, Oct 8, 2010 at 3:35 PM, Joel Goldstick wrote:

>
>
> On Fri, Oct 8, 2010 at 9:00 AM, Evert Rol  wrote:
>
>> > I realise that one cannot have a half integer :) I meant how would one
>> round off to the first decimal nearest to either 0.5, or a whole number.
>> >
>> > Ugh...does anyone get what I'm trying to articulate? :)
>>
>> Multiply by 2, round(), divide by 2?
>>
>
> That sounds like a good idea:
>
>>
>> >>> n = [1.0 + x/10.0 for x in range(10)]  # get some numbers to test
>> >>> n
>> [1.0, 1.1001, 1.2, 1.3, 1.3999, 1.5,
>> 1.6001, 1.7, 1.8, 1.8999]
>> >>> r = [round(2*x)/2.0 for x in n]   # double, round, then divide by 2.0
>> >>> r
>> [1.0, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 2.0, 2.0]
>> >>>
>>
>>
> --
> Joel Goldstick
>
>
> _______
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
Thanks everyone,

I need to round to the nearest half (finally occured).

Made some chnages to Wayne's code as follows:

x = 4.4348
if x % 1 >= 0.5:
   round(x) # gives 5.0 if  the the value of the expression x % 1 exceeds
0.5
else:
   x = round(x) + 0.5 # gives 4.5, as in this case.

Many thanks!



On Fri, Oct 8, 2010 at 3:00 PM, Evert Rol  wrote:

> > I realise that one cannot have a half integer :) I meant how would one
> round off to the first decimal nearest to either 0.5, or a whole number.
> >
> > Ugh...does anyone get what I'm trying to articulate? :)
>
> Multiply by 2, round(), divide by 2?
>
>
> >
> > On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube <
> zebr...@gmail.com> wrote:
> > Hi folks,
> >
> > Supposing I had the float 4.4348 and I wished to round it off to the
> nearest half-integer upwards or downwards, how would I go about it?
> >
> > Many thanks...
> >
> > --
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
I realise that one cannot have a half integer :) I meant how would one round
off to the first decimal nearest to either 0.5, or a whole number.

Ugh...does anyone get what I'm trying to articulate? :)

On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube
wrote:

> Hi folks,
>
> Supposing I had the float 4.4348 and I wished to round it off to the
> nearest half-integer upwards or downwards, how would I go about it?
>
> Many thanks...
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Rounding a Python float to the nearest half integer

2010-10-08 Thread Sithembewena Lloyd Dube
Hi folks,

Supposing I had the float 4.4348 and I wished to round it off to the nearest
half-integer upwards or downwards, how would I go about it?

Many thanks...

-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Django with TortoiseSVN (overwriting files)

2010-08-16 Thread Sithembewena Lloyd Dube
Hi Evert,

You are right, I actually intended the question for the Django list - it was
a posting error. Thanks for the answer, we did soon figure out to exclude
the settings file from any SVN checkins/ checkouts. We will simply apply
this rule as it should be simple enough to observe and to make any
corrections if a team member overwrites it.

Many thanks,
Lloyd


On Mon, Aug 16, 2010 at 10:54 AM, Evert Rol  wrote:

> > We have a project at work where myself and a co-developer are setting up
> Django. Our version control software of choice is TortoiseSVN.
> >
> > When doing a project update from SVN, it pulls the server version to the
> local project. When checking code in, it overwrites the server version of
> the code as expected.
> >
> > I wish to find out whether Django would work happily this way, as it
> requires one to use the django-admin.py tool to create projects and apps.
> Would overwriting these files/ directories with server versions compromise
> their integrity in a Django setup?
>
> That's not a Python question, more a Django question or a system-admin
> question. You'd be better off using the correct mailing list for that.
>
> That said, you should be fine, providing you re-sync the databases every
> time you 'svn up', to keep your model schema's in the database in sync (you
> can use fixtures to sync data in the database). Also the settings files may
> differ on the server or the local machines; it's best to keep those out of
> SVN, or have a little if-else clause in settings.py that takes care
> server-dependent settings.
> Otherwise, all your code can (and should) be machine/server agnostic, and
> thus easily transportable.
>
>  Evert
>
>
>


-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using Django with TortoiseSVN (overwriting files)

2010-08-16 Thread Sithembewena Lloyd Dube
Hi everyone,

We have a project at work where myself and a co-developer are setting up
Django. Our version control software of choice is TortoiseSVN.

When doing a project update from SVN, it pulls the server version to the
local project. When checking code in, it overwrites the server version of
the code as expected.

I wish to find out whether Django would work happily this way, as it
requires one to use the django-admin.py tool to create projects and apps.
Would overwriting these files/ directories with server versions compromise
their integrity in a Django setup?

-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: need computer advice from wise Tutors

2010-06-26 Thread Sithembewena Lloyd Dube
Hi Dick,

In that case, perhaps you could externally back up all your important stuff
and then format your hard disk. That way, any drivers etc intended for
Windows Vista will be wiped off, and you can then perform a clean
installation from your disk. Now, as far as upgrade disks go, I do not know
whether it will work as a "clean installation" disk, or whether it is only
meant for use on an existing windows installation (hence the name
"upgrade"). You might want to check that out first.

Let us know how it goes.

On Sat, Jun 26, 2010 at 10:31 PM, Richard D. Moores wrote:

> On Sat, Jun 26, 2010 at 11:55, Sithembewena Lloyd Dube
>  wrote:
> > Richard, I think you may go ahead without trepidation. I am not a Windows
> > fan at all, I prefer Ubuntu. But I started using Win. 7 at work about a
> > month ago, and I have to say it hasn't given me cause to grumble.
> >
> > Of course, a month is hardly sufficient time to have a strong opinion,
> but I
> > can tell you that it works just fine.
>
> Thanks, but I'm not worried about Win 7 working well when perfectly
> installed. I'm concerned that if I upgrade to 7 from Vista using the
> upgrade disk I have, that THAT 7 on my laptop will end up with
> problems caused by the install.
>
> Dick
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: need computer advice from wise Tutors

2010-06-26 Thread Sithembewena Lloyd Dube
Richard, I think you may go ahead without trepidation. I am not a Windows
fan at all, I prefer Ubuntu. But I started using Win. 7 at work about a
month ago, and I have to say it hasn't given me cause to grumble.

Of course, a month is hardly sufficient time to have a strong opinion, but I
can tell you that it works just fine.

On Sat, Jun 26, 2010 at 2:39 PM, Steven D'Aprano wrote:

> On Sat, 26 Jun 2010 08:53:57 pm you wrote:
> > > Why did they stop? Could it be because the upgrade is sometimes
> > > difficult and requires huge amount of manual effort to get it
> > > working, far more than $40 will cover?
> >
> > Do you know that it does? Please tell me what you know.
>
> I have no idea. That's why I asked:
>
> > > Cynical? Who, me?
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Repeat function until...

2010-06-23 Thread Sithembewena Lloyd Dube
My code has two bugs. If any command other than an int is entered, it falls
over ungracefully. Also, if any integers other than 1 or 0 are entered
successively it exits the Python interpreter.

I thought I would point this out so as not to mislead the OP.

On Wed, Jun 23, 2010 at 8:09 PM, Alan Gauld wrote:

>
> "Steven D'Aprano"  wrote
>
>> The easiest way is to just run forever, and stop when the user interrupts
>> it with ctrl-D (or ctrl-Z on Windows):
>>
>
> I think that would be Ctrl-C on both.
> Ctrl-D/Z is EOF not Interrupt. Certainly on Windows Ctrl-Z won't interrupt
> a loop.
>
> But the principle is good and definitely the easiest option.
>
> --
> Alan Gauld
> 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
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Repeat function until...

2010-06-23 Thread Sithembewena Lloyd Dube
^^ I meant time.sleep(x), rather. Please excuse the double post.

On Wed, Jun 23, 2010 at 4:29 PM, Sithembewena Lloyd Dube
wrote:

> My two cents' worth added below. Seems to do what you want. You probably
> want to call sys.wait(x) after printing an error, so it can be read before
> exiting?
>
> import os, sys, time
>
>
> def check(host):
>try:
>output = os.popen('ping -ns 1 %s' % host).read()
>alive = output.find('Reply from')
>print alive
>if alive is -1:
>print '%s \t\t DOWN ' % host
>else:
>print '%s \t\t OK' % host
>
>except OSError, e:
>print e
>sys.exit()
>
>while alive != -1:
>try:
>time.sleep(4)
>check(host)
>
>except KeyboardInterrupt, k:
>sys.exit()
>
> def get_command():
> prompt = input('Start now? Use 1 to start or 0 to exit.\n')
> return prompt
>
> if __name__ == '__main__':
> cmd = get_command()
> if cmd == 1:
> check('localhost')
>
> elif cmd == 0:
> sys.exit()
>
> else:
> get_command()
>
> On Wed, Jun 23, 2010 at 2:29 PM, Nethirlon .  wrote:
>
>> Hello everyone,
>>
>> I'm new at programming with python and have a question about how I can
>> solve my problem the correct way. Please forgive my grammar, English
>> is not my primary language.
>>
>> I'm looking for a way to repeat my function every 30 seconds.
>>
>> As an example I have written a ping function. But I would like this
>> function to repeat itself every 30 seconds, without stopping until I
>> give it a STOP command (if such a thing exists.)
>>
>> Code:
>> import os, sys
>>
>> def check(host):
>>try:
>>output = os.popen('ping -ns 1 %s' % host).read()
>>alive = output.find('Reply from')
>>print alive
>>if alive is -1:
>>print '%s \t\t DOWN ' % host
>>else:
>>print '%s \t\t OK' % host
>>except OSError, e:
>>print e
>>    sys.exit()
>>
>> check('www.google.com')
>>
>> Let me know if anything is unclear or if there are other
>> recommendations about doing some parts different.
>>
>> Kind regards,
>> Nethirlon
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Repeat function until...

2010-06-23 Thread Sithembewena Lloyd Dube
My two cents' worth added below. Seems to do what you want. You probably
want to call sys.wait(x) after printing an error, so it can be read before
exiting?

import os, sys, time


def check(host):
   try:
   output = os.popen('ping -ns 1 %s' % host).read()
   alive = output.find('Reply from')
   print alive
   if alive is -1:
   print '%s \t\t DOWN ' % host
   else:
   print '%s \t\t OK' % host

   except OSError, e:
   print e
   sys.exit()

   while alive != -1:
   try:
   time.sleep(4)
   check(host)

   except KeyboardInterrupt, k:
   sys.exit()

def get_command():
prompt = input('Start now? Use 1 to start or 0 to exit.\n')
return prompt

if __name__ == '__main__':
cmd = get_command()
if cmd == 1:
check('localhost')

elif cmd == 0:
sys.exit()

else:
get_command()

On Wed, Jun 23, 2010 at 2:29 PM, Nethirlon .  wrote:

> Hello everyone,
>
> I'm new at programming with python and have a question about how I can
> solve my problem the correct way. Please forgive my grammar, English
> is not my primary language.
>
> I'm looking for a way to repeat my function every 30 seconds.
>
> As an example I have written a ping function. But I would like this
> function to repeat itself every 30 seconds, without stopping until I
> give it a STOP command (if such a thing exists.)
>
> Code:
> import os, sys
>
> def check(host):
>try:
>output = os.popen('ping -ns 1 %s' % host).read()
>alive = output.find('Reply from')
>print alive
>if alive is -1:
>print '%s \t\t DOWN ' % host
>else:
>print '%s \t\t OK' % host
>except OSError, e:
>print e
>sys.exit()
>
> check('www.google.com')
>
> Let me know if anything is unclear or if there are other
> recommendations about doing some parts different.
>
> Kind regards,
> Nethirlon
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Beautiful Soup to find an HTML element by its class attribute

2010-05-30 Thread Sithembewena Lloyd Dube
Thanks all, I found the solution: using the attributes dictionary as
follows:

weather_row = soup.findAll('tr', { "class" : "rs1" })

Many thanks.

On Mon, May 31, 2010 at 1:32 AM, Sithembewena Lloyd Dube
wrote:

> Hi everyone,
>
> I am using urllib to scrape an HTML page, and creating an instance of
> BeautifulSoup as follows:
> *
> *from BeautifulSoup import BeautifulSoup
> import re, urllib
>
> doc = urllib.urlopen(my_url).read()
> soup = BeautifulSoup(''.join(doc))
>
> #print soup.prettify()
> weather_row = soup.findAll('tr', class="rs1")
> print weather_row
>
> The call to soup.findAll is failing because "class" is a reserved keyword
> in Python. Using BeautifulSoup, how can I find a specific table row whose
> only identifying attribute is its CSS class?
>
> Thanks.
>
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using Beautiful Soup to find an HTML element by its class attribute

2010-05-30 Thread Sithembewena Lloyd Dube
Hi everyone,

I am using urllib to scrape an HTML page, and creating an instance of
BeautifulSoup as follows:
*
*from BeautifulSoup import BeautifulSoup
import re, urllib

doc = urllib.urlopen(my_url).read()
soup = BeautifulSoup(''.join(doc))

#print soup.prettify()
weather_row = soup.findAll('tr', class="rs1")
print weather_row

The call to soup.findAll is failing because "class" is a reserved keyword in
Python. Using BeautifulSoup, how can I find a specific table row whose only
identifying attribute is its CSS class?

Thanks.

-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using Beautiful Soup to find an HTML element by its class attribute

2010-05-30 Thread Sithembewena Lloyd Dube
Hi everyone,

I am using urllib to scrape an HTML page, and creating an instance of
BeautifulSoup as follows:
*
*from BeautifulSoup import BeautifulSoup
import re, urllib

doc = urllib.urlopen("
http://weather.za.msn.com/local.aspx?wealocations=wc:SFXX0010&q=Cape+Town%2c+Western+Cape
").read()
soup = BeautifulSoup(''.join(doc))

#print soup.prettify()
weather_row = soup.findAll('tr', class="rs1")
print weather_row

The call to soup.findAll is failing because "class" is a reserved keyword in
Python. Using BeautifulSoup, how can I find a specific table row whose only
identifying attribute is its CSS class?

Thanks.

-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python file escaping issue?

2010-02-22 Thread Sithembewena Lloyd Dube
I believe i encountered repr()in the Python tutorial, but i had not kept the
relevance of it in my memory..

On Mon, Feb 22, 2010 at 9:35 PM, Sithembewena Lloyd Dube
wrote:

> @spr, thanks for the explanation, especially on representations of strings.
> To think that i freely used repr(variable_x) without fully understanding the
> meaning and the power of that function..
>
>
>
>
> On Mon, Feb 22, 2010 at 9:37 AM, spir  wrote:
>
>> Just a little complement to Steven's excellent explanation:
>>
>> On Mon, 22 Feb 2010 10:01:06 +1100
>> Steven D'Aprano  wrote:
>>
>> [...]
>>
>> > So if you write a pathname like this:
>> >
>> > >>> path = 'C:\datafile.txt'
>> > >>> print path
>> > C:\datafile.txt
>> > >>> len(path)
>> > 15
>> >
>> > It *seems* to work, because \d is left as backlash-d. But then you do
>> > this, and wonder why you can't open the file:
>>
>> I consider this misleading, since it can only confuse newcomers. Maybe
>> "lonely" single backslashes (not forming a "code" with following
>> character(s)) should be invalid. Meaning literal backslashes would always be
>> doubled (in plain, non-raw, strings). What do you think?
>>
>> > But if the escape is not a special character:
>> >
>> > >>> s = 'abc\dz'  # nothing special
>> > >>> print s
>> > abc\dz
>> > >>> print repr(s)
>> > 'abc\\dz'
>> > >>> len(s)
>> > 6
>> >
>> > The double backslash is part of the *display* of the string, like the
>> > quotation marks, and not part of the string itself. The string itself
>> > only has a single backslash and no quote marks.
>>
>> This "display" is commonly called "representation", thus the name of the
>> function repr(). It is a string representation *for the programmer* only,
>> both on input and output:
>> * to allow one writing, in code itself, string literal constants
>> containing special characters, in a practical manner (eg file pathes/names)
>> * to allow one checking the actual content of string values, at testing
>> time
>>
>> The so-called interactive interpreter outputs representations by default.
>> An extreme case:
>> >>> s = "\\"
>> >>> s
>> '\\'
>> >>> print s, len(s)
>> \ 1
>> >>> print repr(s), len(repr(s))
>> '\\' 4
>> >>>
>> The string holds 1 char; its representation (also a string, indeed) holds
>> 4.
>>
>> > The best advice is to remember that Windows allows both forward and
>> > backwards slashes as the path separator, and just write all your paths
>> > using the forward slash:
>> >
>> > 'C:/directory/'
>> > 'C:textfile.txt'
>>
>> Another solution is to take the habit to always escape '\' by doubling it.
>>
>>
>> Denis
>> 
>>
>> la vita e estrany
>>
>> http://spir.wikidot.com/
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python file escaping issue?

2010-02-22 Thread Sithembewena Lloyd Dube
@spr, thanks for the explanation, especially on representations of strings.
To think that i freely used repr(variable_x) without fully understanding the
meaning and the power of that function..



On Mon, Feb 22, 2010 at 9:37 AM, spir  wrote:

> Just a little complement to Steven's excellent explanation:
>
> On Mon, 22 Feb 2010 10:01:06 +1100
> Steven D'Aprano  wrote:
>
> [...]
>
> > So if you write a pathname like this:
> >
> > >>> path = 'C:\datafile.txt'
> > >>> print path
> > C:\datafile.txt
> > >>> len(path)
> > 15
> >
> > It *seems* to work, because \d is left as backlash-d. But then you do
> > this, and wonder why you can't open the file:
>
> I consider this misleading, since it can only confuse newcomers. Maybe
> "lonely" single backslashes (not forming a "code" with following
> character(s)) should be invalid. Meaning literal backslashes would always be
> doubled (in plain, non-raw, strings). What do you think?
>
> > But if the escape is not a special character:
> >
> > >>> s = 'abc\dz'  # nothing special
> > >>> print s
> > abc\dz
> > >>> print repr(s)
> > 'abc\\dz'
> > >>> len(s)
> > 6
> >
> > The double backslash is part of the *display* of the string, like the
> > quotation marks, and not part of the string itself. The string itself
> > only has a single backslash and no quote marks.
>
> This "display" is commonly called "representation", thus the name of the
> function repr(). It is a string representation *for the programmer* only,
> both on input and output:
> * to allow one writing, in code itself, string literal constants containing
> special characters, in a practical manner (eg file pathes/names)
> * to allow one checking the actual content of string values, at testing
> time
>
> The so-called interactive interpreter outputs representations by default.
> An extreme case:
> >>> s = "\\"
> >>> s
> '\\'
> >>> print s, len(s)
> \ 1
> >>> print repr(s), len(repr(s))
> '\\' 4
> >>>
> The string holds 1 char; its representation (also a string, indeed) holds
> 4.
>
> > The best advice is to remember that Windows allows both forward and
> > backwards slashes as the path separator, and just write all your paths
> > using the forward slash:
> >
> > 'C:/directory/'
> > 'C:textfile.txt'
>
> Another solution is to take the habit to always escape '\' by doubling it.
>
>
> Denis
> 
>
> la vita e estrany
>
> http://spir.wikidot.com/
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python file escaping issue?

2010-02-22 Thread Sithembewena Lloyd Dube
Wow, thank you Steve. This certainly answers a few questions i've had
simmering for a while.

On Mon, Feb 22, 2010 at 1:01 AM, Steven D'Aprano wrote:

> On Mon, 22 Feb 2010 05:22:10 am Sithembewena Lloyd Dube wrote:
> > Hi all,
> >
> > I'm trying to read a file (Python 2.5.2, Windows XP) as follows:
> >
> > assignment_file = open('C:\Documents and Settings\coderoid\My
> > Documents\Downloads\code_sample.txt', 'r+').readlines()
> > new_file = open(new_file.txt, 'w+')
> > for line in assignment_file:
> > new_file.write(line)
> >
> > new_file.close()
> > assignment_file.close()
> >
> > When the code runs, the file path has the slashes converted to double
> > slashes. When  try to escape them, i just seemto add more slashes.
> > What am i missing?
>
> An understanding of how backslash escapes work in Python.
>
> Backslashes in string literals (but not in text you read from a file,
> say) are used to inject special characters into the string, just like C
> and other languages do. These backslash escapes include:
>
> \t tab
> \n newline
> \f formfeed
> \\ backslash
>
> and many others. Any other non-special backslash is left alone.
>
> So when you write a string literal including backslashes and a special
> character, you get this:
>
> >>> s = 'abc\tz'  # tab
> >>> print s
> abc z
> >>> print repr(s)
> 'abc\tz'
> >>> len(s)
> 5
>
> But if the escape is not a special character:
>
> >>> s = 'abc\dz'  # nothing special
> >>> print s
> abc\dz
> >>> print repr(s)
> 'abc\\dz'
> >>> len(s)
> 6
>
> The double backslash is part of the *display* of the string, like the
> quotation marks, and not part of the string itself. The string itself
> only has a single backslash and no quote marks.
>
> So if you write a pathname like this:
>
> >>> path = 'C:\datafile.txt'
> >>> print path
> C:\datafile.txt
> >>> len(path)
> 15
>
> It *seems* to work, because \d is left as backlash-d. But then you do
> this, and wonder why you can't open the file:
>
> >>> path = 'C:\textfile.txt'
> >>> print path
> C:  extfile.txt
> >>> len(path)
> 14
>
>
> Some people recommend using raw strings. Raw strings turn off backslash
> processing, so you can do this:
>
> >>> path = r'C:\textfile.txt'
> >>> print path
> C:\textfile.txt
>
> But raw strings were invented for the regular expression module, not for
> Windows pathnames, and they have a major limitation: you can't end a
> raw string with a backslash.
>
> >>> path = r'C:\directory\'
>  File "", line 1
>path = r'C:\directory\'
>  ^
> SyntaxError: EOL while scanning single-quoted string
>
>
> The best advice is to remember that Windows allows both forward and
> backwards slashes as the path separator, and just write all your paths
> using the forward slash:
>
> 'C:/directory/'
> 'C:textfile.txt'
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python file escaping issue?

2010-02-21 Thread Sithembewena Lloyd Dube
@Wayne, sorry to have replied to you directly..

On Sun, Feb 21, 2010 at 9:23 PM, Sithembewena Lloyd Dube
wrote:

> Solved by moving the file just under C:\
>
> Must be an issue with directory name spaces.
>
> By the way, my code was riddled with bugs. readlines() returns a list
> object, which does not have a close method - and i had not placed quotes
> around the new_file argument.
>
>
>
>
> On Sun, Feb 21, 2010 at 9:01 PM, Sithembewena Lloyd Dube <
> zebr...@gmail.com> wrote:
>
>> Hi Wayne,
>>
>> Thank you for responding. I did try declaring it as a raw string, and what
>> i get is pretty much the same result (slashes in the path become double
>> slashes, so naturally the file is not found).
>>
>> I did try googling around for how to handle/ escape file paths, but i
>> cannot seem to dig up anything useful.
>>
>> Regards,
>> Sithembewena
>>
>>
>> On Sun, Feb 21, 2010 at 8:41 PM, Wayne Werner wrote:
>>
>>> On Sun, Feb 21, 2010 at 12:22 PM, Sithembewena Lloyd Dube <
>>> zebr...@gmail.com> wrote:
>>>
>>>> Hi all,
>>>>
>>>> I'm trying to read a file (Python 2.5.2, Windows XP) as follows:
>>>>
>>>> assignment_file = open('C:\Documents and Settings\coderoid\My
>>>> Documents\Downloads\code_sample.txt', 'r+').readlines()
>>>> new_file = open(new_file.txt, 'w+')
>>>> for line in assignment_file:
>>>> new_file.write(line)
>>>>
>>>> new_file.close()
>>>> assignment_file.close()
>>>>
>>>> When the code runs, the file path has the slashes converted to double
>>>> slashes. When  try to escape them, i just seemto add more slashes. What am 
>>>> i
>>>> missing?
>>>
>>>
>>> try using the r to declare it as a raw string:
>>> >>> filename = r'C:\Documents and Settings\coderoid\otherstuff'
>>> >>> filename
>>> 'C:\\Documents and Settings\\coderoid\\otherstuff'
>>>
>>> that should work.
>>>
>>> HTH,
>>> Wayne
>>>
>>>
>>> --
>>> To be considered stupid and to be told so is more painful than being
>>> called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every
>>> weakness, every vice, has found its defenders, its rhetoric, its ennoblement
>>> and exaltation, but stupidity hasn’t. - Primo Levi
>>>
>>
>>
>>
>> --
>> Regards,
>> Sithembewena Lloyd Dube
>> http://www.lloyddube.com
>>
>
>
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python file escaping issue?

2010-02-21 Thread Sithembewena Lloyd Dube
Hi all,

I'm trying to read a file (Python 2.5.2, Windows XP) as follows:

assignment_file = open('C:\Documents and Settings\coderoid\My
Documents\Downloads\code_sample.txt', 'r+').readlines()
new_file = open(new_file.txt, 'w+')
for line in assignment_file:
new_file.write(line)

new_file.close()
assignment_file.close()

When the code runs, the file path has the slashes converted to double
slashes. When  try to escape them, i just seemto add more slashes. What am i
missing?


-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor