[Tutor] double nodes being enter into tree structure
Hello All, I am back!!! I forget which movie that comes from. Anyway, my issue is I am getting the same node being added to the parent node of my tree below. I have traced the code and do not understand why. This occurs when the loop which calls the addNode function has only looped once. I have had the return statement present and commented out with the same result. The length of the children list is 2 when it should be 1. AddNode is using recursive functionality. I suspect the function should be in the object itself, but I have it outside for now to work out my logic. I am sure there is a logic issue here in the area of OOPS, but I cannot see it. The aim is two only ever have one word in the children list regardless how many times it appears in the original source. Thus if the string has “the brown fox” and “the brown car”. The following has to occur: * Children list in the root node will only have the “the” node once. * Children list in the “the” node will only have the “brown” node once. * Children in the “brown” node will have two nodes ‘cow’ and ‘car’. Below is the code: def addNode(words, tree): # creates the branch of words for the tree if words: wordExists = False for child in tree.children: if words[0] == child.name: wordExists = True # end if # end for if not wordExists: tree = tree.add(words[0], 0) addNode(words[1:], tree) # end if # note, the below has been uncommented with the same issue. #return tree class Node(object): # I am not 100% sure the purpose of (object) does. def __init__(self, name, value): self.parent = None self.children = [] self.name = name self.value = value def add(self, name, value): node1=Node(name, value) self.children.append(node1) node1.parent=self return node1 …. Bunch a code to load the files. for account_no, date, line, amount in records: tree.children.append(addNode(line.split(), tree)) note: I have tried Allan’s suggestion as shown below and could not get it to work. I am currently reading his book on object programming to learn more about OOPS. This is how I worked out the recursive function. 😊 def add_child(self, newNode): if newNode.name != self.name self.children.append(newNode) newNode.parent = self myNode.add_child(Node(name, value)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Environment variables and Flask
On 28/06/2019 06:24, Mayo Adams wrote: > What are these environment variables, exactly, and why is it necessary to > set them? When you run a program under an operating system the OS sets up an "environment" (or context) for the program to run in. (This includes the OS shell that the user interacts with - each user gets their own environment.) Environment variables are variables that are accessible within that environment but not from other environments. Thus two users may have different values for the same variable, such as the HOME variable which dictates the users home directory. Or PATH which tells the OS where the users programs can be found. When you start a new program the OS creates a copy of the current environment(your user environment) and runs the program within that copy. Thus if the program modifies any of the environment variables it does not affect the parent process environment since it is modifying its own copy. (So if it changes HOME to give itself its own default directory that doesn't change the user's HOME - or any other program environment for that matter) Some applications define their own environment variables as a way of setting user specific values. So a web server might define the root directory for the server as an environment variable then each user can set a different root without having to pass that in each time they run the program. Environment variable have fallen out of favour for user settings and config files are now preferred. But some things are a bit easier via en environment variable - especially where you spawn new sub-processes and don't want the sub-process to have to re-read the config file each time. -- 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] double nodes being enter into tree structure
On 28/06/2019 07:10, mhysnm1...@gmail.com wrote: > > Anyway, my issue is I am getting the same node being added to the parent node > of my tree below. I'm not sure about that part but > def addNode(words, tree): > if words: > wordExists = False > for child in tree.children: > if words[0] == child.name: > wordExists = True > if not wordExists: > tree = tree.add(words[0], 0) > > addNode(words[1:], tree) Notice that the recursive call is adding the subsequent words to the same tree that was passed to addNode originally. In other words you are not building a tee you are just building a list of children under the top level tree node. I suspect you want to add the subsequent words to the children of the node you just added? Or the existing one of the same value... So you need something like(untested!) for child in tree.children: if words[0] == child.name nextNode = child else: nextNode = tree.addNode(words[0],0) addNode(words[1:], nextNode) -- 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] Environment variables and Flask
On 28Jun2019 09:34, Alan Gauld wrote: Environment variable have fallen out of favour for user settings and config files are now preferred. But some things are a bit easier via en environment variable - especially where you spawn new sub-processes and don't want the sub-process to have to re-read the config file each time. This is something of a simplification. Most programmes consult a few places for configuration information. A programme may want to run in different ways (different places to write files, different language settings or timezones, etc). Environment variables are a convenient and inheritable way to indicate a specific way to run, because they get inherited (as a copy) from parent programme to child programmes and so on. So a flask application will usually be invoked from within a web server, and various things about how it should run _may_ be indicated by environment variables set by the web server. A flexible programme may decide how to run from several places, in a specific order (to ensure predictable controllable behaviour). A normal order would be: command line arguments, environment variables, personal config file ($HOME/.thingrc), system configfile (/etc/thingrc), inbuilt defaults within the programme. The idea here is that this is a simple hierachy of defaults. Anything can be overridden by a command line option. If not supplied, an environment variable may be consulted. Otherwise the personal config file. Otherwise the system default. Otherwise some default within the programme. Programmatically you don't go: for each setting, look at these things in the order above. Instead you has some "settings" structure in the programme, initially filled out with some internal defaults. You read the system file to override the builtin defaults. Then you read the personal file to override that. Then you consult the environment to override that. Then you process the command line and have it override various things. A single pass across all this stuff. Any of it may be missing. Returning to Flask and the environment: because a Flask app is often invoked from within a web server instead of directly, it isn't feasible to pass it "command line" arguments to control it. So the environment becomes the most convenient place for ad hoc special settings. Cheers, Cameron Simpson "How do you know I'm Mad?" asked Alice. "You must be," said the Cat, "or you wouldn't have come here." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Environment variables and Flask
On 6/27/19 11:24 PM, Mayo Adams wrote: > I have for some time been flummoxed as to the significance of setting > environment variables, for example in order to run a Flask application. > What are these environment variables, exactly, and why is it necessary to > set them? "Googling" here simply leads me into more arcana, and doesn't > really help. As others have noted, it's a way to pass information from one process to another at startup time. Since this is a Python list, I thought it might be instructive to show how it works. In Python, you access these environment variables through a dictionary in the os module, called environ, which "survives" across the call-another-process boundary, unlike any normal variables you might set in your program. Here's a trivial Python app that is able to recognize those environment variables that begin with MYENV_. That points up one issue with environment variables right away: it's a namespace you share with everybody, and there's a chance someone accidentally is using a variable you think is important - because it's important to them in their context, not yours. So tricks like special naming conventions may be useful. In this snip, we build a dictionary from os.environ, using only the keys that seem to be "for us": === child.py === import os myenv = { k: v for k, v in os.environ.items() if "MYENV_" in k } print("child found these settings:", myenv) == Now write another script which sets a value, then calls the child script; then sets a different value, then calls the child script. === parent.py === import os import subprocess print("Calling with MYENV_foo set") os.environ['MYENV_foo'] = "Yes" subprocess.run(["python", "child.py"]) print("Calling with MYENV_bar set") os.environ['MYENV_bar'] = "1" subprocess.run(["python", "child.py"]) == ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] double nodes being enter into tree structure
On 6/28/19 12:10 AM, mhysnm1...@gmail.com wrote: > class Node(object): > > # I am not 100% sure the purpose of (object) does. as to this bit: having your class inherit from object means it's a "new-style class". That's only significant if you're writing code that is expected to run under Python 2; in Python 3 all classes are new-style and you don't need to inherit from object to get that - and if you run a code checker on your program in a Python 3 context, it will likely tell you so. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Environment variables and Flask
Many thanks to some very bright and helpful gentlemen. On Fri, Jun 28, 2019 at 9:24 AM Mats Wichmann wrote: > On 6/27/19 11:24 PM, Mayo Adams wrote: > > I have for some time been flummoxed as to the significance of setting > > environment variables, for example in order to run a Flask application. > > What are these environment variables, exactly, and why is it necessary to > > set them? "Googling" here simply leads me into more arcana, and doesn't > > really help. > > As others have noted, it's a way to pass information from one process to > another at startup time. Since this is a Python list, I thought it > might be instructive to show how it works. In Python, you access these > environment variables through a dictionary in the os module, called > environ, which "survives" across the call-another-process boundary, > unlike any normal variables you might set in your program. Here's a > trivial Python app that is able to recognize those environment variables > that begin with MYENV_. That points up one issue with environment > variables right away: it's a namespace you share with everybody, and > there's a chance someone accidentally is using a variable you think is > important - because it's important to them in their context, not yours. > So tricks like special naming conventions may be useful. > > In this snip, we build a dictionary from os.environ, using only the keys > that seem to be "for us": > > > === child.py === > import os > > myenv = { k: v for k, v in os.environ.items() if "MYENV_" in k } > > print("child found these settings:", myenv) > == > > Now write another script which sets a value, then calls the child > script; then sets a different value, then calls the child script. > > === parent.py === > import os > import subprocess > > print("Calling with MYENV_foo set") > os.environ['MYENV_foo'] = "Yes" > subprocess.run(["python", "child.py"]) > > print("Calling with MYENV_bar set") > os.environ['MYENV_bar'] = "1" > subprocess.run(["python", "child.py"]) > == > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Mayo Adams 287 Erwin Rd. Chapel Hill, NC 27514 (919)-780-3917 mayoad...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Re: Unexpected result when running flask application.
Ooops, forgot to 'reply all' -- Forwarded message -- From: Albert-Jan Roskam Date: 28 Jun 2019 21:31 Subject: Re: [Tutor] Unexpected result when running flask application. To: Cameron Simpson Cc: On 20 Jun 2019 00:56, Cameron Simpson wrote: On 19Jun2019 09:54, Alan Gauld wrote: >On 19/06/2019 05:18, Cravan wrote: >> I am experiencing an unexpected result when I try to >> run my flask application. >> The movie.html page prints out nothing except those in the . This >> appears on my webpage: > >Note that the mail server does not allow (for security reasons) >binary attachments so we lost your image. Cravan, you might find it useful to "View Source" of that page in your browser. You can also use command line tools like "curl" or "wget" to directly fetch the page content. >However, your html files are not in HTML. >I'm not a Flask expert but every time I've used Flask the >html pages have been real HTML. Yours appear to be in some >strange pseudo markup language. It is very common in Flask to write HTML pages using Jinja templates, which is what his examples look like. Of course this adds more complexity, if he forgets to use Jinja to render the content to HTML before returning it. >If this is something unique to Flask then I suspect you will >need to ask on a Flask support page or list. It doesn't seem >to be a Python language related issue at this point. ==》 I haven't seen the templates, but your view function should probably retun flas.render_template('some.html', **kwargs), where kwargs will be e.g. your database values that are going to be displayed/{{interpolated}}. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 3.7 Grids
Hi Looking for a way to use the determine the position of a card in a grid using the mouse click event in Python. Code is attached. Unfortunately using Tinkter grids / frames can't determine between the two demo cards. The Relevant code is below def showCardInitial(cardList): cardsToPlay = [] length = len(cardList) fileFaceDown = 'extracards/FACEDOWN.gif' cardFaceDownCard = fileFaceDown.split('/') cardFaceDownCard = cardFaceDownCard[1].split('.')[0] photo = PhotoImage(file=fileFaceDown) number = random.randrange(0,length) fileFaceUp = cardList[number][2] card = fileFaceUp.split('/') card = card[1].split('.')[0] w = Label(image=photo) w.photo = photo w.grid(row = 0,column = 0,padx = 10) #print('cardListis ',cardList) cardFile = cardList[1][2] #print cardFile photo = PhotoImage(file=cardFile) cardToDisplay = Label(image=photo) cardToDisplay.photo = photo cardToDisplay.grid(row=0,column = 1,padx = 10) #w.grid(row = row,column = count) return def determineCard(): global x global y print( "clicked at", x, y) if(2 <= x and x <= 83) and (0 <= y and y <= 130): print('Card One is Selected') return Any suggestions are welcome -- Dave Merrick TutorInvercargill http://tutorinvercargill.co.nz Daves Web Designs Website http://www.daveswebdesigns.co.nz Email merrick...@gmail.com Ph 03 216 2053 Cell 027 3089 169 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor