Author Topic: A little python help please  (Read 4250 times)

A little python help please
« on: 12 March, 2018, 01:24:49 pm »
I'm writing a  simple program to interrogate an measuring instrument  using  PyVisa. If for  example I send  this command from a  terminal  console:
Quote
>>> MS2712E.write(':MMEMory:MSIS USB')
I get  echoed back:
Quote
(32, <StatusCode.success: 0>)
What I cannot  understand  is how  to capture this a variable within my program code,  I wish to wait for the  response and  then continue - with a timeout and error handler. Yes - I have searched the documentation!

 

Re: A little python help please
« Reply #1 on: 12 March, 2018, 05:48:46 pm »
Is the response not just value returned by the write call? (Ah no, it will return the number of bytes sent by the write() call.)

Looking at the documentation it seems that it shouldn't be, and that a write() can be followed by a read() to get a response, and these can be combined as a query, i.e.

resp = MS2712E.query(':MMEMory:MSIS USB')

What do you get if you do the following in the REPL:-

resp = MS2712E.write(':MMEMory:MSIS USB')
print resp
resp = MS2712E.read()
print resp

...and starting again...

resp = MS2712E.query(':MMEMory:MSIS USB')
print resp

?

Timeouts are handled by a separate variable: https://pyvisa.readthedocs.io/en/stable/resources.html#timeout
"Yes please" said Squirrel "biscuits are our favourite things."

Re: A little python help please
« Reply #2 on: 13 March, 2018, 08:21:07 am »
Greenbank,

Thank you for a  helpful reply
Your first example  returns the string, seems obvious now.

This is the  first  four of  the returned 400 csv screen levels in -dbm ..why, oh why, to six DP's?
Quote
#46393-113.124001,-113.332001,-111.680000,-111.568001

- having  added  the missing ',' to ':#46393,-113.1240...' I am considering how to parse out max and min values.



Re: A little python help please
« Reply #3 on: 13 March, 2018, 01:50:00 pm »
If the data is a string you could start by making it into a list of float:

Code: [Select]
str="#46393,-113.124001,-113.332001,-111.680000,-111.568001"
nms=map(float, str.split(",")[1:])
print min(nms)
print max(nms)

What is the #46393 anyway?  Is it the length of the data?

Re: Moar python help please
« Reply #4 on: 13 March, 2018, 08:14:15 pm »
If I have this:
str = "<!--docstart --> <p><a href=\"documentation  - Copy.html\">documentationCopy.html</a></p><!--docend -->"
I can match it with this:
pat = re.compile(r"^<!--docstart -->[\w*\s\n<>.\"/_=-]+<!--docend -->$", re.IGNORECASE)
 using
 pat.search(str)

However if I put the same string into a file and read the file using
with open(docfile, 'rt') as doc_in: # open file for reading
    docList = doc_in.read()

Then try
pat.search(docList)

I don't get a match.

Any clue why?
<i>Marmite slave</i>

Re: A little python help please
« Reply #5 on: 13 March, 2018, 08:34:21 pm »
Your string literal has escaped quotes -- the two instances of the two character sequence \" in the href. These convert to a single " character in the value of the string. If your file also contains the two character sequences these will not be converted to a single character when the data is read from the file. That means the value read from the file will contain \ characters and so will be different from the value converted from the string literal.

Re: A little python help please
« Reply #6 on: 13 March, 2018, 08:59:08 pm »
Hmm - good point.

The file actually contains this:
<!--docstart --> <p><a href="documentation  - Copy.html">documentationCopy.html</a></p><!--docend -->

along with many other lines

dammit

I just tried it with a stripped version of the file, removing all the other lines. It matches then

So the match fails because of the the preceding lines.
<i>Marmite slave</i>

Re: A little python help please
« Reply #7 on: 13 March, 2018, 09:07:33 pm »
Your re starts with ^ which explicitly requires the match to be at the start of the data. Perhaps you want MULTILINE mode?

ian

Re: A little python help please
« Reply #8 on: 13 March, 2018, 09:19:56 pm »
You have finally created a topic I can't post something stupid in. Congratulations.

Re: A little python help please
« Reply #9 on: 13 March, 2018, 09:28:39 pm »
I may have tried to do something wrong. In googling this, I found a post on stackoverflow
Quote
Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide.
https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

However, I have summoned the ungodly, used re.M and parsed html.
<i>Marmite slave</i>

Re: A little python help please
« Reply #10 on: 14 March, 2018, 08:07:44 am »
The file will contain a carriage return (and possibly a line-feed) at the end of the line but your regex does not expect this (it expects the end of the string, i.e. no more characters, after the last > character).

Either:-
a) Strip off the carriage return so that your regex matches (it will also fail if there are multiple lines as the [\w*\s\n<>.\"/_=-] regex will not match intermediate CR/LF), i.e. rstrip() or rstrip( '\n' )
b) use multiline mode to mask the problem
c) Modify the regex to expect a carriage return at the end, i.e. something like this (untested):-

Code: [Select]
pat = re.compile(r"^<!--docstart -->[\w*\s\n<>.\"/_=-]+<!--docend -->[\r\n]*$", re.IGNORECASE)
Regex can be used for simple parsing of HTML but if the parsing requirements start to grow it quickly escalates into insanity. Then you want to move to a proper parser such as html.parser (https://docs.python.org/3/library/html.parser.html)
"Yes please" said Squirrel "biscuits are our favourite things."

Re: A little python help please
« Reply #11 on: 14 March, 2018, 08:24:36 am »
Using multiline worked.

I ran out of time to do the next bit, which is to replace the matched string and then write out the result to the original file.
<i>Marmite slave</i>

Oaky

  • ACME Fire Safety Officer
  • Audax Club Mid-Essex
    • MEMWNS Map
Re: A little python help please
« Reply #12 on: 14 March, 2018, 09:45:25 am »
You have finally created a topic I can't post something stupid in. Congratulations.

No small serpents in Surrey?
You are in a maze of twisty flat droves, all alike.

85.4 miles from Marsh Gibbon

Audax Club Mid-Essex Fire Safety Officer
http://acme.bike

ian

Re: A little python help please
« Reply #13 on: 14 March, 2018, 09:51:46 am »
Bad Cat used to bring in slow worms but it's not happened recently so I assume they've got faster.

Re: A little python help please
« Reply #14 on: 14 March, 2018, 10:53:24 pm »
Wow! wot have i started!. :-). ATM no  coding  as out on site in Kent (bluebell hill) for a couple of days.  Then I may  well return to this topic  to ask something stupid

Chris S

Re: A little python help please
« Reply #15 on: 14 March, 2018, 11:20:46 pm »
Wow! wot have i started!. :-). ATM no  coding  as out on site in Kent (bluebell hill) for a couple of days.  Then I may  well return to this topic  to ask something stupid

Did you take a bike? Some of the cycling is quite steep round there; up and down the North Downs escarpment is good for the soul.

Re: A little python help please
« Reply #16 on: 15 March, 2018, 09:10:14 am »
lol

Last night I realised I was probably doing things all wrong so needed a different approach. Maybe I should just buy a decent book on Python instead of googling (seriously, can't sites just give syntax?).

There seem to be 9million different ways you could possibly achieve any given goal and the lack of explicit typing does my head in. End up with errors because I don't know what type something is, and end up using incompatible types. Well that would be solved if, erm types had to be declared and it was all, you know, explicit instead of this hacky crap.

<i>Marmite slave</i>

David Martin

  • Thats Dr Oi You thankyouverymuch
Re: A little python help please
« Reply #17 on: 15 March, 2018, 08:32:33 pm »
Python is both fantastic and the worst thing ever, often at the same time.
I've just downloaded some code in 2.7 but I run 3.5 so will be seeing if it runs.
"By creating we think. By living we learn" - Patrick Geddes

Re: A little python help please
« Reply #18 on: 16 March, 2018, 06:06:04 pm »
A recent introduction to Python 3 (as of 3.5) is type hinting


https://www.python.org/dev/peps/pep-0484/
Clever enough to know I'm not clever enough.

vorsprung

  • Opposites Attract
    • Audaxing
Re: Moar python help please
« Reply #19 on: 17 March, 2018, 01:03:38 pm »
If I have this:
str = "<!--docstart --> <p><a href=\"documentation  - Copy.html\">documentationCopy.html</a></p><!--docend -->"
I can match it with this:
pat = re.compile(r"^<!--docstart -->[\w*\s\n<>.\"/_=-]+<!--docend -->$", re.IGNORECASE)
 using
 pat.search(str)

However if I put the same string into a file and read the file using
with open(docfile, 'rt') as doc_in: # open file for reading
    docList = doc_in.read()

Then try
pat.search(docList)

I don't get a match.

Any clue why?

because you are trying to process html with regexp and not with a parser like BeautifulSoup

Re: A little python help please
« Reply #20 on: 17 March, 2018, 01:14:38 pm »
I actually managed to get that working - but it was a blind alley anyway. Will return to this later.

All I want to do is parse an HTML file for two markers, then replace the markers and any text between them with a string that I've created (that starts and ends with the markers).
<i>Marmite slave</i>

Re: A little python help please
« Reply #21 on: 18 March, 2018, 07:06:14 am »
I actually managed to get that working - but it was a blind alley anyway. Will return to this later.

All I want to do is parse an HTML file for two markers, then replace the markers and any text between them with a string that I've created (that starts and ends with the markers).

Aha! That's not parsing, that's just matching, so you won't be raising the undead and feeding your firstborn to them. Regular expressions will match the *bit* of the string you want them to match, so you don't need the (start|end)-of-(string|line) markers, or the *s to match outside what you actually want to match.

A couple of points. Regular expressions match "longest leftmost", so if there are multiple sets of marker pairs you will need to handle that differently to the case where there is only one. I'm not sure about your "character class" in the middle, do you really not want to match when there's a digit between your markers? If you want to match everything then say so (.+)

One of the online regex debuggers is a useful tool to try things out and see why they're not working.
Quote from: tiermat
that's not science, it's semantics.

Re: A little python help please
« Reply #22 on: 18 March, 2018, 03:50:42 pm »
A couple of points. Regular expressions match "longest leftmost", so if there are multiple sets of marker pairs you will need to handle that differently to the case where there is only one. I'm not sure about your "character class" in the middle, do you really not want to match when there's a digit between your markers? If you want to match everything then say so (.+)
I'm not sure what you mean. That expression successfully matches on strings that contain integer characters. The \w matches on alphanumerics.
<i>Marmite slave</i>

Mr Larrington

  • A bit ov a lyv wyr by slof standirds
  • Custard Wallah
    • Mr Larrington's Automatic Diary
Re: A little python help please
« Reply #23 on: 18 March, 2018, 08:51:03 pm »
Just use FORTRAN.

(Runs away)
External Transparent Wall Inspection Operative & Mayor of Mortagne-au-Perche
Satisfying the Bloodlust of the Masses in Peacetime

Re: A little python help please
« Reply #24 on: 18 March, 2018, 09:13:31 pm »
I actually did use FORTAN 77 at uni and would probably find it easier to follow.

In the end this turned out to be incredibly easy, once I ignored all the shite on stackoverflow.
<i>Marmite slave</i>