Re: [Tutor] Regarding Python api script
On Fri, Dec 07, 2018 at 12:30:18PM -0500, Avi Gross wrote: > # > I have often seen something like this done with methods, such as to > # > emulate decorator functionality where a method is created in an > # > object with a name > # > and the very next method created has the same name with the same > # > function name as an argument to replace it. > # Then I asked: > # I don't understand this. Can you show an example? Even if made up? [...] And Avi explained: > class classy(object): > ... > # Original function first of this name > def recreational_fun(args): > ... > > # New function, second of this name > recreational _fun = timing_is_everything(recreational _fun, args) Ah, now I understand! That's a decorator. The only difference is that it predates the introduction of *decorator syntax* using the @ symbol. The *decorator design pattern* is an idiom which uses a wrapper function to add functionality to an existing function. Given a language where functions are first-class values, you can pass a function to another function as argument, and return yet a third newly constructed function. So this gives us the decorate pattern. In pseudo-code: # Decorator pattern def decorate(input): output = wrap input with extra functionality return output new_function = decorate(function) But if you don't need to keep both the old and new versions of the function, you can just write this: function = decorate(function) https://en.wikipedia.org/wiki/Decorator_pattern In Java, it is more common to apply the decorator pattern to instances rather than functions or classes, but the principle is the same. Even if Java makes something conceptually simple incredibly complex :-) *Technically* speaking, such a design could have been used in Python going all the way back to Python 1.4 or older, since functions have always been first-class values. But it only got really useful from about Python 2.2, with the introduction of closures to the language, and the classmethod and staticmethod decorators. But initially, we didn't have dedicated syntax for applying a decorator, and had to write: def function(): ... function = decorate(function) which repeats the function name three times. It wasn't until 2.4 that the @ syntax was approved: @decorate def function(): ... which is literally syntactic sugar for the original version. See PEP 318 for the background: https://www.python.org/dev/peps/pep-0318/ -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regarding Python api script
On Fri, Dec 07, 2018 at 05:59:22PM +, Alan Gauld via Tutor wrote: [...] > > ... In languages without garbage collection, reusing > > the same name, like "index" repeatedly might save some > > small amount of space. > > Garbage collection only helps if the variable loses > its assignment. If the variable retains its value it > won't be garbage collected. So the same space saving applies. > > However it is very rare that Python is used in the kind > of scenarios where that kind of space saving is > significant (usually embedded controllers with > tiny memory spaces). People routinely use Python with seriously large amounts of data, hundreds of megabytes or even gigabytes, and freely make copies of it, secure in the knowledge that they don't have to do much to manage memory because the garbage collector will do so for them. Consider a simple expression like: text = '"' + (text.strip().lower().replace(',', ' ')) + '"' That makes five slightly-modified copies of the original text. And that's just from a single short expression. Memory is cheap, but not so cheap that we can afford to keep around hundreds of redundant copies of large objects. Generally speaking, we can afford to be profligate with copies of objects because we know they won't "leak" and survive for very long: at worst, they will be reclaimed when the current function returns. The only times we need worry about lifetimes of objects are when we are working in the global scope, or when adding attributes to long-lived objects. Regardless of whether we write the above as a single expression, or split it over many lines: text = text.strip() text = text.lower() text = text.replace(',', ' ') text = '"' + text text = text + '"' the result will be the same. And I don't think the above would be more understandable if we invented separate names for each of the intermediate results. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unzip and connect to Oracle database
On 07/12/2018 14:00, Asad wrote: > Hi All , > > I would like to unzip a file using python and then execute the sql > scripts in the file on Oracle database . Are you absolutely sure? That's a very dangerous thing to do from a security point of view. Potentially similar to using exec() on user input... You will need to ensure that the files inside the zip file are very secure and from trusted sources. > >>> from zipfile import ZipFile file_name = 'file.zip' z = ZipFile(file_name) print(z.namelist()) > [] What did you expect to see? What does zip file contain? Recall that zip files are not simply compressed files they are also archives of files. z.extractall() > Doesnot unzip the zip file Are you sure you are looking in the right folder? Can you use os.listdir() to print the file list and verify that file.zip is where you expect and after extracting that the extracted files don't exist? Also recall that zip files may have absolute paths so the extracted files may be in some completely different location. > Also share some code or documentation to connect to Oracle DB and execute > sql scripts. Lets deal with that separately. There are several tutorials on YouTube and assorted web pages showing how to do that. If you don't understand them come back with specific questions. Lets focus on ZipFile for now! -- 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] Regarding Python api script
On 07/12/2018 02:13, Avi Gross wrote: > Alan says he had a challenge evaluating code (below) because the same > variable names were reused It wasn't the reuse per se but the generic nature of the names combined with reuse. Reusing names like i,j,k for simple integer indices etc is not a problem. But when values of domain significance are given the same generic name (like item or file) then it gets confusing. Its very easy to miss a significant reassignment of the variable to a new value. > ... In languages without garbage collection, reusing > the same name, like "index" repeatedly might save some > small amount of space. Garbage collection only helps if the variable loses its assignment. If the variable retains its value it won't be garbage collected. So the same space saving applies. However it is very rare that Python is used in the kind of scenarios where that kind of space saving is significant (usually embedded controllers with tiny memory spaces). > if that is important, you can use "del var" to remove var. Exactly so. > But so much code I see in python does not only > reuse the same variable > names but in a confusing way. Then its probably badly written Python! > file = "some name" > file = open(file, "r") > file = some_wrapper(file) This is a good example of poor reuse since the objects are all totally different and only the second it truly a file. > mylist = [ ... ] > mylist = sorted(mylist) That's not really reusing it, it's just a convoluted way of modifying the value. (IN a purely functional view of the world it is reuse but only a functional purist would argue it that way I suspect) > for index in range(...): > stuff THis usage is possibly ok if range is used to generate indexes into some collection. Although probably iterating over the colection itself would be preferred. > for index in open(...) > more stuff But this is bad since index is not an index its a line, and possibly even more specifically a domain object representation which should get a domain related name. > for index, index2 in enumerate(...) > yet more stuff Again the index is OK here but index2 is very wrong since it holds the value not an index. But these issues are not Python issues they are general naming issues in all languages. It is good practice, regardless of language, to use problem domain names for variables not generic names or type related names. And also to not reuse variables if the name is not applicable to the new value. > I have often seen something like this done with methods, such as to emulate > decorator functionality It's quite common to decorate a method and assign the result to the same name but that is akin to the sorted list example above. The name is being augmented by the decorator not used for a different purpose. It is specifically being assigned to the same name to hide the original non augmented function object. > So is there a guide on when reuse is good > and when it just obfuscates? It's the same in Python as in any language. Use a name that makes sense in the domain. If that name is applicable in multiple places its ok to use the same name. If a name is non domain specific then it should only be used in very localised contexts - such as a loop body - or where it will be replaced by a domain specific variable immediately - such as a tuple from a database being used to instantiate a domain related object. -- 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] Regarding Python api script
[[ Real SUBJECT: emulating decorators ]] Steven, I am not suggesting that a particular way of reusing a name is a good idea. I am asking if some ways are encouraged and others are discouraged in the python community. Just to clarify one point you asked about: # > I have often seen something like this done with methods, such as to emulate # > decorator functionality where a method is created in an object with a name # > and the very next method created has the same name with the same function # > name as an argument to replace it. # # I don't understand this. Can you show an example? Even if made up? The answer is that I saw that mentioned in a Lutz book that I no longer have. What I wrote above should be clear if I give an example: Say you have a method in a class definition called do_this() that does whatever this is. You may also have other methods in the same or different classes with names like do_that() and your programs run fine. At a later point you decide you want to see how long it takes the method to run. So you make or borrow a function called timing_is_everything() that looks like this in pseudocode: def timing_is_everything(fun, args): save the current time call fun with args save the return value of calling fun get the current time calculate time elapsed and do whatever with it. Return saved return value from fun. The argument "fun" above can be replaced by any function such as "do_this" or "do_that" and produce the same side-effect while also running the function unharmed. Then you decide you want a function that logs when another function is called and with what arguments: Again, in PSEUDOCODE, it looks like: def logistics(fun, args): with open(logfile): write info about the current date/time, name of the function and arguments return result of calling the function with those args Now to the point. You could write each and every module you want independently. At the top of the body you could insert any similar code to begin timing it or to log it. Then you place the code needed for that function followed by anything you want done after such an action and finally return a value, if needed. But if you already have written and tested something like the two modules above, you can do something like this: class classy(object): ... # Original function first of this name def recreational_fun(args): ... # New function, second of this name recreational _fun = timing_is_everything(recreational _fun, args) # New function, third of this name recreational _fun = logistics(recreational _fun, args) AGAIN, the above is for illustration only and I am well aware of the proper ways to do the pseudocode such as using notations like *args, **kwargs and so on. We are talking about an IDEA that perhaps predated the introduction of decorators way back when. The point is that you could have used three names here for the various functions but realistically, only the last was needed so why carry around extra methods. With "decorators" as a python feature allowing you to wrap multiple functions around the one being defined, assuming you built the above helper functions properly so they can be used as decorators, the above might look like this: class classified(object): ... # Original function first and only of this name # and highly decorated bottom to top @ logistics @ timing_is_everything def recreational_fun(args): ... Both ways of doing this, again if PROPERLY done, should have the same result, albeit there may be subtle changes such as hidden within the dunder properties. There will be a single function name visible to any user. When called, it will log an entry, note what time it is, do whatever the re-created method is supposed to do, then calculate how much time it took and return some result. And, yes, I know there is overhead added as you end up with layered function calls and so on. This was an answer to a request. I hope I answered it. I do not wish to now continue on this topic. The topic was not decorators. But the decorator functionality used above likely does not confuse anybody as to what the name of the method is. That variable name is only seen once as compared to my first example when it refers to three completely different functions and keeps giving up a connection to what effectively becomes an anonymous function that can only be remembered and called within the enclosing function. I tend to agree with the other points Steve made. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] unzip and connect to Oracle database
Hi All , I would like to unzip a file using python and then execute the sql scripts in the file on Oracle database . >>> from zipfile import ZipFile >>> file_name = 'file.zip' >>> z = ZipFile(file_name) >>> print(z.namelist()) [] >>> z = ZipFile('file.zip') >>> print z.namelist() [] >>> print z >>> z.extractall() Doesnot unzip the zip file I unable to unzip the file what I am doing wrong ? Also share some code or documentation to connect to Oracle DB and execute sql scripts. Thanks, -- Asad Hasan +91 9582111698 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble in dealing with special characters.
On 12/7/18 3:20 AM, Steven D'Aprano wrote: >> How to know whether in a given string(sentence) is there any that is not >> ASCII character and how to replace? > > That's usually the wrong solution. That's like saying, "My program can't > add numbers greater than 100. How do I tell if a number is greater than > 100, and turn it into a number smaller than 100?" yes, it's usually the wrong solution, but in the case of quote marks it is *possible* is is the wanted solution: certain text editing products (cough cough Microsoft Word) are really prone to putting in typographic quote marks. Everyone knows not to use Word for editing your code, but that doesn't mean some stuff doesn't make it into a data set we forced to process, if someone exports some text from an editor, etc. There are more quoting styles in the world than the English style, e.g. this one is used in many languages: „quoted text“ (I don't know if that will survive the email system, but starts with a descended double-quote mark). It's completely up to what the application needs; it *might* as I say be appropriate to normalize text so that only a single double-quote and only a single single-quote (or apostrophe) style is used. Or it might not. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regarding Python api script
On Thu, Dec 06, 2018 at 09:13:01PM -0500, Avi Gross wrote: > But so much code I see in python does not only reuse the same variable names > but in a confusing way. > > file = "some name" > file = open(file, "r") > file = some_wrapper(file) I agree this is confusing: you have the same name, "file", used to mean a file name, an open file object, and whatever some_wrapper returns. This is not the clearest code I've ever seen. > mylist = [ ... ] > mylist = sorted(mylist) There's nothing wrong with that, except that it ought to be written: mylist = [ ... ] mylist.sort() to sort in place instead of making a sorted copy. > for index in range(...): > stuff > > for index in open(...) > more stuff Using "index" to iterate over the lines in a file is just a crappy choice of name. [...] > I have often seen something like this done with methods, such as to emulate > decorator functionality where a method is created in an object with a name > and the very next method created has the same name with the same function > name as an argument to replace it. I don't understand this. Can you show an example? Even if made up? > So is there a guide on when reuse is good and when it just obfuscates? What > is good practice? If you pick *meaningful* names, it will be pretty obvious when to reuse the same name: is the name still meaningful? Then you MAY reuse. If the name is not meaningful, then you MUST NOT reuse it, unless you are deliberately writing obfuscated code. If you have a name "column" which you use for a columns, then you shouldn't reuse it for rows, or lines of text read from a file, or the length of a list, or the date. But you can reuse it for another column, provided there's no confusion over which column is which. The same applies to any other language. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] playing sound files in python
On Fri, Dec 07, 2018 at 02:17:55AM +, nathan tech wrote: > Hello all! > > My name is nate, and I am relatively new to this list, relatively being > just signed up. > > I have a question that you would think would be obvious, but alas I have > struggled to figure out. > > How do I play sound files in python. Alas, its not obvious nor easy. https://duckduckgo.com/?q=python+play+sound+files > More specificly, I want to play ogg files especially, with wav and mp3 > also being a high priority. I believe that wxPython supports .wav files, but not .ogg or .mp3. Playing sound in Python is a definite weakness unless you have a third-party library like PyGame that supports it in a platform- independent way. You could try: print('\a') but this requires that you are running in a terminal that supports the BEL character, that the system beep has not been turned off, and even if it works, its only a short beep. If your OS (Windows?) has a sound file player, you could try calling out to it with os.system. Under Linux, I might try something like this: os.system('mpg123 -q why_is_the_rum_gone-remix.mp3') but that pauses until paying is over, and requires the mpg123 player. There are other players, like ogg123. What you get on Windows, I don't know. Pygame is starting to sound more attractive :-) -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble in dealing with special characters.
On Fri, Dec 07, 2018 at 02:06:16PM +0530, Sunil Tech wrote: > Hi Alan, > > I am using Python 2.7.8 That is important information. Python 2 unfortunately predates Unicode, and when it was added some bad decisions were made. For example, we can write this in Python 2: >>> txt = "abcπ" but it is a lie, because what we get isn't the string we typed, but the interpreters *bad guess* that we actually meant this: >>> txt 'abc\xcf\x80' Depending on your operating system, sometimes you can work with these not-really-text strings for a long time, but when it fails, it fails HARD with confusing errors. Just as you have here: > >>> tx = "MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" > >>> tx.decode() > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 19: > ordinal not in range(128) Here, Python tried to guess an encoding, and picked some platform-specific encoding like Latin-1 or CP-1252 or something even more exotic. That is the wrong thing to do. But if you can guess which encoding it uses, you can make it work: tx.decode("Latin1") tx.decode("CP-1252") But a better fix is to use actual text, by putting a "u" prefix outside the quote marks: txt = u"MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" If you need to write this to a file, you can do this: file.write(txt.encode('utf-8')) To read it back again: # from a file using UTF-8 txt = file.read().decode('utf-8') (If you get a decoding error, it means your text file wasn't actually UTF-8. Ask the supplier what it really is.) > How to know whether in a given string(sentence) is there any that is not > ASCII character and how to replace? That's usually the wrong solution. That's like saying, "My program can't add numbers greater than 100. How do I tell if a number is greater than 100, and turn it into a number smaller than 100?" You can do this: mystring = "something" if any(ord(c) > 127 for c in mystring): print "Contains non-ASCII" But what you do then is hard to decide. Delete non-ASCII characters? Replace them with what? If you are desperate, you can do this: bytestring = "something" text = bytestring.decode('ascii', errors='replace') bytestring = text.encode('ascii', errors='replace') but that will replace any non-ascii character with a question mark "?" which might not be what you want. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble in dealing with special characters.
On Fri, Dec 07, 2018 at 01:28:18PM +0530, Sunil Tech wrote: > Hi Tutor, > > I have a trouble with dealing with special characters in Python There are no special characters in Python. There are only Unicode characters. All characters are Unicode, including those which are also ASCII. Start here: https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ https://blog.codinghorror.com/there-aint-no-such-thing-as-plain-text/ https://www.youtube.com/watch?v=sgHbC6udIqc https://nedbatchelder.com/text/unipain.html https://docs.python.org/3/howto/unicode.html https://docs.python.org/2/howto/unicode.html Its less than a month away from 2019. It is sad and shameful to be forced to use only ASCII, and nearly always unnecessary. Writing code that only supports the 128 ASCII characters is like writing a calculator that only supports numbers from 1 to 10. But if you really must do so, keep reading. > Below is > the sentence with a special character(apostrophe) "MOUNTAIN VIEW WOMEN’S > HEALTH CLINIC" with actually should be "MOUNTAIN VIEW WOMEN'S HEALTH CLINIC > ". Actually, no, it should be precisely what it is: "WOMEN’S" is correct, since that is an apostrophe. Changing the ’ to an inch-mark ' is not correct. But if you absolutely MUST change it: mystring = "MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" mystring = mystring.replace("’", "'") will do it in Python 3. In Python 2 you have to write this instead: # Python 2 only mystring = u"MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" mystring = mystring.replace(u"’", u"'") to ensure Python uses Unicode strings. What version of Python are you using, and what are you doing that gives you trouble? It is very unlikely that the only way to solve the problem is to throw away the precise meaning of the text you are dealing with by reducing it to ASCII. In Python 3, you can also do this: mystring = ascii(mystring) but the result will probably not be what you want. > Please help, how to identify these kinds of special characters and replace > them with appropriate ASCII? For 99.99% of the characters, there is NO appropriate ASCII. What ASCII character do you expect for these? § π Й খ ₪ ∀ ▶ 丕 ☃ ☺️ ASCII, even when it was invented in 1963, wasn't sufficient even for American English (no cent sign, no proper quotes, missing punctuation marks) let alone British English or international text. Unless you are stuck communicating with an ancient program written in the 1970s or 80s that cannot be upgraded, there are few good reasons to cripple your program by only supporting ASCII text. But if you really need to, this might help: http://code.activestate.com/recipes/251871-latin1-to-ascii-the-unicode-hammer/ http://code.activestate.com/recipes/578243-repair-common-unicode-mistakes-after-theyve-been-m/ -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble in dealing with special characters.
On 07/12/2018 08:36, Sunil Tech wrote: > I am using Python 2.7.8 tx = "MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" tx.decode() > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 19: > ordinal not in range(128) > > How to know whether in a given string(sentence) is there any that is not > ASCII character and how to replace? How to detect is to do wat you just did but wrap a try/except around it: try: tx.decode() except UnicodeError: print " There were non ASCII characters in the data" Now, how you replace the characters is up to you. The location of the offending character is given in the error. (Although there may be more, once you deal with that one!) What would you like to replace it with from the ASCII subset? But are you really sure you want to replace it with an ASCII character? Most display devices these days can cope with at least UTF-8 version of Unicode. Maybe you really want to change your default character set so it can handle those extra characters?? -- 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] Trouble in dealing with special characters.
Hi Alan, I am using Python 2.7.8 >>> tx = "MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" >>> tx.decode() Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 19: ordinal not in range(128) How to know whether in a given string(sentence) is there any that is not ASCII character and how to replace? On Fri, Dec 7, 2018 at 2:01 PM Alan Gauld via Tutor wrote: > On 07/12/2018 07:58, Sunil Tech wrote: > > > I have a trouble with dealing with special characters in Python Below is > > the sentence with a special character(apostrophe) "MOUNTAIN VIEW WOMEN’S > > HEALTH CLINIC" with actually should be "MOUNTAIN VIEW WOMEN'S HEALTH > CLINIC > > ". > > How do you define "special characters"? > There is nothing special about the apostraphe. It is just as > valid a character as all the other characters. > What makes it special to you? > > > Please help, how to identify these kinds of special characters and > replace > > them with appropriate ASCII? > > What is appropriate ASCII? > ASCII only has 127 characters. > Unicode has thousands of characters. > How do you want to map a unicode character into ASCII? > There are lots of options but we can't tell what you > think is appropriate. > > Finally, character handling changed between Python 2 and 3 > (where unicode became the default), so the solution will > likely depend on the Python version you are using. > Please tell us which. > > > -- > 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] Trouble in dealing with special characters.
On 07/12/2018 07:58, Sunil Tech wrote: > I have a trouble with dealing with special characters in Python Below is > the sentence with a special character(apostrophe) "MOUNTAIN VIEW WOMEN’S > HEALTH CLINIC" with actually should be "MOUNTAIN VIEW WOMEN'S HEALTH CLINIC > ". How do you define "special characters"? There is nothing special about the apostraphe. It is just as valid a character as all the other characters. What makes it special to you? > Please help, how to identify these kinds of special characters and replace > them with appropriate ASCII? What is appropriate ASCII? ASCII only has 127 characters. Unicode has thousands of characters. How do you want to map a unicode character into ASCII? There are lots of options but we can't tell what you think is appropriate. Finally, character handling changed between Python 2 and 3 (where unicode became the default), so the solution will likely depend on the Python version you are using. Please tell us which. -- 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] playing sound files in python
Hello all! My name is nate, and I am relatively new to this list, relatively being just signed up. I have a question that you would think would be obvious, but alas I have struggled to figure out. How do I play sound files in python. More specificly, I want to play ogg files especially, with wav and mp3 also being a high priority. I am using windows, and building for windows machines. I am currently building my gui using wx python, and using something like pygame just seems overly blotey to me. Thanks for any help you can offer. Nate ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regarding Python api script
I left the "subject" above to be the same, as requested. The actual subject might have been "Pythonic variable name use" Alan says he had a challenge evaluating code (below) because the same variable names were reused and it made me wonder if the python community has general ideas about name re-use. I am keeping this short. In languages without garbage collection, reusing the same name, like "index" repeatedly might save some small amount of space. With garbage collection, reuse may be one way to hint it can be used again but if that is important, you can use "del var" to remove var. For short programs, or in a context like a "normal" function that goes away when completed, it probably is best to make the code very readable. PLEASE do not mention the many exceptions as I do not intend much of what I say to be taken as completely accurate. But so much code I see in python does not only reuse the same variable names but in a confusing way. file = "some name" file = open(file, "r") file = some_wrapper(file) mylist = [ ... ] mylist = sorted(mylist) for index in range(...): stuff for index in open(...) more stuff for index, index2 in enumerate(...) yet more stuff I have often seen something like this done with methods, such as to emulate decorator functionality where a method is created in an object with a name and the very next method created has the same name with the same function name as an argument to replace it. So is there a guide on when reuse is good and when it just obfuscates? What is good practice? -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Thursday, December 6, 2018 7:26 PM To: tutor@python.org Subject: Re: [Tutor] Regarding Python api script On 06/12/2018 14:17, Ravi Kumar wrote: > 1)The for loops that have written I am able to access all the > networks,able to loop through all access points(Devices) in the > network,able to loop through and get all clients in each access points > but when it comea to client log events I am able to loop through and > get only the last row of accesspoints list and get those particular > and clients and its log events I don't know the cause, but did take a quick look at the code. One thing that makes it very difficult to follow is that you use the same variable names over and over making it hard to keep track of what any given 'item' or 'json_string' or 'r' actually holds at any given time. Buy using a different name for each json_string reflecting the expected data - such as json_networks or json_clients - it would be very much easier to follow the flow of the code. > I assume I am going wrong in the last for loop code and output as > shown below Probably but I confess I couldn't spot it from a casual read through. > for client in json_string: > > hostname = client.get("dhcpHostname") > description = client.get("description") > ipaddress = client.get("ip") > macaddress = client.get("mac") > > usage = client.get("usage") > sentbytes = usage.get("sent") > recvbytes = usage.get("recv") > > print ('{0:20} {1:20} {2:20}{3:30} {4:20} > {5:20}'.format(hostname, description, ipaddress, macaddress,sentbytes, > recvbytes)) > > > > for item in serialnumlist: > print ("\nQuerying Meraki for clientlogevents on Devices: (" + > item.get("mac") + ")") > > for item in json_string: > config_url = "/networks/"+"/N_63***1050/"+"/clients/"+ > item.get("mac") > + "/events?perPage=1000" > r = requests.get(base_url + config_url, headers=headers) > > print ('{} '.format(r.content)) > **> The 'item's in the last loop appear to be the same as the 'client's in the previous loop? Since 'json_string' never changes... Also the setialnumlist only prints the result it never stores anything. Then the last loop uses item.get(mac) which will not be the one printed earlier since item is now read from json_string(back tome earlier confusion over names!) I suspect this lack of continuity may be the root of your problem but the name collisions are twisting my brain and its late at night... -- 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
[Tutor] Trouble in dealing with special characters.
Hi Tutor, I have a trouble with dealing with special characters in Python Below is the sentence with a special character(apostrophe) "MOUNTAIN VIEW WOMEN’S HEALTH CLINIC" with actually should be "MOUNTAIN VIEW WOMEN'S HEALTH CLINIC ". Please help, how to identify these kinds of special characters and replace them with appropriate ASCII? Thanks, Sunil. G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor