From hjabeen2 at gmail.com Sat Oct 1 19:11:05 2022 From: hjabeen2 at gmail.com (huma jabeen) Date: Sun, 2 Oct 2022 04:11:05 +0500 Subject: [Tutor] Need help in code Message-ID: <0306BB2B-3E09-4D58-AE37-37E6AA729CDF@gmail.com> Dear tutor, I am trying to do classification of autism using multiple atlases, but I really need help about how to apply multiple atlases to find a good accuracy. Regards Huma From alan.gauld at yahoo.co.uk Sun Oct 2 04:32:59 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 2 Oct 2022 09:32:59 +0100 Subject: [Tutor] Need help in code In-Reply-To: <0306BB2B-3E09-4D58-AE37-37E6AA729CDF@gmail.com> References: <0306BB2B-3E09-4D58-AE37-37E6AA729CDF@gmail.com> Message-ID: On 02/10/2022 00:11, huma jabeen wrote: > Dear tutor, > I am trying to do classification of autism using multiple atlases, > but I really need help about how to apply multiple atlases to find a good accuracy. You need to undestand that we are willing to hep but know nothing about your problem. We don't understand the language or concepts so you need to explain them. I know what autism is(roughly) but I don't know what an atlas is in this context. (To me it is a book full of maps.) And I don't know how you classify autism (in any way) and especially how you relate it to "an atlas". Can you give a few examples of the type of data you are using. What does an autism record look like? What is an atlas? How are the two related? My first guess at a solution is that it will either feature a dictionary or a database. It might involve classes too but I can't tell at this stage. -- 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 From mertoner at yahoo.ca Mon Oct 3 06:30:13 2022 From: mertoner at yahoo.ca (Mert Oner) Date: Mon, 3 Oct 2022 10:30:13 +0000 (UTC) Subject: [Tutor] OOP, classes , References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> Message-ID: <1434118723.2077919.1664793013726@mail.yahoo.com> Hello? tutors, I am studying python with the help of a book called ' Learn Python3 The Hard Way' by Zed Shaw. I am having a really hard time understanding OOP and classes, grandparent, parent and child concepts in classes and Inheritance vs Comparison. Could you please help me out in layman terms??Or maybe some easy to understand study metarials. I really appreciate you guys, taking precious time from your daily life to help us beginners.? Peace! Mert Sage Sent from Yahoo Mail on Android From leamhall at gmail.com Mon Oct 3 14:10:26 2022 From: leamhall at gmail.com (Leam Hall) Date: Mon, 3 Oct 2022 13:10:26 -0500 Subject: [Tutor] OOP, classes , In-Reply-To: <1434118723.2077919.1664793013726@mail.yahoo.com> References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> Message-ID: <40b8d5df-ae57-d66a-77d5-af890c9b6de0@gmail.com> On 10/3/22 05:30, Mert Oner via Tutor wrote: > Hello? tutors, > I am studying python with the help of a book called ' Learn Python3 The Hard Way' by Zed Shaw. I am having a really hard time understanding OOP and classes, grandparent, parent and child concepts in classes and Inheritance vs Comparison. > Could you please help me out in layman terms??Or maybe some easy to understand study metarials. > I really appreciate you guys, taking precious time from your daily life to help us beginners. > Peace! > Mert Sage > Section 9.5 might help: https://docs.python.org/3/tutorial/classes.html Zed's books are usually pretty good. Are you doing the coding with each chapter? You can also put some of the code in a reply to the email. But not an attachment or an image. Leam -- Automation Engineer (reuel.net/resume) Scribe: The Domici War (domiciwar.net) General Ne'er-do-well (github.com/LeamHall) From mats at wichmann.us Mon Oct 3 14:22:04 2022 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 3 Oct 2022 12:22:04 -0600 Subject: [Tutor] OOP, classes , In-Reply-To: <1434118723.2077919.1664793013726@mail.yahoo.com> References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> Message-ID: <0c05eee0-7815-8c4c-a40c-c66c08cc70b7@wichmann.us> On 10/3/22 04:30, Mert Oner via Tutor wrote: > Hello? tutors, > I am studying python with the help of a book called ' Learn Python3 The Hard Way' by Zed Shaw. I am having a really hard time understanding OOP and classes, grandparent, parent and child concepts in classes and Inheritance vs Comparison. > Could you please help me out in layman terms??Or maybe some easy to understand study metarials. > I really appreciate you guys, taking precious time from your daily life to help us beginners. > Peace! Boy, what an easy question :) I bet the book will descriibe this somewhere... and the author will have spent lots of time thinking about that, so will probably have a more comprehensive description than we can fire off here. A Python class definition is your opportunity to define a new object type (data type, if you prefer), analagous to predefined types like integer, or list, or dictionary, which are other datatypes. It could be as simple as a couple of data elements (modern Python has a shorthand notation for such, called dataclasses), or it can add in functions which operate on the internals of that datatype - called methods. Inheritance simply means you aren't writing your whole custom data type from scratch, you reuse parts of an existing class. This relationship is characterized by the term "is a", e.g if you start with a UserDict from the Python standard library and write your own class MyDict, you can say MyDict Is A UserDict - but with special features. class MyDict(UserDict): .... Composition is where one class can contain an instance of another. This relationship is often expressed as Has A. You actually do this a lot without knowing it: class MyClass: def __init__(self): self.data = {} The "data" attribute is initialized to an empty dictionary - so it "has a" dict. Beyond that maybe it's better if you ask actual questions? From alan.gauld at yahoo.co.uk Mon Oct 3 14:46:38 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 3 Oct 2022 19:46:38 +0100 Subject: [Tutor] OOP, classes , In-Reply-To: <1434118723.2077919.1664793013726@mail.yahoo.com> References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> Message-ID: On 03/10/2022 11:30, Mert Oner via Tutor wrote: > I am studying python with the help of a book called ' Learn Python3 > The Hard Way' by Zed Shaw. I am having a really hard time understanding > OOP and classes, grandparent, parent and child concepts in classes and > Inheritance vs Comparison. OOP and classes are two related but separate issues. Many people find them confusing so don't worry about it, you are not alone. OOP is a style of programming where you organise the program as a set of objects that communicate by sending messages to each other. This is a very different way of thinking about program structure compared to the traditional hierarchy of function calls acting on some central data stores. Each object can be thought of as a little independant program processing messages from the other programs(ie objects) around it. Each object has its own internal data and the only way to read or modify that data is by sending a message to the object. Each object has its own method of processing the messages it receives. Many different objects can be sent the same message but each object type will respond differently - they have different methods of handling the same message. This is called polymorphism. OOP is often implemented within a language by providing a structure called a class. Such a language is often called Object Based or Object Oriented. The difference being whether it supports inheritance(OOPL) or not (OBPL). You can do OOP in either (or neither!). A class is a container structure which can hold both data and functions. Functions can be connected to messages (eg by having the same name) in which case they are the object's methods. Not every function in a class needs to be a method (private internal functions used by the methods for example). A class is effectively a data type definition (like int, float, string etc) and it is used to create instances of the type which are the actual objects used in OOP. But objects can be used in non-OOP programs too when they become simple instances of a user defined type. Then they can be used alongside the built-in types to create hybrid style programs. Hybrid programs are by far the most common kind seen in the real world. Pure OOP is only worthwhile for certain problem types and sizes(generally big complex problems related to real-world objects.) We use classes and objects to model many different types of object. They can be real-world physical entities such as cars, vehicles, animals etc They can be real-world abstract concepts such as shapes, bank-accounts, calendars etc They can also be computer science or math data types such as a bag or stack or directed graph etc. Any concept which has both data (or "state" - alive, dead, active, visible, hidden etc) as well as operations that alter (or read) that data can be an object. That's kind of a layman's description of OOP and Classes/objects. You need to ask more specific questions for more specific answers. I mentioned inheritance but deliberately didn't elaborate because it brings a whole extra set of complications. Best to grasp the core concepts first. And then, of course, you need to find out how python chooses to implement all these core ideas. For a different approach (with code/examples)you can try reading the OOP topic in my tutorial(see link 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 From samiraeastcoast at gmail.com Mon Oct 3 16:14:20 2022 From: samiraeastcoast at gmail.com (samira khoda) Date: Mon, 3 Oct 2022 17:14:20 -0300 Subject: [Tutor] Python text file read/compare In-Reply-To: References: Message-ID: Hi Thank you very much for your feedback.I just started working on the file again. Unfortunately I am still not getting anywhere with the modifications I made to the codes. I don't know when I can create the new file and write to it and close it. Basically as I mentioned before I need to find where the timestamps jump down to zero or the lowest number then start time so I can split the file and make a new file.***For your information the timestamps are in milliseconds*** Thanks in advance for your help. Please let me know if you need more clarification to assist me. kyk=( 'hfx-test-.txt' , 'r') line_numbers=[] i=0 current_time="" count=1 for line in kyk: if i < 488: i=0 line_numbers.append(current_time) current_time="" else: current_time+=line i+=1 **************this is where It does not write to the file***** *opf=open("kyk_txt_new_file.txt","w")* * if current_time load sourceFile (a copy of the raw data file) line_number = 0 start_time = 0 split_numbers = [] not_done = true while(not_done): ->read line from sourcefile ->split line on ',' ->convert last item on line to unsigned long and store in current_time if current_time < start_time: split_numbers.add(line_number) start_time = current_time if end_of_file: not_done = false; for s in split_numbers: ->create newfile for i = 0, i < s, i++: ->read line from sourcefile ->write line to newfile ->close newfile ->Close sourcefile On Thu, Sep 29, 2022 at 6:46 PM Cameron Simpson wrote: > On 29Sep2022 15:09, samira khoda wrote: > >Below is the snapshot of the data. Basically I need to find where the > >timestamps jump down to zero or the lowest number so I can split the file. > >For your information the timestamps are in milliseconds on the last > column. > > > >[image: image.png] > > This list strips nontext attachments. Please just paste a few lines of > example data directly into your message. > > >*And below is the code I wrote but nothing came out of it. > > I'll make a few remarks about the code inline below. > > >with open('hfx-test-.txt') as kyk: > > op='' > > start=0 > > count=1 > > for x in kyk.read().split("\n"): > > This reads all the data and then breaks it up on newlines. For big files > that can be expensive. Text files are iterables, yielding lines, so you > can write this: > > for line in kyk: > x = line.rstrip() # this removes the newline > > > if(x=='0'): > > You don't need brackets in Python if-statements: > > if x =='0': > > > if (start==1): > > If you don't get any files, presumably `start` is never equal to `1`. > > > with open(str(count)+ '.txt', 'w') as opf: > > opf.write(op) > > opf.close() > > The `with open()` form does the `close()` for you, you do not need the > `opf.close()`. > > > op='' > > count+=1 > > and I'd have these lines outside the `with` i.e. less indented. > > > else: > > start=1 > > > > else: > > if(op==''): > > op = x > > else: > > op= op+ '\n' + x > > It looks like you're accumulating the `x` values as one big string. > That will work, but it would be more common to accumulate a list< eg: > > Up the top: ops = [] # an empty list. > Down here: ops.append(x) > In the `opf` with-statement: > > for op in ops: > print(op, file=opf) > ops = [] # we have writtne them, reset the list to empty > > >kyk.close() > > As with `opf`, the with statement closes the file for you. You don't > need this line. > > Finally, if your code's not doing what you intend, then _either_ the > if-statements have the wrong tests _or_ the variables you're testing do > not have the values you expect. > > Put some `print()` calls in the code to see what's going on. Examples: > > for line in kyk: > x = line.rstrip() # this removes the newline > print("x =", repr(x)) > print("start =", start, "count =", count, "op = ", repr(op)) > > That should show you the lines of data as you read them, and the values > of the variable you're testing. Hopefully that should show you when > things go wrong, and lead you to a fix. > > The `repr(x)` expression prints a representation of the value, which is > particularly handy for things like strings. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From threesomequarks at proton.me Mon Oct 3 14:24:11 2022 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Mon, 03 Oct 2022 18:24:11 +0000 Subject: [Tutor] OOP, classes , In-Reply-To: <1434118723.2077919.1664793013726@mail.yahoo.com> References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> Message-ID: Mert, could it be the hard way is not the best way for you to start? Your book may not be at the best level for you at this time. Having people here explain in detail here may be overkill. It would help if you asked something more specific. I will try and answer partially from a Python perspective. Object Oriented Programming is not done the same way in different languages or sometimes at all. I think I view a class as a sort of recipe. You have a recipe for a cake that may list some ingredients and methods. You do not yet have a specific soup or cake, just a recipe and that is called a class. You can make an object using a recipe and now you have a cake or whatever. You can make many objects of the same kind, but that are also different in that the recipe may say to use a cup of any kind of jam, or optionally do some extra preparation at the end like adding icing on a cake. Once you have a recipe, you might want to make something similar enough so that instead of redoing the entire recipe, you simply say that the recipe inherits from another recipe EXCEPT for some changes such as additions or somewhat different methods. You can, for example, have a recipe for making bread that requires flour and yeast and so on, and another recipe for making French Toast that incorporates making the bread first and slicing it then adds eggs and so on. The recipe for French Toast can Inherit from the recipe for Bread and you can talk about Bread being the parent and French Toast being the child. You can play with analogies all you want and come up with oher concepts but the idea in general is that a class in Python contains a combination of things in it that range from storage of something to functions that can manipulate the contents and so on. It can get much more complex but in some ways is a bit like a recipe. Classes do not in some ways normally exist unless used as a prototype to make objects. I mean, yes, they exist, but mainly as an idea. Objects are instantiations of the idea with some fine-tuning. If a class is declared as having one (or more) other classes as parents, then any object you create from such a class inherits attributes from not only what the class declares, but what various parents and grandparents have declared. It is a hierarchy of a kind but you also can make a sort of tree of ancestors but probably rarely need to. There are rules galore but in general, some things inherited can be changed and tailored. All the above is part of the evolution of programming languages so you can package related data and procedures together but also a different paradigm in how you think about solving problems in an object-oriented way. So starting with a somewhat easier book and then learning the hard one may be best. Or get one-on-one tutoring from a live human near you. You need to explain where "Comparison" comes into your question. Are we talking about comparing classes or objects or asking if an object has a particular parent or about telling the system how to compare an object with another instance and declare which order they are in, or something entirely different? - 3 Sent with Proton Mail secure email. ------- Original Message ------- On Monday, October 3rd, 2022 at 6:30 AM, Mert Oner via Tutor wrote: > Hello tutors, > I am studying python with the help of a book called ' Learn Python3 The Hard Way' by Zed Shaw. I am having a really hard time understanding OOP and classes, grandparent, parent and child concepts in classes and Inheritance vs Comparison. > Could you please help me out in layman terms? Or maybe some easy to understand study metarials. > I really appreciate you guys, taking precious time from your daily life to help us beginners. > Peace! > Mert Sage > > Sent from Yahoo Mail on Android > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Tue Oct 4 04:40:34 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 4 Oct 2022 09:40:34 +0100 Subject: [Tutor] Python text file read/compare In-Reply-To: References: Message-ID: On 03/10/2022 21:14, samira khoda wrote: > Thank you very much for your feedback.I just started working on the file > again. Unfortunately I am still not getting anywhere with the modifications > I made to the codes. I don't know when I can create the new file and write > to it and close it. Can you write a program to simply split your input file(s) after every N lines? ie something like: open the input open output linecount = 0 for line in input linecount += 1 if linecount > N close output open new output linecount = 1 write line to output If you can get that to work then you can replace the line if linecount > N with if zerotimestamp(line) And write a function to determine if the timestamp indicates a new file is needed. But get the basic structure in place first, don't try to solve both problems at once. > Basically as I mentioned before I need to find where > the timestamps jump down to zero or the lowest number then start time so I > can split the file and make a new file.***For your information the > timestamps are in milliseconds*** I think you will need to explain how that works. Your sample data below is not clear. You need to show us a sample where the timestamp is not zero then one that is. And explain which field defines that. > > kyk=( 'hfx-test-.txt' , 'r') Not that kyk is just a tuple of 2 strings. > > line_numbers=[] > i=0 > current_time="" > count=1 > > for line in kyk: So this will return the 2 strings 'hfx-test-.txt' and 'r' > if i < 488: > i=0 > line_numbers.append(current_time) > current_time="" > else: > current_time+=line > i+=1 This doesn't make much sense to me. Where does the magic number 488 fit in? First time through the loop i will always be zero so you will always start the list with an empty string in line_numbers. (BTW line_numbers is a very misleading name since it appears to be a list of times?) And because i is always 0 you never go into the else part so it never gets increased. So the for loop executes twice storing 2 empty strings in the line_numbers list. Even if you opened the file and iterated over it you would still just get a blank string for each line in the file. > **************this is where It does not write to the file***** > > *opf=open("kyk_txt_new_file.txt","w")* > > * if current_time > * splitlines(True).add(line_number);* > > * opf.write("this is the timestamps after the line splits.")* > > * kyk.close()* And this makes even less sense. where is start_time coming from? what is splitlines()? Where is it defined? And the only thing you write to the file is the message, you never write any data? And you close kyk but not opf? > *Here are the first and last few lines of my text file data. * > > time stamps > -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, *488* > -1.75, 1.12, 10.39, -0.10, -0.01, -0.01, 23.20, *521* > > 9.65, -1.31, -1.95, -0.11, -0.06, -0.02, 22.05, *15339436* > 9.56, -1.32, -1.97, -0.10, -0.00, -0.01, 22.05, *15339495* Does a zero timestamp appear in any of those lines? This is just a set of numbers. Are they all timestamps? If so, why is the last number much bigger than the rest? And is the 488 at the end of line 1 significant in being the same as the magic number in your code? > > *I was also provided with the * pseudocode * below which I am trying to > follow if that helps to guide me along the way.* The pseudo code makes some sense - although a for loop would be simpler than the while. And it does not appear to be doing what you describe as the required task. But your code is not even close to what it does. > -> load sourceFile (a copy of the raw data file) I think that should say open sourcefile, not load... > line_number = 0 > start_time = 0 > split_numbers = [] > not_done = true > > while(not_done): > ->read line from sourcefile > ->split line on ',' > ->convert last item on line to unsigned long and store in > current_time > > if current_time < start_time: > split_numbers.add(line_number) > start_time = current_time > if end_of_file: > not_done = false; > > for s in split_numbers: > ->create newfile > for i = 0, i < s, i++: > ->read line from sourcefile > ->write line to newfile > > ->close newfile > > ->Close sourcefile -- 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 From threesomequarks at proton.me Wed Oct 5 01:29:39 2022 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Wed, 05 Oct 2022 05:29:39 +0000 Subject: [Tutor] OOP, classes , In-Reply-To: References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> Message-ID: <98Re02KpuTszA34kAeYm0E8kvPXfOUcZsbQPqJ-qVtObHa24YAyzWSfzu7fHgCH_2LW_XNgcZvKyn_vh_n_tTT8oMj7bxmT5eycqrKVvDCM=@proton.me> Did I miss any comments or replies or updates or even a thank you from the one asking a rather vague and extremely broad question? Is that normal here? I naively expect some give and take. With no further feedback, I won't participate in what may never have been someone wanting specific help. Are there spammers here? 3 Sent from Proton Mail mobile -------- Original Message -------- On Oct 3, 2022, 2:46 PM, Alan Gauld via Tutor wrote: > On 03/10/2022 11:30, Mert Oner via Tutor wrote: > I am studying python with the help of a book called ' Learn Python3 > The Hard Way' by Zed Shaw. I am having a really hard time understanding > OOP and classes, grandparent, parent and child concepts in classes and > Inheritance vs Comparison. OOP and classes are two related but separate issues. Many people find them confusing so don't worry about it, you are not alone. OOP is a style of programming where you organise the program as a set of objects that communicate by sending messages to each other. This is a very different way of thinking about program structure compared to the traditional hierarchy of function calls acting on some central data stores. Each object can be thought of as a little independant program processing messages from the other programs(ie objects) around it. Each object has its own internal data and the only way to read or modify that data is by sending a message to the object. Each object has its own method of processing the messages it receives. Many different objects can be sent the same message but each object type will respond differently - they have different methods of handling the same message. This is called polymorphism. OOP is often implemented within a language by providing a structure called a class. Such a language is often called Object Based or Object Oriented. The difference being whether it supports inheritance(OOPL) or not (OBPL). You can do OOP in either (or neither!). A class is a container structure which can hold both data and functions. Functions can be connected to messages (eg by having the same name) in which case they are the object's methods. Not every function in a class needs to be a method (private internal functions used by the methods for example). A class is effectively a data type definition (like int, float, string etc) and it is used to create instances of the type which are the actual objects used in OOP. But objects can be used in non-OOP programs too when they become simple instances of a user defined type. Then they can be used alongside the built-in types to create hybrid style programs. Hybrid programs are by far the most common kind seen in the real world. Pure OOP is only worthwhile for certain problem types and sizes(generally big complex problems related to real-world objects.) We use classes and objects to model many different types of object. They can be real-world physical entities such as cars, vehicles, animals etc They can be real-world abstract concepts such as shapes, bank-accounts, calendars etc They can also be computer science or math data types such as a bag or stack or directed graph etc. Any concept which has both data (or "state" - alive, dead, active, visible, hidden etc) as well as operations that alter (or read) that data can be an object. That's kind of a layman's description of OOP and Classes/objects. You need to ask more specific questions for more specific answers. I mentioned inheritance but deliberately didn't elaborate because it brings a whole extra set of complications. Best to grasp the core concepts first. And then, of course, you need to find out how python chooses to implement all these core ideas. For a different approach (with code/examples)you can try reading the OOP topic in my tutorial(see link 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 at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed Oct 5 04:21:25 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 5 Oct 2022 09:21:25 +0100 Subject: [Tutor] OOP, classes , In-Reply-To: <98Re02KpuTszA34kAeYm0E8kvPXfOUcZsbQPqJ-qVtObHa24YAyzWSfzu7fHgCH_2LW_XNgcZvKyn_vh_n_tTT8oMj7bxmT5eycqrKVvDCM=@proton.me> References: <1434118723.2077919.1664793013726.ref@mail.yahoo.com> <1434118723.2077919.1664793013726@mail.yahoo.com> <98Re02KpuTszA34kAeYm0E8kvPXfOUcZsbQPqJ-qVtObHa24YAyzWSfzu7fHgCH_2LW_XNgcZvKyn_vh_n_tTT8oMj7bxmT5eycqrKVvDCM=@proton.me> Message-ID: On 05/10/2022 06:29, ThreeBlindQuarks via Tutor wrote: > Did I miss any comments or replies or updates or even a thank you from the one asking Nothing so far, and that is not unknown here. It is also quite early days yet, there are several possibly reasons: 1) email is slow, it can take 24 hours for delivery, so 48 hours from first posting to responce in the worst case. 2) Some posters wait a few days to assimilate a varioety of responses before sending a collective response 3) By definition we attract a lot of newbies, including those new to 'net fora and don't yet know the ettiquette. 4) Some people don't use email that much and only check it every few days, so they may not have even seen the responces yet. (I recently got a reply from a friend 6 months after I sent the message! He just hadn't checked that particular account for ages) Given that a couple of the replies explicitly suggested they ask a more specific question I would hope they would respond, but you never know. -- 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 From learn2program at gmail.com Thu Oct 6 17:00:30 2022 From: learn2program at gmail.com (Alan Gauld) Date: Thu, 6 Oct 2022 22:00:30 +0100 Subject: [Tutor] Fwd: Python text file read/compare In-Reply-To: References: Message-ID: <58dbe687-d5ba-19af-172b-2e3b323c7433@yahoo.co.uk> Forwarding to tutor list. Please aleways use Reply-All or reply-List when responding to tutr posts. ================ > The data is logged in Timestamps which starts at 488 milliseconds. That still doesn't help much. If we look at your data file which field/s represent the tie stamps? All of them or just some? Here is your sample data: -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, *488* -1.75, 1.12, 10.39, -0.10, -0.01, -0.01, 23.20, *521* 9.65, -1.31, -1.95, -0.11, -0.06, -0.02, 22.05, *15339436* I see 488 as the last feld in the first line. but the next line has 521 so is getting bigger not smaller. And the third is much, much bigger. But what about the other numbers are they also timestamps of some kind(deltas on the last field maybe?) or do they represent something else? If you only need to extract the last field then it is vdery easy, just split and use a -1 index: ts = line.split()[-1) And if they are always integers rather than actual times comparison is trivial too, just subytract them. > some point the number supposed to drop below this number.. > As the data is large,I am trying to find out when that happens > and what number it is so I can split the file to two for the > moment so I can work on the two separate file. It is easier to just keep loading each line into the new file until you reach one with zero (or less?). That way you don't need to store your data anywhere except in the final files. > I hope this helps you to understand what I am trying to > do and kindly assist me with clear explanation how I can proceed. Only slightly, it depends on my assumptions above. Can you use a file search tool(or text editor?) to find where the timestamps reach zero? It would be most useful to see the lines just before-just after that point. Alan G. On Tue., Oct. 4, 2022, 5:42 a.m. Alan Gauld via Tutor, > wrote: On 03/10/2022 21:14, samira khoda wrote: > Thank you very much for your feedback.I just started working on the file > again. Unfortunately I am still not getting anywhere with the modifications > I made to the codes.? I don't know when I can create the new file and write > to it and close it. Can you write a program to simply split your input file(s) after every N lines? ie something like: open the input open output linecount = 0 for line in input ? ? linecount += 1 ? ? if linecount > N ? ? ? ?close output ? ? ? ?open new output ? ? ? ?linecount = 1 ? ? write line to output If you can get that to work then you can replace the line if linecount > N with if zerotimestamp(line) And write a function to determine if the timestamp indicates a new file is needed. But get the basic structure in place first, don't try to solve both problems at once. > Basically as I mentioned before I need to find where > the timestamps jump down to zero or the lowest number then start time so I > can split the file and make a new file.***For your information the > timestamps are in milliseconds*** I think you will need to explain how that works. Your sample data below is not clear. You need to show us a sample where the timestamp is not zero then one that is. And explain which field defines that. > > kyk=( 'hfx-test-.txt'? , 'r') Not that kyk is just a tuple of 2 strings. > > line_numbers=[] > i=0 > current_time="" > count=1 > > for line in kyk: So this will return the 2 strings 'hfx-test-.txt' and 'r' >? ? ?if i < 488: >? ? ? ? ?i=0 >? ? ? ? ?line_numbers.append(current_time) >? ? ? ? ?current_time="" >? ? ?else: >? ? ? ? ?current_time+=line >? ? ? ? ?i+=1 This doesn't make much sense to me. Where does the magic number 488 fit in? First time through the loop i will always be zero so you will always start the list with an empty string in line_numbers. (BTW line_numbers is a very misleading name since it appears to be a list of times?) And because i is always 0 you never go into the else part so it never gets increased. So the for loop executes twice storing 2 empty strings in the line_numbers list. Even if you opened the file and iterated over it you would still just get a blank string for each line in the file. > **************this is where It does not write to the file***** > > *opf=open("kyk_txt_new_file.txt","w")* > > *? ? if current_time > *? ? ? ? splitlines(True).add(line_number);* > > *? ? opf.write("this is the timestamps after the line splits.")* > > *? ? kyk.close()* And this makes even less sense. where is start_time coming from? what is splitlines()? Where is it defined? And the only thing you write to the file is the message, you never write any data? And you close kyk but not opf? > *Here are the first and last few lines of my text file data.? * > > time stamps > -1.75, 1.08, 10.35, -0.10, -0.01, -0.01, 23.19, *488* > -1.75, 1.12, 10.39, -0.10, -0.01, -0.01, 23.20, *521* > > 9.65, -1.31, -1.95, -0.11, -0.06, -0.02, 22.05, *15339436* > 9.56, -1.32, -1.97, -0.10, -0.00, -0.01, 22.05, *15339495* Does a zero timestamp appear in any of those lines? This is just a set of numbers. Are they all timestamps? If so, why is the last number much bigger than the rest? And is the 488 at the end of line 1 significant in being the same as the magic number in your code? > > *I was also provided with the * pseudocode * below which I am trying to > follow if that helps to guide me along the way.* The pseudo code makes some sense - although a for loop would be simpler than the while. And it does not appear to be doing what you describe as the required task. But your code is not even close to what it does. > -> load sourceFile (a copy of the raw data file) I think that should say open sourcefile, not load... > line_number = 0 > start_time = 0 > split_numbers = [] > not_done = true > > while(not_done): >? ? ? ? ->read line from sourcefile >? ? ? ? ->split line on ',' >? ? ? ? ->convert last item on line to unsigned long and store in > current_time > >? ? ? ? if current_time < start_time: >? ? ? ? ? ? ? ?split_numbers.add(line_number) >? ? ? ? ? ? ? ?start_time = current_time >? ? ? ? if end_of_file: >? ? ? ? ? ? ? ?not_done = false; > > for s in split_numbers: >? ? ? ? ->create newfile >? ? ? ? for i = 0, i < s, i++: >? ? ? ? ? ? ? ?->read line from sourcefile >? ? ? ? ? ? ? ?->write line to newfile > >? ? ? ? ->close newfile > > ->Close sourcefile -- 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 at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From brianjwheatley at gmail.com Thu Oct 6 17:37:17 2022 From: brianjwheatley at gmail.com (Brian Wheatley) Date: Thu, 6 Oct 2022 17:37:17 -0400 Subject: [Tutor] Pytest - Sounddevice callback function Message-ID: Hello, Is there any way to pytest a callback function, using the sounddevice library? I basically just want to test if the Inputstream is working. Or even assert that no exception is found. Any help is greatly appreciated! Thank you -- Brian Wheatley From PythonList at DancesWithMice.info Thu Oct 6 21:07:21 2022 From: PythonList at DancesWithMice.info (dn) Date: Fri, 7 Oct 2022 14:07:21 +1300 Subject: [Tutor] Pytest - Sounddevice callback function In-Reply-To: References: Message-ID: <570bf36b-df0c-6f6f-4216-1f9e36be7ed0@DancesWithMice.info> Hello, On 07/10/2022 10.37, Brian Wheatley wrote: > Hello, > > Is there any way to pytest a callback function, using the sounddevice > library? > I basically just want to test if the Inputstream is working. > Or even assert that no exception is found. Am probably misunderstanding the question: why not have the callback-function return either/both the data-received (or len() thereof) and 'status'? NB don't know if incoming 'silence' is actually zero/null or if there will be values for 'static'/white-noise! If revealed to be necessary, please explain further... -- Regards, =dn From PythonList at DancesWithMice.info Thu Oct 6 21:47:48 2022 From: PythonList at DancesWithMice.info (dn) Date: Fri, 7 Oct 2022 14:47:48 +1300 Subject: [Tutor] Pytest - Sounddevice callback function In-Reply-To: References: <570bf36b-df0c-6f6f-4216-1f9e36be7ed0@DancesWithMice.info> Message-ID: <9b785625-a1f7-b05b-1281-608c6f590201@DancesWithMice.info> On 07/10/2022 14.32, Brian Wheatley wrote: > Hey DN, > > I'm not too sure.. I'm really new to python. > There is alot of sources on the web saying this can't be done, but I > thought I would reach out to this forum. > > Can you give an example of how you might do this? 1 have a known sound record something which you know, and save this in both numpy and audio format. 2 Ensure the test fixture is actually 'the sound' by outputting (both) to speakers. 3 Build a test which accepts any input sound, and compares that against the above (both formats) - if the input-sound is the same*, the test passes, else... * I fear that defining this, is a bit more involved than describing it! > On Thu., Oct. 6, 2022, 21:09 dn, > wrote: > > Hello, > > On 07/10/2022 10.37, Brian Wheatley wrote: > > Hello, > > > > Is there any way to pytest a callback function, using the sounddevice > > library? > > I basically just want to test if the Inputstream is working. > > Or even assert that no exception is found. > > Am probably misunderstanding the question: why not have the > callback-function return either/both the data-received (or len() > thereof) and 'status'? > > NB don't know if incoming 'silence' is actually zero/null or if there > will be values for 'static'/white-noise! > > If revealed to be necessary, please explain further... > -- > Regards, > =dn > _______________________________________________ > Tutor maillist? - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- Regards, =dn From PythonList at DancesWithMice.info Fri Oct 7 00:43:33 2022 From: PythonList at DancesWithMice.info (dn) Date: Fri, 7 Oct 2022 17:43:33 +1300 Subject: [Tutor] From NZPUG: Second round of the Smart Iterator Challenge Message-ID: A week-by-week Challenge series run by the Auckland Branch of the New Zealand Python Users' Group (AuckPUG) Challenge-week 2: Modular Programming, starts today! All welcome! Kiwi accent optional. Details from the Meetup site: https://www.meetup.com/nzpug-auckland/events/288813698/ Summary: - Three parts: Review Challenge-week 1, Tutorial, and Coding Challenge - during AuckPUG's 'off-weeks' - will suit Python-Apprentices ready to move-on from 'the basics' and Python-Journeymen - special Challenge for Python-Masters and others who want 'more' - specs and templates to download, explanation, references - attempt in bite-sized chunks: work on the Challenge in your own time, when you have the time - you do the coding and (privately) self-check against "Acceptance Test" - "Office Hours" for queries, successes, frustrations, ... (see below) - closes Sunday-week - review at beginning of next Challenge-Week Details on Meetup-site (as above) Please note: all times are NZDT, ie UTC+13 With special thanks to our own Leam Hall, for feedback and advice - that said, all errors and omissions are mine. Are you up for a challenge? Regards =dn (for Pete and DJ) From brianjwheatley at gmail.com Thu Oct 6 21:32:23 2022 From: brianjwheatley at gmail.com (Brian Wheatley) Date: Thu, 6 Oct 2022 21:32:23 -0400 Subject: [Tutor] Pytest - Sounddevice callback function In-Reply-To: <570bf36b-df0c-6f6f-4216-1f9e36be7ed0@DancesWithMice.info> References: <570bf36b-df0c-6f6f-4216-1f9e36be7ed0@DancesWithMice.info> Message-ID: Hey DN, I'm not too sure.. I'm really new to python. There is alot of sources on the web saying this can't be done, but I thought I would reach out to this forum. Can you give an example of how you might do this? On Thu., Oct. 6, 2022, 21:09 dn, wrote: > Hello, > > On 07/10/2022 10.37, Brian Wheatley wrote: > > Hello, > > > > Is there any way to pytest a callback function, using the sounddevice > > library? > > I basically just want to test if the Inputstream is working. > > Or even assert that no exception is found. > > Am probably misunderstanding the question: why not have the > callback-function return either/both the data-received (or len() > thereof) and 'status'? > > NB don't know if incoming 'silence' is actually zero/null or if there > will be values for 'static'/white-noise! > > If revealed to be necessary, please explain further... > -- > Regards, > =dn > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From brianjwheatley at gmail.com Thu Oct 6 21:59:31 2022 From: brianjwheatley at gmail.com (Brian Wheatley) Date: Thu, 6 Oct 2022 21:59:31 -0400 Subject: [Tutor] Pytest - Sounddevice callback function In-Reply-To: <9b785625-a1f7-b05b-1281-608c6f590201@DancesWithMice.info> References: <570bf36b-df0c-6f6f-4216-1f9e36be7ed0@DancesWithMice.info> <9b785625-a1f7-b05b-1281-608c6f590201@DancesWithMice.info> Message-ID: I think you are right lol Thanks for the direction! On Thu., Oct. 6, 2022, 21:48 dn, wrote: > On 07/10/2022 14.32, Brian Wheatley wrote: > > Hey DN, > > > > I'm not too sure.. I'm really new to python. > > There is alot of sources on the web saying this can't be done, but I > > thought I would reach out to this forum. > > > > Can you give an example of how you might do this? > > 1 have a known sound > record something which you know, and save this in both numpy and audio > format. > > 2 Ensure the test fixture is actually 'the sound' by outputting (both) > to speakers. > > 3 Build a test which accepts any input sound, and compares that against > the above (both formats) - if the input-sound is the same*, the test > passes, else... > > * I fear that defining this, is a bit more involved than describing it! > > > > > On Thu., Oct. 6, 2022, 21:09 dn, > > wrote: > > > > Hello, > > > > On 07/10/2022 10.37, Brian Wheatley wrote: > > > Hello, > > > > > > Is there any way to pytest a callback function, using the > sounddevice > > > library? > > > I basically just want to test if the Inputstream is working. > > > Or even assert that no exception is found. > > > > Am probably misunderstanding the question: why not have the > > callback-function return either/both the data-received (or len() > > thereof) and 'status'? > > > > NB don't know if incoming 'silence' is actually zero/null or if there > > will be values for 'static'/white-noise! > > > > If revealed to be necessary, please explain further... > > -- > > Regards, > > =dn > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Regards, > =dn > > From mats at wichmann.us Fri Oct 7 11:47:19 2022 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 7 Oct 2022 09:47:19 -0600 Subject: [Tutor] Pytest - Sounddevice callback function In-Reply-To: References: Message-ID: <7823e392-7ce7-ee63-4919-948ea27470dc@wichmann.us> On 10/6/22 15:37, Brian Wheatley wrote: > Hello, > > Is there any way to pytest a callback function, using the sounddevice > library? > I basically just want to test if the Inputstream is working. > Or even assert that no exception is found. Generally speaking, when the thing you want to test is "out of your control" - such as device input and callbacks in your case - you need to create a situation where the environment of the unit test *is* in your control. mocking and monkeypatching are tools you can use to help with this. That's a very general answer and certainly doesn't help with specific details (entirely unfamiliar with sounddevice). From nulla.epistola at web.de Sun Oct 9 06:49:50 2022 From: nulla.epistola at web.de (Sibylle Koczian) Date: Sun, 9 Oct 2022 12:49:50 +0200 Subject: [Tutor] for -- else: what was the motivation? In-Reply-To: References: <433a0cd3-48b1-6aae-7bd0-f13828002947@declassed.art> Message-ID: <9f8eac11-7a77-a88d-7417-f726d9c15da6@web.de> Am 08.10.2022 um 06:13 schrieb Dan Stromberg: > The else is executed if you don't "break" out of the loop early. > > It cuts down on boolean flags. > Which means that "for -- else" can't be useful for a loop without a "break" in it - and the OP's examples didn't have breaks. From alexkleider at gmail.com Mon Oct 10 01:35:22 2022 From: alexkleider at gmail.com (Alex Kleider) Date: Sun, 9 Oct 2022 22:35:22 -0700 Subject: [Tutor] Thanksgiving Message-ID: Over the years since discovering Python and the existence of this (Python tutor) mailing list I've been struck by how many of the 'tutors' are from Commonwealth countries (Scotland, Australia, New Zealand.) Whatever any of them might feel about the monarchy, while myself celebrating Canadian Thanksgiving, I'd like to express my personal gratitude to all the tutors, Commonwealth or not. Thank you all! Sincerely, Alex From moore.3972 at buckeyemail.osu.edu Sun Oct 9 19:42:04 2022 From: moore.3972 at buckeyemail.osu.edu (Moore, Colleen) Date: Sun, 9 Oct 2022 23:42:04 +0000 Subject: [Tutor] Python problem Message-ID: Hi, I figured I could post a question here that I wasn't sure how to solve. Below are the problems: Question 2. Write a Python program to create a list of 1000 random points. These points must be objects of the Point class. The X and Y coordinates of these points must be random float between a range and any arbitrary range will be fine. (Hint: random.uniform can be used to generate a random floating point value between any two given numbers.) Question 3. After you finish the above, write a program to print out the number of points that fall in the left half of the area. If a point is exactly on the middle of the area, you are free to decide to either count it in the left half, or right half. Please note in your program how which is the case. Question 4. Write a Python program to draw the points for the above question. Points counted as falling into the left are rendered in darkgrey and right in lightgrey. Submit both your code and a screenshot of the plotting result. Thanks! From alan.gauld at yahoo.co.uk Mon Oct 10 03:58:24 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 10 Oct 2022 08:58:24 +0100 Subject: [Tutor] Python problem In-Reply-To: References: Message-ID: On 10/10/2022 00:42, Moore, Colleen wrote: > Hi, I figured I could post a question here that I wasn't sure how to solve. That's fine, but although you have posted your assignment you haven't asked us a question. What is it you want to know? What is it that puzzles you about the assignment? Have you made a start? What code do you have? Do you get error messages? If so include them in your post. [ Although I do note that your assignment is labeled as 3 questions when it is, in fact, 3 exercises. Personally I would worry about a course where the course tutors are so illiterate as to not understand the difference. Or worse, don't care!] > Below are the problems: > > Question 2. Write a Python program to create a list of 1000 random points. These points must be objects of the Point class. The X and Y coordinates of these points must be random float between a range and any arbitrary range will be fine. (Hint: random.uniform can be used to generate a random floating point value between any two given numbers.) > > Question 3. After you finish the above, write a program to print out the number of points that fall in the left half of the area. If a point is exactly on the middle of the area, you are free to decide to either count it in the left half, or right half. Please note in your program how which is the case. > > Question 4. Write a Python program to draw the points for the above question. Points counted as falling into the left are rendered in darkgrey and right in lightgrey. Submit both your code and a screenshot of the plotting result. > > Thanks! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- 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 From nulla.epistola at web.de Mon Oct 10 07:41:03 2022 From: nulla.epistola at web.de (Sibylle Koczian) Date: Mon, 10 Oct 2022 13:41:03 +0200 Subject: [Tutor] for -- else: what was the motivation? In-Reply-To: <9f8eac11-7a77-a88d-7417-f726d9c15da6@web.de> References: <433a0cd3-48b1-6aae-7bd0-f13828002947@declassed.art> <9f8eac11-7a77-a88d-7417-f726d9c15da6@web.de> Message-ID: <1ef5adbc-1848-9551-5938-ba4f680363c0@web.de> Am 09.10.2022 um 12:49 schrieb Sibylle Koczian: > Am 08.10.2022 um 06:13 schrieb Dan Stromberg: >> The else is executed if you don't "break" out of the loop early. >> >> It cuts down on boolean flags. >> > > Which means that "for -- else" can't be useful for a loop without a > "break" in it - and the OP's examples didn't have breaks. > Sorry - this should have gone to the general Python mailing list. Confused the "for -- else:" thread there with the "if else statement" thread here. From nulla.epistola at web.de Mon Oct 10 07:41:03 2022 From: nulla.epistola at web.de (Sibylle Koczian) Date: Mon, 10 Oct 2022 13:41:03 +0200 Subject: [Tutor] for -- else: what was the motivation? In-Reply-To: <9f8eac11-7a77-a88d-7417-f726d9c15da6@web.de> References: <433a0cd3-48b1-6aae-7bd0-f13828002947@declassed.art> <9f8eac11-7a77-a88d-7417-f726d9c15da6@web.de> Message-ID: <1ef5adbc-1848-9551-5938-ba4f680363c0@web.de> Am 09.10.2022 um 12:49 schrieb Sibylle Koczian: > Am 08.10.2022 um 06:13 schrieb Dan Stromberg: >> The else is executed if you don't "break" out of the loop early. >> >> It cuts down on boolean flags. >> > > Which means that "for -- else" can't be useful for a loop without a > "break" in it - and the OP's examples didn't have breaks. > Sorry - this should have gone to the general Python mailing list. Confused the "for -- else:" thread there with the "if else statement" thread here. From wlfraed at ix.netcom.com Mon Oct 10 12:00:48 2022 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Mon, 10 Oct 2022 12:00:48 -0400 Subject: [Tutor] Python problem References: Message-ID: <2oe8khlk0tkk41r59ppjh4qt0dun1ck6u1@4ax.com> On Sun, 9 Oct 2022 23:42:04 +0000, "Moore, Colleen" declaimed the following: >Hi, I figured I could post a question here that I wasn't sure how to solve. Below are the problems: > And what is your question... You've posted three assignments (at least, to me they are as they each specify a /separate/ program, and not just three phases within a single program). You have not stated where you are stuck, nor shown any code. We do not "do homework" for you -- you must make the attempt, show us the code and any error messages (DO NOT ATTACH SCREEN GRABS -- they get stripped; cut&paste the TEXT into a message), and we may guide you into correct understanding of the construct giving problems... >Question 2. Write a Python program to create a list of 1000 random points. These points must be objects of the Point class. The X and Y coordinates of these points must be random float between a range and any arbitrary range will be fine. (Hint: random.uniform can be used to generate a random floating point value between any two given numbers.) > Hmmm, what was "Question 1"? The above assumes you know how to perform basic Python logic... IE: loop over a section of code for 1000 times; declaring simple Classes and creating instances of that class; accumulate the 1000 points into some structure. Actually, given the separate program phrasing -- said "accumulation" doesn't even have to be internal; you could write out the coordinates to a file which the following two programs read... Granted, unless one is using pickle or related to save the points, one loses the entire Class construct -- generating a "Point" just involves generating two random values and writing them out as a line to some text file. If you haven't learned these simple tasks, you may want to ask yourself: Do I belong in this course? >Question 3. After you finish the above, write a program to print out the number of points that fall in the left half of the area. If a point is exactly on the middle of the area, you are free to decide to either count it in the left half, or right half. Please note in your program how which is the case. > And as mentioned above, this assumes some means of passing the "Points" of the first assignment into the second. Without using pickle, that means reading each line of the file, parsing out two coordinates, and recreating the Point instance... All to simply look at the X coordinate and determine if it is left or right of the midpoint range (which has to somehow be carried over from the first assignment -- maybe written as the first row of the data file). This time, instead of a definite loop (first assignment: 1000 points), one likely has an indefinite loop (loop until end of file), reading a line, determining if it is left/right (and why bother with creating a Point instance when you throw it away on the next iteration) summing how many are on each side. >Question 4. Write a Python program to draw the points for the above question. Points counted as falling into the left are rendered in darkgrey and right in lightgrey. Submit both your code and a screenshot of the plotting result. This is the only assignment where a Points class might be of use, as most plotting packages want ALL the data in one chunk. You are basically repeating the second assignment, but this time rather than counting left/right, you may be accumulating the points into some structure that can be fed to the plotting package (and that may not want "Points" per se, but two vectors: one of only X coordinates, and the other of the Y coordinates). BUT! WHAT TYPE OF PLOT IS REQUIRED? Simple histogram? (In this case, you ARE duplicating the previous assignment -- the only thing changing is that rather than printing numeric results you are creating a two column histogram with the counts. Side by side scatter-plots (dots for each point, taking into account the left/right split, and using Y coordinate for height of the dot in the plot)? Scatter-plot with connecting lines showing the order the points were created? -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From nathan-tech at hotmail.com Mon Oct 10 12:03:18 2022 From: nathan-tech at hotmail.com (Nathan Smith) Date: Mon, 10 Oct 2022 17:03:18 +0100 Subject: [Tutor] Can I have some assistance understanding the wave module Message-ID: Hello, So I am trying to work with the wave module, with the end goal of taking raw data from an audio CD and writing it to a Wav file. To do this I was looking at the wave module (it's there, so surely it's the answer) but:: file.setsampwidth: What actually is this? It seems to except a number between 1 and 4, but 4 produces a file that is 46 seconds long off disc (but only 24 on disc). To that end, is this related to bit depth? Audio CD's are recorded at an sr of 44100, and a bit depth of 16, but this being the case, can python' s wave module not handle this? In that instance, if I want to write the raw data in 16 bit format so it actually plays, how would I begin to look into such a venture? Thanks Nathan -- Best Wishes, Nathan Smith, BSC My Website: https://nathantech.net From mats at wichmann.us Mon Oct 10 13:47:15 2022 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 10 Oct 2022 11:47:15 -0600 Subject: [Tutor] Can I have some assistance understanding the wave module In-Reply-To: References: Message-ID: <38be58d8-d346-f3af-836a-4c27ba65665e@wichmann.us> On 10/10/22 10:03, Nathan Smith wrote: > Hello, > > > So I am trying to work with the wave module, with the end goal of taking > raw data from an audio CD and writing it to a Wav file. > > To do this I was looking at the wave module (it's there, so surely it's > the answer) but:: > > > file.setsampwidth: What actually is this? It seems to except a number > between 1 and 4, but 4 produces a file that is 46 seconds long off disc > (but only 24 on disc). > > > To that end, is this related to bit depth? Audio CD's are recorded at an > sr of 44100, and a bit depth of 16, but this being the case, can python' > s wave module not handle this? sample width and bit depth are the same thing, consistent terminology is not something we get... that's in bytes, so you'd want a value of 2 for 16-bit. does that help? beyond that we'll have to wait for someone who's actually fluent in audio stuff. > In that instance, if I want to write the raw data in 16 bit format so it > actually plays, how would I begin to look into such a venture? > > > Thanks > > Nathan > From alan.gauld at yahoo.co.uk Mon Oct 10 14:08:10 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 10 Oct 2022 19:08:10 +0100 Subject: [Tutor] Can I have some assistance understanding the wave module In-Reply-To: References: Message-ID: On 10/10/2022 17:03, Nathan Smith wrote: > So I am trying to work with the wave module, with the end goal of taking > raw data from an audio CD and writing it to a Wav file. Caveat: I have no experience with the wave module I just read the docs.... > file.setsampwidth: What actually is this? It seems to except a number > between 1 and 4, but 4 produces a file that is 46 seconds long off disc > (but only 24 on disc). The docs say the number is the sample size in bytes. So a 4 means you are writing 4 byte = 32 bit samples. Maybe the original file was using 16 bit? > To that end, is this related to bit depth? Audio CD's are recorded at an > sr of 44100, and a bit depth of 16, but this being the case, can python' > s wave module not handle this? Based on the documentation you would need a samplesize of 2. Documentation is really useful. :-) -- 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 From bohiles at gmail.com Mon Oct 10 05:54:11 2022 From: bohiles at gmail.com (A L.V) Date: Mon, 10 Oct 2022 11:54:11 +0200 Subject: [Tutor] Thanksgiving In-Reply-To: References: Message-ID: Yep, Gracias from Spain . Alex "Be kind, for everyone you meet is fighting a hard battle." El lun, 10 oct 2022, 7:35, Alex Kleider escribi?: > Over the years since discovering Python and the existence of this (Python > tutor) mailing list I've been struck by how many of the 'tutors' are from > Commonwealth countries (Scotland, Australia, New Zealand.) Whatever any of > them might feel about the monarchy, while myself celebrating Canadian > Thanksgiving, I'd like to express my personal gratitude to all the tutors, > Commonwealth or not. > Thank you all! > Sincerely, > Alex > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From threesomequarks at proton.me Mon Oct 10 14:00:13 2022 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Mon, 10 Oct 2022 18:00:13 +0000 Subject: [Tutor] Python problem In-Reply-To: <2oe8khlk0tkk41r59ppjh4qt0dun1ck6u1@4ax.com> References: <2oe8khlk0tkk41r59ppjh4qt0dun1ck6u1@4ax.com> Message-ID: With so much to choose from, I am considering doing the shortest question #1 which will be neglected by others. To be fair, questions 2 to 4 build on each other and this looks like a single program. That makes me wonder if the missing question helps set up the rest. I await replies showing some attempts to work these problemd or ca reply that contains actual questions of a tutoring nature ant not the actual assignment the teacher gave. I want to know if they figured out question one on their own. Sent from Proton Mail mobile -------- Original Message -------- On Oct 10, 2022, 12:00 PM, Dennis Lee Bieber wrote: > On Sun, 9 Oct 2022 23:42:04 +0000, "Moore, Colleen" declaimed the following: >Hi, I figured I could post a question here that I wasn't sure how to solve. Below are the problems: > And what is your question... You've posted three assignments (at least, to me they are as they each specify a /separate/ program, and not just three phases within a single program). You have not stated where you are stuck, nor shown any code. We do not "do homework" for you -- you must make the attempt, show us the code and any error messages (DO NOT ATTACH SCREEN GRABS -- they get stripped; cut&paste the TEXT into a message), and we may guide you into correct understanding of the construct giving problems... >Question 2. Write a Python program to create a list of 1000 random points. These points must be objects of the Point class. The X and Y coordinates of these points must be random float between a range and any arbitrary range will be fine. (Hint: random.uniform can be used to generate a random floating point value between any two given numbers.) > Hmmm, what was "Question 1"? The above assumes you know how to perform basic Python logic... IE: loop over a section of code for 1000 times; declaring simple Classes and creating instances of that class; accumulate the 1000 points into some structure. Actually, given the separate program phrasing -- said "accumulation" doesn't even have to be internal; you could write out the coordinates to a file which the following two programs read... Granted, unless one is using pickle or related to save the points, one loses the entire Class construct -- generating a "Point" just involves generating two random values and writing them out as a line to some text file. If you haven't learned these simple tasks, you may want to ask yourself: Do I belong in this course? >Question 3. After you finish the above, write a program to print out the number of points that fall in the left half of the area. If a point is exactly on the middle of the area, you are free to decide to either count it in the left half, or right half. Please note in your program how which is the case. > And as mentioned above, this assumes some means of passing the "Points" of the first assignment into the second. Without using pickle, that means reading each line of the file, parsing out two coordinates, and recreating the Point instance... All to simply look at the X coordinate and determine if it is left or right of the midpoint range (which has to somehow be carried over from the first assignment -- maybe written as the first row of the data file). This time, instead of a definite loop (first assignment: 1000 points), one likely has an indefinite loop (loop until end of file), reading a line, determining if it is left/right (and why bother with creating a Point instance when you throw it away on the next iteration) summing how many are on each side. >Question 4. Write a Python program to draw the points for the above question. Points counted as falling into the left are rendered in darkgrey and right in lightgrey. Submit both your code and a screenshot of the plotting result. This is the only assignment where a Points class might be of use, as most plotting packages want ALL the data in one chunk. You are basically repeating the second assignment, but this time rather than counting left/right, you may be accumulating the points into some structure that can be fed to the plotting package (and that may not want "Points" per se, but two vectors: one of only X coordinates, and the other of the Y coordinates). BUT! WHAT TYPE OF PLOT IS REQUIRED? Simple histogram? (In this case, you ARE duplicating the previous assignment -- the only thing changing is that rather than printing numeric results you are creating a two column histogram with the counts. Side by side scatter-plots (dots for each point, taking into account the left/right split, and using Y coordinate for height of the dot in the plot)? Scatter-plot with connecting lines showing the order the points were created? -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From threesomequarks at proton.me Mon Oct 10 16:10:08 2022 From: threesomequarks at proton.me (ThreeBlindQuarks) Date: Mon, 10 Oct 2022 20:10:08 +0000 Subject: [Tutor] Look Familiar but numbered 1 Message-ID: <-HGqJh1TClYHVfJ5_hVEEqQhE8BxNkLGEuiPRA3dxc6ewpDKqiJjoP0yyj4axj-u6tjWk8K8IwH4JIoGLnqh_Oxkt8PDSa6EptR00MM5kR0=@proton.me> It seems the darn question 2 someone asked is question 1 here: https://www.chegg.com/homework-help/questions-and-answers/1write-python-program-create-list-1000-random-points-points-must-objects-point-class-x-y-c-q102520838 > Question:1.Write A Python Program To Create A List Of 1000 Random Points. These Points Must Be Objects Of The Point Class. The X And Y Coordinates Of These Points Must Be Random Float Between A Range And Any Arbitrary Range Will Be Fine. (Hint: Random.Uniform Can Be Used To Generate A Random Floating Point Value Between Any Two Given Numbers.) After You Finish This, > > This problem has been solved! > > You'll get a detailed solution from a subject matter expert that helps you learn core concepts. > > See Answer > > 1.Write a Python program to create a list of 1000 random points. These points must be objects of the Point class. The X and Y coordinates of these points must be random float between a range and any arbitrary range will be fine. (Hint: random.uniform can be used to generate a random floating point value between any two given numbers.) After you finish this, write a program to print out the number of points that fall in the left half of the area. If a point is exactly on the middle of the area, you are free to decide to either count it in the left half, or right half. Please note in your program how which is the case. Word for Word? I did not sign in to view an answer. - 3 - Sent with [Proton Mail](https://proton.me/) secure email. From nathan-tech at hotmail.com Tue Oct 11 13:28:59 2022 From: nathan-tech at hotmail.com (Nathan Smith) Date: Tue, 11 Oct 2022 18:28:59 +0100 Subject: [Tutor] Can I have some assistance understanding the wave module In-Reply-To: References: Message-ID: Hello, As ever Alan, spot on the mark, and thank you Mats, too, for your help. Both proved invaluable, I admit I'd read the wave write part of the wave module multiple times, but did not read the wave read bit because, well, I didn't need to read! Hence I missed the explanations and such. Seems to be working now, time to wrestle with ffmepg! Joys. Thanks all Nathan On 10/10/2022 19:08, Alan Gauld via Tutor wrote: > On 10/10/2022 17:03, Nathan Smith wrote: > >> So I am trying to work with the wave module, with the end goal of taking >> raw data from an audio CD and writing it to a Wav file. > Caveat: I have no experience with the wave module I just > read the docs.... > > >> file.setsampwidth: What actually is this? It seems to except a number >> between 1 and 4, but 4 produces a file that is 46 seconds long off disc >> (but only 24 on disc). > The docs say the number is the sample size in bytes. So a 4 means you > are writing 4 byte = 32 bit samples. Maybe the original file was using > 16 bit? > >> To that end, is this related to bit depth? Audio CD's are recorded at an >> sr of 44100, and a bit depth of 16, but this being the case, can python' >> s wave module not handle this? > Based on the documentation you would need a samplesize of 2. > Documentation is really useful. :-) > -- Best Wishes, Nathan Smith, BSC My Website: https://nathantech.net From PythonList at DancesWithMice.info Tue Oct 11 15:33:49 2022 From: PythonList at DancesWithMice.info (dn) Date: Wed, 12 Oct 2022 08:33:49 +1300 Subject: [Tutor] [Meeting] Problem-solving, perspectives, programming, and presentation, 19Oct Message-ID: With all that "p"-alliteration, it must be a Pppresentation with the virtual-Auckland branch of NZPUG! Wed 19 Oct, 1800 for 1830 ~ 2030 NZDT 0500, 0530, and 0730 UTC+13 resp by web-conference Our own Nathan Smith had an 'itch' caused by a problem related to the game of Scrabble. Solving a problem with Python, from start-to-finish. A journey of challenges, a lesson of not giving in and hopefully a good story all round. Featuring, steps forwards, brick-walls, data-structures related to words, dictionaries (of both definitions) and Directed Acyclic Word Graphs (DAWG - application of DAG graph/network structures). Plus, a blind person's perspective on coding through the project, presenting overview information on the way a blind person may access code and what similarities or differences may crop up. Creative Coding With Python: Learn how to code simple visuals in Python for games, innovative stats, simulations and generative art using a flavour of the processing library. This can be incredibly useful for teaching programming, visualising concepts and sharpening thinking skills. Abdur-Rahman Janhangeer organises the Python Mauritius User Group and FlaskCon* (amongst many other contributions to the Python Community including graphic presentation libraries) * Flask is a very popular web framework More info about the group, and to RSVP (and thus receive the meeting URL) see Meetup group at: https://www.meetup.com/nzpug-auckland/events/njdjssydcnbzb/ Regards =dn (Pete and DJ) From larry.united at t-online.de Thu Oct 13 04:04:00 2022 From: larry.united at t-online.de (Emilio C) Date: Thu, 13 Oct 2022 10:04:00 +0200 Subject: [Tutor] Python - resize ReBarWindow32 with SetWindowPos References: <73c6da36-5fa5-4850-9011-92f987b40806@Spark> Message-ID: I am trying to resize the ReBarWindow32 (the main part of the taskbar). In theory it works, as the new width and height get accepted, however it doesn't seem to translate visually, because the bar doesn't change in appearance. This is the cropped out resizing part, unrelated code removed import win32gui import win32con taskbar_shell = win32gui.FindWindow("Shell_TrayWnd",None) print("SHELL: " , taskbar_shell) place = win32gui.GetWindowPlacement(taskbar_shell) print("SHELL place: " , place) taskbar_rebar = win32gui.FindWindowEx(taskbar_shell, None, "ReBarWindow32", None) print("ReBar: " , taskbar_rebar) place = win32gui.GetWindowPlacement(taskbar_rebar) print("ReBar place: " , place) ############## SETWINDOWPOS resize the bar ##################win32gui.MoveWindow(taskbar_rebar, 0, 0, 200, 22, True)##win32gui.SetWindowPos(taskbar_rebar, None, 0, 0, 200, 22, win32con.SWP_NOSENDCHANGING) win32gui.SetWindowPos(taskbar_rebar, None, 0, 0, 200, 22, win32con.SWP_NOMOVE | win32con.SWP_NOSENDCHANGING | win32con.SWP_NOREDRAW) win32gui.ShowWindow(taskbar_rebar, win32con.SW_SHOW) win32gui.UpdateWindow(taskbar_rebar) place = win32gui.GetWindowPlacement(taskbar_rebar) print("Rebar place after SetWindowPos: " , place) ###win32gui.RedrawWindow(taskbar_rebar, None, None, win32con.RDW_INVALIDATE | win32con.RDW_INTERNALPAINT | win32con.CS_HREDRAW | win32con.CS_VREDRAW | win32con.RDW_UPDATENOW) print shows successfully altered w and h. But visually nothing changes in the taskbar. I tried so many different flags.?The flags in place rn are just the last to have been tried by me Do you guys happens to know what I?m doing wrong? System: Windows 11, Python 3 From PythonList at DancesWithMice.info Fri Oct 21 00:39:00 2022 From: PythonList at DancesWithMice.info (dn) Date: Fri, 21 Oct 2022 17:39:00 +1300 Subject: [Tutor] Third round of the Smart Iterator Challenge: September-October 2022 Message-ID: Challenge-week 3: Generalising the solution, starts today! Details from the Meetup site: https://www.meetup.com/nzpug-auckland/events/288813734/ A virtual event run by the Auckland Branch of the New Zealand Python Users' Group. It's Week 3 of the Smart Iterator Challenge! Time to see how modules and namespaces provide building-blocks which enable us to cope with change. Can you anticipate and manage change? The tutorial demonstrates SOLID's SRP (and a bit of OCP) for those who want to learn more than Python-coding. This Challenge will interest Python-Journeymen, and Python-Apprentices ready to move-on from ?the basics?. There is a separate-but-related question for Python-Masters and any advanced Journeymen who are finding the main Challenge too-easy (available upon personal request), in recognition of being prepared to help others. We start with a review of Challenge-week 2 and a sample-answer to download and compare with your own efforts. Challenge-week 3 starts with either your own or a provided template-script, so you don't have to have completed Challenge-weeks 1 and 2 (but it will help). Again, there is a tutorial in case you haven't met namespaces before. Followed by multiple specifications to implement. In many ways, the challenge is not so much writing code, as it is designing a code-solution. Putting code-modules together, something like a jig-saw puzzle! Challenge Schedule: Generalising the solution Starting: Sat 22 Oct Office Hours: 1830*, Wed 26 Oct Concluding: midnight after Sun 30 Oct * all times NZDT (UTC+13) Are you up for a challenge? Regards =dn (for Pete and DJ) From PythonList at DancesWithMice.info Thu Oct 27 17:34:47 2022 From: PythonList at DancesWithMice.info (dn) Date: Fri, 28 Oct 2022 10:34:47 +1300 Subject: [Tutor] Crafting Software (PUG practical meeting) Message-ID: <5e41066d-8f80-1854-57cd-88eb7bbd8232@DancesWithMice.info> Wed, 2 Nov 2022 at 1830~2030 NZDT (0530~0730 UTC) Come to our Coding Evening with your Python questions, suggestions, problems, etc; and/or bring a Lightning Talk about your current topic-of-interest - something new, a recent discovery, a contribution you have made... Thereafter, we will continue the Crafting Software series. These are code-along-at-home exercises. The initial sessions were designed for Beginners, and have gradually 'grown' into areas which will challenge and benefit Journeyman Python Programmers and even Masters. The aim is to complement our Software Craftsmanship Presentation series with practical coding and observing the advantages of craftsmanship, as we/the code becomes more sophisticated. We are enjoying meeting attendees from all over the world (Europeans invert their screens to be able to see us 'right way up' (!?)) Please RSVP to be advised of the web-conference URL, and QuickStart Guide. https://www.meetup.com/nzpug-auckland/events/hgxmwsydcpbdb/ -- Regards, =dn Auckland Branch, New Zealand Python Users' Group From nathan-tech at hotmail.com Fri Oct 28 07:38:59 2022 From: nathan-tech at hotmail.com (Nathan Smith) Date: Fri, 28 Oct 2022 12:38:59 +0100 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not Message-ID: Hello, I have some code like so: ??? p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) ??? [stdout, stderr]=p.communicate(data) Where data is some data taken from io.bytesIO.read And cmd is a command for ffmpeg like so: cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y" When run in an interactive shell this works like a charm. When run in a script file, this breaks. At first I thought it was an issue with FFMPEG, but now I'm wondering if python is changing something under the hood? When running from a script, should I be using shell=True? thanks in advance, Nathan -- Best Wishes, Nathan Smith, BSC My Website: https://nathantech.net From alan.gauld at yahoo.co.uk Sat Oct 29 09:23:56 2022 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 29 Oct 2022 14:23:56 +0100 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: References: Message-ID: On 28/10/2022 12:38, Nathan Smith wrote: > ??? p = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stdin=subprocess.PIPE, stderr=subprocess.PIPE) > ??? [stdout, stderr]=p.communicate(data) The square brackets should not be needed. Unpacking will work based on the comma alone. > Where data is some data taken from io.bytesIO.read > > And cmd is a command for ffmpeg like so: > > cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y" so far so good. > When run in an interactive shell this works like a charm. When run in a > script file, this breaks. Breaks? How? Error message? Bad data? No data? Give us a clue. > At first I thought it was an issue with > FFMPEG, but now I'm wondering if python is changing something under the > hood? How are you running the script file? Is it the same user environment as the interactive Python session or via a cron job or similar? > When running from a script, should I be using shell=True? Probably not. shell=True simply says to read the command in a shell environment expanding wildcards etc. You don't seem to be using anything that needs shell expansion. -- 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 From mats at wichmann.us Sat Oct 29 11:59:32 2022 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 29 Oct 2022 09:59:32 -0600 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: References: Message-ID: <2c43bf7e-dbd5-6a01-efc3-52adf1a44e4c@wichmann.us> On 10/29/22 07:23, Alan Gauld via Tutor wrote: > On 28/10/2022 12:38, Nathan Smith wrote: > >> ??? p = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> stdin=subprocess.PIPE, stderr=subprocess.PIPE) >> ??? [stdout, stderr]=p.communicate(data) > > The square brackets should not be needed. Unpacking > will work based on the comma alone. > >> Where data is some data taken from io.bytesIO.read >> >> And cmd is a command for ffmpeg like so: >> >> cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y" > > so far so good. You don't call out the platform you're using - it's useful to be explicit about that. This usage form ought work okay on Windows, which wants a string for the command - the inclusion of ".exe" suggests it's Windows. It would break on POSIX platforms which can't pass arguments in the same string when using Popen directly, you need to use a list of strings instead. So the problem isn't immediately clear to me, but can still make a suggestion: using "subprocess.run" is usually a fair bit easier than fiddling directly with the Popen constructor and communicate, and often "wait" and other methods to make sure everything's complete and cleaned up. run handles the communication and cleanup and hands you back a nice CompletedProcess instance that you can grab whatever data you need from. Have you confirmed that the value of "data" is what you expect when you run it as a script? From roel at roelschroeven.net Sat Oct 29 11:51:42 2022 From: roel at roelschroeven.net (Roel Schroeven) Date: Sat, 29 Oct 2022 17:51:42 +0200 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: References: Message-ID: <450ebb1d-2b88-185e-4403-6ef9cfde9cb9@roelschroeven.net> Nathan Smith schreef op 28/10/2022 om 13:38: > I have some code like so: > > > ??? p = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stdin=subprocess.PIPE, stderr=subprocess.PIPE) > ??? [stdout, stderr]=p.communicate(data) > > > Where data is some data taken from io.bytesIO.read > > And cmd is a command for ffmpeg like so: > > cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y" > > > When run in an interactive shell this works like a charm. When run in a > script file, this breaks. At first I thought it was an issue with > FFMPEG, but now I'm wondering if python is changing something under the > hood? Python doesn't magically change anything. What do you mean by "breaks"? Do you get a traceback? What does it say? Two points: 1) Important note though: when not using shell=True, you need to pass the name of the program and all arguments/options as a list with separate items, like so: cmd = [ ??? "ffmpeg/ffmpeg.exe", ??? "-f", "wav", ??? "-ac", "2", ??? "-i", "-", ??? "-f", "mp3", ??? "-ab", "128000", ??? "-", ??? "-y" ] I'm actually very surprised that things worked correctly in the shell with cmd being one large string instead. I can't get it to work like that, unless I use shell=True which is best avoided. 2) What can be different between the two situations is the current working directory and/or executable search path (environment variable PATH). Check those; if needed use an absolute path, or some other way to unambiguously specify the path to ffmpeg. > When running from a script, should I be using shell=True? shell=True is to be avoided as much as possible, so no; see https://docs.python.org/3.10/library/subprocess.html#security-considerations for why. The "shell" here does not refer to the interactive Python shell, but the operating system's command interpreter. As the documentation says, " The only time you need to specify |shell=True| on Windows is when the command you wish to execute is built into the shell (e.g. *dir* or *copy*). You do not need |shell=True| to run a batch file or console-based executable." (Assuming you're on Windows because of the .exe suffix you use) -- "I've come up with a set of rules that describe our reactions to technologies: 1. Anything that is in the world when you?re born is normal and ordinary and is just a natural part of the way the world works. 2. Anything that's invented between when you?re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. 3. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams, The Salmon of Doubt From wlfraed at ix.netcom.com Sat Oct 29 13:54:29 2022 From: wlfraed at ix.netcom.com (Dennis Lee Bieber) Date: Sat, 29 Oct 2022 13:54:29 -0400 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not References: Message-ID: <9tpqlhlto7q1vb8nk4nqjge5hr00gkt4s0@4ax.com> On Sat, 29 Oct 2022 14:23:56 +0100, Alan Gauld via Tutor declaimed the following: > >Probably not. shell=True simply says to read the command >in a shell environment expanding wildcards etc. You don't seem >to be using anything that needs shell expansion. Documentation is a bit confusing... """ Changed in version 3.6: args parameter accepts a path-like object if shell is False and a sequence containing path-like objects on POSIX. Changed in version 3.8: args parameter accepts a path-like object if shell is False and a sequence containing bytes and path-like objects on Windows. The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence. """ That "is False and a sequence" throws me? I want to read it as "or". The OP is specifying "cmd" as a string. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ From mats at wichmann.us Sat Oct 29 14:02:03 2022 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 29 Oct 2022 12:02:03 -0600 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: <9tpqlhlto7q1vb8nk4nqjge5hr00gkt4s0@4ax.com> References: <9tpqlhlto7q1vb8nk4nqjge5hr00gkt4s0@4ax.com> Message-ID: <00fa597f-7aa8-5081-d3ea-c0524d2f07a0@wichmann.us> On 10/29/22 11:54, Dennis Lee Bieber wrote: > On Sat, 29 Oct 2022 14:23:56 +0100, Alan Gauld via Tutor > declaimed the following: > >> >> Probably not. shell=True simply says to read the command >> in a shell environment expanding wildcards etc. You don't seem >> to be using anything that needs shell expansion. > > Documentation is a bit confusing... > > """ > Changed in version 3.6: args parameter accepts a path-like object if shell > is False and a sequence containing path-like objects on POSIX. > > Changed in version 3.8: args parameter accepts a path-like object if shell > is False and a sequence containing bytes and path-like objects on Windows. > > The shell argument (which defaults to False) specifies whether to use the > shell as the program to execute. If shell is True, it is recommended to > pass args as a string rather than as a sequence. > """ > > That "is False and a sequence" throws me? I want to read it as "or". > > The OP is specifying "cmd" as a string. Pretty messy, isn't it? These two snips - they're not adjacent in the doc: > If args is a string, the interpretation is platform-dependent and described below. > On Windows, if args is a sequence, it will be converted to a string in a manner described in Converting an argument sequence to a string on Windows. This is because the underlying CreateProcess() operates on strings. So in the Windows case, it always wants a string underneath, and since that's what was passed by the OP it doesn't seem likely that's "the" problem. The further reference that didn't survive the copy/paste is here: https://docs.python.org/3/library/subprocess.html#converting-argument-sequence From roel at roelschroeven.net Sat Oct 29 14:38:50 2022 From: roel at roelschroeven.net (Roel Schroeven) Date: Sat, 29 Oct 2022 20:38:50 +0200 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: <00fa597f-7aa8-5081-d3ea-c0524d2f07a0@wichmann.us> References: <9tpqlhlto7q1vb8nk4nqjge5hr00gkt4s0@4ax.com> <00fa597f-7aa8-5081-d3ea-c0524d2f07a0@wichmann.us> Message-ID: <590558ac-d444-a752-b0b5-e7e6c4f161b3@roelschroeven.net> Mats Wichmann schreef op 29/10/2022 om 20:02: > Pretty messy, isn't it? These two snips - they're not adjacent in the doc: > > > If args is a string, the interpretation is platform-dependent and > described below. > > > On Windows, if args is a sequence, it will be converted to a string > in a manner described in Converting an argument sequence to a string on > Windows. This is because the underlying CreateProcess() operates on strings. > > So in the Windows case, it always wants a string underneath, and since > that's what was passed by the OP it doesn't seem likely that's "the" > problem. Oh, right, I was wrong. I tried before but I must have a mistake. I just tried again and indeed, passing a string works indeed on Windows even without shell=True. -- "Cheer up," they said, "things could be worse." So I cheered up, and sure enough, things got worse. -- Paraphrased from James C. Hagerty From eryksun at gmail.com Mon Oct 31 01:15:18 2022 From: eryksun at gmail.com (Eryk Sun) Date: Mon, 31 Oct 2022 00:15:18 -0500 Subject: [Tutor] Does python change subprocess based on whether it is run in the interactive shell or not In-Reply-To: References: Message-ID: On 10/28/22, Nathan Smith wrote: > > p = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stdin=subprocess.PIPE, stderr=subprocess.PIPE) > [stdout, stderr]=p.communicate(data) > > Where data is some data taken from io.bytesIO.read > > And cmd is a command for ffmpeg like so: > > cmd="ffmpeg/ffmpeg.exe -f wav -ac 2 -i - -f mp3 -ab 128000 - -y" > > When run in an interactive shell this works like a charm. When run in a > script file, this breaks. At first I thought it was an issue with > FFMPEG, but now I'm wondering if python is changing something under the > hood? To clarify, do you mean that the subprocess.Popen instance and communicate() call work in Python's interactive shell (i.e. the REPL), but the same code fails in some way in a non-interactive script? Or do you mean that running the command line in an interactive CLI shell such as CMD works fine, but the Popen instance and communicate() call don't work in a Python script? If it's the latter, the main difference is that in Python you're spawning the "ffmpeg.exe" process with its standard I/O redirected to pipes instead of using the console. The resulting behavior can be complicated depending on how the handles for the pipes are used by the child process and inherited by any descendant processes that it spawns. Calling communicate() without a timeout won't return as long there is at least one potential writer to the stdout or stderr pipe (e.g. some descendant process that inherited a handle to the write end of the pipe).