Re: [Tutor] which of these is more efficient?
On 19/08/2019 00:55, nathan tech wrote: > Is this an efficient method compared to 1? Efficient in what sense? The amount of code to write? The amount of computing resources used? The speed - and does that matter in an interactive game like this? -- 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] python question
On 18/08/2019 05:35, Thejal Ramesh wrote: > Hi, i have a question regarding this question. I'm not quite sure what the > question is asking. > Part A: Popular (1.5 Marks) > Write a function popular(graph list, n) that returns a list of people who > have at least n friends. Each person is identified by the number of their > vertex. > Examples clayton_list = [ [1,2,3], [0,3], [0,4], [0,1], [2] ] > The example graph clayton list is provided for illustrative purpose. Your > implemented function must be able to handle arbitrary graphs in list form > . a) Calling popular(clayton list,2) returns [0,1,2,3]. > b) Calling popular(clayton list,3) returns [0] > . c) Calling popular(clayton list,0) returns [0,1,2,3,4]. The question is asking you to write a function that returns the indexes of the elements in the input list of length equal to or greater than the input value. Looking at the sample data, clayton_list a) returns all but the last item because only the last item has less than 2 members b) returns only the first item because only the first item contains 3 or more members c) returns all items because all have zero or more members. What the question does not specify is what the function should return if there are no members found. I would assume an empty list... To do this you will need some kind of loop and a test and build a list. If you know about list comprehensions that might be one option. (If you don't, use a normal for loop) 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
Re: [Tutor] What is Tuple in the typing module?
On 17/08/2019 00:46, C W wrote: The formatting seems messed up I'll try to straighten it out. I hope I get it right! Caveat: I'm no expert in the typing module, I've read the docs but never found any use for it. > What exactly is Tuple in the typing module? What does it do? It specifies a tuple type. I assume you understand what a tuple is in regular Python code? > This is the definition from its website. > https://docs.python.org/3/library/typing.html > "A type alias is defined by assigning the type to the alias" > > I have no idea what that means. It just means that an alias is a label that you can use to signify a type. And you can effectively create new "types" by combining simpler types > Here's the example from the documentation: > > from typing import Dict, Tuple, Sequence ConnectionOptions = Dict[str, str] Address = Tuple[str,int]Server = Tuple[Address, ConnectionOptions] So this creates 3 new type aliases: ConnectionOptions, Address, Server which are defined in terms of basic Python types,. So Address is a tuple of a string and integer. > def broadcast_message(message: str, servers: Sequence[Server]) -> None: Ad this defines a function where the parameter types are specified in terms of the new type names that we just created. > # The static type checker will treat the previous type signature as# > being exactly equivalent to this one. > def broadcast_message( > message: str, > servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) -> None: And this is the same function but with the parameters expanded into their traditional basic types. Without all the type hinting gubbins it would simply be: def broadcast_message(message, servers): So all the detritus above is doing is attempting to make it clearer what the two parameters actually are in data terms. The first is a string, the second is a complex structure of nested tuples and a dict (which probably should all be wrapped in a class!). > I think this even more confusing. Can someone explain this in simple words? > I don't have a intense computer science background. Type hints are a way to specify the types of function parameters. They are an attempt to make complex type structures more readable rather than fixing the data to be simpler (which, to be fair, may not always be possible/easy). They are also an attempt to bring some of the "benefits" of static typing into Python but with very limited success. But they are only hints, you can happily survive without them. -- 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] Search for Text in File
On 15/08/2019 15:10, Stephen P. Molnar wrote: > I need to find a phrase in i text file in order to read a unique text > string into a python3 application. > > I have become stuck and Google has not been of any use. I'm sorry but there is so much wrong here it is difficult to know where to begin. > #!/usr/bin/env python3 > # -*- coding: utf-8 -*- > # > # Extract.Data.py > > import re You don't need re since you are not using any regular expressions. > name = input("Enter Molecule Name: ") > > name_in = name+'_apo-1acl.dlg' This creates a string called name_in. > re.search("RMSD TABLE",name_in()) And here you search for the string(not regular expression) "RMSD TABLE" in the return value from name_in() But name_in is a string, you cannot call a string. >>> "foo"() Traceback (most recent call last): File "", line 1, in "foo"() TypeError: 'str' object is not callable > and here is the portion of the text file (Acetyl_apo-1acl.dlg) > containing the information: > > Number of multi-member conformational clusters found = 4, out of 20 runs. > > RMSD TABLE > __ And this is the only line containing your string so, even if your code worked, this is the only line you would locate. It doesn't look very useful... > Everything that I have tried returns 'TypeError: 'str' object is not > callable'. See my comments above. -- 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] HELP PLEASE
On 13/08/2019 12:09, Sithembewena L. Dube wrote: > Hi Marissa, > > I really think that you could consider doing an introductory Python > tutorial and then venture back into solving this problem. >>> This is the output of my updated code: >>> Traceback (most recent call last): >>> File "/Applications/Python 3.7/exercises .py", line 37, in >>>main() Based on the file name I suspect she is already doing some kind of tutorial. However, you are right about needing to review some of the basic concepts. As a plug I'll just mention my own tutorial linked in my .sig below :-) -- 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] Fwd: Re: HELP PLEASE
On 12/08/2019 19:35, Alan Gauld via Tutor wrote: > To save some typing convert the?? int conversion loop into a function: > > > def?? to_ints(strings): > ?? num_copy = [] > ?? for num in nums: > num_copy.append(float(num)) > > ?? return num_copy # No idea where all the ? came from, however I made a bad error in that code... It should read: def to_ints(strings): num_copy = [] for num in strings: # strings not nums! num_copy.append(float(num)) return num_copy Apologies. -- 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] Fwd: Re: HELP PLEASE
Forwarding to tutorblist for info... Forwarded Message Subject:Re: [Tutor] HELP PLEASE Date: Mon, 12 Aug 2019 19:34:48 +0100 From: Alan Gauld Reply-To: alan.ga...@yahoo.co.uk To: Marissa Russo On 12/08/2019 19:17, Marissa Russo wrote: > I fixed some things up??? OK, But please use ReplyAll when responding to list mails otherwise it just comes to me (or whoever posted) instead of the whole list. (And I might be busy. at the time..) > import math > > def get_numbers(): > print("This program will compute the mean and standard deviation") > file1 = input("Please enter the first filename: ") > file2 = input("Please enter the second filename: ") > x = open(file1, "r") > y = open(file2, "r") > nums = x.readlines() > nums2 = y.readlines() > > num_copy = [] > for num in nums: > num_copy.append(float(num)) > nums = num_copy > > return nums You are only returning one list of numbers. you need to convert nums2 as well and then return both. To save some typing convert the?? int conversion loop into a function: def?? to_ints(strings): ?? num_copy = [] ?? for num in nums: num_copy.append(float(num)) ?? return num_copy then call that on both list of strings. return to_ints(nums), to_ints(nums2) > def mean(): > _sum = 0 > return(sum(nums)/len(nums)) Notice you don't pass any values to mean so you are relying on the function seeing the values of nums outside the function somewhere. It is beter where you passs in the values as you did originally: def mean(numbers): > def main(): > data = get_numbers() > m = mean(data[0]) > m2 = mean2(data[1]) You call mean2() but don't have a mean2() function anymore. You only need mean() so call it both times but with different input data. > print("The mean of the first file is: ", m) > print("The mean of the second file is: ", m2) > main() > > This is now the output error: > Traceback (most recent call last): > File "/Applications/Python 3.7/exercises .py", line 34, in > main() > File "/Applications/Python 3.7/exercises .py", line 30, in main > m = mean(data[0]) > TypeError: mean() takes 0 positional arguments but 1 was given See the comment above. You removed the input parameter to the mean() function. But then you call it as if it was still there. You must be consistent between your function definitions and function calls.. You need to start thinking like the interpreter as you walk through the code. Think about what it knows at any given point and how the various functions communicate?? - usually by storing intermediate results in variables. (Many find it convenient to jot down the variables and their values as they walk through the code, updating the values as they go). So check what those variables contain - using print statements to debug it if necessary. -- 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] HELP PLEASE
On 12/08/2019 17:54, Marissa Russo wrote: > def mean(nums): > for num in nums: > _sum += num > return _sum / len(nums) > > def mean2(nums2): > for num in nums2: > _sum += nums2 > return _sum / len(nums2) > > def main(): > data = get_numbers() > > print("The mean of the first file is: ", mean) > print("The mean of the second file is: ", mean2) Notice that in the print statement you use the names of the functions. Python therefore evaluates those names and discovers that they are functions, so that's what it tells you. To get the values you need to call the functions, which you do by adding parens to the name: print("The mean of the first file is: ", mean(data[0]) ) print("The mean of the second file is: ", mean2(data[1]) ) That will then execute the functions Which leads us to the next problem. In both functions you have a line like: _sum += num But that expands to _sum = _sum + num Which means you are trying to get a value from _sum before assigning any value. You need to initialize _sum before using it like that. _sum = 0 But better still would be to use the builtin sum method: sum(nums) So your mean function turns into: def mean(nums): return( sum(nums)/len(nums)) But that leads to the next problem which is that your data is still in string format, which is what you read from the file with getlines(). You can convert it to integers using a list comprehension inside your get_numbers function: nums = [int(s) for s in nums] which assumes the file is a list of numbers each on one line? If you are not familiar with the list comprehension shortcut you can do it longhand like this: num_copy = [] for num in nums: num_copy.append(int(num)) nums = num_copy Incidentally, the two mean functions look identical to me. What do you think is different other than the names? Finally, you could just use the mean() function defined in the statistics library of the standard library. import statistics as stats ... print("The mean of the first file is: ", stats.mean(data[0]) ) ... That should be enough to be getting on with. Once you get those things done you could post again and we might suggest some ways to tidy the code up a little. -- 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] Object creation query
On 10/08/2019 09:48, mhysnm1...@gmail.com wrote: > HI all, > > I have multiple different accounts that I am creating reports for. Thus I > cannot have a centralised transaction table that contains all the records. > These different accounts are for different organisations or for other > members in my family. That shouldn't make any difference. It would be impractical to have different tables for each customer or organisation in something like a large utility company or say., Amazon where there will be literally millions of account holders. You would need millions of tables which would be a nightmare to manage. The standard model for these kinds of scenarios is a single account table with a reference to the customer(or user) table and a transaction table with a reference to the account. That supports the concept of multiple customers, each with (potentially)multiple accounts and each account having multiple transactions. > I hope this explains what I am trying to do. I am trying to make things as > dynamic as possible without myself recoding the model. You can make it as dynamic as you like using a single table and you certainly shouldn't need to recode the model, although I'm not familiar with SQLAlchemy (which is the ORM wrapper you are using I think?) But you might need to tweak you object definition to make it single table based. > Also was wondering if it was possible as well. Anything is possible, it just takes more effort. In this case quite a lot more effort. -- 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] Object creation query
On 09/08/2019 09:54, mhysnm1...@gmail.com wrote: > updates and insertions. I have multiple tables with the same structure with > different names. Umm, why? Assuming by structure you mean they have the same fields then that's usually a bad design decision. Why not have one table with an attribute "name" or "Type" or similar? That should greatly simplify your code. If that's not what you mean by the same structure you need to give us a little more detail. Not necessarily the full descriptions but some simplified examples maybe? -- 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] Name for this type of class?
On 04/08/2019 09:15, Alan Gauld via Tutor wrote: >>> Classes should never be named for their data but for their function. > I was not suggesting that a class name should be a verb, I think my biggest mistake here was the use of the word "function" which, in a programming community, has a very specific meaning whereas I was thinking of the natural English meaning. It might have been better if I'd said classes should be named for their *purpose* or their *role*. And, in fact, that's true of any variable in any language, it should express intent not internal structure. Apologies for any resulting confusion. -- 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] Name for this type of class?
On 04/08/2019 04:44, David L Neil wrote: >> Classes should never be named for their data but for their function. >> What does this class do? What operations does it support. > > > This statement caused me to double-take. Do I misunderstand? I suspect so, thats why I wrote my follow up. I was not suggesting that a class name should be a verb, not at all. Objects are things so class names should be nouns. Method names should usually be verbs although that's not always the case. > They are almost exclusively nouns, eg Customer, Person, Organisation; > rather than verbs/functions, eg Selling, Addressing, Billing. Correct and as it should be. But what those nouns do is tell you about the high level abstraction that the class implements. They do not tell you explicitly about the data inside the class. A Person is not a "NameManager" for example. > [I'm not an OOP-native, and embraced OOP concepts as extensions to > practices learned whilst programming with languages such as COBOL!] Relatively few people are OOP natives, nearly everyone comes at it with a previous experiece of procedural programming. That's down to how we teach programming. > Part of my introduction to OOP included the word "entity". A class was > to represent an entity. An entity would be described (have attributes), > just like a data-record; and it would have associated actions (methods > or procedures) which acted on the entity's attributes. An entity was > described as a 'thing' - no mention of an entity being an action, even > though 'things' do 'stuff'. It is not an action, but it has actions. And the only way to interact with that entity is through its actions. Therefore the entity is defined to the outside world by its actions. One highly unfortunate side effect of using the term entity when talking to traditional programmers is that they associate it with entity relationship diagrams where entities are effectively data tables. It sub-consciously causes people to start thinking of objects as collections of data rather than as collections of operations. > For this discussion then, a Wikipedia definition* 'will do'. What is > included in OOP? [sometimes have 'translated' into Python terminology] > > - objects > - data attributes > - methods > - inheritance > - instances and dynamic dispatch > - encapsulation [dotted notation] > - composition [will come back to this...] > - inheritance/delegation [ditto] > - polymorphism [sub-classing] Very little in that list has to do with OOP. Most of it is implementation details of how OOP languages work. I would narrow that list down to: objects abstraction polymorphism All the rest are implementation features. > Simple comprehensions of inheritance and composition boil down to the > phrases "is a" and "has a". The subjects and objects of such sentences > will surely be a noun, rather than a verb? Absolutely, I was not suggesting verbs as class names. My bad for not making that clearer first time around. > Employee is a Person Yes > Circle/Oval/Square/Triangle/Shape has a CentralPoint Not necessarily. Or at least, yes they do but that may or may not be significant in terms of their definition in an OOP system. What is significant is that Shape is an abstraction of all the others. So the definition of the methods of all shapes sits in the shape class and the others specialise those methods to reflect their individual behaviours. > class Person(): ... > > class Employee( Person ): ... > > Python says "everything is an object" and makes little/no distinction > between "type" and "class": True, everything is an object but not everything has a class. At least not explicitly. But everything has a type. But the distinction between types and classes is a subtlety that most programmers need not worry about. > So, what are Python's base types/objects/classes? eg int, str, list. Are > these "data or function"? Neither and both. But they are named for what we do to them. integers have an expected set of operations that we can perform. add, subtract, multiply etc. > "pathlib — Object-oriented filesystem paths". Function or data? > > Let's argue that it is a library not a class/object per-se. Fine. > However, the six (major) classes that library contains, eg Path, > PosixPath, are all nouns! Yes but they are objects that expose methods. They are things that you can actively use, not just storage boxes. > At first I thought Inspect might be different, but (it is billed as a > module of functions cf classes!) the classNMs are nouns (one is even > called "Attribute"). The functions though, are indeed verbs, eg getargs(). Ad that is how it should be. > Whither "Classes should never be named for their data but for their > function."? It's a very old OOP meme going back to the days of Smalltalk, Lisp, Object Pascal and Objective C (ie the late 80's). It does not mean the class names are verbs rather that the names reflect what you will use the
Re: [Tutor] Name for this type of class?
On 03/08/2019 17:56, Malcolm Greene wrote: >... interesting thoughts including Alan's "classes should never be named > for their data but for their function" feedback. I'm going to have to > noodle on that one. Let me expand slightly. Go back to the fundamentals of OOP. OOP is about programs composed of objects that communicate by sending each other messages. Those messages result in the objects executing a method. Objects, therefore are defined by the set of messages that can be sent to them. By their behaviour in other words. The data they contain should only be there to support the behaviour. So an object should expose a set of methods that determine what it can do. It therefore stands to reason that the class (the template for the objects) should have a name that reflects the nature of the messages that can be sent. What kind of thing offers those kinds of capabilities? Once you can describe the operations that the object can perform you can usually find a name that describes the kind of object. One of the anti-patterns of OOP is having classes that expose methods that are unrelated to each other. Then we just have a bunch of data and some functions collected together but no coherent concept. It could be two(or more) genuine classes accidentally merged into one. Or it could be a plain module masquerading as a class. Another clue is where you only ever need a single instance of this "class". That's often a sign that it should just be a module. (There are a very few genuine singleton use cases though, so it's not a cast iron rule.) 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
Re: [Tutor] Name for this type of class?
On 03/08/2019 00:47, Malcolm Greene wrote: > Anyways, I'm looking for help coming up for the proper name for a class that > collects the following type of telemetry data Classes should never be named for their data but for their function. What does this class do? What operations does it support. Does the internal data support those operations? Or is it really several classes conflated into one for "convenience"? -- 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] just a quick logic check if someone has two seconds
On 01/08/2019 23:10, nathan tech wrote: > > import speedtest This is not a standard library module so I have no idea what it does so obviously there could be magic afoot of which I am unaware. But assuming it behaves like most Python code... > def do-test(): > test=speedtest.Speedtest() > test.download() > test.upload() > return [test.download_speed, test.upload_speed] test is garbage collected sat this point since it goes out of scope and the returned values are passed to the caller. Note that the returned values are not part of the test object. The test attributes refer to those values but it is the values themselves that are returned. > Now. If I was to put this into a GUI application, I was thinking of > having it something like this: The fact it is a GUI is completely irrelevant. There is nothing special about how a GUI calls a function. > user clicks button, > button calls function which: > > 1. Shows the screen which updates with test status. > 2, does: results=do_test() > 3. Updates the screen with the contents of results. The fact that the GUI calls this function is irrelevant. A function gets called and performs some actions. One of which is to call do_test(). It would be exactly the same if you did this: for n in range(3): result = do_test() print(result) You still call the function repeatedly. > If the user clicks the button, say, 3 times, will I have three separate > speedtest objects? You will have created 3 separate speedtest instances and each will have been garbage collected when do_test() terminated. So you will have no speedtest instances left hanging around. > or will the python garbage collector clean them up for me so I only have > one, which gets cleaned when do_test returns. You only ever have one at a time during the execution of do_test(). You have a total of 3 during your programs lifetime. (or however many times you click the button!) -- 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] Create Logging module
On 01/08/2019 10:11, Sinardy Xing wrote: > start here--- > > import logging > > ..snip... > from functools import wraps > > def logme(func_to_log): > import logging You don't need the import, it's already done in the first line. > #Check log level within understanable parameter, set to INFO if is not > permitable value > def check_log_level(logleveltocheck): This looks like an indentation error? It should be at the same level as the import statement. > if any(logleveltocheck.upper() in lf for lf in ['DEBUG', > 'INFO', 'WARNING', 'ERROR', 'CRITICAL']): > return logleveltocheck.upper() Are you sure that is what you want? It seems very complicated unless you are allowing the user to supply an abbreviated form. Otherwise if logleveltocheck.upper() in ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']: return logleveltocheck.upper() might be easier? > else > return 'INFO' > > log_file_level='INFO' #check_log_level('info') > log_console_level='INFO' #check_log_level('info') > > #root level > logger.setLevel('INFO') I'm not sure what this is supposed to be doing! > formatter = logging.Formatter('%(asctime)s :: %(name)s :: %(levelname)s > :: %(message)s') > > #Read log file from parameter > logfile='mylogfile.log' > file_handler = logging.FileHandler(logfile) > file_handler.setLevel(log_file_level) > file_handler.setFormatter(formatter) > > stream_handler = logging.StreamHandler() > stream_handler.setLevel(log_console_level) > stream_handler.setFormatter(formatter) > > logger.addHandler() > logger.addHandler(stream_handler) > > #this wraps is to make sure we are returning func_to_log instead of > wrapper > @wraps(func_to_log) > def wrapper(*args, **kwargs): > logger.info('Ran with args: {}, and kwargs: {}'.format(args, > kwargs)) > return func_to_log(*args, **kwargs) > > return wrapper -- 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] Difference between decorator and inheritance
On 31/07/2019 18:57, Gursimran Maken wrote: > Anyone could please let me know the difference between decorators and > inheritance in python. > > Both are required to add additional functionality to a method then why are > we having 2 separate things in python for doing same kind of work. Inheritance and decorators can both achieve similar results in that they provide a mechanism to alter the effect of a method, but they work very differently. In particular, decorators can be used outside of a class, on a normal function. They also work across multiple different classes whereas works within a single heirarchy to create a subtype structure (but see Mixins below). Also, inheritance is used for much more than modifying methods. You can add new methods and attributes and it also provides conceptual structure to your classes as well as being a tool for software reuse (probably its least useful, and most abused, function). Inheritance in other OOP languages is used for many other things too, especially as the mechanism that enables polymorphism, but that is less significant in Python. Where there is considerable overlap is in the use of mixin classes. Mixins are a form of inheritance which allow modification of methods in a way quite similar (from a users perspective) to decorators. The underlying mechanisms are different and decorators are more elegant, but the end result is similar. I'm not sure how much technical detail you wanted, so I'll leave it thee, if you want to dig into the inner mechanisms then ask for more details. -- 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] Which is better in principle: to store (in file) calculated data or to re-calculate it upon restarting program?
On 31/07/2019 03:02, boB Stepp wrote: > preceding scores plus the current one. If the data in the file > somehow got mangled, it would be an extraordinary coincidence for > every row to yield a correct total score if that total score was > recalculated from the corrupted data. True but the likelihood of that happening is vanishingly small. What is much more likely is that a couple of bits in the entire file will be wrong. So a 5 becomes a 7 for example. Remember that the data in the files is a character based (assuming its a text file) not numerical. The conversion to numbers happens when you read it. The conversion is more likely to detect corrupted data than any calculations you perform. > But the underlying question that I am trying to answer is how > likely/unlikely is it for a file to get corrupted nowadays? It is still quite likely. Not as much as it was 40 years ago, but still very much a possibility. Especially if the data is stored/accessed over a network link. It is still very much a real issue for anyone dealing with critical data. > worthwhile verifying the integrity of every file in a program, or, at > least, every data file accessed by a program every program run? Which > leads to your point... Anything critical should go in a database. That will be much less likely to get corrupted since most RDBMS systems include data cleansing and verification as part of their function. Also for working with large volumes of data(where corruption risk rises just because of the volumes) a database is a more effective way of storing data anyway. >> Checking data integrity is what checksums are for. > > When should this be done in normal programming practice? Any time you gave a critical piece of data in a text file. If it is important to know that the data has changed (for any reason, not just data corruption) then use a checksum. Certainly if it's publicly available or you plan on shipping it over a network a checksum is a good idea. -- 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] Which is better in principle: to store (in file) calculated data or to re-calculate it upon restarting program?
On 30/07/2019 18:20, boB Stepp wrote: > What is the likelihood of file storage corruption? I have a vague > sense that in earlier days of computing this was more likely to > happen, but nowadays? Storing and recalculating does act as a good > data integrity check of the file data. No it doesn't! You are quite likely to get a successful calculation using nonsense data and therefore invalid results. But they look valid - a number is a number... Checking data integrity is what checksums are for. -- 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] Which is better in principle: to store (in file) calculated data or to re-calculate it upon restarting program?
On 30/07/2019 17:21, boB Stepp wrote: > musings I am wondering about -- in general -- whether it is best to > store calculated data values in a file and reload these values, or > whether to recalculate such data upon each new run of a program. It depends on the use case. For example a long running server process may not care about startup delays because it only starts once (or at least very rarely) so either approach would do but saving diskspace may be helpful so calculate the values. On the other hand a data batch processor running once as part of a chain working with high data volumes probably needs to start quickly. In which case do the calculations take longer than reading the extra data? Probably, so store in a file. There are other options too such as calculating the value every time it is used - only useful if the data might change dynamically during the program execution. It all depends on how much data?, how often it is used?, how often would it be calculated? How long does the process run for? etc. -- 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] REQUIRED SUPPORT FOR CODE
On 25/07/2019 16:58, NITESH KUMAR wrote: > I want to make Autocomplete searchbox using database .Please suggest me the > code for this. Since you tell us next to noting we can only make wild suggestions. Try to find a project that does the same thing - ideally one written in Python (assuming that you are using python?) and see how it does it. Failing that provide us with a lot more detail. What kind of application is it? - Desktop? GUI? command line? Web based? Mobile app? What tools are you using - specifically any web or GUI toolkits. What OS and python versions are you using? What kind of database? How do you anticipate this would work? Give us some examples? # -- 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] Fw: CSC1010H 4th assignment
On 25/07/2019 20:04, Mahima Daya wrote: > hi there, please could you assist. With what? Your subject tells us absolutely nothing. Your message gives us no clues. The fact that you are posting to the Python tutor list suggests you might have a question about Python programming. But what that is we cannot even begin to speculate. Please ask again with a concise description of your task, what you have tried and the outcome. Include any code, include the full text of any error messages. Tell us the OS and programming language (including versions) that you are using. We will not do your homework for you. We expect to see your effort and a specific question that we can answer. -- 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] pass arg to list
On 18/07/2019 19:34, Anirudh Tamsekar wrote: > # User Args > > version = sys.argv[1] > build = sys.argv[2] > > add_file = (... > ) > Traceback (most recent call last): > > File "/Users/atamsekar/Projects/PulseSO/trunk/swrelease/samplexls.py", > line 5, in > > version = sys.argv[1] > > IndexError: list index out of range The obvious thing to do is add a print statement before the asignments print(sys.argv) and check that it contains your expected values. If that doesn't help show us your full code (including import statements) and your full execution output, including whree you call the program. -- 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] Calling a C shared library with PyObject
On 17/07/2019 18:03, Jesse Ibarra wrote: > I am using Python3.6 > > Working with C/Python embedding: > https://docs.python.org/3.6/extending/embedding.html#beyond-very-high-level-embedding-an-overview > > I have to call a shared library (.so) that I created using PyObjects. To be honest embedding is quite an advanced topic and the tutor list is generally targeted at beginners so you might find limited help here. (Although technically it does fall within our remit, it's just that it doesn't come up too often!) I'd suggest you post your queries on the main python list where there will likely be far more experts available. -- 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] Lengthy copyright notices?
On 17/07/2019 21:01, David L Neil wrote: > One line offers plenty of space to exert a claim (such can be very > simple and does not need to be lawyer-speak!) which should also refer to > the template's/package's external file or web-page. Yes, I've seen that and if the lawyer speak is very verbose its a good compromise. > Aside from possibly irritating 'the good guys', does such really 'stop' > a determined rapscallion? Nothing will stop a determined rapscallion(love that phrase! ;-) But it lets the good guys know who to contact at least if they do need to. For example, in my last book the publishers required me to get a disclaimer from the author of some open source files even though they clearly stated they could be used for any purpose. Having the copyright notice with email link made that easy. -- 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] Unit testing: Just the API or internal use only methods, too?
On 16/07/2019 23:41, boB Stepp wrote: > essentially said that I should be testing the public interface to my > classes, but not the methods only used internally by the class and not > meant to be publicly accessible. I suspect he meant that you should publish the tests for the API but not necessarily for the internal/private methods. You should definitely test all code you write, but how formally you test the private stuff is up to you. But publishing the public API tests allows clients to run them too. -- 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] raising exceptions in constructor code?
On 16/07/2019 22:56, Mats Wichmann wrote: >> thrown. This gets watered down to the mantra "Don't throw exceptions from >> within constructors." Does this carry over to Python? > > If you mean __init__, that's not a constructor, so you should set your > mind at rest :) It's more properly an "initializer", the instance has > already been constructed when it's called. FWIW The true constructor is __new__() and its quite rarely overridden by application programmers. But if you do, it's not common that you'd do anything that would merit an exception. __new__ pretty much just sets up the structure of the object ready for initialisation by __init__. Incidentally, this two stage construction/initialisation is also found in other OOP languages like Smalltalk and Objective C (and Lisp?). -- 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] Reading .csv data vs. reading an array
On 16/07/2019 09:59, Oscar Benjamin wrote: >> All true, but sed - once you get used to it! - is easier IMHO >> and usually faster than Python - it's written in C... > > I always think I'll like sed but whenever I actually try to use it I > just can't get the syntax together. Id been using awk and sed for 15 years before I discovered Python (and perl) so they are kind of second nature. If you can't settle with sed try awk, it's much simpler to use and almost as powerful but not as fast. I think awk is one of the most neglected of the *nix tools now that scripting languages like perl/python/ruby exist. But for line by line file processing it is superb. -- 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] Lengthy copyright notices?
On 15/07/2019 23:34, Mats Wichmann wrote: > Rule #1: it's all opinion in the end... ditto... > The common practice is that licence/copyright text is included as a > comment in the code, not in a docstring. I'd second that opinion. I don't like losing the copyright stuff to a separate file - too easy to get lost. But I certainly don't want it in my help() output either. A comment solves both for the downside of some initial scrolling when reading or editing the file -- 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] Reading .csv data vs. reading an array
On 15/07/2019 21:28, Mats Wichmann wrote: >> a float of s. Of course I can't edit a 4G file in any editor that I >> have installed, so I have to work with the fact that there is a bit of >> text in there that isn't quoted. Try sed, it's on most Unix like OS. It doesn't read the entire file into memory so file size is not usually an issue. I've never tried 4G but I have gone over 1GB before with no issues. If you have never used sed before its batch oriented so you need to practice your commands in advance on something like vim or ex then translate them to a file. But it sounds like it would be a worthwhile automation step in your workflow. Write once, use often... > course Python can do that too, by working line-at-a-time, explicitly by > calling readlines() or implicitly by looping over the file handle. The > latter looks something like this; > > with open("/path/to/datafile", "r") as f: > for line in f: > if REDFLAGTEXT in line: # skip these > continue > do-something-with line All true, but sed - once you get used to it! - is easier IMHO and usually faster than Python - it's written in C... -- 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] Fwd: RE: pointers or references to variables or sub-sets of variables query.
Forwarding to list Forwarded Message Subject:RE: [Tutor] pointers or references to variables or sub-sets of variables query. Date: Mon, 15 Jul 2019 17:13:23 +1000 From: mhysnm1...@gmail.com To: 'Alan Gauld' All, Sorry for the late response. I have moved my program to SQLite. AS I found I was going down the rabbit hole of wasting time. As allan said, the select statement and other SQL statements are far easier to work with then writing the code yourself. What I did I learn a lot and started my road on OOPS. The first program I programmed on was an Apple IIE using Applesoft basic. Tried to learn assembly 6802 myself. But never got there. In the 90's I learnt C Clipper 68 and Turbo Pascal. Never became very strong with these languages as my roles were never programming focused. Late 90's, early 2000 forget, I learnt some basic Visual Basic 6. Again, didn't do much with it. I learnt the basic of PERL or enough to get by. I haven't really touched programming for years and there is a lot I have forgotten. Never had the need to do tree's, link-lists or any complex data structures. This time, I am trying to learn Python beyond what I used to know. I have used just about every type of PC since the early 80's. Even used DEC VAX, IBM 3270's as well. Yes, been around for a while now. -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Monday, 8 July 2019 8:55 AM To: tutor@python.org Subject: Re: [Tutor] pointers or references to variables or sub-sets of variables query. On 07/07/2019 20:54, David L Neil wrote: > (However, some of us grew-up at a time when RAM was expensive and even > in our relaxed state, such 'costs' still impinge on our consciousness - Indeed, my first computer was at the local university and had 64KB. My second computer was a Sinclair ZX81 (Timex in the USA?) with 16K My third, a CP/M machine with 64K and 256K RAM disk and dual floppies - such luxury! :-) So I agree, it is hard to get out of that mode of thinking. But today the minimum RAM is typically 4GB or more. My desktop boxes all have 16GB and even my ancient Netbook has 4G. My 20 year old iBook has 640M and even that is enough to run Python with many thousands of data objects instantiated. > particularly temporary, DB tables into MySQL's MEMORY storage (and > with almost zero code-change/risk)! Yes, I use SQLite's MEMORY facility reguilarly. Not for managing high volumes but where I need flexible search capability. A SQL SELECT statement is much more flexible and faster than any Python search I could cobble together. > (appreciating that I have no difficulty moving from (Python) > procedural programming to (SQL) declarative, but many of our > colleagues hate such, and with a passion) Yes, I've never quite understood why some programmers are reluctant to use SQL. For complex structured data it is by far the simplest approach and usually very efficient, especially with big volumes. But simple searches on small datasets are easier (or as easy) in native Python. -- 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] Web framework module for Python.
On 15/07/2019 07:56, mhysnm1...@gmail.com wrote: > like apache. If there is a simple python module that can run a web server, > this would be great. If there is a python module that can assist in building > the HTMl, this would be great as well. I am not sure how to link python and > the web HTML page together and haven't found anything that is clear on > addressing my needs. I have 4 topics in my tutorial that address this area. All you need is in the standard library. For a basic web server app look at the topic: Writing Web Server Applications However if you want more than the very basics then something like Flask would make life easier (also in my tutorial! :-) See the topic: Using web Application Frameworks. This even covers accessing SQLite data... -- 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] How to store output of Python program in XML
On 13/07/2019 07:40, Asad wrote: > want to print the output of the script in a xml file so that I can add some > tags . How can it be done ? XML is just text dso you can just use the normal Python string operations to format an XML string with your data. However there are XML libraries aplenty that can aid you in constructing a valid XML tree structure. The standard library contains an xml package which includes the dom module which might be a good starting place if you want to understand the concepts. The etree module is probably easier to use in the long term though. Look at the "Building XML documents" section in the etree documentation. -- 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] Output reason
On 12/07/2019 15:24, Gursimran Maken wrote: > Can someone please explain me the reason for below output. You've been bitten by one of the most common gotchas in Python :-) > def fun(n,li = []): > a = list(range(5)) > li.append(a) > print(li) > > fun(4) > fun(5,[7,8,9]) > fun(4,[7,8,9]) > fun(5) # reason for output (why am I getting to values in this output.) When you define a default value in Python it creates the default value at the time you define the function. It then uses that value each time a default is needed. in the case of a list that means Python creates an empty list and stores it for use as the default. When you first call the function with the default Python adds values to the defaiult list. Second time you call the function using the default Python adds (more) values to (the same) default list. Sometimes that is useful, usually it's not. The normal pattern to get round this is to use a None default and modify the function like so def fun(n,li = None): if not ni: ni = [] # create new list a = list(range(5)) li.append(a) return li # bad practice to mix logic and display... 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
Re: [Tutor] Multiprocessing with many input input parameters
On 12/07/2019 01:51, DL Neil wrote: > older articles! We haven't discussed hardware. Most modern PC CPUs offer > multiple "cores". Assuming (say) four cores, asyncio is capable of > running up to four processes concurrently - realising attendant > acceleration of the entirety. Just to pick up on this point because I often see it being cited. The number of concurrent processes running to achieve performance gain is only very loosely tied to the number of cores. We ran concurrent processes for many years before multi-core processors were invented with significant gains. Indeed any modern computer runs hundreds of "concurrent" processes on a small umber of cores and the OS switches between them. What the number of cores affects is the number of processes actually executing at the same time. If you just want to chunk up the processing of a large amount of data and run the exact same code multiple times then there is no point in having more than the number of cores. But if your concurrent processes are doing different tasks on different data then the number of cores is basically irrelevant. And especially if they are performing any kind of I/O operations since they are likely to be parked by the OS for most of the time anyway. Of course, there is a point where creating extra processes becomes counter-effective since that is an expensive operation, and especially if the process will be very short lived or only execute for tiny lengths of time (such as handling a network event by passing it on to some other process). But for most real world uses of multiprocessing the number of cores is not a major factor in deciding how many processes to run. I certainly would not hesitate to run 10xN where N is the number of cores. Beyond that you might need to think carefully. In Sydney's scenario is sounds like the processes are different and explicitly halt to perform I/O so the cores issue should not be a problem. -- 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] RE Embedding Python in C
On 09/07/2019 15:13, Ibarra, Jesse wrote: Caveat: I'm no expert on embedding and indeed have only done it once using the examples in the docs. However, based on my general Python experience... > I then embedded the example using C/Python API: > https://docs.python.org/3.6/extending/embedding.html#pure-embedding > > int main(){ > PyObject *pModule, *pName; > > Py_Initialize(); > PyRun_SimpleString("import sys"); > PyRun_SimpleString("sys.path.append('.')"); > > pModule = PyImport_ImportModule("Rosenbrock_Function"); > pName = PyImport_ImportModule("Rosenbrock_Function"); These import the same module twice. But Python usually checks if a module is already imported so I'm guessing it does the same here. > Py_XDECREF(pModule); > Py_XDECREF(pName); Maybe if you decrement the count after each import you will get the result you want? Although I'm guessing that relying on code to run via an import is probably a bad practice. You'd normally expect to import the module then call a function. But that's mostly guesswork, so I could well be wrong! -- 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] Fw: [docs] Python Embedding PyImport_ImportModule
On 08/07/2019 15:14, Ibarra, Jesse wrote: > I cannot seem to figure this potential bug out. Neither can we since we cannot see any code. You need to give us some context. What are you trying to do? What libraries are you using? Which OS and Python versions? Did you get ay errors? If so post them in their entirety. But most of all post the code! -- 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] pointers or references to variables or sub-sets of variables query.
On 07/07/2019 20:54, David L Neil wrote: > (However, some of us grew-up at a time when RAM was expensive and even > in our relaxed state, such 'costs' still impinge on our consciousness - Indeed, my first computer was at the local university and had 64KB. My second computer was a Sinclair ZX81 (Timex in the USA?) with 16K My third, a CP/M machine with 64K and 256K RAM disk and dual floppies - such luxury! :-) So I agree, it is hard to get out of that mode of thinking. But today the minimum RAM is typically 4GB or more. My desktop boxes all have 16GB and even my ancient Netbook has 4G. My 20 year old iBook has 640M and even that is enough to run Python with many thousands of data objects instantiated. > particularly temporary, DB tables into MySQL's MEMORY storage (and with > almost zero code-change/risk)! Yes, I use SQLite's MEMORY facility reguilarly. Not for managing high volumes but where I need flexible search capability. A SQL SELECT statement is much more flexible and faster than any Python search I could cobble together. > (appreciating that I have no difficulty moving from (Python) procedural > programming to (SQL) declarative, but many of our colleagues hate such, > and with a passion) Yes, I've never quite understood why some programmers are reluctant to use SQL. For complex structured data it is by far the simplest approach and usually very efficient, especially with big volumes. But simple searches on small datasets are easier (or as easy) in native Python. -- 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] pointers or references to variables or sub-sets of variables query.
On 07/07/2019 09:19, David L Neil wrote: > First-off, it has to be said that "100's of elements" suggests using an > RDBMS - particularly if 'age' (eg 23 and 99) is not the only likely > selection mechanism. Multiple selection mechanisms might suggest an RDBMS but hundreds of items is chickenfeed and an RDBMS would be overkill for such small numbers, if volume was the only criteria. Millions of items would certainly warrant such an approach but nowadays holding 10's of thousands of items in memory is entirely reasonable. -- 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] Neutral Networks
On 07/07/2019 03:49, Rashmi Vimalendran wrote: > I have a python program for the project which is about developing an AI > program to identify persons. The program I have identities the first > person. It is not able to identify the 2nd person. When we run it, it is > not even showing an error. I need someone to look at it and let me know me > the issue. I can share the source code. We will need to see code. If it is less than say 100 lines post it in the body of your mail(not an attachment!) and if it is longer put it in a pastebin web site and send a link. Send us some sample data too so we can see the structures. A little bit more detail on what exactly the output looks like and how you identified the problem would help. Finally, tell us the OS, Python version and any third party libraries you are using - SciPy, Rpy, etc. -- 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] pointers or references to variables or sub-sets of variables query.
On 07/07/2019 03:39, mhysnm1...@gmail.com wrote: > In C, you can use pointers to reference variables, arrays, ETC. In python, I > do not recall anything specifically that refers to such a capability. In Python a variable is a name that refers to an object. Many names can refer to the same object. So in that respect Python variables are more like pointers than regular C variables which are a named location in memory. > Data = [ > ['2019-01-19','Fred Flintstone',23], > ['2019-02-01','Scooby doo', 99] > ] > > > Category = {'under-50':[data[0]], 'over-50':[data[1]]} > > If I understand things correctly with Python. The above will copy the value > into the list within the key. No, that is not correct. It will create a reference to the same data object So Category['under-50'][0] and Data[0] will both reference the same list object. Modifying the data through either variable will affect both because it will be the same list being modified. > Not the memory address of the nested list I am > referencing. It is best to forget all about memory addresses when thinking about Python. They are irrelevant for the most part.. > Category['under-50'] = [ List1 pointer, List22 pointer, List52 pointer] That is exactly what happens in Python, as standard. The usual issue that people have with this is that they modify the data in one place and are surprised to discover it has been modified elsewhere too. If that is a problem then you must explicitly create a copy. But the behaviour that you apparently want is the default. -- 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] enumerate over Dictionaries
On 06/07/2019 05:28, Suhit Kumar wrote: > I have made the complete program but when I am compiling the program it is > showing errors. Can you please help to resolve this? > The code is in the file attached with this mail. And where are the errors? Do not expect us to run unknown code received over the internet. Only a fool would do such a thing! Show us the error messages, with full traceback text. A quick scan of the code reveals some stylistic things that you could do but they are not the cause of your errors, whatever they may be: for i,ex in teacher.iterrows(): lat1 = ex['Latitude'] lon1 = ex['Longitude'] Id = i for b,c in df.iterrows(): lat2 = c['latitude'] lon2 = c['longitude'] nameVen = c['name'] listEmpty.append((distanceCalculator(float(lat1), float(lon1),float(lat2),float(lon2)),Id,b)) demian = [] listEmpty.sort() demian = listEmpty[0] dictionaryTeacher[ex['Name']] = demian listEmpty = [] demian = [] listEmpty is a terrible name choice. You should never name variables after their data type name them for the purpose they serve. And don;t call it empty since that only applies at one particular poit. name it after what you intend to store in it... secondly you initialise demian to a list. Then you throw that list away and assign a different value to it. The initial assignment is pointless. You initialise listEmpty outside the loop and then again at the end. If you move the initialisation into the loop body at the top you won't need to reset it at the end. It only saves a line of code but if you ever need to change the initial value it means you only need to do it in one place. I don't have time to read through the rest. You really should refactor your code into functions. It will make it easier to modify, easier to debug, and much easier to read and discuss. -- 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 help in python algorithm
On 05/07/2019 07:25, Suhit Kumar wrote: > I am a student at an university. Currently I was working on an algorithm > using python. It is based on scheduling the teachers to their nearest > venues. And at the venues there can be atmost 400 teachers and these are to > be divided into the Batches of 40 i.e. ten batches. All the batches will > have teachers having same group number assigned to them. Who assigns the group numbers? Is that the program or part of the data? > should get only the two days from the working days in the month. I have no idea what that means. How does time enter into the calculation? You allocate teachers to their *nearest* venue. Where does time enter into that? There must be some other criteria? You schedule to them to the nearest venue that they have not already visited within the last week/month or something? > This is only the last part of the complete algorithm. I am sending the > files associated to it and the code that I have made till now. The code got through but the data didn't. The server only permits text based attachments. However your code is over 300 lines long and poorly structured (hint create some well named functions!) Meanwhile you have not given us any clues about what kind of help you need. Does it work? Does it fail with an error - what error? Does it fail to load the data correctly - in what way? You cannot seriously expect us to wade through 300+ lines of code that, by your own admission, only partially describes the problem with nothing more than a loosely defined problem description. > Please help me in making it as I need it urgently. We won't do your homework for you. Tell us what you are having difficulties with and we will try to help. But first, for all our sakes, go back and restructure your code into functions with reasonable names. Each one performing one clearly defined part of your solution. Then perhaps it will be easier to see where the issue(s) lie and certainly easier to describe them. -- 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] enumerate over Dictionaries
On 04/07/2019 18:02, Animesh Bhadra wrote: > Hi All, > My python version is 3.6.7 > I need help to understand this piece of code? > > rainbow ={"Green": "G", "Red": "R", "Blue": "B"} > # ennumerate with index, and key value > for i, (key, value) in enumerate(rainbow.items()): Lets unpick t from the inside out. What does items() return? >>> rainbow.items() dict_items([('Green', 'G'), ('Red', 'R'), ('Blue', 'B')]) For our purposes dict_items is just a fancy kind of list. In this case it is a list of tuples. Each tuple being a key,value pair. What does enumerate do to a list? It returns the index and the value of each item in the list. So if we try >>> for index, val in enumerate(rainbow.items()): print( index, val) 0 ('Green', 'G') 1 ('Red', 'R') 2 ('Blue', 'B') We get the 0,1,2 as the index of the 'list' and the corresponding tuples as the values. Now when we replace val with {key,value) Python performs tuple unpacking to populate key and value for us. > print(i, key, value) So now we get all three items printed without the tuple parens. Which is what you got. > This gives a output as always:- > > 0 Green G > 1 Red R > 2 Blue B > Does this means that the Dict is ordered? or it is implementation dependent? Neither, it means the items in a list always have indexes starting at zero. By pure coincidence dictionaries in recent Python versions (since 3.6 or 3.7???) retain their insertion order. But that was not always the case, but the result would have been the same so far as the 0,1,2 bit goes. -- 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 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] Python and DB
On 27/06/2019 22:20, Brave Heart via Tutor wrote: > I would like python to write to DB so I can from DB write on a webpage with > PHP... Yes, that's easy enough. Python supports access to many databases, do you have one in mind? If not the SQLite module that comes in the standard library is probably more than adequate for your needs. PHP has modules for reading SQLite too. If you are familiar with using databases in any other language the Python DBAPI should be easy to pick up. If you don;t know SQL then you might like the database topic in my tutorial(see below) -- 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] Range command
On 27/06/2019 00:07, Brick Howse via Tutor wrote: > Hello all, > > New to programming and I noticed the range command did not function like it > does in the tutorial. > For example, > I type > range(5, 10) > And the output is > range(5, 10) You have a Python 2 tutorial but are using Python 3. In Python 3 range has changed and returns a fancy kind of object (a range) that you don't really need to know about. If you really want the values convert it to a list: >>> list(range(5,10)) > Any suggestions is greatly appreciated. To avoid any similar confusion either find a Python 3 tutorial or download Python 2 and use it. Once you know Python 2 converting to Python 3 is not hard. But it is probably best just to learn Python 3 from the start! Thee are quite a few of these kinds of changes between Python 2 and 3. For experienced programmers they are "A Good Thing" because they make Python more efficient and more consistent in the way it works. But for a beginner they just seem bizarre and unintuitive. For now, just accept them and eventually you will understand why they exist. -- 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] data structures general query
On 26/06/2019 19:46, Mats Wichmann wrote: > I forgot to add the snide-comment part of "what are these good for": > > (a) binary search trees are excellent for Computer Science professors > who want to introduce recursion into their classes. > > (b) all the classic data structures are candidates for being job > interview questions ("implement a linked list in Python on the whiteboard") That's a good point that I intended to make in my response but forgot. In practice, Python's data structure can mimic these classic data structures and do so more efficiently than trying to write them from scratch in Python. So while they all have Computer Science theoretic pros and cons, in practice, Python programmers tend to ignore them and use lists, sets, tuples and dictionaries. And if they need anything more complex classes or pre-built modules. It's a very rare problem that needs a traditional data structure in Python. -- 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] data structures general query
On 26/06/2019 11:40, mhysnm1...@gmail.com wrote: > When would you use the below structures and why? If you can provide a real > life example on when they would be used in a program This would be great. > Link lists Link lists are very flexible and ideal for when you have a varying amount of data and don;t want to reserve space for a maximum amount when you only use a fraction of it. Examples are process lists in an OS. Jobs in a queue (although technically queues are data structures in their own right!) Think of situations where you use a list in everyday life - a shopping list. You add items in an ad-hoc way and don't know in advance how any items you will eventually have. The advantages are that they have a low memory footprint, easy to move things around (including deletion), insertion is quick if adding to the top. Searching not so much. > Double link-lists Same as single linked lists except you can access both ends so a sorted list becomes much faster to search but at the cost of making inserts and moves etc slightly more complex(but only slightly). Real world uses? Hmmm, for me not so many. I'll let somebody else suggest something! > Binary trees Good for searching. Not so good for insertions/moves. Always a danger of the tree becoming unbalanced in which case it tends towards becoming a linked list. Real world use - heirarchical or ordered data arriving at random. Trees are inherently ordered so inserting the data puts it "in order" Code tends to be more complex than for lists though and is often recursive in nature which can be an issue for big trees. > What other trees are their other than hierarchical tree (file systems) Thee are whole families of trees including multi child trees such as yours. AVL trees (if memory serves) try to force the tree to be balanced. Non binary trees need selector functions etc (often hash based). Real world use - database caches, network management heirarchies parsing structures. -- 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] tree or link-list questions.
On 26/06/2019 11:34, mhysnm1...@gmail.com wrote: > The reason why I am using the tree structure like a file system. Is I am > going to attempt to write some code to walk down the tree based upon certain > conditions which I am still working on. Based upon the test conditions will > determine how I move up and down the tree. Defining the test conditions is the hard part of building and searching any tree. That's why binary trees are so much easier, the usual comparison (<,=,>) apply. When you have multiple subtrees your test function must return a selection index telling you which subtree to go down. > The high picture is I am trying to build a dynamic search functionality > based upon keywords in the text. Like a store name or organisation name. The > text has been cleaned up before applying it to the tree based upon text or > punctuation and digits that are not required. Such as receipt numbers, > postcodes, braces, etc. This is mostly done. I am hoping the tree structure > will help in finishing the search capability. I will only know once I have > tried it. If not, then I will look for another method. One thing to consider is an LDAP directory. It is a tree structure that acts like nested dictionaries and is specifically designed for handling hierarchies. Specifically organisational hierarchies, but any kind will do. It what's at the heart of most corporate Directory systems - such an Microsoft Exchange. And the good news is that there are open source options available and Python modules to work with them. but it might be overkill for your project. But worth a quick Google and Wikipedia browse... -- 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] Training, Was: Basic Question about Visualization for enduser
Caveat: This is a long term bug bear of mine and I spent many, many hours over the last 20 years in discussion with our local universities discussing how software-engineering training could be made more relevant to the real world. I'll try to keep it focused :-) On 25/06/2019 21:00, David L Neil wrote: > This illustrates an interesting (at least to me?us) side-effect: in > order to reduce "cognitive load" we often introduce training by using > the most straight-forward or 'simple' tools. In this case Jupyter (which > I think a most marvellous invention). Indeed, and for those with a use for it, extremely valuable and not really very simple! > However, such illustrative devices are often not what is used in the > 'average' development department (assuming there is such a thing). Maybe not, but then Python covers a wide gamut and some researchers for example never get beyond Jupyter, matplotlib etc etc. Those tools deliver all they need. You have to remember that software development is only one sphere in which Python is used. It is also a research tool, a prototyping tool, a personal productivity tool, a devops production scripting tool in addition to an application development language. Many Python users have no need to distribute their software. And for others Python is likely to be just one small component in a much bigger project involving Java, C++, SQL, etc. Professional devs may well need to build distributable apps. And they will use different tools. But there is little point in teaching students about them because there are few standards and different toolsets for just about every combination of app type and OS. In fact just about every web server framework has its own solution. And what you do for Python is different to Java or C++ or Smalltalk or Lisp... And if the project has multiple languages - and most do nowadays then you need a hybrid solution. There is no point in trying to do more than demonstrate some arbitrary example of what it takes to build a distributable package. Then make it clear that this is not a general solution, just one example. > the training meets the knowledge-need (we hope) but not the requirements > of practical application in the work-place. Frankly the biggest problem with most training regimes is that they put way too much focus on coding and programming languages. Professional devs spend much more of their time analysing, designing and testing than they do coding. In my experience very few trainees have any real experience of analysis, at best some rudimentary theoretical knowledge. In the real world the bigger the project the greater the proportion of time spent analyzing to understand exactly what you need to do. Often up to 25% of the total project time-scale. (Of course, those early activities have lower team sizes so the proportion of budget is more like 10%) Fred Brooks made the same point in his classic essay "No Silver Bullet". He talked about the inherent and intrinsic difficulty of software and understanding the essence of the solution as the major challenges. And no toolset (or training course) can shield you from that. At best they can free you to concentrate on it. Design is only covered at a shallow depth - usually restricted to some UML class diagrams and maybe a message sequence chart. Testing is covered at unit test level but rarely at integration or system test levels. And virtually never at UX or performance level. > On the other hand, should training in Python, and more specifically, the > likes of matplotlib, involve any forlorn attempt at universal coverage > of 'the programmer's tool-set'? My personal thought is that the key word there is 'forlorn' You could teach them to be experts in one toolset and in their very first job they discover they have to learn a complete new toolset. The principles will be similar but the practice very different. -- 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] Python Imported Code
On 25/06/2019 14:50, stephen.m.sm...@comcast.net wrote: > using global, but that fails. I also can't seem to use arguments because the > module that tkinter fires up with this command: > > self.process_button = tk.Button(master, text='Process Request', \ > font = ('times', regular_font, > 'bold'),\ > fg = "blue", > command=self.Process_Reservation_Request) > > does not seem to support the presence of arguments. There is a common trick to get round that: self.process_button = tk.Button(master, text='Process Request', font = ('times', regular_font,'bold'), fg = "blue", command=lambda :self.Process_Reservation_Request(x,y) ) The lambda defines a function that takes no arguments but which then calls your function with arguments. Of course x and y need to be visible within the function defining the lambda. > Demo Program - This currently bombs in part 3 - it can't seem to see the > value/space created in part1. Any help or reference that will clearly show > Part 1 > > import nice > import nice2 > global myGlobal > myGlobal = "initial value" global here does nothing. global is only useful inside a function. Remember that global in Python means module level - it does not mean visible between modules! > print(myGlobal) > nice.changeGlobal() > print (nice.myGlobal) > nice2.changeGlobal() > > print("nice version = ", nice.myGlobal) > print("nice2 version = ", nice2.myGlobal) > print("mainline=", myGlobal) > > part 2 - stored as nice.py > > def changeGlobal(): >global myGlobal This tells changeGlobal() that there is a variable called myGlobal defined at the module level. That is, within nice. It does not refer to the one you defined in Part 1. >#print("entering changeGlobal part 1", myGlobal) >myGlobal = "hello" This now creates that module level variable with the value 'hello'. >print("top of myGlobal", myGlobal) > >myGlobal="bye" And this redefines nice.myGlobal to be 'bye' The final value will now be seen by Part 1 code as nice.myGlobal with a value of 'bye'. > Part3 stored as nice2 > myGlobal = "hello" This creates a new global variable called myGlobal within the nice2 module. It is visible to Part 1 as nice2.myGlobal > def changeGlobal(): >global myGlobal This tells changeGlobal() to use the module level variable above. ie nice2.myGlobal. >print("first value = ", nice.myGlobal) You didn't import nice into nice2 so this should give an error. >myGlobal="it worked" >print("in changeGlobal2 =", myGlobal) This should set the variable nice2.myGlobal to 'it worked' and that will be seen in Part 1 code as nice2.myGlobal. -- 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] Basic Question about Visualization for enduser
On 25/06/2019 15:52, Sinardy Xing wrote: > My question is, how currently all of this great technology glue together > and as a final product for the enduser. Because I cant imagine that we > install Anaconda Jupyter Notebook at frontend for the enduser to use it, > and give end user bunch of *.py You sort of can and there are tools for wrapping it all up in an installable package. More commonly you create a desktop application using the GUI toolkit of your choice and generate the graphical output and display it yourself (using pyplot etc) Or > I read a little bit about Django web framework, I am not sure if my > understanding is correct. Therefore we are using Django as frontend tier > then it connected to backend python server running the calculation and all > other python stuff including the charting and send back the result to > django front end server for end user to consume. More or less although the Django framework may well host the server code too. Django is a web framework so it generates web pages(HTML) as output. Thus if you can generate png files in your python code Django can put links to those files in its HTML output and the users browser will display them. > My question is how is the end to end commonly use by company product, how > they present those charts to end user.? Nowadays its usually via the web. Depending on how fancy you want to get. It could simply be a static graphics file (png/pdf/jpeg etc) or maybe a Flash graphic(not common now) or even a movie generated by back end code. Or it could be something more dynamic controlled by Javascript in the browser in which case your backend code simply serves up raw data and the Javascript code turns it into dynamic graphics within the browser screen, possibly via SVG or similar technology. There have never been so many options for delivering content to end users! -- 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] tree or link-list questions.
On 25/06/2019 13:24, mhysnm1...@gmail.com wrote: ... the tree I am trying to create is: > > * A single root node > * A node can only have a single parent. > * A parent can have multiple children. > * Each node is unique. A tree with multiple children (where multiple is more than 2) is entirely possible but does make the code more complex. Have you considered a binary tree where items go either left or right depending on whether they are "greater than" or "less than" (for some arbitrary definition of greater and less) than the parent node? This is much simpler to code, and you will find lots of examples online. -- monospace font needed --- Root the brown fox cow car yellow car green house quick brown fox yellow flowers -- end of monospace font --- The immediate questions you must answer (because only you understand the requirements) are: 1) How do you know which tree branch to navigate down? For example you have two car nodes. If searching for car which is the correct answer? Similarly for fox? 2) How do you know to go down an extra level. For example there are 4 nodes below 'the' What determines whether a new node sits alongside the existing nodes or at a level below. 3) Having decided you need to go a level lower how do you know which subnode to go down? How do you insert the node car into the correct subnode of the?(either brown or yellow) It looks like you are doing this based on the textual context. That means you insertion code has to be stateful since it must remember the previous word inserted (or have access to the original data). That gets complicated really quickly. Similarly, when you try to retrieve the data how do yu know which node to fetch. For example if I search for fox? Surely you will need to provide the full 'path' to fox topick the right one? But if you know the path why search a tree? In that case nested dictionaries would seem a better solution? Something like(missing all the quotes!): data = { the: [ {brown: [ {fox:[]}, {cow:[]}, {car:[]}, ]}, {yellow: [ {car: []} ]}, {green: [ {house: []} ]}, {quick: [ {brown: [ {fox: []} ]}, ]}, ]}, {yellow: [ {flowers: []} ]} ]} Then you can access the appropriate path: myFox = data[the][quick][brown][fox] Just a thought. Since I don't really know how you intend to use this I'm not sure if that would work for you. -- 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] tree or link-list questions.
On 25/06/2019 13:24, mhysnm1...@gmail.com wrote: A much less open ended question :-) > class Node: > > def __init__(self, name, value): > self.parent = None > self.child = [] This should be plural if its a list. So call it children... However I'm not really sure it should be a list, most trees have two child nodes - called left and right by convention. But some trees do have multiple children, it's up to you. > self.name = name > self.value = value > > def add_child(self, name, value): > # Compare the new value with the parent node > if self.name: > if name != self.name: > if self.child is None: > self.parent = self.child > self.child = Node(name, value) > else: > self.child.add_child(name, value) > else: > self.name = name > self.value = value > My first comment would be that in OOP we should be trying to use objects so rather than pass name,value into the method pass another Node instance: def add_child(self, newNode): if newNode.name != self.name self.children.append(newNode) newNode.parent = self And if necessary call it like myNode.add_child(Node(name, value)) However, looking at your code above you seem to be confused about which node it adding which. self is the top level node to which you are adding a child. So when finished you want the existing nodes parent to be untouched and it's children list to include the new node. Instead you are overwriting the list with the new Node instance and setting the top level node's parent to the child. You really want the child to have the top level as its parent. The initial test of if self.name shouldn't be necessary since you assign name when you create the node. Without a name the node is pointless so you should check if name is None in the init method and throw an exception (ValueError?) if name is not valid. -- 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] Basic Question about Visualization for enduser
On 25/06/2019 13:39, Sinardy Xing wrote: > All of the example that I learned from internet currently are using the > Anaconda Jupyter Notebook. > I know there are API where we can output the result of the graph to png, > will that be possible all these done automatically and dynamically via an > apps ? If you mean "is it possible to write an app that does it all?" then the answer is yes. If you mean "is there an existing app that does it all?" the answer is maybe. It depends on exactly what you want to do, what kind of data you have etc. But a Google search is probably your best bet. Python itself is just a programming language so somebody - possibly you! - has to write the apps. -- 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] replacing a loop
On 24/06/2019 17:15, johnf wrote: > def locChoices(self): > locDS = self.eslocation.getDataSet() > loc_Choices=[''] > locKeys=[0] > for row in locDS: > loc_Choices.append(row['facility']) > locKeys.append(row['pkid']) > > return loc_Choices,locKeys > ... and wonder if it will improve the performance. Because you are building two lists in one loop it probably won't do much for performance. It might even be worse. As always measure. But something else that might make a small difference is the pre-fetch of the locations. Why not: for location in self.eslocation.getDataSet(): You don't use the locDS value anywhere other than in the loop. Also the choice of method name is unfortunate in that you return both choices and keys but the method name implies only choices. Minor nit-picks... -- 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] collections and mappings
On 21/06/2019 11:39, mhysnm1...@gmail.com wrote: > I think I understand, but that type of maths I have not touched in 40 years. The real point is that in Python terms a mapping is nearly always just another name for a dictionary. Either a set of key/value pairs or a set of key/function pairs. Very occasionally it will be a class rather than a dictionary. -- 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] Fwd: Re: Hii
On 21/06/2019 14:59, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times you've struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. If you only wanted to do this for your own application it would be relatively simple but since it seems you want to do it for the computer as a whole that raises a whole extra level of complexity. Some issues to consider: 1) If this is not just installed on your own personal computer it could be illegal - breach of personal privacy legislation in many countries prohibits key logging. 2) Do you care about users logging in remotely? Do you need to just log the current user logged in for the current GUI session or do you also want to record activity by other remote hosts logging in? 3) What about virtual machines running on the computer? Do you want to capture keystrokes within those VMs? That might be tricky as it may not show up in the native OS. It may even depend on the VM the user is running. 4) Do you care about which user is logged in or do you want to record keystrokes for every user of the computer? 5) Is it only counting for a single session or for multiple sessions? This is more of an application design question that keylogging per se... Leaving those issues aside and looking only at the keylogging aspects. Your best bet is to find a third party module that does it for you. Failing that you will need to use the OS facilities which is never a trivial exercise. It is also the kind of low level feature that can change between OS versions (especially between 32bit and 64bit versions) If you plan to use it on multiple OS then the technique will likely differ between OS - MacOS and Linux may be similar but windows will be different. Let's assume the simplest case where you only want this for personal use on a computer where you are the only user and don't run any other OS either dual boot or in a VM. in that case you could write a Python application that does keylogging and put it in your startup group. How you notify it to stop recording before the computer shuts down is another issue and how it records/displays its results needs thought too. On Windows you might need to use ctypes to access the raw Win32 API or the PyWwin32 Python package may include functions that will do the job for you. 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
Re: [Tutor] collections and mappings
On 21/06/2019 01:01, mhysnm1...@gmail.com wrote: > I have reviewed the collection module and do not understand mappings. I have > seen this in other languages and have never got the concept. Can someone > explain this at a very high level. OK. You are a master of the open ended question so I'm not sure what aspect you don't understand. But I'll start at the beginning and take it as far as Python. But you are opening a vary large can of worms... Mappings, in programming terms, are related to a mathematical concept. See Wikipedia for a more detailed account of math mapping. In simplistic terms a mapping comprises two sets of data, one an input the other an output. The map is the set of relationships between input and output. Thus given input of {a, b, c} and output of {1,2,3,4,5} We might have any of several mappings between these. A simple 1:1 mapping might be {a,1}, {b,2}, {c,3} But we can have 1:N or N:1 or N:M mappings too: {a,1,2} {b,3} {c,4} - a 1:N mapping {a,1} {b,1}, {c,3} - an N:1 mapping {a,1,2} {b,2},{c,1,3} - a N:M mapping Note that the mapping does not have to include all of the output elements. The mapping may be arbitrary, as above or it may be defined as a function: {a, f(a)} {b,f(b)} {c,f(c)} Or even as a set of functions... In programming, and particularly in Python, this tends to be represented as a dictionary (or sometimes a class). So the mappings above could be shown as: input = ['a','b','c'] output = [1,2,3] 1_1 = {'a':1,'b':2,'c':3} 1_N = {'a':(1,2)'b':(1,),'c':(3,)} N_1 = {'a':1,'b':1,'c':3} N_M = {'a':(1,2),'b':(2,),'c:(1,3)} def f(x): return x*2 1_f = {'a':f('a'),'b':f('b'),'c':f('c')} List comprehensions and generator expressions are also commonly used to create mappings, especially the functional sort. I have no idea if the addressed any of your questions but if not please ask again, but with something more specific. PS. To the real mathematicians on the list. My math is very rusty, if I've made any major gaffes please feel free to correct/amend my scribblings. -- 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] Fwd: Re: Hii
Forwarding to list. Please use Reply-All or Reply-List when responding to list emails. Forwarded Message Subject:Re: [Tutor] Hii Date: Thu, 20 Jun 2019 08:50:31 -0500 From: Antonio Arizpe To: Alan Gauld i am using python 3.7 On Thu, Jun 20, 2019 at 8:43 AM Antonio Arizpe mailto:aarizpe...@gmail.com>> wrote: it is for windows 7 64 bits but i will be targeting windows 7 and 10 32 and 64 bits i currently use a script i was able to work together and its for automated screenshots i imagined for the key strike counter for it to be similar because i imagined it as defining keystrikes as x = 1 and for every new key strike would apply x+=1 and saving it in a text file in a directory. im sorry im a little new for third party libraries im using im really just using the default script that comes with python installation to make example here is the code i use for automated screenshots import sys import os from datetime import date import pyautogui import time import shutil today = str(date.today()) os.chdir ('C:\\Program Files\\Python37\\tll') os.mkdir (today) x=1 while x<1080: pyautogui.screenshot('/Program Files/Python37/tll/'+str(today)+'/image'+str(x)+'.png') x+=1 time.sleep(30) On Thu, Jun 20, 2019 at 3:30 AM Alan Gauld via Tutor mailto:tutor@python.org>> wrote: On 19/06/2019 23:30, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times youve struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. It is possible, but it will likely be OS specific so you need to tell us which OS you are using/targeting. Also any code that you've tried always helps along with any error messages. Also tell us about any 3rd party libraries you are using. -- 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 <mailto: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] word printing issue
On 20/06/2019 11:44, mhysnm1...@gmail.com wrote: > I have a list of strings that I want to break them into separate words, and > a combination of words then store them into a list. Example below of a > string: > "Hello Python team". > The data structure: > [ ['Hello'], > ['Hello', 'Python'], > ['Hello', 'Python', 'team'], > ]'Python'], > ]'Python', 'team'], > ['team'] ] > > > > I want to know if there is a better method in doing this without the > requirement of a module. Modules are there to be used... Here is one with itertools from the standard library that gets close: input = "hello Python team".split() result = [] for n in range(len(input): result += [item for item in it.combinations(input,n+1)] If you really want to do it from scratch then Google combinations algorithm, or look on wikipedia. -- 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] Hii
On 19/06/2019 23:30, Antonio Arizpe wrote: > i just need help with a script thats registers keystrikes and adds up all > the times youve struck a key and gives a number of the total amount of > times the keyboard was struck. nothing specific about characters. just how > many times it was struck in a real number. It is possible, but it will likely be OS specific so you need to tell us which OS you are using/targeting. Also any code that you've tried always helps along with any error messages. Also tell us about any 3rd party libraries you are using. -- 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] Unexpected result when running flask application.
On 19/06/2019 05:18, Cravan wrote: > Hi all, > > 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. 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. 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. And your layout.html template is virtually empty. I think you need to write some valid HTML somewhere. -- 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] Installing Python on Server
On 18/06/2019 14:28, Ben Wadsworth wrote: > Hi, > When installing Python on a windows server, will the server require a > restart? I've never tried so can't be sure. But it shouldn't. Python doesn't require any special access. But then again, it is Windows, so you can never tell. -- 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] How to Scrape Text from PDFs
On 17/06/2019 06:30, Cem Vardar wrote: > some PDF files that have links for some websites and I need to extract these > links There is a module that may help: PyPDF2 Here is a post showing how to extract the text from a PDF which should include the links. https://stackoverflow.com/questions/34837707/how-to-extract-text-from-a-pdf-file There may even be more specific extraction tools if you look more closely... -- 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] Installing Python v3 on a laptop Windows 10
On 15/06/2019 22:23, Ken Green wrote: > I understood there is a preferable method > of installing Python into Windows. I pray > tell on how about to do it, gentlemen. It depends a bit on which python distribution you use, there are several. Personally for Windows I always recommend the ActiveState free version. It bundles several useful extra Windows tools and puts the docs in Windows help format for you. -- 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] deleting elements out of a list.
On 15/06/2019 05:51, mhysnm1...@gmail.com wrote: Caveat: I'm picking this up late in the day and only had a cursory look at it, so may be missing some critical insight... > I have a list of x number of elements. Some of the elements are have similar > words in them. For example: Define "similar". It implies not identical. What is different? What makes them similar? Every time you introduce vague inequalities you imply the need for some kind of intelligent function that removes the ambiguity and vagueness. it definitively says that these two items are similar or not similar. Can you write such a function? If so the problem should become relatively simple. > Dog food Pal > Dog Food Pal qx1323 > Cat food kitty > Absolute cleaning inv123 > Absolute Domestic cleaning inv 222 > Absolute d > Fitness first 02/19 > Fitness first > > I wish to remove duplicates. So what would the output look like if the above is the input? My guess of what you want is: qx1323 Cat kitty Domestic d 02/19 Is that right? Or is my idea of similar and duplicate different to yours? > I could use the collection.Count method. This > fails due to the strings are not unique, only some of the words are. Sorry, I can't understand that. It makes no sense to me. You need to define strings and words in this context > description = load_files() # returns a list A list of what? characters, words, lines? > for text in description: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) This is weird. enumerate returns tuples which you assign to i. But then you use i in a slice opertion. But slice expects an integer. > print (word) > answer = input('Keep word?') > if answer == 'n': > continue > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] Without any clue what the description data looks like we can't really decipher what the code above does. > description list will cause a error. If I copy the description list into a > new list. And use the new list for the outer loop. I will receive multiple > occurrences of the same text. I'm not sure thats true but it denends on what description looks like. > > description = load_files() # returns a list > > search_txt = description.copy() > > for text in search_txt: > words = text.split() > for i in enumerate(words): > Word = ' '.join(words[:i]) > print (word) > answer = input('Keep word (ynq)?') > if answer == 'n': > continue > elif answer = 'q': > break > > for i, v in enumerate(description): > if word in description[i]: > description.pop[i] The usual way to remove things from a list is to create a new list using a filter newlist = filter(test_function, oldlist) or a list comprehension newlist = [item for item in oldlist if test_function(item)] Which brings us back to the beginning. Can you write a test function that unambiguously defines what needs to be removed? -- 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] Differences between while and for
On 15/06/2019 05:53, mhysnm1...@gmail.com wrote: > In C, Perl and other languages. As a point of interest not all languages have these constructs. Oberon, for example, only has a while loop because it can be used to simulate all other loop types. Some Lisp dialects don't even have a loop construct because recursion can be used instead. In addition to for, while and repeat/until some languages (eg ADA and some BASICs) include a general loop construct. And of course in assembler GOTO is the ultimate loop construct. Don;t assume that just because one language supports a particular construct that others will or should also support it. The variety of control structures offered is one of the defining features of any programming 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] os.is_file and os.is_dir missing from CPython 3.8.0b?
On 14/06/2019 15:53, Tom Hale wrote: > I'm trying to use os.is_dir, but I'm not finding it or os.is_file. I've never heard of these functions, but I'm still on v3.6, never having found a reason to upgrade. So I assume... > Python 3.8.0b1 (tags/v3.8.0b1:3b5deb01, Jun 13 2019, 22:28:20) ...these are new introductions in 3.8? If so, how do they differ from the os.path.isfile() and os.path.isdir() functions that already exist? Could you use those as alternatives? As to why the new functions aren't showing up, I've no idea, sorry. -- 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] Download audios & videos using web scraping from news website or facebook
On 14/06/2019 07:35, Sijin John wrote: > I am trying to Download audios & videos using web scraping from news website > (eg: https://www.bbc.com/news/video_and_audio/headlines) or Facebook & I > could't. > So in real scenario is it really possible to download audios/videos using > python code ? Of course, just as its possible to do it in any other language. In fact there are several specialist libraries available to make the task easier. It may not be legal however and the web site may have taken steps to prevent you from succeeding or at least make it very difficult. But that has nothing to do with Python, it would be just as difficult in any language. So, if you are having difficulty the problem likely lies with 1) your code and how you are using the tools. 2) the website you are scraping having anti-scraping measures in place. But since you haven't shown us any code ewe can't really comment or make any suggestions. -- 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] Installing Python
On 10/06/2019 22:20, Avi Chein wrote: > I'm trying to install Python 3.6 on my MacOS Mojave but it isn't installing > properly. When asking for help, on any forum, it's never a good idea to say that something "doesn't work" or "isn't installing properly". That gives us nothing to work on. What exactly is happening? Where did you download from? How did you try to install it? What actually happened? - Error messages? Or just a non-functioning icon or menu? Or nothing at all? The more specific the information you give us the better the chance that we can answer. -- 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] Python printing parentheses and quotes
On 10/06/2019 17:50, Sai Allu wrote: > Basically what happened was that I had a few lines in the script like this > ip = "10.41.17.237" > print(" Welcome to Squid Monitoring for ", ip) > print("") > > and the output was like this > > (" Welcome to Squid Monitoring for 10.41.17.237") > > ("") Are you sure? Is that a cut n paste or just how you think you remember it? The reason i ask is that its not what i see and not what I'd expect. In Python v2 print is a statement which means that Python sees your first print line like: print (" Welcome to Squid Monitoring for ", "10.41.17.237") That is it thinks you want it to print a tuple of 2 strings and what I see as output is: (' Welcome to Squid Monitoring for ', '10.41.17.237') Which is a tuple of 2 strings... Now if I remove the parentheses it looks like: print " Welcome to Squid Monitoring for ", "10.41.17.237" Which is telling Python to print two strings joined by a space. And I see the output: Welcome to Squid Monitoring for 10.41.17.237 And in both cases the second print just prints out an empty string with no quotes. Are you sure that's not what you saw? > P.S. After I upgrade to Python3 this started working. In Python 3 print is a function so it needs the parentheses. Without them it will report a syntax error. So for Python 3 your original code is correct. -- 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] Pickles and Shelves Concept
On 08/06/2019 01:02, Alan Gauld via Tutor wrote: > keys to your data and then restore it in any sequence, or > only restore some of it, you can. Apologies for the Yoda-speak at the end! A bit of editing that didn't quite work out as intended... -- 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] Pickles and Shelves Concept
On 07/06/2019 18:42, Gursimran Maken wrote: > I am not getting the concept of pickle and shelves in python, I mean what's > the use of both the concepts, when to use them in code instead of using > file read and write operations. You are right in that you could save all your data to a file using read/write primitives. Of course you'd need to convert them all to/from strings. And all collections would need to be iterated over (and if the collection contained other collections they'd need to be iterated too). But with pickle you can just save the entire data structure with all its contents, in a single operation and it can be a collection (of any type and depth) and the contents can be any kind of mixed data. You don't need to care, pickle takes care of it all for you. And you can read it back again in a single operation and all the data will be restored to its original type and location. But with pickle you still need to store things in sequence and read them back out in the sequence that you stored them. If all you are doing is saving the state of your program and restoring it that isn't a problem. But if you only want to retrieve one piece of your data later then its a bit of a nuisance. That's where shelve comes in. By acting like a persistent dictionary you can assign keys to your data and then restore it in any sequence, or only restore some of it, you can. Does that help? -- 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] Fwd: Re: would someone please explain this concept to me
On 06/06/2019 00:57, Alan Gauld via Tutor wrote: > But in the second example you actially change the object > that checklimit refers to. > > checklimit = 22 # immutable value assigned > feeds['bar'] = checklimit # both refer to same immutable value > base_var = 66 # now feeds refers to original object: 22 > # and checklimit refers to new object: 66 Oops, base_var should of course be checklimit! I started out using base_var then changed it to checklimit to match the original code. But this one slipped through unchanged. Sorry. -- 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] Fwd: Re: would someone please explain this concept to me
On 05/06/2019 20:47, nathan tech wrote: > so for example if I do: > > feeds[feed1]["limit"]=g.checklimit > > And later did g.checklimit=7000 > > Would it change feeds[feed1]["limit"] too? No, because the feeds value is still referencing the original value object. The issue arises when you modify a mutable object that is references by two (or more) variables. If the value is immutable then the references will retain the original value. Specifically, in your case. The first example you set the feeds value to a dictionary. Then you modified the contents of the dictionary but did not change the dictionary itself. base_dict = {} # create object feeds['foo'] = base_dict # reference to same dict object base_dict['x'] = bar # modified dict referred to by both variables But in the second example you actially change the object that checklimit refers to. checklimit = 22 # immutable value assigned feeds['bar'] = checklimit # both refer to same immutable value base_var = 66 # now feeds refers to original object: 22 # and checklimit refers to new object: 66 In the first case you do not change the object that base_dict refers to, you only change its content. In the second case you make checklimit refer to a completely new object. Does that make sense? PS. Notice that the use of a globals module, g, is completely irrelevant to this issue. It has nothing to do with the values being in a module, the issue is purely about references to objects and whether you modify the referenced object or its contents. -- 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] Fwd: Re: would someone please explain this concept to me
> That is why I was going to use g.blank-feed as the template, > assign that to d["feed 1's link"] then just update the feed part. The simplest way is just to assign a new blank dictionary. Don;t assign the same dictionary to each feed. (Incidentally your description above is much clearer than the one you initially posted!) feeds[link] = {key1:value1, key2:value2} If you need to pre-populate the dictionary with many values when you assign it (or need to compute the values) I'd recommend writing a small function that creates a new dictionary, and adds the values then returns the dictionary. Or maybe, better still, use a class and populate the feeds dictionary with instances of the class. class Feed: def __init__(self, val1=default1, val2=default2, val3=default3): self.key1 = val1 self.key2 = val2 self.key3 = val3 feeds[link1] = Feed(v1,v2,v3) feeds[link2] = Feed() # use default values After all that's exactly what a class is - a template for an object. What you definitely don't want to do is what you have been doing and assigning the same single dictionary object to each link entry. -- 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] would someone please explain this concept to me
On 05/06/2019 00:37, nathan tech wrote: > Hi there, > > So I have just fixed a huge bug in my program, but don't understand > exactly... How it bugged. Neither do I, your explanation raises more questions than answers. > globals.py: > feeds={} > blank_feed={} > blank_feed["checked"]=1 > blank_feed["feed"]=0 > > > main file: > > import globals as g > # some code that loads a feed into the variable knm Your terminology is odd here. You are not loading anything into knm, rather you are assigning the value of knm to the "feed" dictionary entry. > g.feeds[link]=g.blank_feed; > g.feeds[link]["feed"]=knm > > #in the below code, the variable link has a different value: > > # load a feed into the variable r Similarly this assigns the value of r to the "feed" entry - the same "feed" entry as for knm so you have now overwritten the knm value in the dictionary.. > g.feeds[link]=g.blank_feed > g.feeds[link]["feed"]=r > Now at this point, python would set the first loaded feed to the same > thing as the second loaded feed. It also set g.blank_feed to the second > feed, as well. Sorry, I have no idea what you mean by that. Which is the "first loaded feed"? knm?, r or the feeds dictionary? And what is the "second loaded feed"? What do you mean by a "feed" in this context? You are setting g.feeds[link] to g.blank_feed in both cases. Your line g.feeds[link]["feed"]=r is exactly the same as if you typed g.blank_feed["feed"]=r If you set the knm and r variables to fixed integer values (say 42 and 666) that would allow you to tell us what variable has what value. And what you expected them to store. (The fact that knm and r may normally contain some other kind of object is not relevant to this question.) > I replaced the last three lines with this: > > # load a feed into the variable r > g.feeds[link]=g.blank_feed; > g.feeds[link]["feed"]=r > > And it works. What works? What is different? I still don't know what you think should be happening, what is happening first time round, and what is now happening. > but why does it work? > > Why does that semi unlink all the variables? The semicolon should not "unlink all variables", whatever that means. But I have no idea what you are seeing so cannot comment any further. You need to give us more specifics. -- 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] Interactive editing of variables.
On 02/06/2019 01:10, mhysnm1...@gmail.com wrote: > What I do not know, how this applies to open source. If there is no > commercial transaction. Then this is the area I am unsure if any of the laws > I am indirectly referring to impact. Caveat: I am not a lawyer... I know it has a big impact on commercial software but that has some potential return on investment in terms of increased market share. For open source it is hard to see the return and the most likely scenario would be to greatly reduce the availability of such software. Consider the potential impact of applying such requirements to open source or other "free" software. This would potentially mean that any software that was shared or distributed in any way would need to comply. That would include a bit of code I knocked together for my own benefit and my friend wanted to get a copy. I've got to say, sorry I need to make it accessible first... now consider that my friend and I are working on some scientific research, maybe even medical research into treatments for blindness or cancer or whatever. Now I can't share my research tools with other researchers because I lack the time and or knowledge to convert the software to be accessible. The implications are that a lot of software would never see the light of day regardless of the potential benefits it could deliver. It would be a foolish law which prevented (or even substantially delayed) progress in the name of making all software accessible. It would also be very difficult to enforce since you would need to either prohibit the sharing of all non-accessible software or somehow, arbitrarily, define what constitutes regulated "distribution". Legislating for commercial distribution is much easier and more justifiable since profits are available to pay for the extra effort/costs. Where no profit exists then distribution is much less easily defined; it would be a legal minefield I suspect. > Anyway, this is getting off scope. Just highlighting > so people are aware. Accessibility is a part of best practice > for UX, UI and development. Indeed, and in an ideal world all of the building blocks would incorporate it at the foundation level. Unfortunately we are a long way from that. Also, in most cases the supporting hardware is not readily available to deliver the potential accessibility options that could be provided. It is very difficult to write software for features that you cannot test (think cursor control via blow tube or eye movement or command input by tones. All possible now but need specialist kit which individual developers won't have.) It is hard enough testing software for all the "standard" forms of input - mice, pens, touch screens, keyboards, voice, etc -- 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] Interactive editing of variables.
On 01/06/2019 09:52, mhysnm1...@gmail.com wrote: > the underlying graphic library. Win32 could work if I could load it. Since > then I could use standard windows objects. If you are running windows then you can access the Win32 DLLs via ctypes. The win32 package should also be easily installable as a binary. If not try using the ActiveState distribution of python because it bundles all the windows tools in the installer. Personally I always use ActiveState pyton for my Windows boxes. -- 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] Interactive editing of variables.
On 01/06/2019 08:55, mhysnm1...@gmail.com wrote: > As I am using Python 3.7 under windows. I have tried to use the win32gui, > and Tkinter. Both generate the below errors and I cannot identify a module > release to support the version of Python I am using. Tkinter should be included in the standard Windows download you should not need to install it. try >>> import tkinter # not Tkinter! If there are no errors it is available > I gave up on the cursers due to the above issues and looking at example > code. It was far to complex to achieve the simple thing I wanted too do. Here is some basic Tkinter code that does what I think you want. You have to close the window by clicking the close icon but that could be incorporated in the button code. import tkinter as tk myVar = "" def get_value(edt): global myVar myVar = edt.get() def edit_val(val): top = tk.Tk() ed = tk.Entry(top) ed.insert(tk.END, val) ed.pack() tk.Button(top,text="Store changes", command=lambda : get_value(ed)).pack() top.mainloop() myVar = input("Enter a start value: ") print( "Before: ", myVar) edit_val(myVar) print( "After: ", myVar) > The issue I have with a lot of GUI programs built for Python they generally > fail in the accessibility department for a screen reader. I can't help there I have nearly zero experience of using accessibility tools. But I'd expect any GUI toolkit to work with the standard OS tools. After all they are ultimately all built using the underlying primitive GUI API > Most open source software which is multi-platform supported fail in this Of course, since most open source authors have no incentive to develop accessible specific code. They are just scratching their own itch and making it available to anyone who wants to use it. That's how open source works. > This is a major issue for software developers > not considering all users and there is legal requirements here. Sorry, I am > falling on to my band wagon of in-accessible or non-inclusive design > products which is my passion. I can understand why folks get excited about it, especially if they (or friends/family) need that feature. But the cost (both in time and money) of doing so is considerable and if nobody is paying (or there is a time deadline) then it tends not to get done. That's capitalism in action. -- 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] is this doable
On 01/06/2019 00:13, Alan Gauld via Tutor wrote: > Is the language C/C++? If so you may know the OS API calls needed > and you could access those directly from Python using ctypes > That might make your job more familiar and easier. I meant to add a nod to Mark Hammond's win32 package too. It includes a process control module with access to most of the Win32 API for process control, which might be simpler than using ctypes to call the raw C API -- 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] Interactive editing of variables.
On 01/06/2019 03:53, mhysnm1...@gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively edit > a content of a variable at the command line. I do not want to use GUI at > all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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] is this doable
On 31/05/2019 20:41, nathan tech wrote: > Is it possible, in python, to store a running task id in the registry? >From mention of the registry I assume you are running Windows? There is no registry on Unixlike systems. The answer in either case is yes since a task ID is just a number. However if the task ends the number will still be stored, so checking whether the ID refers to a live task is the trickier bit. > I might be using the complete wrong terms here, because I'm only used to > doing this with a specific language, Is the language C/C++? If so you may know the OS API calls needed and you could access those directly from Python using ctypes That might make your job more familiar and easier. Alternatively, there are OS shell commands that might work that you can call from subprocess. The os module has a bunch of functions that might help but many of them are Unix only or behave differently on Windows/Unix so you will need to study the documentation and probably experiment a bit. Things like waitpid() might work for example, but I haven't tried. Personally I'd use the shell command approach for Unix but no idea what I'd use for Windows. I suspect there may be a dynamic registry entry you can read using the winreg registry module. 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
Re: [Tutor] File extension against File content
On 31/05/2019 11:03, Sunil Tech wrote: > Hi Tutor, > > Is there any way that I can actually programmatically compare the file > extension with its content? For images the standard library offers imghdr I'm not sure how reliable or accurate it is but it claims to identify a dozen or so of the most common formats. For sound there is the similar sndhdr module which tries to do the same thing for audio files. There are also modules for reading exif data from image files and ID tags from audio files. The existence of such tags can indicate that the file is of the appropriate type. Combine with try/except to test a file... > This is because we can manually save the file in one extension and later > rename the file extension to some other. And indeed store the file with no extension at all. -- 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] Query about python recipies for practices
On 27/05/2019 23:44, Mats Wichmann wrote: > and, if you meant problems for practicing... there are a lot of those > around. Most courses have problems for you to solve, naturally, and a > brief hunt around turned up some sites like these that are not courseware: And don't forget the wonderful Python Challenge http://www.pythonchallenge.com/ Now up to 33 levels! -- 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] Fwd: Re: Setting Command Line Arguments in IDLE
Oops, Forgot to include the list! Forwarded Message Subject:Re: Setting Command Line Arguments in IDLE Date: Sun, 26 May 2019 09:03:36 +0100 From: Alan Gauld To: Richard Damon On 26/05/2019 02:55, Richard Damon wrote: > I am working on a python script that will be provided arguments when run > from the system command line. Is there any place in IDLE to provide > equivalent arguments for testing while developing in IDLE? > There used to be a dialog for that but in the latest version of IDLE I can't find it. I wonder when it disappeared and why? The best place to ask is probably on the IDLE-dev list. It can be found here: https://mail.python.org/mailman/listinfo/idle-dev > Is there any way to define the working directory for the program, os.getcwd() # read current dir os.chdir() # set current dir > If not, is there an easy way to detect that I am running in IDLE so I > can fake the command line arguments when testing? Not that I'm aware, but the idle-dev gurus may have some ideas. -- 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] Fwd: RE: regular expressions query
Forwarding to the list, plase use reply-all or reply-list when responding to list mails. Alan G. Forwarded Message Subject:RE: [Tutor] regular expressions query Date: Fri, 24 May 2019 20:10:48 +1000 From: mhysnm1...@gmail.com To: 'Alan Gauld' Allan, I have gone back to the drawing board as I found to many problems with the approach I was using. As the original data has multiple spaces between words. I want to find unique phrases in the strings such as "Hello World" regardless of the number of spaces that might be in the string. I have used the following lines of code which finds the number of unique complete strings. transaction = [item for item, count in collections.Counter(narration).items() if count > 1] none-dup-narration = [item for item, count in collections.Counter(narration).items() if count < 2] So I end up with two lists one containing complete unique strings with more than one occurrence and another with only one. As there is common words in the none-dup-narration list of strings. I am trying to find a method of extracting this information. I am still reading collections as this could help. But wanted to understand if you can inject variables into the pattern of regular expression which was the intent of the original question. Each time the regular expression is executed, a different word would be in the pattern. In Python 3.7, I want to understand Unions and Maps. I have read information on this in different places and still don't understand why, how and when you would use them. Something else I have been wondering. Goal here is to grow my knowledge in programming. # end if # end for print (count) # end for input () # end for -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, 24 May 2019 7:41 PM To: tutor@python.org Subject: Re: [Tutor] regular expressions query On 24/05/2019 01:15, mhysnm1...@gmail.com wrote: > Below I am just providing the example of what I want to achieve, not > the original strings that I will be using the regular expression against. While I'm sure you understand what you want I'm not sure I do. Can you be more precise? > The original strings could have: > "Hello world" > "hello World everyone" > "hello everyone" > "hello world and friends" > I have a string which is "hello world" which I want to identify by > using regular expression how many times: > > * "hello" occurs on its own. Define "on its own" Is the answer for the strings above 4? Or is it 1 (ie once without an accompanying world)? > * "Hello world" occurs in the list of strings regardless of the number > of white spaces. I assume you mean the answer above should be 3? Now for each scenario how do we treat "helloworldeveryone"? "hello to the world" "world, hello" "hello, world" > Splitting the string into an array ['hello', 'world'] and then > re-joining it together and using a loop to move through the strings > does not provide the information I want. So I was wondering if this is > possible via regular expressions matching? It is probably possible by splitting the strings and searching, or even just using multiple standard string searches. But regex is possible too. A lot depends on the complexity of your real problem statement, rather than the hello world example you've given. I suspect the real case will be trickier and therefore more likely to need a regex. > Modifying the original string is one option. But I was wondering if > this could be done? I'm not sure what you have in mind. For searching purposes you shouldn't need to modify the original string. (Of course Python strings are immutable so technically you can never modify a string, but in practice you can achieve the same effect.) -- 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] I'm having a small problem with my code
Forwarding to the list. Always use Reply-All or Reply-List when responding to the list. Otherwise it only goes to the member who posted. Alan G. On 24/05/2019 10:20, David Lifschitz wrote: > Hi. > I'm learning the processes of python so I'm trying to figure out how > to sort it manually. > > On Fri, May 24, 2019 at 3:00 AM Alan Gauld via Tutor <mailto:tutor@python.org>> wrote: > > On 23/05/2019 13:16, David Lifschitz wrote: > > > The next job of the code is to sort the list of numbers that > were inputted > > in an ascending fashion. > > You are aware that Python lists have a built in sort method? > It will be more efficient than anything you can create > yourself in Python. Assuming you know that and decided > to write a sort routine for fun > > > > There is no error from the code, however, when I run the code > the first > > inputted number stays in its place and doesn't get sorted with > the rest of > > the numbers. > > Any advice??? > > Yes, see below: > > > emptyList = [] > > This is a terrible name since it becomes misleading the > instant you put anything into it! > > number_list or similar would be more accurate. > Or even just 'data' > > > nOFN = int(input("how many numbers do you want to sort: ")) > > > > for x in range(nOFN): > >?? ?? ??number1 = int(input("input number: ")) > >?? ?? ??emptyList.append(number1) > > You could have used a list comprehension: > > emptyList = [int(input("input number: ")) for n in range(nOFN)] > > Now onto the problem sort code > > > firstElement = emptyList[0] > > Why did you do this? You never refer to firstElement again... > > > n = len(emptyList) > > for j in range(1, n): > >?? ?? ??if emptyList[j-1] > emptyList[j]: > >?? ?? ?? ?? ??(emptyList[j-1], emptyList[j]) = (emptyList[j], > emptyList[j-1]) > > Consider the first case, j=1 > > If the first element is greater than the second > you swap them. Otherwise you leave them in place. > > The loop now considers elements 2 and 3. > If 2 >3 you reverse them, otherwise move on. > But if element 3 is less than element 1 you never > go back to move it to the top. > > Consider this example - [3,2,1] > > 1st iteration?? ??-> 2,3,1 > 2nd iteration?? ??-> 2,1,3 > > Loop ends. > But you never swapped 1 and 2 after(or during) the last iteration. > > Your sort routine is fundamentally flawed. You need a rethink. > But not too much because the built in sort will nearly always be > preferred! > > Incidentally, creating working sort algorithms is one of the > hardest things to get right in computing. It is one of > those things that can seem right then one specific pattern > will break it. > > -- > 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 <mailto:Tutor@python.org> > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- > Sent from an email account -- 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] regular expressions query
On 24/05/2019 01:15, mhysnm1...@gmail.com wrote: > Below I am just providing the example of what I want to achieve, not the > original strings that I will be using the regular expression against. While I'm sure you understand what you want I'm not sure I do. Can you be more precise? > The original strings could have: > "Hello world" > "hello World everyone" > "hello everyone" > "hello world and friends" > I have a string which is "hello world" which I want to identify by using > regular expression how many times: > > * "hello" occurs on its own. Define "on its own" Is the answer for the strings above 4? Or is it 1 (ie once without an accompanying world)? > * "Hello world" occurs in the list of strings regardless of the number > of white spaces. I assume you mean the answer above should be 3? Now for each scenario how do we treat "helloworldeveryone"? "hello to the world" "world, hello" "hello, world" > Splitting the string into an array ['hello', 'world'] and then re-joining it > together and using a loop to move through the strings does not provide the > information I want. So I was wondering if this is possible via regular > expressions matching? It is probably possible by splitting the strings and searching, or even just using multiple standard string searches. But regex is possible too. A lot depends on the complexity of your real problem statement, rather than the hello world example you've given. I suspect the real case will be trickier and therefore more likely to need a regex. > Modifying the original string is one option. But I was wondering if this > could be done? I'm not sure what you have in mind. For searching purposes you shouldn't need to modify the original string. (Of course Python strings are immutable so technically you can never modify a string, but in practice you can achieve the same effect.) -- 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] I'm having a small problem with my code
On 23/05/2019 13:16, David Lifschitz wrote: > The next job of the code is to sort the list of numbers that were inputted > in an ascending fashion. You are aware that Python lists have a built in sort method? It will be more efficient than anything you can create yourself in Python. Assuming you know that and decided to write a sort routine for fun > There is no error from the code, however, when I run the code the first > inputted number stays in its place and doesn't get sorted with the rest of > the numbers. > Any advice??? Yes, see below: > emptyList = [] This is a terrible name since it becomes misleading the instant you put anything into it! number_list or similar would be more accurate. Or even just 'data' > nOFN = int(input("how many numbers do you want to sort: ")) > > for x in range(nOFN): > number1 = int(input("input number: ")) > emptyList.append(number1) You could have used a list comprehension: emptyList = [int(input("input number: ")) for n in range(nOFN)] Now onto the problem sort code > firstElement = emptyList[0] Why did you do this? You never refer to firstElement again... > n = len(emptyList) > for j in range(1, n): > if emptyList[j-1] > emptyList[j]: > (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) Consider the first case, j=1 If the first element is greater than the second you swap them. Otherwise you leave them in place. The loop now considers elements 2 and 3. If 2 >3 you reverse them, otherwise move on. But if element 3 is less than element 1 you never go back to move it to the top. Consider this example - [3,2,1] 1st iteration -> 2,3,1 2nd iteration -> 2,1,3 Loop ends. But you never swapped 1 and 2 after(or during) the last iteration. Your sort routine is fundamentally flawed. You need a rethink. But not too much because the built in sort will nearly always be preferred! Incidentally, creating working sort algorithms is one of the hardest things to get right in computing. It is one of those things that can seem right then one specific pattern will break it. -- 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] Learning something new.
On 23/05/2019 08:38, Jessica Rabbit wrote: > Hello, my name is Jessica and I hope I found the right e mail to help me > learn of to write in code and eventually learn to hack. Hi Jessica and welcome to the tutor list. > know how to do something that my father can. If this email is read by > someone who is willing to help, *teach*, and *yell* at me (figuratively) > please let me know. The way the list works is that people ask questions of the list membership, who then answer them. The more specific the question the more specific will be the answer. Include any code you have written and any error messages in the message body, do not use attachments, especially not binary ones like screenshots, since the server will drop them. It also sometimes helps to know your Python version and your OS. Have fun, we look forward to your questions and we try not to yell at anyone. -- 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] A Python Newbie Requesting help with Lambdas, Filters, Maps, and Built-in Functions to solve problems for a List of Songs
On 21/05/2019 14:30, Rahul Alawani wrote: > # 4. The longest song title in the entire songs list > def longest_title (songmix): > temp = max(songmix, key=lambda num: len(max(num['titles']))) > return list(map(lambda x: max(songmix, key=lambda num: > len(max(num['titles'])))['artist'], > filter(lambda title: max(len('title')), temp))) > print (longest_title (songmix)) There are several problems here. The first and the one that the error is reporting lies in the call to max() max() expects a sequence of values and returns the biggest. But you are passing len('title') which is a single integer value. Hence the error about int not being iterable... But there is another problem with len('title') That will give the result 5 each time since its taking the length of the string 'title'. I suspect you wanted len(title) without the quotes? Finally max() applied to strings returns the highest in lexical value - ie 'b' is higher than 'a'... So >>> max(['a','aa','','aaa','baa']) 'baa' >>> Which I don't think is what you want? However, there is also a problem with the filter call. filter expects a function that returns a boolean result. It then applies that test to each member of its sequence parameter and returns a list containing only those members for which the test was true. In your case the lambda would return a number - the result of max. All non zero numbers are treated as true by Python so you will get back the same list you started with since all the max calls will (probably) return >0 results. Also I notice you are using key functions to max but that is hardly ever necessary since max will return the largest item regardless of the order. It only needs a key if there are multiple items of the same size and you want to return a specific one of those. Sorting will ensure the right value is picked. But in your case you don't need any sorting. I know you want to practice lambdas but I think initially it would be better for you to practice with map and filter using small named functions. You can then convert them to lambdas later. So for example(untested, so probably buggy): def title_with_size(song): return (len(song), song) def song_lengths(singer): return map(title_with_size, singer['titles']) def max_title(singer): return max(song_lengths(singer)) def longest_title (songmix): titles = map(max_title, songmix) return max(titles)[1] Removing the lambdas makes everything easier to read and therefore easier to understand. Once you understand how the map/filter etc work you can decide where the named functions can be replaced by lambdas. As it stands your code is a fine demonstration of why lambdas are relatively rarely used! -- 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] Case Insensitive Globing
On 20/05/2019 09:49, Alan Gauld via Tutor wrote: > On 19/05/2019 19:19, Alan Gauld via Tutor wrote: > ... >> So I always end up with two copies - the original file and the >> edited version. > I forgot I had moved all my photos onto my NAS box > and then mounted that in my pictures library under > Windows 10. On closer study it was slightly more complex. In fact, the errant editor is actually a Linux utility program that I only rarely use but which uses the same NAS folder as is used by the Windows box(obviously, that's why it's on the NAS!) Now, because I do 90% of my photo editing on Windows (Affinity Photo really is superb!), I only noticed the duplicate file names there and so assumed it was a Windows thing. So,to summarize: I copy the files from the camera to the (Linux based) NAS. I edit the file on my Linux PC (which saves with the .jpg extension) Then I open the folder for serious editing on Windows and see two filenames differing only in the case of the extension. No NTFS involved. Just memory loss from a befuddled senior... :-( -- 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] Case Insensitive Globing
On 19/05/2019 19:19, Alan Gauld via Tutor wrote: > Hmm, odd. My NTFS filesystems on Windows all appear to be case > sensitive. For example I have a photo editor that saves its files > with a jpg extension but the files from my camera all end in JPG. > So I always end up with two copies - the original file and the > edited version. Amazing how you forget the simple things over time. I forgot I had moved all my photos onto my NAS box and then mounted that in my pictures library under Windows 10. Because you access them via the Library feature I'd completely forgotten they were on the NAS. So the filesystem is really ext3 and I'm assuming that's why the double names exist... -- 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] Case Insensitive Globing
On 19/05/2019 01:37, Steven D'Aprano wrote: > That's not quite right -- case sensitivity of the OS isn't important, > case sensitivity of the *file system* is. And the standard file system > on Mac OS, HFS+, defaults to case-preserving but case-insensitive. > > (There is an option to turn case-sensitivity off, but hardly anyone uses > it because too many applications break.) Oops, my mistake. Almost all my MacOS knowledge is based on my ancient iBook. Having had a look, it appears it's formatted in UFS rather than HFS+ so I assume I must have done something to force that when I first installed the system back in 2001... It certainly appears to be case sensitive but life is too short for me to do extensive testing right now! > Fun fact: even NTFS supports a case-sensitive mode! But again, hardly > anyone uses it. Hmm, odd. My NTFS filesystems on Windows all appear to be case sensitive. For example I have a photo editor that saves its files with a jpg extension but the files from my camera all end in JPG. So I always end up with two copies - the original file and the edited version. I'm not aware of having done anything to cause that. More investigation required I think... -- 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] Two Scripts, Same Commands, One Works, One Doesn't
On 19/05/2019 04:58, DL Neil wrote: > last time I used the term "bomb", I'm guessing that "abend" wouldn't > pass muster either... Gosh, I haven't seen a reference to abend in 20 years. I'd almost managed to erase the memory... :-) -- 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] How arguments to the super() function works?
On 18/05/2019 17:21, Arup Rakshit wrote: > class Role(db.Model): > > def __init__(self, **kwargs): > super(Role, self).__init__(**kwargs) > > Here, why super(Role, self).__init__(**kwargs) is used instead > of super().__init__(**kwargs) ? I suspect you are reading an older tutorial. It used to be the case that you had to provide the class and instance values to super() but that's no longer needed in the latest versions. -- 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