Re: [Tutor] XML Programs

2018-04-21 Thread Peter Otten
Glen wrote:

> Thank you for your comprehensive reply. It was very helpful!
> Just to clarify one point, is it not possible to search for a node
> directly in the element / elementTree or do you first need to pull the
> data into a class/dict?

You certainly have other options

>>> catalog.xpath("//catalog/book[contains(title,'Guide')]/title/text()")
["XML Developer's Guide", 'MSXML3: A Comprehensive Guide', 'Visual Studio 7: 
A Comprehensive Guide']
>>> catalog.xpath("//catalog/book[starts-
with(author,'Corets')]/title/text()")
['Maeve Ascendant', "Oberon's Legacy", 'The Sundered Grail']

but then the question is whether you want to learn Python or XPath.

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


Re: [Tutor] XML Programs

2018-04-21 Thread Albert-Jan Roskam

On Apr 21, 2018 09:06, Peter Otten <__pete...@web.de> wrote:
>
> Glen wrote:
>
> > Thank you for your comprehensive reply. It was very helpful!
> > Just to clarify one point, is it not possible to search for a node
> > directly in the element / elementTree or do you first need to pull the
> > data into a class/dict?
>
> You certainly have other options
>
> >>> catalog.xpath("//catalog/book[contains(title,'Guide')]/title/text()")
> ["XML Developer's Guide", 'MSXML3: A Comprehensive Guide', 'Visual Studio 7:
> A Comprehensive Guide']
> >>> catalog.xpath("//catalog/book[starts-
> with(author,'Corets')]/title/text()")
> ['Maeve Ascendant', "Oberon's Legacy", 'The Sundered Grail']
>
> but then the question is whether you want to learn Python or XPath.

Neat! Can you recommend a good resource (book, webpage) for learning Xpath?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Theory of computation non-emptiness

2018-04-21 Thread tracey jones-Francis
Hi there, I've been working on code that takes a text file that represents a 
specific Deterministic Finite Automata.


The text files are set up in a specific way so that line 1 specifies the number 
of states. Line 2 specifies the states (i.e., just a list of the names of the 
states, separated by spaces). Line 3 specifies the size of the alphabet. Line 4 
specifies the alphabet. Lines 5-7 give the transition function, each row 
corresponding to a state (in order specified on line 2) and each column 
corresponding to a symbol from the alphabet (in order specified on line 4). 
Line 8 specifies the start state. Line 9 specifies the number of final/accept 
states. Line 10 specifies the final states.


I have already constructed some functions but next I want to construct a 
function determining, for any DFA M, whether or not L(M) = 0  i.e., whether or 
not there exists at least one string that is accepted by M. if the language If 
L(M) = 0 then I want “language empty” printed. If L(M) != 0; then i need to 
print “language non-empty -  accepted”, where  is replaced by some 
string that is accepted by M.


I wanted to use breath first search in order to determine the existence of a 
path. Code below is all I've done so far but i'm struggling to find a way in 
which to link the states with the transitions in the text file.


hope you can give me some direction and advice.


def NonEmptiness(dic):

dic = openTextFile(dic)

#print(dic['states'].split())


theString = ""
visited = []
queue = dic['states'].split()

newStates = dic['states']


newStatesFinal = re.sub(' ','', newStates)
newAplhabet = re.sub(' ','', dic['alphabet'])

#print(dic['finalStates'][2])
print(newStatesFinal)
#print(newStatesFinal[0])
while queue:
currentState = queue.pop(0)
visited.append(currentState)
#print(visited)
for a in newAplhabet:
if (currentState, a) == (newStatesFinal[0], newAplhabet[0]):
if dic['transitionStates'][0][0] != dic['finalStates'][0] or 
dic['finalStates'][2]:
theString + a
else:

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


Re: [Tutor] Theory of computation non-emptiness

2018-04-21 Thread Alan Gauld via Tutor
On 21/04/18 12:50, tracey jones-Francis wrote:
> Hi there, I've been working on code 

I've only glanced at this but one thing jumped out at me:


> while queue:
> currentState = queue.pop(0)
> visited.append(currentState)
> #print(visited)
> for a in newAplhabet:
> if (currentState, a) == (newStatesFinal[0], newAplhabet[0]):
> if dic['transitionStates'][0][0] != dic['finalStates'][0] or 
> dic['finalStates'][2]:

This reads to Python like

if X != A or B

which it parses as

if (X != A) or B


Is that what you intended?
It may be the case but, if so, it's a rather cryptic way of
expressing it.


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