Re: [Tutor] Python for Grade 12 Calculus
On Thu, Feb 11, 2016 at 05:56:00PM -0500, Nicholas Tomasic wrote: > Hi, > > I'm a 12th grade Calculus teacher at a school in Toronto, Ontario, and I'm > thinking about incorporating Python into an Independent Study Project for > two of my students. Both are passionate about coding with Python and I'm > thinking of asking them to produce something that can either a) calculate > derivatives of simple functions or b) assign 1 student the product rule, > one the quotient rule, and have them write a program that can tackle > derivatives in those forms. Sounds like a very reasonable project. An alternative is to give them a project to work with SymPy, which is a symbolic algebra package for Python. I presume that Yr 12 students have access to CAS calculators in Canada, is that right? Perhaps you could get them to write a comparison between the differentiation capabilites of their CAS and that of SymPy. The advantage to *you* is that this doesn't require any programming skill on your part, since your students will be submitting an essay rather than a computer program you will need to judge. Another idea might be to get them to write a small program that randomly generates a simple polynomial, asks the user to differentiate it, and then checks their answer against what SymPy calculates. http://www.sympy.org/en/index.html > The problem is: I know very little about Python. That would be a problem :-) Do you have any programming experience at all? If it's just a matter that you aren't familiar with Python, I can suggest running through a tutorial or two, and then asking questions here. If you have no programming experience at all, this may be a bit hard on you, since your students will know more about programming than you. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python for Grade 12 Calculus
On 11/02/16 22:56, Nicholas Tomasic wrote: > I'm a 12th grade Calculus teacher at a school in Toronto, I'm from Scotland and so have no idea what that means, but I take it that they are in high school rather than at a college/university? > two of my students. Both are passionate about coding with Python and I'm > thinking of asking them to produce something that can either a) calculate > derivatives of simple functions or b) assign 1 student the product rule, > one the quotient rule, and have them write a program that can tackle > derivatives in those forms. Programming is usually better for numerical analysis than doing formula translations (eg calculus). However there is a library, SymPy, that is part of the SciPy bundle that allows such things. If they don't mind downloading SciPy and learning a new package it might do what you want. Have a look at this web site for a few ideas of its potential: http://www.sympy.org/en/features.html There is a tutorial that might help too: http://docs.sympy.org/latest/tutorial/index.html#tutorial 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
[Tutor] Python for Grade 12 Calculus
Hi, I'm a 12th grade Calculus teacher at a school in Toronto, Ontario, and I'm thinking about incorporating Python into an Independent Study Project for two of my students. Both are passionate about coding with Python and I'm thinking of asking them to produce something that can either a) calculate derivatives of simple functions or b) assign 1 student the product rule, one the quotient rule, and have them write a program that can tackle derivatives in those forms. The problem is: I know very little about Python. Unfortunately, I don't have a specific question but I'm looking for any valuable insights or suggestions. I appreciate any information you can give me on the subject. Thanks so much, Nick Tomasic ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] declare a variable inside a class
On 11/02/2016 20:28, richard kappler wrote: Trying to optimize the modular input we wrote for Splunk, the basic structure (dictated by Splunk) is the entire script is a class with numerous methods within. The modular input reads a tcp stream, starts a new thread for each source, reads the messages from that source into a buffer, checks for stx and etx, formats it using an xslt and sends the result out to splunk. The method that actually formats the messages opens the xslt file, then uses it to format the message. Clever boy that I am, I figured it would be better to open the xslt file once, rather than once per message, so I moved the line xslt = ('path/to/xslt.file') out of the method it was in and up to the top level of the class, to wit: class MyClass(): xslt = ('path/to/xslt.file') def a_bunch-of-methods(): and so on. Obviously this didn't work, when the formatting method was called, it through an exception that the variable xslt wasn't defined. This is my first not-a-toy try at OOP, I could use a clue. regards, Richard I'd be inclined to put "xslt = ('path/to/xslt.file')" right back where it started, otherwise every instance of Myclass is limited to whatever that path is. If you insist on keeping xslt at the class level, then you need something like:- myclass = MyClass() myclass.xslt ... or:- def aMethod(self): self.xslt ... -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] declare a variable inside a class
>Thanks for the reply Martin, and in this instance I cannot post the >actual code (company rules). That's often the case. I think most of us understand that you may not be able to post the original code. >What I can do is say that with the xslt variable defined within the >formatter method, everything works, but when I pull it out and put >it in the upper level of the class, it gives me a traceback that >says the global variable xslt is not defined. Does that help? I understand what you are saying, but cannot make a more specific suggestion, beyond the example I posted before, which included the most common reason that people have that problem when moving names out of a method into the enclosing class. Summary: 'xslt' is not in the namespace you think it is in. I (or others) may be able to help if you use a short, self-contained correct example of your problem. You can change all of the variable names. You can (and should) omit everything that is irrelevant. When I'm trying to solve a problem like this, I usually crack open an editor and try to reproduce the problem in as few lines of code as I can. This often helps me to see my own errors. Good luck, -Martin -- 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] Need help with audio manipulation
On 11/02/16 18:03, Swift, Robert wrote: > I was wondering how to write a code that would take a recorded audio and > place these values into a numpy array? I have written various codes that > can record and playback sound and work properly. I need the recorded audio > to be accessed from a numpy array so I can process the sound and manipulate > it to play back the inverse of the array for a noise cancelling affect. Any > ideas would be great. There are numerous audio processing toolkits available, some work with SciPy, others are independent. There is a useful page here: wiki.python.org/moin/PythonInMusic You may find useful pointers there. As a subject its a bit off topic for this list which is about the standard library and language. -- 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] declare a variable inside a class
On 11/02/16 21:20, Peter Otten wrote: >> defined within the formatter method, everything works, but when I pull it >> out and put it in the upper level of the class, it gives me a traceback >> that says the global variable xslt is not defined. Does that help? > > You need to qualify the class attribute with self to access it from within a > method: Or you could use the class name since it is a class attribute: class MyClass: > ... xslt = "/path/to/file" > ... def some_method(self): > ... print(MyClass.xslt) If you need the possibility of having different paths for different instances you need to initialise it in an __init__() method: class MyClass: def __init__(self,path): self.xslt = path ... instance1 = MyClass('/some/path/here') instance2 = MyClass('/another/path/here') The way you have it currently both instances share the same path. But that may be what you want. -- 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] Mock filesystem operations only for specific test doubles (was: mock file reader object)
Anshu Kumar writes: > I need to mock file reader object , i tried using > @patch('__builtin__.open') but it will patch my all file open readers. For this reason I have written and published the Gajja library https://pypi.python.org/pypi/gajja/>. Its FileDouble objects will allow fine-grained control over exactly which file accesses are mocked, leaving the rest to behave normally. To date it is only used in one code base. I would be pleased to receive feedback either to my email address or at the “testing in Python” forum http://lists.idyll.org/listinfo/testing-in-python>. -- \ “I never forget a face, but in your case I'll be glad to make | `\ an exception.” —Groucho Marx | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] declare a variable inside a class
richard kappler wrote: > Thanks for the reply Martin, and in this instance I cannot post the actual > code (company rules). What I can do is say that with the xslt variable > defined within the formatter method, everything works, but when I pull it > out and put it in the upper level of the class, it gives me a traceback > that says the global variable xslt is not defined. Does that help? You need to qualify the class attribute with self to access it from within a method: >>> class MyClass: ... xslt = "/path/to/file" ... def some_method(self): ... print(self.xslt) ... >>> m = MyClass() >>> m.some_method() /path/to/file ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] declare a variable inside a class
Thanks for the reply Martin, and in this instance I cannot post the actual code (company rules). What I can do is say that with the xslt variable defined within the formatter method, everything works, but when I pull it out and put it in the upper level of the class, it gives me a traceback that says the global variable xslt is not defined. Does that help? regards, Richard On Thu, Feb 11, 2016 at 3:43 PM, Martin A. Brown wrote: > > Hi there again Richard, > > [snipped a bit of stuff] > > >The method that actually formats the messages opens the xslt file, > >then uses it to format the message. Clever boy that I am, I figured > >it would be better to open the xslt file once, rather than once per > >message, so I moved the line > > > >xslt = ('path/to/xslt.file') > > Yes, this is a good idea. > > >out of the method it was in and up to the top level of the class, to wit: > > > >class MyClass(): > > > >xslt = ('path/to/xslt.file') > > > > def a_bunch-of-methods(): > > An impossible method name. That would be a syntax error. > > >and so on. > > > >Obviously this didn't work, > > Why is it obvious? What was obvious to me is the syntax error, but > that does not appear to be what you are asking. > > What was the error message? > > >when the formatting method was called, it through an exception that > >the variable xslt wasn't defined. This is my first not-a-toy try at > >OOP, I could use a clue. > > So, before addressing your question, could I gently recommend that > you post your actual code (clean up any variables or data that you > don't want the public to see), whether it is running or not, along > with any error messages (pasted, please). > > This is just a reminder, that this reduces the guesswork on the part > of the members of the list. > > I think your problem is simply not knowing the name of the variable > you called 'xslt'. Perhaps the below example helps? > > -Martin > > #! /usr/bin/python3 > > class MyClass(object): > > xslt = '/usr/share/nmap/nmap.xsl' > > def find_xslt(self): > print(xslt) > > def oh_there_it_is(self): > print(self.xslt) > > def redefine_in_instance(self): > self.xslt = '/usr/share/sgml/X11/xorg.xsl' > > if __name__ == '__main__': > > # -- instantiate the class > c = MyClass() > > # -- this is what I think you were doing > try: > c.where_is_xslt() > except AttributeError: > pass > > # -- but, try locating the class attribute through self > c.oh_there_it_is() > > # -- class attribute can be overridden in instances > c.redefine_in_instance() > c.oh_there_it_is() > > # -- newly defined instances will get the default class attribute > d = MyClass() > d.oh_there_it_is() > > -- > Martin A. Brown > http://linux-ip.net/ > -- *Java is like Alzheimers; it starts slow and eventually, it takes away all of your memory.* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] declare a variable inside a class
Hi there again Richard, [snipped a bit of stuff] >The method that actually formats the messages opens the xslt file, >then uses it to format the message. Clever boy that I am, I figured >it would be better to open the xslt file once, rather than once per >message, so I moved the line > >xslt = ('path/to/xslt.file') Yes, this is a good idea. >out of the method it was in and up to the top level of the class, to wit: > >class MyClass(): > >xslt = ('path/to/xslt.file') > > def a_bunch-of-methods(): An impossible method name. That would be a syntax error. >and so on. > >Obviously this didn't work, Why is it obvious? What was obvious to me is the syntax error, but that does not appear to be what you are asking. What was the error message? >when the formatting method was called, it through an exception that >the variable xslt wasn't defined. This is my first not-a-toy try at >OOP, I could use a clue. So, before addressing your question, could I gently recommend that you post your actual code (clean up any variables or data that you don't want the public to see), whether it is running or not, along with any error messages (pasted, please). This is just a reminder, that this reduces the guesswork on the part of the members of the list. I think your problem is simply not knowing the name of the variable you called 'xslt'. Perhaps the below example helps? -Martin #! /usr/bin/python3 class MyClass(object): xslt = '/usr/share/nmap/nmap.xsl' def find_xslt(self): print(xslt) def oh_there_it_is(self): print(self.xslt) def redefine_in_instance(self): self.xslt = '/usr/share/sgml/X11/xorg.xsl' if __name__ == '__main__': # -- instantiate the class c = MyClass() # -- this is what I think you were doing try: c.where_is_xslt() except AttributeError: pass # -- but, try locating the class attribute through self c.oh_there_it_is() # -- class attribute can be overridden in instances c.redefine_in_instance() c.oh_there_it_is() # -- newly defined instances will get the default class attribute d = MyClass() d.oh_there_it_is() -- 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
[Tutor] declare a variable inside a class
Trying to optimize the modular input we wrote for Splunk, the basic structure (dictated by Splunk) is the entire script is a class with numerous methods within. The modular input reads a tcp stream, starts a new thread for each source, reads the messages from that source into a buffer, checks for stx and etx, formats it using an xslt and sends the result out to splunk. The method that actually formats the messages opens the xslt file, then uses it to format the message. Clever boy that I am, I figured it would be better to open the xslt file once, rather than once per message, so I moved the line xslt = ('path/to/xslt.file') out of the method it was in and up to the top level of the class, to wit: class MyClass(): xslt = ('path/to/xslt.file') def a_bunch-of-methods(): and so on. Obviously this didn't work, when the formatting method was called, it through an exception that the variable xslt wasn't defined. This is my first not-a-toy try at OOP, I could use a clue. regards, Richard -- *Java is like Alzheimers; it starts slow and eventually, it takes away all of your memory.* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Need help with audio manipulation
I was wondering how to write a code that would take a recorded audio and place these values into a numpy array? I have written various codes that can record and playback sound and work properly. I need the recorded audio to be accessed from a numpy array so I can process the sound and manipulate it to play back the inverse of the array for a noise cancelling affect. Any ideas would be great. Thanks, Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] mock file reader object
Hi, I need to mock file reader object , i tried using @patch('__builtin__.open') but it will patch my all file open readers. More precisely i have code something like below def restore_segments_and_lines_json(segments_file_path,lines_file_path,deleted_segment_ids): with open(segments_file_path, "rb") as segments_reader: segments = segments_reader.read() with open(lines_file_path, "rb") as lines_reader: lines = lines_readers.read() """ Do some processing with lines and segments""" logger.info("updating segments file") with open(segments_file_path, "wb") as segments_writer: segments_writer.write(segments, segments_writer) logger.info("updating lines file") with open(lines_file_path, "wb") as lines_writer: lines_writer.write(lines, lines_writer) I need to mock two different file read and file write opens, can i get some suggestion. Thanks in advance, Anshu ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] genfromtxt vs. reading csv to a list or dictionary
Thanks Danny! I realized that I should not be concerned with speed/efficiency since I am a beginner in python and giving the data I work with, speed is not that important now. I am learning how to read files via several ways and transform them to numpy arrays. I just thought genfromtxt provides many options and one could use it to read and convert a file into 2d array with proper format; instead of reading it then format it. But genfromtxt gives byte strings which i could not convert to integer, float, string, etc. Thanks again--EKE ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor