[Tutor] Modules and variable usage in functions from other files

2015-12-24 Thread Richard Bekenstein
   Dear All,

   I have a main .py script which contains code that occasionally calls some
   functions. This script calls in these defined functions from other .py
   files in the same directory as the main .py. However, even after importing
   the functions from the different .py files, I get error messages, such as
   "global name 'np' is not defined" even though I define "import numpy as
   np" initially in my main .py script.
   To show some detail, I import some modules at the beggining of my main .py
   script as such:

   from scipy.constants import m_p,G,k
   import numpy as np
   from math import cos, pi, floor
   import matplotlib.pyplot as plt
   from findall import findall
   from pylab import *
   import sys
   import timeit
   from copy import copy
   from array import array
   from bol_heatFTCS_nonadaptive import heatFTCS #this is my first
   user-defined function that calls the defined function 'heatFTCS' from file
   'bol_heatFTCS_nonadaptive.py'
   from bol_runge_kutta_functions import dy1_dt,dy2_dt,dy3_dt,dy5_dt
   #this is my second user-defined function that calls the defined functions
   'dyi_dt's from file 'bol_runge_kutta_functions.py'
   from bol_runge_kutta_evl_nonadaptive import runge_kutta_evl #this is
   my first user-defined function that calls the defined function
   'runge_kutta_evl' from file 'bol_runge_kutta_evl_nonadaptive.py'
   m_range,p_range,T_range,r_range,rho_range = np.loadtxt('(there are
   some other directories here that don't matter for this
   discussion)/planetary_structure_params.txt', unpack=True,
   usecols=[0,1,2,3,4])  #imports some data to 5 arrays


   When I run this main.py, I get an error message from the imported
   'runge_kutta_evl' function that numpy is not defined. Why is it that even
   though I have it defined in my script it does not recognize it in my
   function called to the script? It turns out that even if I then state
   'import numpy as np' in my 'runge_kutta_evl' function, I then another
   error that 'r_range' is not defined. But aren't 'r_range' and 'np'
   supposed to be accesible to all called functions in the main .py?

   Thank you,
   R.B.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Modules and variable usage in functions from other files

2015-12-24 Thread Alan Gauld
On 24/12/15 01:45, Richard Bekenstein wrote:

>from scipy.constants import m_p,G,k
>import numpy as np
>from math import cos, pi, floor
...
>from bol_runge_kutta_evl_nonadaptive import runge_kutta_evl 

>When I run this main.py, I get an error message from the imported
>'runge_kutta_evl' function that numpy is not defined. Why is it that even
>though I have it defined in my script it does not recognize it in my
>function called to the script? 

The function is in the other module. You only imported the names
into your main module, that does not make them visible in the
other modules.

Making the function *name* visible in main (by importing it) does
not make the function *object* to which the name refers part of
main. The function object remains in the original module. So
you need to import all of the names the function uses into
its module too.(**)

> It turns out that even if I then state
>'import numpy as np' in my 'runge_kutta_evl' function, I then another
>error that 'r_range' is not defined. But aren't 'r_range' and 'np'
>supposed to be accesible to all called functions in the main .py?

They are visible in main but they are not visible in the
other modules that main imports. Importing only makes things
visible to the module doing the importing. Python has no
concept of a "main" module as a kind of global namespace,
it treats your main.py just like any other module. It only
exposes the names you import to main.py Every module must
import the names it needs and in that way becomes an
independent, and therefore reusable, piece of code.

BTW In future, please include the full error message in any
posts. Although in this case we could just about guess
what it looked like, its better to see a cut n' paste
version in case there are subtle clues hidden in the message.

(**)The fact you are getting these kinds of errors suggests
you are not testing the individual modules in isolation
from your main program. It's a good habit to (as a minimum!)
import any modules you write into the interpreter and exercise
the functions manually to be sure they work. That would have
revealed that the modules did not have access to all the
names they needed to work. Even better would be to start
using some of the automated test tools such as unittest
or doctest to ensure your modules work consistently after
each change.

-- 
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


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


[Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread boB Stepp
My Google-fu is weak on this question.  I keep getting lots of hits on
web scraping, but my interest is actually as follows:

I find myself doing the same boring, repetitive tasks by hand, which
amounts to copying certain information from one program and putting it
into other programs.  None of these programs, to my knowledge, have a
publicly accessible API.  Are there ways to programmatically
accurately click the right buttons (or check boxes, radio buttons,
etc.), copy desired fields and then switch to another program and
paste the desired information into the desired fields, accurately
clicking all things that need to be clicked, etc.?  This is mostly a
Windows-based scenario, but if the techniques (if they exist) can be
abstracted to any OS I can find plenty of uses elsewhere as well!

TIA!
Merry Christmas!!!

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


Re: [Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread Jim Byrnes

On 12/24/2015 08:54 AM, boB Stepp wrote:

My Google-fu is weak on this question.  I keep getting lots of hits on
web scraping, but my interest is actually as follows:

I find myself doing the same boring, repetitive tasks by hand, which
amounts to copying certain information from one program and putting it
into other programs.  None of these programs, to my knowledge, have a
publicly accessible API.  Are there ways to programmatically
accurately click the right buttons (or check boxes, radio buttons,
etc.), copy desired fields and then switch to another program and
paste the desired information into the desired fields, accurately
clicking all things that need to be clicked, etc.?  This is mostly a
Windows-based scenario, but if the techniques (if they exist) can be
abstracted to any OS I can find plenty of uses elsewhere as well!

TIA!
Merry Christmas!!!



I don't know if there is a Python solution, as I am just starting to 
read up on using Python on the web. Either iMarcos or GreaseMonkey could 
probably do what you what you want.


Both are browser extensions.  iMacros is available for both Firefox and 
Chrome, GreaseMonkey is available for Firefox, but I'm not sure about 
Chrome. Both also have user forums.


Regards,  Jim

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


Re: [Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread Oscar Benjamin
On 24 Dec 2015 14:55, "boB Stepp"  wrote:
>
> My Google-fu is weak on this question.  I keep getting lots of hits on
> web scraping, but my interest is actually as follows:
>
> I find myself doing the same boring, repetitive tasks by hand, which
> amounts to copying certain information from one program and putting it
> into other programs.  None of these programs, to my knowledge, have a
> publicly accessible API.  Are there ways to programmatically
> accurately click the right buttons (or check boxes, radio buttons,
> etc.), copy desired fields and then switch to another program and
> paste the desired information into the desired fields, accurately
> clicking all things that need to be clicked, etc.?  This is mostly a
> Windows-based scenario, but if the techniques (if they exist) can be
> abstracted to any OS I can find plenty of uses elsewhere as well!

This is certainly doable for Windows. You can make the mouse move and click
on things, send keypress events. Look for Windows specific modules like
pywin32 etc. I've done this from Python possibly using ctypes but I don't
remember exactly.

For OSX I think there's a policy that prohibits programmatic control of
user interfaces. There will be solutions for all X11 systems (Linux etc) as
well.

Don't expect anything for all OSes but searching PyPI is probably
worthwhile...

...I googled "Python windows control mouse" and got:

http://stackoverflow.com/questions/1181464/controlling-mouse-with-python

Also this:
https://pypi.python.org/pypi/PyAutoGUI

Which apparently does solve the problem for the main platforms (contrary to
what I wrote above).

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


[Tutor] trouble with beautiful soup

2015-12-24 Thread marcus lütolf
dear pythonistas,
for hours I'am traying to use Beautiful Soup for scraping some websites as an 
exercise.
But each time I run the code below:

>>> import urllib
>>> from BeautifulSpoup import *

>>> url =  raw_input( 'Enter -')
  
>>> html = urllib.urlopen(url).read()

>>> soup = BeautifulSoup(html)
>>> print soup

I am getting the following trace back without beeing prompted for an input:

Traceback (most recent call last):
  File "C:/Python27/Beautiful Soup_ex1.py", line 2, in 
from beautifulspoup import *
ImportError: No module named beautifulspoup

I have installed and unzipped etc. the latest file from www.crummy.com many 
times. 
If I click setup.py the command window appears only a fraction of a second.
??

Happy holidays and thanks for help, Marcus.





---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

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


Re: [Tutor] trouble with beautiful soup

2015-12-24 Thread Jos Kerc
Hi, see below.

On Thu, Dec 24, 2015 at 6:21 PM, marcus lütolf
 wrote:
> dear pythonistas,
> for hours I'am traying to use Beautiful Soup for scraping some websites as an 
> exercise.
> But each time I run the code below:
>
 import urllib
 from BeautifulSpoup import *
>
 url =  raw_input( 'Enter -')
>
 html = urllib.urlopen(url).read()
>
 soup = BeautifulSoup(html)
 print soup
>
> I am getting the following trace back without beeing prompted for an input:
>
> Traceback (most recent call last):
>   File "C:/Python27/Beautiful Soup_ex1.py", line 2, in 
> from beautifulspoup import *
> ImportError: No module named beautifulspoup


Change beautifulspoup to beautifulsoup It's just a typo...

>
> I have installed and unzipped etc. the latest file from www.crummy.com many 
> times.
> If I click setup.py the command window appears only a fraction of a second.
> ??
>
> Happy holidays and thanks for help, Marcus.
>

HTH & nice holidays too!
>
>
>
>
> ---
> Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
> https://www.avast.com/antivirus
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] reading an input stream

2015-12-24 Thread richard kappler
I have to create a script that reads  xml data over a tcp socket, parses it
and outputs it to console. Not so bad, most of which I already know how to
do. I know how to set up the socket, though I am using a file for
development and testing, am using lxml and have created an xslt that does
what I want with the xml, and it outputs it to console.

What I'm not really sure of, each xml 'message' is preceeded by an STX
(\x02) and ends with an ETX (\x03). These 'messages' (Danny, are you noting
I don't say -lines- anymore? :-)  ) need to be parsed and output whole as
opposed to partial.

My concern is, there will actually be numerous machines sending data to the
tcp socket, so it's entirely likely the messages will come in fragmented
and the fragments will need to be held until complete so they can be sent
on whole to the parser. While this is the job of tcp, my script needs to

I think what I need to do would be analogous to (pardon if I'm using the
wrong terminology, at this poing in the discussion I am officially out of
my depth) sending the input stream to a buffer(s) until  the ETX for that
message comes in, shoot the buffer contents to the parser while accepting
the next STX + message fragment into the buffer, or something analogous.

Any guidance here?

And Merry Christmas / Happy Holidays / Festive whatever you celebrate!!!

regards, Richard

-- 

"I want to makes shoes!" -> elf fleeing the fire engulfed Keebler Tree
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with beautiful soup

2015-12-24 Thread Danny Yoo
The error message suggests that something is misspelled.  Let's look
at the message again:

>> Traceback (most recent call last):
>>   File "C:/Python27/Beautiful Soup_ex1.py", line 2, in 
>> from beautifulspoup import *
>> ImportError: No module named beautifulspoup


A "spoup" is, according to Urban Dictionary, a: "A descriptive word,
mainly used in the North Northumberland border area...it describes a
person that is being obviously silly, but they do not notice."
http://www.urbandictionary.com/define.php?term=Spoup

A "soup" is something slightly different.  As Jos suggests, try
correcting the spelling and try again.

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


Re: [Tutor] Using python 3 on Ubuntu 14.04

2015-12-24 Thread Jim Byrnes

On 12/23/2015 07:52 PM, Alan Gauld wrote:

On 23/12/15 23:15, Nnamdi Anyanwu wrote:

If you're installing modules with pip, install pip3 and install the
appropriate modules using pip3 instead of using regular pip.


Most v3 modules are also available via the Ubuntu package system
so you can install via synaptic which I tend to find more reliable
than pip.

YMMV,



Thanks for all the info guys. I got myself confused because I thought 
that python 3 was the default for Ubuntu 14.04, but it is just installed 
by default. I realize now that the modules need to be installed in the 
proper environment.


I know Pythoncard is not maintained any more. I have one program I wrote 
using it that I use often so I wanted to see it worked on 14.04. It will 
be a good learning experience to rewrite it for python 3 using something 
else.


Regards,  Jim





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


Re: [Tutor] trouble with beautiful soup

2015-12-24 Thread marcus lütolf
dear pythonistas, 
Jos, Alex, Danny, Francois and Rodney

I am absolutely thrilled about your prompt responses to my problem on this 
Christmas' eve and I am somewhat ashamed by my typing error.
However, neither the correct typing of beautifulsoup nor the replacment of the 
2nd line of my code by 
relieved the trace back mentioned in my previous mail.

Bless you, Marcus.


-Ursprüngliche Nachricht-
Von: Jos Kerc [mailto:josk...@gmail.com] 
Gesendet: Donnerstag, 24. Dezember 2015 19:50
An: marcus lütolf 
Cc: tutor@python.org
Betreff: Re: [Tutor] trouble with beautiful soup

Hi, see below.

On Thu, Dec 24, 2015 at 6:21 PM, marcus lütolf  
wrote:
> dear pythonistas,
> for hours I'am traying to use Beautiful Soup for scraping some websites as an 
> exercise.
> But each time I run the code below:
>
 import urllib
 from BeautifulSpoup import *
>
 url =  raw_input( 'Enter -')
>
 html = urllib.urlopen(url).read()
>
 soup = BeautifulSoup(html)
 print soup
>
> I am getting the following trace back without beeing prompted for an input:
>
> Traceback (most recent call last):
>   File "C:/Python27/Beautiful Soup_ex1.py", line 2, in 
> from beautifulspoup import *
> ImportError: No module named beautifulspoup


Change beautifulspoup to beautifulsoup It's just a typo...

>
> I have installed and unzipped etc. the latest file from www.crummy.com many 
> times.
> If I click setup.py the command window appears only a fraction of a second.
> ??
>
> Happy holidays and thanks for help, Marcus.
>

HTH & nice holidays too!
>
>
>
>
> ---
> Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
> https://www.avast.com/antivirus
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

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


Re: [Tutor] reading an input stream

2015-12-24 Thread Danny Yoo
> I think what I need to do would be analogous to (pardon if I'm using the
> wrong terminology, at this poing in the discussion I am officially out of
> my depth) sending the input stream to a buffer(s) until  the ETX for that
> message comes in, shoot the buffer contents to the parser while accepting
> the next STX + message fragment into the buffer, or something analogous.

Yes, I agree.  It sounds like you have one process read the socket and
collect chunks of bytes delimited by the STX markers.  It can then
send those chunks to the XML parser.


We can imagine one process that reads the socket and spits out a list
of byte chunks:

chunks = readDelimitedChunks(socket)

and another process that parses those chunks and does something with them:

for chunk in chunks:



It would be nice if we could organize the program like this.  But one
problem is that chunks might not be finite!  The socket might keep on
returning bytes.  If it keeps returning bytes, we can't possibly
return a finite list of the chunked bytes.


What we really want is something like:

chunkStream = readDelimitedChunks(socket)
for chunk in chunkStream:


where chunkStream is itself like a socket: it should be something that
we can repeatedly read from as if it were potentially infinite.


We can actually do this, and it isn't too bad.  There's a mechanism in
Python called a generator that allows us to write function-like things
that consume streams of input and produce streams of output.  Here's a
brief introduction to them.

For example, here's a generator that knows how to produce an infinite
stream of numbers:

##
def nums():
n = 0
while True:
yield n
n += 1
##

What distinguishes a generator from a regular function?  The use of
"yield".  A "yield" is like a return, but rather than completely
escape out of the function with the return value, this generator will
remember what it was doing  at that time.  Why?  Because it can
*resume* itself when we try to get another value out of the generator.

Let's try it out:

#

>>> numStream = nums()
>>> numStream.next()
0
>>> numStream.next()
1
>>> numStream.next()
2
>>> numStream.next()
3
>>> numStream.next()
4
#

Every next() we call on a generator will restart it from where it left
off, until it reaches its next "yield".  That's how we get this
generator to return an infinite sequence of things.


That's how we produce infinite sequences.  And we can write another
generator that knows how to take a stream of numbers, and square each
one.


def squaring(stream):
for n in stream:
yield n



Let's try it.




>>> numStream = nums()
>>> squaredNums = squaring(numStream)
>>> squaredNums.next()
0
>>> squaredNums.next()
1
>>> squaredNums.next()
4
>>> squaredNums.next()
9
>>> squaredNums.next()
16



If you have experience with other programming languages, you may have
heard of the term "co-routine".  What we're doing with this should be
reminiscent of coroutine-style programming.  We have one generator
feeding input into the other, with program control bouncing back and
forth between the generators as necessary.


So that's a basic idea of generators.  It lets us write processes that
can deal with and produce streams of data.  In the context of sockets,
this is particularly helpful, because sockets can be considered a
stream of bytes.


Here's another toy example that's closer to the problem you're trying
to solve.  Let's say that we're working on a program to alphabetize
the words of a sentence.  Very useless, of course.  :P  We might pass
it in the input:

this
is
a
test
of
the
emergency
broadcast
system

and expect to get back the following sentence:

 hist
 is
 a
 estt
 fo
 eht
 ceeegmnry
 aabcdorst
 emssty

We can imagine one process doing chunking, going from a sequence of
characters to a sequence of words:

###
def extract_words(seq):
"""Yield the words in a sequence of characters."""
buffer = []
for ch in seq:
if ch.isalpha():
buffer.append(ch)
elif buffer:
yield ''.join(buffer)
del buffer[:]
# If we hit the end of the buffer, we still might
# need to yield one more result.
if buffer:
yield ''.join(buffer)
###


and a function that transforms words to their munged counterpart:

#
def transform(word):
Munges a word into its alphabetized form."""
chars = list(word)
chars.sort()
return ''.join(chars)
#

This forms the major components of a program that can do the munging
on a file... or a socket!


Here's the complete example:



Re: [Tutor] trouble with beautiful soup

2015-12-24 Thread Danny Yoo
> I have installed and unzipped etc. the latest file from www.crummy.com many 
> times.
> If I click setup.py the command window appears only a fraction of a second.
> ??

Ah, you're on Windows.  I missed this detail earlier.

Double-clicking setup.py isn't enough to install that module.  You
need to do something extra.  Here:


http://stackoverflow.com/questions/12324601/how-to-install-a-python-module-via-its-setup-py-in-windows


You want to run the following at the Windows command prompt:

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


Re: [Tutor] Using python 3 on Ubuntu 14.04

2015-12-24 Thread Ben Finney
Jim Byrnes  writes:

> Thanks for all the info guys. I got myself confused because I thought
> that python 3 was the default for Ubuntu 14.04, but it is just
> installed by default.

Even if that were true, the ‘python’ command will likely still invoke
a Python 2 interpreter. Most systems that install a Python 3 interpreter
will install the command as ‘python3’.

> I realize now that the modules need to be installed in the proper
> environment.

That also remains true when Python 3 is the default.

> I know Pythoncard is not maintained any more. I have one program I
> wrote using it that I use often so I wanted to see it worked on 14.04.
> It will be a good learning experience to rewrite it for python 3 using
> something else.

Have you considered using Python 3 and the standard Tkinter tookit?
https://docs.python.org/3/library/tkinter.html>
https://docs.python.org/3/library/tkinter.ttk.html>
http://www.tkdocs.com/>

-- 
 \   “I’ve been an atheist ever since I heard there was only a |
  `\ stairway to heaven.” —Stella Young, 1982–2014 |
_o__)  |
Ben Finney

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


Re: [Tutor] trouble with beautiful soup

2015-12-24 Thread Walter Prins
Hi,

On 24 December 2015 at 17:21, marcus lütolf 
wrote:

> I am getting the following trace back without beeing prompted for an input:
>
> Traceback (most recent call last):
>   File "C:/Python27/Beautiful Soup_ex1.py", line 2, in 
> from beautifulspoup import *
> ImportError: No module named beautifulspoup
>
> I have installed and unzipped etc. the latest file from www.crummy.com
> many times.
> If I click setup.py the command window appears only a fraction of a second.
> ??
>


Slow down right there.  It sounds like you're manually downloading the
module and then trying to install it by double clicking setup.py.  Am I
right?   If so, don't do that.  (Perhaps.)  Manually downloading and
installing modules is arguably not the easiest/preferred way.

I'll explain an alternate/easier way below.  But if you must and because
it's arguably also good to know how to do this a bit more manually, open a
command prompt, then enter the following commands:

cd 
python setup.py install

Obviously you need to substitute where you extract the module sources into
the above cd command.

Then pay careful attention to any error messages output when the setup
script runs and post these if the installation is not successful.

I'll further note that it's much easier to install python packages with the
package manager "pip".  To make this work you need to however get pip
itself installed first.  To do this, download the instructions on the
following page: https://pip.pypa.io/en/stable/installing/  (basically
download and run the get-pip.py script.)

Once it's installed you can install modules by simply typing:

pip install 

In your case this would simply be:

pip install beautifulsoup

At which you should get output something like the following:

C:\Python27\Scripts>pip install beautifulsoup
You are using pip version 7.0.1, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting beautifulsoup
  Downloading BeautifulSoup-3.2.1.tar.gz
Installing collected packages: beautifulsoup
  Running setup.py install for beautifulsoup
Successfully installed beautifulsoup-3.2.1

C:\Python27\Scripts>

You can issue the pip command from any folder, providing your
"C:\Python27\Scripts" folder is on your system path.  While it might be by
default, I don't want to assume it, and if you have multiple versions of
Python then you need to careful to not make assumptions about which Python
and which pip is being run and should check or be explicit.

After installing a module the easiest way to sanity check the module is in
fact installed, is to immediately run the Python interpreter and try to
import the module:

C:\Python27\Scripts>cd ..

C:\Python27>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import BeautifulSoup
>>> dir(BeautifulSoup)
['BeautifulSOAP', 'BeautifulSoup', 'BeautifulStoneSoup', 'CData',
'Comment', 'DEFAULT_OUTPUT_ENCODING', 'Declaration',
'ICantBelieveItsBeautifulSoup', 'MinimalSoup', 'NavigableString',
'PageElement', 'ProcessingInstruction', 'ResultSet', 'RobustHTMLParser',
'RobustInsanelyWackAssHTMLParser', 'RobustWackAssHTMLParser',
'RobustXMLParser', 'SGMLParseError', 'SGMLParser', 'SimplifyingSOAPParser',
'SoupStrainer', 'StopParsing', 'Tag', 'UnicodeDammit', '__author__',
'__builtins__', '__copyright__', '__doc__', '__file__', '__license__',
'__name__', '__package__', '__version__', '_match_css_class',
'buildTagMap', 'chardet', 'codecs', 'generators', 'markupbase',
'name2codepoint', 're', 'sgmllib', 'types']
>>> print BeautifulSoup.__version__
3.2.1

Note that the Python package name is case sensitive, and that the 3.2.1
version package name is BeautifulSoup whilst the 4.x package name is bs4.
Obviously this may also explain why you're having problems.

Merry Christmas,

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


Re: [Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread boB Stepp
On Thu, Dec 24, 2015 at 11:34 AM, Oscar Benjamin
 wrote:
>
> On 24 Dec 2015 14:55, "boB Stepp"  wrote:

[...]

>> I find myself doing the same boring, repetitive tasks by hand, which
>> amounts to copying certain information from one program and putting it
>> into other programs.  None of these programs, to my knowledge, have a
>> publicly accessible API.  Are there ways to programmatically
>> accurately click the right buttons (or check boxes, radio buttons,
>> etc.), copy desired fields and then switch to another program and
>> paste the desired information into the desired fields, accurately
>> clicking all things that need to be clicked, etc.?  This is mostly a
>> Windows-based scenario, but if the techniques (if they exist) can be
>> abstracted to any OS I can find plenty of uses elsewhere as well!

[...]

> ...I googled "Python windows control mouse" and got:
>
> http://stackoverflow.com/questions/1181464/controlling-mouse-with-python

> Also this:
> https://pypi.python.org/pypi/PyAutoGUI

My last of several unsatisfactory searches was "how to
programmatically click a button in python", which turned up a bunch of
web scraping results.  Sigh--very weak Google-fu indeed!  ~(:>)

Thanks, Oscar!  PyAutoGUI looks very promising.  It looks like my next
problem will be figuring out how to find the pixel coordinates of the
GUI elements which I wish to manipulate.  This package mentions
screenshot functions which may be able to locate these coordinates
programattically, but from the example given

>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('button.png') # returns (left, 
>>> top, width, height) of matching region
>>> button7location
(1416, 562, 50, 41)
>>> buttonx, buttony = pyautogui.center(button7location)
>>> buttonx, buttony
(1441, 582)
>>> pyautogui.click(buttonx, buttony)  # clicks the center of where the button 
>>> was found

it is not clear to me (Yet!) how "button7location" is mapped to the
actual button on-screen, so that I will know which button is being
identified.  But it looks like there is some good documentation which
will hopefully make things clear soon!

Merry Christmas!

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


Re: [Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread Alan Gauld
On 24/12/15 14:54, boB Stepp wrote:

> publicly accessible API.  Are there ways to programmatically
> accurately click the right buttons (or check boxes, radio buttons,
> etc.), copy desired fields and then switch to another program and
> paste the desired information into the desired fields, accurately
> clicking all things that need to be clicked, etc.?  This is mostly a
> Windows-based scenario, but if the techniques (if they exist) can be
> abstracted to any OS I can find plenty of uses elsewhere as well!


To quote my recent book:

The final option for GUI applications with no API is to interact with
the GUI itself by sending user event messages into the application. Such
events could include key-presses, mouse-clicks, and so forth. This
technique is known as robotics because you are simulating a human user
from your Python program. It is really an extension of the native code
access described in the previous section, but operating at a much
lower level.

This is a frustrating technique that is very error prone and also very
vulnerable to changes in the application being controlled—for example,
if an upgrade changes the screen layout, your code will likely break.
Because of the difficulty of writing the code, as well as the fragility
of the solution, you should avoid this unless every other possibility
has failed.
-

And again

...you reviewed the options available for scripting with their pros and
cons, including the last resort option for GUIs of sending OS events to
the application windows. This last option is fraught with difficulty and
should only ever be used when all other means have been explored and
exhausted.


So, as you see I don't recommend it unless you are absolutely sure
nothing else is possible.

The other things to check are (On Windows):

1) File export/import (CSV, JSON or XML maybe? or a Windows app
   such as Word/Excel?))
2) COM object model access via PyWin32
3) A C DLL exposed via ctypes
4) A Web front end or web service

If you really must go down the screen scraping robotics route and
you are thinking of upgrading to Windows 10 make sure you do that
first or you will almost certainly have to rewrite from scratch
(and after most every other upgrade of OS or app thereafter).
And don't even think of changing your system fonts - ever!

If you had a Mac things are usually easier because you can often
use osascript as an interface to AppleScript but on X Windows
things are just as messy as for windows...


-- 
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


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


Re: [Tutor] reading an input stream

2015-12-24 Thread Alan Gauld
On 24/12/15 18:54, richard kappler wrote:

> I think what I need to do would be analogous to (pardon if I'm using the
> wrong terminology, at this poing in the discussion I am officially out of
> my depth) sending the input stream to a buffer(s) until  the ETX for that
> message comes in, shoot the buffer contents to the parser while accepting
> the next STX + message fragment into the buffer, or something analogous.

You could use a Stringbuffer in memory. But simpler still is just to
append the bits in a file, one per incoming source. Any reason that
wouldn't work?

-- 
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


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


Re: [Tutor] reading an input stream

2015-12-24 Thread boB Stepp
On Thu, Dec 24, 2015 at 3:54 PM, Danny Yoo  wrote:

I tried to follow your example:
>
> For example, here's a generator that knows how to produce an infinite
> stream of numbers:
>
> ##
> def nums():
> n = 0
> while True:
> yield n
> n += 1
> ##
>
> What distinguishes a generator from a regular function?  The use of
> "yield".  A "yield" is like a return, but rather than completely
> escape out of the function with the return value, this generator will
> remember what it was doing  at that time.  Why?  Because it can
> *resume* itself when we try to get another value out of the generator.
>
> Let's try it out:
>
> #
>
 numStream = nums()
 numStream.next()
> 0

But I got an exception:

Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600
64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def nums():
n = 0
while True:
yield n
n += 1


>>> numStream = nums()
>>> numStream.next()
Traceback (most recent call last):
  File "", line 1, in 
numStream.next()
AttributeError: 'generator' object has no attribute 'next'


If I instead do this:

>>> next(numStream)
0
>>> next(numStream)
1
>>> next(numStream)
2

Things work as you described.  Is your example from Python 2?  If yes,
is this something that changed between Python 2 and 3?  I have not
made it to generators yet, but you have now whetted my appetite!

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


Re: [Tutor] Using python 3 on Ubuntu 14.04

2015-12-24 Thread Jim Byrnes

On 12/24/2015 04:03 PM, Ben Finney wrote:

Jim Byrnes  writes:


Thanks for all the info guys. I got myself confused because I thought
that python 3 was the default for Ubuntu 14.04, but it is just
installed by default.


Even if that were true, the ‘python’ command will likely still invoke
a Python 2 interpreter. Most systems that install a Python 3 interpreter
will install the command as ‘python3’.


Misunderstanding that was also part of my confusion.


I realize now that the modules need to be installed in the proper
environment.


That also remains true when Python 3 is the default.


I realize that now.


I know Pythoncard is not maintained any more. I have one program I
wrote using it that I use often so I wanted to see it worked on 14.04.
It will be a good learning experience to rewrite it for python 3 using
something else.


Have you considered using Python 3 and the standard Tkinter tookit?
https://docs.python.org/3/library/tkinter.html>
https://docs.python.org/3/library/tkinter.ttk.html>
http://www.tkdocs.com/>



Yes, that is something I plan to look at once I get 14.04 all set up the 
way I want and it becomes my default system.


Thanks,  Jim


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


Re: [Tutor] reading an input stream

2015-12-24 Thread Cameron Simpson

On 24Dec2015 13:54, richard kappler  wrote:

I have to create a script that reads  xml data over a tcp socket, parses it
and outputs it to console. Not so bad, most of which I already know how to
do. I know how to set up the socket, though I am using a file for
development and testing, am using lxml and have created an xslt that does
what I want with the xml, and it outputs it to console.

What I'm not really sure of, each xml 'message' is preceeded by an STX
(\x02) and ends with an ETX (\x03). These 'messages' (Danny, are you noting
I don't say -lines- anymore? :-)  ) need to be parsed and output whole as
opposed to partial.

My concern is, there will actually be numerous machines sending data to the
tcp socket, so it's entirely likely the messages will come in fragmented
and the fragments will need to be held until complete so they can be sent
on whole to the parser. While this is the job of tcp, my script needs to

I think what I need to do would be analogous to (pardon if I'm using the
wrong terminology, at this poing in the discussion I am officially out of
my depth) sending the input stream to a buffer(s) until  the ETX for that
message comes in, shoot the buffer contents to the parser while accepting
the next STX + message fragment into the buffer, or something analogous.

Any guidance here?


Since a TCP stream runs from one machine to another (may be the same machine); 
presumably your actually have multiple TCP streams to manage, and at the same 
time as otherwise you could just process one until EOF, then the next and so 
on. Correct?


My personal inclination would start a Thread for each stream, and have that 
thread simple read the stream extracting XML chunks, and then .put each chunk 
on a Queue used by whatever does stuff with the XML (accept chunk, parse, etc).  
If you need to know where the chunk came from, .put a tuple with the chunk and 
some context information.


Does that help you move forward?

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading an input stream

2015-12-24 Thread Danny Yoo
 numStream.next()
> Traceback (most recent call last):
>   File "", line 1, in 
> numStream.next()
> AttributeError: 'generator' object has no attribute 'next'
>
>
> If I instead do this:
>
 next(numStream)
> 0
 next(numStream)
> 1
 next(numStream)
> 2
>
> Things work as you described.  Is your example from Python 2?  If yes,
> is this something that changed between Python 2 and 3?  I have not
> made it to generators yet, but you have now whetted my appetite!


Hi BoB,

Ah, yes, thank you!  Yes, I was using Python 2.  I'll have to set up
Python 3 on my server and get some more experience with it during the
break, then!

Let me double check the docs... ok, yeah, I should be using the next()
function, since that's available in Python 2 as well.  Reference:
https://docs.python.org/2/library/functions.html#next
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT: How to automate user interactions with GUI elements of closed-source programs?

2015-12-24 Thread boB Stepp
On Thu, Dec 24, 2015 at 5:38 PM, Alan Gauld  wrote:


> To quote my recent book:

[...]

> This is a frustrating technique that is very error prone and also very
> vulnerable to changes in the application being controlled—for example,
> if an upgrade changes the screen layout, your code will likely break.
> Because of the difficulty of writing the code, as well as the fragility
> of the solution, you should avoid this unless every other possibility
> has failed.

After reading the docs on PyAutoGUI 0.9.31, I've been playing around
with it and investigating some of these "fragilities".  If I decide to
go this route it appears that I need to have the main window I'd be
working with maximized to full screen.  Otherwise, any coordinates to
position the mouse cursor could very well be incorrect.  It would be
better to use any and all keyboard shortcuts available in the
application I'm working with, as these should have reproducible
behavior.  Trying to use the package's image location functions for
"commonly" named buttons (such as "OK" or "Cancel") could easily be
iffy since it searches the entire monitor screen, which might trigger
the wrong button.  However, PyAutoGUI does have a region-defining tool
that can limit the area of screen real estate being searched.  Since I
don't have a dual-monitor setup at home, I cannot test what would
happen in that situation.  I notice that multiple monitors is on the
package author's TODO list.  Etc.  Definitely not software nirvana
here!

I've been trying to think of what things I can abstract out of the
possible different environments.  The only thing that has occurred to
me so far is do my mapping of a given software application window in
terms of relative coordinates.  This way I can detect the current
monitor viewing size and then compute the needed absolute mouse
coordinates.

> The other things to check are (On Windows):
>
> 1) File export/import (CSV, JSON or XML maybe? or a Windows app
>such as Word/Excel?))

On one of the software packages I work with every day, there are
limited text file exports.  Unfortunately, they don't contain all of
the information I need, though they do contain most of it.

> 2) COM object model access via PyWin32

I used to have a very little bit of COM knowledge, but that has long
since been forgotten.  I don't know now what possibilities that might
open up for me in accessing one of these commercial applications.

> 3) A C DLL exposed via ctypes

Oscar very briefly mentioned taking this type of approach once upon a
time.  This is also something I currently know little about and don't
know what possibilities it might give me.

> 4) A Web front end or web service

This is a total no go for the software I use at work.

> If you really must go down the screen scraping robotics route and
> you are thinking of upgrading to Windows 10 make sure you do that
> first or you will almost certainly have to rewrite from scratch
> (and after most every other upgrade of OS or app thereafter).
> And don't even think of changing your system fonts - ever!
>

I am at the mercy of my IS department re possible upgrades and they
often don't give notice of coming upgrades!

Thanks, Alan!

Merry Christmas!
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading an input stream

2015-12-24 Thread eryk sun
On Thu, Dec 24, 2015 at 9:13 PM, boB Stepp  wrote:
> AttributeError: 'generator' object has no attribute 'next'

The iterator protocol was added in Python 2.2 (circa 2001) as a
generalization for use in "for" loops, but the language didn't have
built-in next() at the time. Instead the method to get the next item
from an iterator was defined without double underscores. You'd simply
call it.next() to manually get the next item of iterator "it".

Python 3 added built-in next() and changed the method name to
"__next__". The built-in function was backported to 2.6 to have a
common idiom even though the method is still named "next" in Python 2.

The name change in Python 3  reflects that "__next__" is a special
method that's looked up on the type (in CPython it's the tp_iternext
field of the PyTypeObject). You can't simply add a bound next method
to an instance to make Python think it's an iterator. The same applies
in Python 2, but the name "next" doesn't suggest that this is the
case.

For example, let's start out with a normal Python 2 iterator that
simply iterates a count from some initial value.

class Iterator(object):
def __init__(self, start):
self.value = start - 1
def __iter__(self):
return self
def next(self):
self.value += 1
return self.value

>>> it = Iterator(0)
>>> it.next()
0
>>> next(it)
1

Now store the bound next method directly on the instance

>>> it.next = it.next
>>> it.next.__self__ is it
True

and remove the method from the class:

>>> del Iterator.next

The bound method still works:

>>> it.next()
2

But the interpreter doesn't look for "next" on the instance:

>>> next(it)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Iterator object is not an iterator

>>> for i in it:
... if i == 3: break
...
Traceback (most recent call last):
  File "", line 1, in 
TypeError: iter() returned non-iterator of type 'Iterator'

Since "next" is a special method, it should have the special name
"__next__". So let it be written. So let it be done... in Python 3.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor