Re: [Tutor] Validating String contains IP address

2017-03-31 Thread Steven D'Aprano
On Fri, Mar 31, 2017 at 07:35:48PM -0400, Ed Manning wrote:
> 
> What's the best way to validate a string contains a IP address 

Don't reinvent the wheel, use the ipaddress module.

py> import ipaddress
py> ipaddress.ip_address('99.99.99.99')
IPv4Address('99.99.99.99')


If the address is not a valid address syntax, it will raise ValueError.


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


Re: [Tutor] Validating String contains IP address

2017-03-31 Thread Alex Kleider

On 2017-03-31 16:35, Ed Manning wrote:

What's the best way to validate a string contains a IP address
Sent from my iPad
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


The re module perhaps?

How about:

import re

ip_regex = r"""
[12]?\d?\d[.]
[12]?\d?\d[.]
[12]?\d?\d[.]
[12]?\d?\d
"""

ip_pattern = re.compile(ip_regex, re.VERBOSE)

# then test for ip_pattern.search(source).group():
res = ip_pattern.search(source).group()
if res:
print("IP is {}".format(res))
else:
print("Source doesn't contain an IP address")

# This assumes that an address would never be written as follows: 
076.191.211.205

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


Re: [Tutor] Validating String contains IP address

2017-03-31 Thread Martin A. Brown

Hello there,

>What's the best way to validate a string contains a IP address 

If you are only talking about validating that something is an IP 
address, then I can even give you a little Python2 / Python3 snippet 
that can do that.  I like the EAFP [0] approach for this sort of 
thing.

There are slight differences between the python2 third-party module 
ipaddr [1] and the python3 standard ipaddress module [2].  The 
latter is (I think) name-changed from the former, although I'm not 
utterly sure of the history here.  These two modules both parse IPs 
(and have tools to allow you to parse IPv6 and IPv4 IPs and 
prefixes).

So, rather than try to build your own, why not grab something 
convenient from the (batteries-included) shelf?

Note, if you actually want to do something with the IP address 
(convert to use for socket operations, store in a binary format 
somewhere, use in network bitmath operations), then you'll want to 
study these libraries and possibly also the socket [3] library.  
But, if all you want to know is whether somebody provided a string 
that is a valid IP (whether network address, host address or 
broadcast address), you can use this little snippet:

  from __future__ import print_function
  
  import sys
  
  try:
  from ipaddress import ip_address as ipo
  except ImportError:
  from ipaddr import IPAddress as ipo
  
  for arg in sys.argv[1:]:
  try:
  ipo(arg)
  print("%s is an IP." % (arg,))
  except ValueError:
  print("%s is not an IP." % (arg,))

When I save and run this (works the same in Python2 or Python3):

  $ python isarganip.py  192.168.7.0 192.168.7.256 192.v.12.7 text 
fe80::a64e:31ff:fe94:a160 255.255.255.255 0.0.0.0
  192.168.7.0 is an IP.
  192.168.7.256 is not an IP.
  192.v.12.7 is not an IP.
  text is not an IP.
  fe80::a64e:31ff:fe94:a160 is an IP.
  255.255.255.255 is an IP.
  0.0.0.0 is an IP.

Basically, I'm lazy.  I let somebody else do the hard work of validation!

Good luck and happy IParsing!

-Martin

 [0] 
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#eafp-try-except-example
 [1] https://pypi.python.org/pypi/ipaddr
 [2] https://docs.python.org/3/library/ipaddress.html
 [3] https://docs.python.org/2/library/socket.html
 https://docs.python.org/3/library/socket.html

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Validating String contains IP address

2017-03-31 Thread Alan Gauld via Tutor
On 01/04/17 00:35, Ed Manning wrote:
> 
> What's the best way to validate a string contains a IP address 

It depends on how thorough you want to be.
You can define a regex to check that its 4 groups of numbers
separated by periods.

Or you can split the string into fields and validate that
the values are each less than 255

Or you could try connecting to it and see if you get a
response...

How sure do you need to be?

-- 
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] Validating String contains IP address

2017-03-31 Thread Ed Manning

What's the best way to validate a string contains a IP address 
Sent from my iPad
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen / proc.communicate issue

2017-03-31 Thread Cameron Simpson

On 31Mar2017 06:13, eryk sun  wrote:

On Thu, Mar 30, 2017 at 10:51 PM, Cameron Simpson  wrote:

This suggests that .communicate uses Threads to send and to gather data
independently, and that therefore the deadlock situation may not arise.


For Unix, communicate() uses select or poll. It uses threads on
Windows. Either way it avoids deadlocking.


Good to know. Thanks!

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


Re: [Tutor] reading files in Python 3

2017-03-31 Thread eryk sun
On Thu, Mar 30, 2017 at 8:45 PM, Mats Wichmann  wrote:
> Yeah, fun.  You need to escape the \ that the idiot MS-DOS people chose
> for the file path separator. Because \ is treated as an escape character.

The COMMAND.COM shell inherited command-line switches (options) that
use slash from TOPS-10 by way of CP/M, so using backslash in paths was
less ambiguous for the shell (e.g. dir/w could be intended to run "w"
in the "dir" subdirectory, or it could mean to run "dir" with the
option "/w"). The DOS kernel did support both slash and backslash in
file-system paths.

Also, C wasn't a common language on the PC back then. BASIC was.
Instead of using escapes in string literals, BASIC used addition at
runtime with the CHR$ function or predefined constants. Support for
hierarchical paths (DOS 2.0) came around at about the same time that C
was rising in popularity, so the pain came on slowly like boiling a
lobster.

The system designers who really have no excuse are the NT kernel
developers circa 1988-93. They were working in C on a system that
already required converting paths from the DOS namespace to a native
object namespace. They could have easily implemented the native object
system to use slash instead of backslash.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract Block of Data from a 2D Array

2017-03-31 Thread Stephen P. Molnar

On 03/31/2017 10:50 AM, Peter Otten wrote:

Stephen P. Molnar wrote:


I have a block of data extracted from a quantum mechanical calculation:

CARTESIAN COORDINATES (A.U.)

NO LB  ZAFRAG MASS X   Y   Z
 0 C 6.012.011   -3.2656360.1988940.090858
 1 C 6.012.011   -1.3071611.5222121.003463
 2 C 6.012.0111.2133360.948208   -0.033373
 3 N 7.014.0073.2386501.0415231.301322
 4 C 6.012.011   -5.9544890.6508780.803379
 5 C 6.012.0115.6544760.4800660.013757

where the number of lines depends upon the molecule being considered.

I want to extract the block of data starting on line 4 and column 4.
Unfortunately, the only programming language in which I used to be
competent in is Fortran.  I have attempted researching this problem, but
have only succeeded in increasing my mental entropy.

Help will be much appreciated.  Thanks in advance.



pandas is the swiss army knife of data manipulation in Python -- albeit with
a non-negligable learning curve. Some examples (from an amateur):

$ cat data.txt
CARTESIAN COORDINATES (A.U.)

NO LB  ZAFRAG MASS X   Y   Z
 0 C 6.012.011   -3.2656360.1988940.090858
 1 C 6.012.011   -1.3071611.5222121.003463
 2 C 6.012.0111.2133360.948208   -0.033373
 3 N 7.014.0073.2386501.0415231.301322
 4 C 6.012.011   -5.9544890.6508780.803379
 5 C 6.012.0115.6544760.4800660.013757
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

import pandas
table = pandas.read_table("data.txt", skiprows=2, delimiter=" ",

skipinitialspace=True)

table

NO LB  ZA  FRAGMASS X Y Z
0   0  C   6 0  12.011 -3.265636  0.198894  0.090858
1   1  C   6 0  12.011 -1.307161  1.522212  1.003463
2   2  C   6 0  12.011  1.213336  0.948208 -0.033373
3   3  N   7 0  14.007  3.238650  1.041523  1.301322
4   4  C   6 0  12.011 -5.954489  0.650878  0.803379
5   5  C   6 0  12.011  5.654476  0.480066  0.013757

[6 rows x 8 columns]

table[3:]

NO LB  ZA  FRAGMASS X Y Z
3   3  N   7 0  14.007  3.238650  1.041523  1.301322
4   4  C   6 0  12.011 -5.954489  0.650878  0.803379
5   5  C   6 0  12.011  5.654476  0.480066  0.013757

[3 rows x 8 columns]

table[["X", "Y", "Z"]]

   X Y Z
0 -3.265636  0.198894  0.090858
1 -1.307161  1.522212  1.003463
2  1.213336  0.948208 -0.033373
3  3.238650  1.041523  1.301322
4 -5.954489  0.650878  0.803379
5  5.654476  0.480066  0.013757

[6 rows x 3 columns]

table.MASS.mean()

12.343


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



Problem solved!

Many thanks.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] All Entry Boxes taking the same value

2017-03-31 Thread Pooja Bhalode
Hi Peter and Alan,

Yes, thank you for your suggestions. I really appreciate it. I would look
into a proper tutorial and try to follow it up. The suggestion regarding
this piece of code worked when I tried StringVar instead of " ".

Thank you once again.
Yours truly,
Pooja


On Fri, Mar 31, 2017 at 6:28 AM, Alan Gauld via Tutor 
wrote:

> On 30/03/17 21:35, Pooja Bhalode wrote:
>
> > *However, when I execute it, and type something in one entrybox, it shows
> > in all the entry boxes using multi-cursor option. *
>
> I'm not sure whats going on and don;t habe tome to experiment but one
> thing I noticed:
>
> > average = [" ", " ", " "]
> > lowest = [" ", " ", " "]
> > highest = [" ", " ", " "]
> ...
> > for i in range(len(reactants)):
> > *Entry*(root, textvariable =* average[i]*, width = 15,
> > state=DISABLED).grid(row = 3+i, column = 1, sticky = W)
>
> You are setting textvariable to a string but it should be
> a StrinVar object. You could probably fix that by changing
> your data definitions to StringVars:
>
> average = [StringVar(), StringVar(), StringVar()]
>
> I don't know if that will fix the problem but its probably
> a good thing to do anyhow...
>
>
> --
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract Block of Data from a 2D Array

2017-03-31 Thread Stephen P. Molnar

On 03/31/2017 10:29 AM, Alan Gauld via Tutor wrote:

On 31/03/17 13:19, Stephen P. Molnar wrote:

I have a block of data extracted from a quantum mechanical calculation:


How is this data stored? On paper? In a database? In XML? A CSV file?
Plain text? The answer to that will go a long way to pointing you in the
right direction for a solution.


CARTESIAN COORDINATES (A.U.)

NO LB  ZAFRAG MASS X   Y   Z
 0 C 6.012.011   -3.2656360.1988940.090858
 1 C 6.012.011   -1.3071611.5222121.003463
 2 C 6.012.0111.2133360.948208   -0.033373
 3 N 7.014.0073.2386501.0415231.301322
 4 C 6.012.011   -5.9544890.6508780.803379
 5 C 6.012.0115.6544760.4800660.013757

where the number of lines depends upon the molecule being considered.

I want to extract the block of data starting on line 4 and column 4.


A block of data needs a start and end. Where is the end?
Also which of the above lines is line 4?
I'd read it as the one starting the actual raw data values.
But it could be the 4th line of values that you want?

It might help to know the OS you are using too because Unix
in particular has some OS level tools that might simplify
preparing the data.



Sorry about the omission.  The data is in a tab separated file.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading files in Python 3

2017-03-31 Thread Mats Wichmann
On 03/31/2017 09:44 AM, Alex Kleider wrote:
> On 2017-03-30 13:45, Mats Wichmann wrote:
> 
>>
>> Yeah, fun.  You need to escape the \ that the idiot MS-DOS people chose
>> for the file path separator.
> 
> I also believe that the "MS-DOS people" are making a poor choice
> but to call them idiots is perhaps a bit strong.
> Remember that for many the use of MicroSoft's OS is not a choice but an
> edict handed down by superiors, or a requirement caused by other factors.
> Perhaps "idiot" was meant to apply to the OS, not to it's users?

I'm not sure it's even necessary to clarify, but yes, absolutely - it
was referring to the design choice.

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


Re: [Tutor] Using an XML file for web crawling

2017-03-31 Thread Mats Wichmann
On 03/31/2017 05:23 AM, Igor Alexandre wrote:
> Hi!
> I'm a newbie in the Python/Web crawling world. I've been trying to find an 
> answer to the following question since last week, but I couldn't so far, so I 
> decided to ask it myself here: I have a sitemap in XML and I want to use it 
> to save as text the various pages of the site. Do you guys know how can I do 
> it? I'm looking for some code on the web where I can just type the xml 
> address and wait for the crawler to do it's job, saving all the pages 
> indicated in the sitemap as a text file in my computer. 
> Thank you!
> Best,
> Igor Alexandre

There's a surprisingly active community doing web crawling / scraping
stuff... I've gotten the impression that the scrapy project is a "big
dog" in this space, but I'm not involved in it so not sure.  A couple of
links for you to play with:

http://docs.python-guide.org/en/latest/scenarios/scrape/

the first part of this might be enough for you - lxml + Requests
I just had occasion to look over that page a few days ago, but I'm sure
a web search would turn that up easily

https://scrapy.org/

there are plenty of other resources, someone is bound to have what
you're looking for.


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


Re: [Tutor] reading files in Python 3

2017-03-31 Thread Alex Kleider

On 2017-03-30 13:45, Mats Wichmann wrote:



Yeah, fun.  You need to escape the \ that the idiot MS-DOS people chose
for the file path separator.


I also believe that the "MS-DOS people" are making a poor choice
but to call them idiots is perhaps a bit strong.
Remember that for many the use of MicroSoft's OS is not a choice but an 
edict handed down by superiors, or a requirement caused by other 
factors.

Perhaps "idiot" was meant to apply to the OS, not to it's users?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract Block of Data from a 2D Array

2017-03-31 Thread Peter Otten
Stephen P. Molnar wrote:

> I have a block of data extracted from a quantum mechanical calculation:
> 
> CARTESIAN COORDINATES (A.U.)
> 
>NO LB  ZAFRAG MASS X   Y   Z
> 0 C 6.012.011   -3.2656360.1988940.090858
> 1 C 6.012.011   -1.3071611.5222121.003463
> 2 C 6.012.0111.2133360.948208   -0.033373
> 3 N 7.014.0073.2386501.0415231.301322
> 4 C 6.012.011   -5.9544890.6508780.803379
> 5 C 6.012.0115.6544760.4800660.013757
> 
> where the number of lines depends upon the molecule being considered.
> 
> I want to extract the block of data starting on line 4 and column 4.
> Unfortunately, the only programming language in which I used to be
> competent in is Fortran.  I have attempted researching this problem, but
> have only succeeded in increasing my mental entropy.
> 
> Help will be much appreciated.  Thanks in advance.
> 

pandas is the swiss army knife of data manipulation in Python -- albeit with 
a non-negligable learning curve. Some examples (from an amateur):

$ cat data.txt
CARTESIAN COORDINATES (A.U.)

   NO LB  ZAFRAG MASS X   Y   Z
0 C 6.012.011   -3.2656360.1988940.090858
1 C 6.012.011   -1.3071611.5222121.003463
2 C 6.012.0111.2133360.948208   -0.033373
3 N 7.014.0073.2386501.0415231.301322
4 C 6.012.011   -5.9544890.6508780.803379
5 C 6.012.0115.6544760.4800660.013757
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> table = pandas.read_table("data.txt", skiprows=2, delimiter=" ", 
skipinitialspace=True)
>>> table
   NO LB  ZA  FRAGMASS X Y Z
0   0  C   6 0  12.011 -3.265636  0.198894  0.090858
1   1  C   6 0  12.011 -1.307161  1.522212  1.003463
2   2  C   6 0  12.011  1.213336  0.948208 -0.033373
3   3  N   7 0  14.007  3.238650  1.041523  1.301322
4   4  C   6 0  12.011 -5.954489  0.650878  0.803379
5   5  C   6 0  12.011  5.654476  0.480066  0.013757

[6 rows x 8 columns]
>>> table[3:]
   NO LB  ZA  FRAGMASS X Y Z
3   3  N   7 0  14.007  3.238650  1.041523  1.301322
4   4  C   6 0  12.011 -5.954489  0.650878  0.803379
5   5  C   6 0  12.011  5.654476  0.480066  0.013757

[3 rows x 8 columns]
>>> table[["X", "Y", "Z"]]
  X Y Z
0 -3.265636  0.198894  0.090858
1 -1.307161  1.522212  1.003463
2  1.213336  0.948208 -0.033373
3  3.238650  1.041523  1.301322
4 -5.954489  0.650878  0.803379
5  5.654476  0.480066  0.013757

[6 rows x 3 columns]
>>> table.MASS.mean()
12.343


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


Re: [Tutor] subprocess.Popen / proc.communicate issue

2017-03-31 Thread bruce
Cameron!!!

You are 'da man!!

Read your exaplanation.. good stuff to recheck/test and investigate
over time

In the short term, I'll implement some tests!!

thanks!


On Thu, Mar 30, 2017 at 6:51 PM, Cameron Simpson  wrote:
> I wrote a long description of how .communicate can deadlock.
>
> Then I read the doco more carefully and saw this:
>
>  Warning: Use communicate() rather than .stdin.write, .stdout.read
>  or .stderr.read to avoid deadlocks due to any of the other OS
>  pipe buffers filling up and blocking the child process.
>
> This suggests that .communicate uses Threads to send and to gather data
> independently, and that therefore the deadlock situation may not arise.
>
> See what lsof and strace tell you; all my other advice stands regardless,
> and
> the deadlock description may or may not be relevant. Still worth reading and
> understanding it when looking at this kind of problem.
>
> Cheers,
> Cameron Simpson 
>
>
> On 31Mar2017 09:43, Cameron Simpson  wrote:
>>
>> On 30Mar2017 13:51, bruce  wrote:
>>>
>>> Trying to understand the "correct" way to run a sys command ("curl")
>>> and to get the potential stderr. Checking Stackoverflow (SO), implies
>>> that I should be able to use a raw/text cmd, with "shell=true".
>>
>>
>> I strongly recommend avoiding shell=True if you can. It has many problems.
>> All stackoverflow advice needs to be considered with caution. However, that
>> is not the source of your deadlock.
>>
>>> If I leave the stderr out, and just use
>>>s=proc.communicate()
>>> the test works...
>>>
>>> Any pointers on what I might inspect to figure out why this hangs on
>>> the proc.communicate process/line??
>>
>>
>> When it is hung, run "lsof" on the processes from another terminal i.e.
>> lsof the python process and also lsof the curl process. That will make clear
>> the connections between them, particularly which file descriptors ("fd"s)
>> are associated with what.
>>
>> The run "strace" on the processes. That shoud show you what system calls
>> are in progress in each process.
>>
>> My expectation is that you will see Python reading from one file
>> descriptor and curl writing to a different one, and neither progressing.
>>
>> Personally I avoid .communicate and do more work myself, largerly to know
>> precisely what is going on with my subprocesses.
>>
>> The difficulty with .communicate is that Python must read both stderr and
>> stdout separately, but it will be doing that sequentially: read one, then
>> read the other. That is just great if the command is "short" and writes a
>> small enough amount of data to each. The command runs, writes, and exits.
>> Python reads one and sees EOF after the data, because the command has
>> exited. Then Python reads the other and collects the data and sees EOF
>> because the command has exited.
>>
>> However, if the output of the command is large on whatever stream Python
>> reads _second_, the command will stall writing to that stream. This is
>> because Python is not reading the data, and therefore the buffers fill
>> (stdio in curl plus the buffer in the pipe). So the command ("curl") stalls
>> waiting for data to be consumed from the buffers. And because it has
>> stalled, the command does not exit, and therefore Python does not see EOF on
>> the _first_ stream. So it sits waiting for more data, never reading from the
>> second stream.
>>
>> [...snip...]
>>>
>>> cmd='[r" curl -sS '
>>> #cmd=cmd+'-A  "Mozilla/5.0 (X11; Linux x86_64; rv:38.0)
>>> Gecko/20100101 Firefox/38.0"'
>>> cmd=cmd+"-A  '"+user_agent+"'"
>>> ##cmd=cmd+'   --cookie-jar '+cname+' --cookie '+cname+''
>>> cmd=cmd+'   --cookie-jar '+ff+' --cookie '+ff+''
>>> #cmd=cmd+'-e "'+referer+'"   -d "'+tt+'"  '
>>> #cmd=cmd+'-e "'+referer+'"'
>>> cmd=cmd+"-L '"+url1+"'"+'"]'
>>> #cmd=cmd+'-L "'+xx+'" '
>>
>>
>> Might I recommand something like this:
>>
>> cmd_args = [ 'curl', '-sS' ]
>> cmd_args.extend( [ '-A', user_agent ] )
>> cmd_args.extend( [ '--cookie-jar', ff, '--cookie', ff ] )
>> cmd_args.extend( [ '-L', url ]
>>
>> and using shell=False. This totally avoids any need to "quote" strings in
>> the command, because the shell is not parsing the string - you're invoking
>> "curl" directly instead of asking the shell to read a string and invoke
>> "curl" for you.
>>
>> Constructing shell commands is tedious and fiddly; avoid it when you don't
>> need to.
>>
>>> try_=1
>>
>>
>> It is preferable to say:
>>
>> try_ = true
>>
>>> while(try_):
>>
>>
>> You don't need and brackets here:
>>
>> while try_:
>>
>> More readable, because less punctuation.
>>
>>>   proc=subprocess.Popen(cmd,
>>> shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>
>>
>> proc = subprocess.Popen(cmd_args,
>>  stdout=subprocess.PIPE,
>>  stderr=subprocess.PIPE)
>>
>>>   s,err=proc.communicate()
>>>   s=s.strip()
>>>   err=err.strip()
>>>   if(err==0):
>>> try_=''
>>
>>
>> It is preferable to say:
>>
>> try_ = False
>>
>> Also, you should be looking at proc.ret

Re: [Tutor] Extract Block of Data from a 2D Array

2017-03-31 Thread Karim



On 31/03/2017 14:19, Stephen P. Molnar wrote:


I have a block of data extracted from a quantum mechanical calculation:

CARTESIAN COORDINATES (A.U.)

  NO LB  ZAFRAG MASS X   Y   Z
   0 C 6.012.011   -3.2656360.198894 0.090858
   1 C 6.012.011   -1.3071611.522212 1.003463
   2 C 6.012.0111.2133360.948208 -0.033373
   3 N 7.014.0073.2386501.041523 1.301322
   4 C 6.012.011   -5.9544890.650878 0.803379
   5 C 6.012.0115.6544760.480066 0.013757

where the number of lines depends upon the molecule being considered.

I want to extract the block of data starting on line 4 and column 4. 
Unfortunately, the only programming language in which I used to be 
competent in is Fortran.  I have attempted researching this problem, 
but have only succeeded in increasing my mental entropy.


Help will be much appreciated.  Thanks in advance. starting on line 4 
and column 4. Unfortunately, the only programming language in which I 
used to be competent in is Fortran.  I have attempted researching this 
problem, but have only succeeded


Hello,

If your delimiter is a tabulation in your data tab you can use the CSV 
module in this way using python 2.7 (code not tested):


-
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
>>>
>>> import csv

>>> my_data_file = open( "my_tab_file")

>>> reader = csv.DictReader(my_data_file, delimiter='\t')

>>> for line_number, row in enumerate(reader, start=1):
  if line_number >= 4:
  print row['MASS'],

and in a file called extract_data.py:

#

import csv

my_data_file = open( "my_tab_file")
reader = csv.DictReader(my_data_file, delimiter='\t')

for line_number, row in enumerate(reader, start=1):
  if line_number >= 4:
  print row['MASS'],

#--

Just execute:
$ python extract_data.py

Just be aware that the first line of you data file is recognized as the 
header of all columns.
The csv module allows you to to easily reference the element of each 
column with its column

name (given by the header == the first line of the file)

Cheers
Karim

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


Re: [Tutor] Extract Block of Data from a 2D Array

2017-03-31 Thread Alan Gauld via Tutor
On 31/03/17 13:19, Stephen P. Molnar wrote:
> I have a block of data extracted from a quantum mechanical calculation:

How is this data stored? On paper? In a database? In XML? A CSV file?
Plain text? The answer to that will go a long way to pointing you in the
right direction for a solution.

> CARTESIAN COORDINATES (A.U.)
> 
>NO LB  ZAFRAG MASS X   Y   Z
> 0 C 6.012.011   -3.2656360.1988940.090858
> 1 C 6.012.011   -1.3071611.5222121.003463
> 2 C 6.012.0111.2133360.948208   -0.033373
> 3 N 7.014.0073.2386501.0415231.301322
> 4 C 6.012.011   -5.9544890.6508780.803379
> 5 C 6.012.0115.6544760.4800660.013757
> 
> where the number of lines depends upon the molecule being considered.
> 
> I want to extract the block of data starting on line 4 and column 4. 

A block of data needs a start and end. Where is the end?
Also which of the above lines is line 4?
I'd read it as the one starting the actual raw data values.
But it could be the 4th line of values that you want?

It might help to know the OS you are using too because Unix
in particular has some OS level tools that might simplify
preparing the data.
-- 
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] Using an XML file for web crawling

2017-03-31 Thread Alan Gauld via Tutor
On 31/03/17 12:23, Igor Alexandre wrote:

> I have a sitemap in XML and I want to use it to save as text the various pages

What about non-text pages such as images and media files?

> I'm looking for some code on the web where I can just type the xml address 
> and wait for the crawler to do it's job, saving all the pages
> indicated in the sitemap as a text file in my computer.

I assume you mean multiple text files? And I assume you want to recreate
the site structure too - with folders etc?

There are tools around to do that but I don't know of any Python code
that you can just pick up and use, you will need to do a bit of work.
But I'm not an expert in web crawling so I could be wrong! :-)

-- 
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] Extract Block of Data from a 2D Array

2017-03-31 Thread Stephen P. Molnar

I have a block of data extracted from a quantum mechanical calculation:

CARTESIAN COORDINATES (A.U.)

  NO LB  ZAFRAG MASS X   Y   Z
   0 C 6.012.011   -3.2656360.1988940.090858
   1 C 6.012.011   -1.3071611.5222121.003463
   2 C 6.012.0111.2133360.948208   -0.033373
   3 N 7.014.0073.2386501.0415231.301322
   4 C 6.012.011   -5.9544890.6508780.803379
   5 C 6.012.0115.6544760.4800660.013757

where the number of lines depends upon the molecule being considered.

I want to extract the block of data starting on line 4 and column 4. 
Unfortunately, the only programming language in which I used to be 
competent in is Fortran.  I have attempted researching this problem, but 
have only succeeded in increasing my mental entropy.


Help will be much appreciated.  Thanks in advance.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Using an XML file for web crawling

2017-03-31 Thread Igor Alexandre
Hi!
I'm a newbie in the Python/Web crawling world. I've been trying to find an 
answer to the following question since last week, but I couldn't so far, so I 
decided to ask it myself here: I have a sitemap in XML and I want to use it to 
save as text the various pages of the site. Do you guys know how can I do it? 
I'm looking for some code on the web where I can just type the xml address and 
wait for the crawler to do it's job, saving all the pages indicated in the 
sitemap as a text file in my computer. 
Thank you!
Best,
Igor Alexandre


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


Re: [Tutor] All Entry Boxes taking the same value

2017-03-31 Thread Alan Gauld via Tutor
On 30/03/17 21:35, Pooja Bhalode wrote:

> *However, when I execute it, and type something in one entrybox, it shows
> in all the entry boxes using multi-cursor option. *

I'm not sure whats going on and don;t habe tome to experiment but one
thing I noticed:

> average = [" ", " ", " "]
> lowest = [" ", " ", " "]
> highest = [" ", " ", " "]
...
> for i in range(len(reactants)):
> *Entry*(root, textvariable =* average[i]*, width = 15,
> state=DISABLED).grid(row = 3+i, column = 1, sticky = W)

You are setting textvariable to a string but it should be
a StrinVar object. You could probably fix that by changing
your data definitions to StringVars:

average = [StringVar(), StringVar(), StringVar()]

I don't know if that will fix the problem but its probably
a good thing to do anyhow...


-- 
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] All Entry Boxes taking the same value

2017-03-31 Thread Peter Otten
Pooja Bhalode wrote:

> average = [" ", " ", " "]

> for i in range(len(reactants)):
> Entry(root, textvariable = average[i], width = 15,
> state=DISABLED).grid(row = 3+i, column = 1, sticky = W)

Hint: What is passed as textvariable? What should be?

Unfortunately the Entry accepts both StringVar instances and names of 
StringVar instances which automagically spring into existence -- and " " is 
a valid name...

Pooja, I have been watching your efforts to write one or more python/tkinter 
scripts mostly via try and error for a while now. It is obvious that it 
doesn't work as there are so many more ways to go wrong than right.

I recommend that you see if you can find a tutorial for both Python and 
Tkinter, and work through that for a few weeks, say. Once you have a general 
understanding of programming your progress will be faster, and you won't 
destroy the result of the previous step with the changes you make in the 
next.

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


Re: [Tutor] python gtk serial loop thread readind data close

2017-03-31 Thread Alan Gauld via Tutor
On 30/03/17 13:40, Alexandru Achim via Tutor wrote:
> Dear users,
> I had a problem regarding Threads in python and Gtk3.

This list is really for the core language and library, I suspect you
might get a better response by asking on the PyGTK forum where there are
more likely to be people who have come across the same issues.

-- 
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] super constructor usage

2017-03-31 Thread Alan Gauld via Tutor
On 30/03/17 21:08, Alan Gauld via Tutor wrote:

> Of course, the __init__ methods are special in any way

Should have said *not special* in any way...

> But remember that not calling super potentially leaves
> some attributes of your superclass uninitialized. By not
> calling super you assume full responsibility for
> initializing both your sub class and the superclass.

And it's worth reminding ourselves that there could be several
superclasses not just the one we immediately inherit from.

Finally, this discussion has been in the context of
constructors but super() applies to all methods. There is
nothing unique about construction, it's just like any
other method call where you want to include the
superclass functionality.

-- 
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] All Entry Boxes taking the same value

2017-03-31 Thread Pooja Bhalode
Hi,
I am working on a GUI application where I have a multiple number of entry
boxes and I created a loop for the label and entry boxes depending upon the
number of times it needs to be shown. However, I tried assigning different
values to each label and entry boxes in the for loop using lists, so that I
can save different values obtained from the entry boxes in the entry lists
and use them later.

*However, when I execute it, and type something in one entrybox, it shows
in all the entry boxes using multi-cursor option. *

Can someone please tell me how I can avoid this error?
I am trying to put a for loop since I need to create the gui such that the
number of times it needs to be done would be unknown. Hence, the way I went
about this was to take an upper limit of 3 and thus, reacoriginal = [" ", "
", " "]. This can be used as an upper limit and the required number can
thus fit in as needed.
Here, for example, I have taken the required number as two. this would be
later modified.

Code:
from Tkinter import *

root = Tk()
root.geometry("500x350")

# Variables:
reacoriginal = [" ", " ", " "]
conca = [1,0,0]
print conca[0]
concunit = [" ", " ", " "]

concunit11 = StringVar()
concunit21 = StringVar()
concunit31 = StringVar()
conca1 = IntVar()
conca1.set(1)
concb1 = IntVar()
concc1 = IntVar()

average = [" ", " ", " "]
lowest = [" ", " ", " "]
highest = [" ", " ", " "]
averageformulation = [" ", " ", " "]
lowestformulation = [" ", " ", " "]
highestformulation = [" ", " ", " "]

print average
reactants = ["A", "B", "C"]
for i in range(len(reactants)):
Label(root, text = "Experimental Range").grid(row = 0, column = 0, sticky =
W)
labeldown = Label(root, text = "For Parameter Estimation")
labeldown.grid(row = 1, column = 0, sticky = W)
labeldown.config(state='disable')

Label(root, text = "Average ").grid(row = 2, column = 1, sticky = W)
Label(root, text = "Lowest ").grid(row = 2, column = 2, sticky = W)
Label(root, text = "Highest").grid(row = 2, column = 3, sticky = W)
Label(root, text = "Units").grid(row = 2, column = 4, sticky = W)
Checkbutton(root, text = reactants[i], variable = conca[i]).grid(row = 3+i,
column = 0, sticky = W)
optionlist2 = [u'\u03bcmol/L',  'mmol/L','mol/L']
concunitdrop = OptionMenu(root, concunit[i], *optionlist2)
concunitdrop.config(width = 8)
concunitdrop.grid(row = 3+i, column = 4, sticky = W)

for i in range(len(reactants)):
*Entry*(root, textvariable =* average[i]*, width = 15,
state=DISABLED).grid(row = 3+i, column = 1, sticky = W)
*Entry*(root, textvariable = *lowest[i]*, width = 15).grid(row = 3+i,
column = 2, sticky = W)
*Entry*(root, textvariable = *highest[i]*, width = 15).grid(row = 3+i,
column = 3, sticky = W)

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


Re: [Tutor] super constructor usage

2017-03-31 Thread Mats Wichmann
On 03/30/2017 05:39 AM, Rafael Knuth wrote:
 I am trying to wrap my head around the super constructor.
> 
> Is it possible to embed a super constructor into an if / elif
> statement within the child class?
> 
> if message == "string A": return X
> elif: return Y
> 
> How should I modify my code below?
> (I couldn't solve that by myself)
> 
> class A:
> def __init__(self, message):
> self.message = message
> print(message)
> 
> class B(A):
> def __init__(self, message):
> print("This is the message from your parent class A:")
> super(B, self).__init__(message)
> 
> B("BlaBla")

For grins, try this (decorated with prints).  There's an additional
argument allowed to the class B initializer, but set as a default
argument so that if you omit it it defaults to True.

===
class A(object):
def __init__(self, msg):
print('A: Initializing instance of', self.__class__.__name__)
self.message = msg

class B(A):
def __init__(self, msg, doinit=True):
print('B: Initializing instance of', self.__class__.__name__)
if doinit:
super().__init__(msg)

print("Instantiating an A:")
a = A("some message")
print(a.message)

print("Instantiating a B:")
b = B("some message")
print(b.message)

print("Instantiating a B without calling superclass __init__:")
c = B("some message", False)
print(c.message)
===
When you run it:

Instantiating an A:
A: Initializing instance of A
some message
Instantiating a B:
B: Initializing instance of B
A: Initializing instance of B
some message
Instantiating a B without calling superclass __init__:
B: Initializing instance of B
Traceback (most recent call last):
  File "constr-tutor.py", line 22, in 
print(c.message)
AttributeError: 'B' object has no attribute 'message'
===

So note that: the instance attribute "message" is set in the class A
initializer, as in all your postings in this thread.  Just like Alan
pointed out, there are implications if you don't call up to the
superclass, and it shows up pretty clearly here: in the third case,
where we decide not to call the parent's  __init__, this initialization
doesn't happen and the attribute is missing, so accessing it blows up
with an AttributeError exception.


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


Re: [Tutor] HTML module for Python

2017-03-31 Thread ਪੰਜਾਬ ਪੰਜਾਬੀ
Thank you Alan. I needed to generate a report using a python script (will
become part of a web service) and send it as an email. For now I put the
required HTML tags wherever they were needed.

Thanks Sri Kavi.  I'll check out your suggestion.



On Wed, Mar 22, 2017 at 12:29 AM, Alan Gauld via Tutor 
wrote:

> On 20/03/17 22:09, ਪੰਜਾਬ ਪੰਜਾਬੀ wrote:
>
> > Looking for recommendations on Python module to use to generate HTML
> > pages/tables, other HTML content.  Kindly help.
>
> While thee are some modules that help with this most Python
> programs I've seen just generate the HTML directly as strings.
> There is no direct equivalent of, say, Perl's CGI module.
>
> However, if you are using a web framework, it will
> generally provide a templating mechanism which will
> separate out the HTML code  (the view) from your
> Python code (the model and controller).
>
> So if you give is more information about what the
> HTML is being used for, we might be able to provide
> more help. Is it part of a web application or just
> a data file that you make available to some third
> party?
>
> --
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading files in Python 3

2017-03-31 Thread Mats Wichmann
On 03/30/2017 11:02 AM, Rafael Knuth wrote:
> I can read files like this (relative path):
> 
> with open("Testfile_B.txt") as file_object:
> contents = file_object.read()
> print(contents)
> 
> But how do I read files if I want to specify the location (absolute path):
> 
> file_path = "C:\Users\Rafael\Testfile.txt"
> with open(file_path) as file_object:
> contents = file_object.read()
> print(contents)
> 
> The above does not work ...

Yeah, fun.  You need to escape the \ that the idiot MS-DOS people chose
for the file path separator. Because \ is treated as an escape character.

Get familiar with the path function in the os module, it will let you do
examinations of file paths, by the way, including constructing them in ways

https://docs.python.org/2/library/os.path.html

simple workaround: use a r letter in front of the string (r for raw):

file_path = r"C:\Users\Rafael\Testfile.txt"
os.path.exists(file_path)

ugly workaround: escape the backslashes so they aren't treated specially:


file_path = "C:\\Users\\Rafael\\Testfile.txt"
os.path.exists(file_path)

you can use os.path.join() to build up a path string in a more
independent way, although it doesn't work quite as expected. To get what
you're asking for, though:

file_path = os.path.join(os.path.expanduser('~Rafael'), 'Testfile.txt')
print(file_path)





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