Re: [Tutor] HELP PLEASE
On 13/08/19 7:11 AM, Marissa Russo wrote: This is my code: 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() return nums, nums2 Do you understand the concept of a "loop" - Code which is repeated as many times as necessary? How many files must be opened, read, and then averaged? def to_ints(strings): num_copy = [] for num in nums: num_copy.append(float(num)) return num_copy return to_ints(nums), to_ints(nums2) What is the purpose of this line, given that the previous line has returned to the calling code? Have I missed something? When is to_ints() used? def mean(nums): _sum = 0 return(sum(nums)/len(nums)) def main(): data = get_numbers() m = mean(data[0]) Do you know what data[ 0 ] (or [ 1 ]) contains? Might this knowledge be helpful? m2 = mean(data[1]) print("The mean of the first file is: ", m) print("The mean of the second file is: ", m2) main() This is the output of my updated code: Traceback (most recent call last): File "/Applications/Python 3.7/exercises .py", line 37, in main() File "/Applications/Python 3.7/exercises .py", line 33, in main m = mean(data[0]) File "/Applications/Python 3.7/exercises .py", line 29, in mean return(sum(nums)/len(nums)) TypeError: unsupported operand type(s) for +: 'int' and 'str' What do you think "TypeError" means? Do you know the difference between an "int" and a "str[ing]"? Given that both sum() and len() return numbers, what do you think is the "str"? Might this refer back to the earlier suggestion that you need to 'see' the data being read? -- Regards =dn ___ 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?
NB am heading somewhat OT NBB Python3+ On 3/08/19 12:38 PM, Alan Gauld via Tutor wrote: 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. This statement caused me to double-take. Do I misunderstand? Quick survey of my PC's Projects directory:- sure-enough, few of my classNMs are verbs and convey little/no idea of function. (to a degree one's expectations/experience lead to ideas of (what might be) included in its functionality) They are almost exclusively nouns, eg Customer, Person, Organisation; rather than verbs/functions, eg Selling, Addressing, Billing. [I'm not an OOP-native, and embraced OOP concepts as extensions to practices learned whilst programming with languages such as COBOL!] 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'. Python is not particularly dogmatic or "pure" in its following of the OOP paradigm. Unlike some languages it does not insist on OOP and will support elements of both "procedural" and "functional" programming. 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] NB the article was no help with regard to the naming of objects/classes. 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? Employee is a Person Circle/Oval/Square/Triangle/Shape has a CentralPoint Thus: class Person(): ... class Employee( Person ): ... Python says "everything is an object" and makes little/no distinction between "type" and "class": >>> class C: pass ... >>> i = C() >>> type( i ) >>> i.__class__ >>> type( C ) >>> C.__class__ So, what are Python's base types/objects/classes? eg int, str, list. Are these "data or function"? Expand that outwards into the PSL. Same: numbers, decimals, fractions. However, does "math" convey more "function" than "data"? There's current discussion about concerns of the age/relevance/maintenance of certain members within the PSL. So, let's look at a recent addition (it even features O-O in its description): "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! 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(). Whither "Classes should never be named for their data but for their function."? WebRef: https://en.wikipedia.org/wiki/Object-oriented_programming -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python code
On 2/08/19 3:23 AM, Spencer Wannemacher wrote: I'm new to python and I was trying to perform a simple one code. All that is included in the code is print(61). I save it as 61.py and change the directory before typing in python 61.py and I don't get an output. There is no error and the output is blank. Please let me know what I'm doing wrong. Thank you so much! What do you mean by "and change the directory before"? Python will start searching for 61.py in the *current* directory! -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Inserting long URL's into comments & docstrings?
On 30/07/19 8:36 AM, James Hartley wrote> On occasion, I put long URL's into comments/docstrings simply to document where I found specific information. However, to be a good disciple of PEP8, anything which can't fit within 72 characters needs to be split across multiple lines. Since a number of you seem to be prolific Python coders, what opinion do you have about splitting URL's in comments/docstrings? Assuming the use-case for including a URL-comment is to be able to locate the resource again, I'd leave it integral so that it is ready for a quick copy-paste. Otherwise you'd have to re-assemble the URL in the browser, from multiple comment-lines in the code... In addition to PEP-8, please consider 'the Zen of Python' (practicality and purity). python3 -c "import this" -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A question about daunting KeyError
On 24/07/19 3:21 PM, TAISHI KAWAMURA wrote: Hi tutors on Tutor, I'm Taishi from Japan working in a data analytics team. Currently, I'm trying to analyse purchase data of a fashion brand. However, mysterious KeyErrors started occurring continuously when I was coding, and I haven't been able to get that fixed. Although I asked this question on Stack Over Flow and another platform, I haven't got any solution. I'm suspecting that Anaconda might keep raising the error, or simply there are bugs in the codes. However, I can't be sure what the real cause is. Do you think you can help me out? If yes, please give me some assistance. People here are happy to help, but you'll need to show us the relevant code and perhaps some data if that's possibly the source of the problem. -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Generator expressions
Hi Animesh, Unfortunately the list server/email has removed the formatting from your sample, but no matter... On 24/07/19 5:06 AM, Animesh Bhadra wrote: # This code creates a generator and not a tuple comprehensions. my_square =(num *num fornum inrange(11)) print(my_square) # at 0x7f3c838c0ca8> # We can iterate over the square generator like this. try: whileTrue: print(next(my_square)) # Prints the value 0,1,4 exceptStopIterationasSI: print("Stop Iteration") # Another iteration forx inmy_square: print(x) # This prints nothing. Does the generator exhausts its values when we run the iterator once? Yes, it involves a "lazy" approach - the value is not calculated until the next() requests it. Whereas, a list comprehension calculates all its values at one time (and requires the storage space to hold them). https://docs.python.org/3/reference/datamodel.html?highlight=generator - notice that there is a yield and a next facility, but when it terminates StopIteration is raised. There is no 'start again' command! The Python docs are informative: https://docs.python.org/3/reference/expressions.html?highlight=generator https://docs.python.org/3/tutorial/classes.html?highlight=generator#generators If you have an actual reason for running through a generator expression more than once, then consider return-ing it from a function/class (which will then be directly accessible to the for-loop/next method). Lastly any specific reason for not having a tuple comprehensions? Have checked this link, but could not understood the reason? https://stackoverflow.com/questions/16940293/why-is-there-no-tuple-comprehension-in-python I don't know. Have you understood the differences between lists and tuples - specifically "mutability" and "immutability"? Let's take a list comprehension. If you 'unwind it', can you reproduce it as a multi-line for-loop? Yes, but before the loop the 'target' list must be defined/declared to be a list; and within the loop the list is appended with the 'generated' values. Ok? (sorry, don't know if this will be new to you, or not) Now, instead of a list, try using a tuple? How do you append() to a tuple? Yes, many people have confused generator expressions - surrounded/"delimited" by parentheses, ie ( and ), with tuples. However, try this little demonstration: a, b = 1, 2 a 1 b 2 a, b = ( 1, 2 ) a 1 b 2 ( a, b ) = ( 1, 2 ) a 1 b 2 type( a ) type( ( 1, 2 ) ) The principle here is known as "tuple unpacking". The two constants (right-hand side) are arranged as a tuple, as are the two variables (a and b/left-hand side), regardless of the presence/absence of the parentheses! https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/ Clarifying the difference/similarity in appearance between a generator expression and a tuple, it might help to think that it is the comma(s) which make it a tuple! -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lengthy copyright notices?
On 18/07/19 10:08 AM, Alan Gauld via Tutor wrote: 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. Open source: I've had the same - even for short "shazzam" or "drum-roll-please" sound-clips (which advertisers use all the time - who can name the pieces of music without saying "The lady loves Milk Tray" or "British Airways"?). That said, internationally there are many definitions and variations of "fair use" - and some jurisdictions don't even recognise such a thing! (I think the British law is quite 'tight'). I refused such a request?instruction, suggesting that the publishers AND their lawyers should enter 'the real world' and learn to understand (?and embrace) "open" concepts. In response to the inevitable grumpy push-back, I pointed-out that I am an author/content-producer and not legally-trained (not quite true, but they don't know that) so why on earth would they take MY advice... Another one is 'images', which as a keen (amateur) photographer I readily understand from both 'sides' - which simply meant that in a fit of pique at the aforementioned KYA-bureaucrats, I made a point of copyrighting EVERY training diagram/illustration/code-snippet that I produced and then REQUIRED 'the blighters' to license them from me... (yet insisted that such be 'free' for trainees to download for their own use) Hah! Closed source: I was once contacted by someone who had hold of a small system I'd written way back in the mists-of-time. How, they ever obtained it, I can't imagine. The 'license' clearly said "no license", but he wanted to negotiate - which was great (although sadly, the client still "owned" the software, so no 'winning the lottery' for me then!) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lengthy copyright notices?
On 16/07/19 12:47 PM, Alan Gauld via Tutor wrote: On 15/07/2019 23:34, Mats Wichmann wrote: Rule #1: it's all opinion in the end... Not quite. Different jurisdictions (remember, this list has an international membership!) have different understandings of (even, respect for) copyrights and permissions. I live in a jurisdiction where whatever I write ("create") is mine - or my employer's. It is not even necessary to claim or "exert" copyright! However, under the 'different strokes...' rule, I still include a copyright/license statement - if only to avoid misunderstandings in places that have other requirements and to at least 'make an effort' to communicate with those who have no such concepts - either in society, or (un)enshrined in law. 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 Some disagreement here. (but nothing worth fighting-over!) 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. The latter giving as much space for whatever you (or your legal representative(s) ) might want to say! There can be quite an accumulation of 'paper-work' at the top of modules, which then has to be scrolled-through before we can get stuck-in to function/class/__main__ code - even with an editor's code-folding assistance. Should it be left to the (far) end of the file? Would it lessen any legal implication? Aside from possibly irritating 'the good guys', does such really 'stop' a determined rapscallion? (...desperate student, lazy 'professional', corporate 'raider'...?) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lengthy copyright notices?
On 16/07/19 10:34 AM, Mats Wichmann wrote: On 7/15/19 3:25 PM, James Hartley wrote: help(module_name) will place any text in the *first* module-level docstring into the description section of the help page in Python 3.4.5. Subsequent docstrings found at module level are ignored. I have been using this factoid for placement of a copyright & licensing notice. By placing a rather lengthy copyright & license in the code in a the second module-level docstring, it is prominent within the code, but not cluttering up help() output. Two questions. Is there a more standardized way of including long license descriptions in code, & is it documented that any other module-level docstring will be ignored in help() output? Rule #1: it's all opinion in the end... The common practice is that licence/copyright text is included as a comment in the code, not in a docstring. Packaging and project templating offer/recommend a separate file for 'legal-stuff', eg licensing. That said, I also include a one-liner at the top of every module, adding __license__ to several other similar labels/definitions. One thing is for-sure: when calling for help or reminding myself of method signatures, I'd be greatly irritated by having to wade-through a flood of irrelevance. On the other hand, if 'you' use my work... Whether either/both of these pass for pythonic, I can't say. -- Regards =dn ___ 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 8/07/19 10:54 AM, Alan Gauld via Tutor wrote: 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 Wow, go you! I could never cope with using analog tape decks as digital storage devices. I've just decided to take a look at SBCs. I guess the 'splash' of news about the Raspberry Pi 4 contributed to that, and yet also contributes to my prevarication/procrastination... My 'first' was at high school - an over-grown accounting machine. Paper-tape program-software, Teletype-style input and 'line-flow' output, and a magnetic drum for data-storage (and yes, I was computing physical locations in order to optimise response times - try that!) At uni, we built Motorola D2 Kits - IIRC an 8-bit Motorola MC6800 processor. Maybe a 6809 - or more likely, that was the one to which we aspired. Those who could 'afford' more hardware courses started building intelligent devices, eg embedding a microprocessor within a 'dumb terminal'/'green screen'. Intelligent devices, Internet of Things. Plus ça change! My third, a CP/M machine with 64K and 256K RAM disk and dual floppies - such luxury! :-) Ah nostalgia. One of my divertissements of that era (early-80s) was MP/M - a bus network of what were effectively single-board processors. It implemented my first 'computer lab' and we taught everything from COBOL to accounting software and word processing on it. Fantastic stuff in its day! (interestingly, my SBC research last night, took me to a R.Pi device embodying exactly these concepts: https://www.notebookcheck.net/Raspberry-Pi-Combine-a-Raspberry-Pi-with-up-to-4-Raspberry-Pi-Zeros-for-less-than-US-50-with-the-Cluster-HAT.426998.0.html) 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. Agreed, but if we move beyond standard dict-s, into multi-keyed data structures - even with PSL and PyPI at our disposal, isn't it much-of-a-muchness to use MySQL/SQLite versus linked-lists or trees? (or perhaps am showing too much bias from personal experience?) The "reluctance" (good word!) is intriguing: (a) one more package/language to learn - yet such claimants might well have been the ones leaping into NoSQL a few years back; and (b) it is a different way of thinking - compare 'spaghetti' and monolithic code to "structured", procedural to OOP, OOP to 'functional'... I notice a similar likelihood to avoid HTML/CSS because of their 'declarative' approaches. Hey, if you don't like the green ones, that's all the more Smarties/M for me! (also, avoid the brown ones, they may be 'sheep pellets'/rabbit droppings...) -- Regards =dn ___ 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 8/07/19 2:48 AM, Alan Gauld via Tutor wrote: 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. Assuming plentiful RAM: agreed. (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 - also, in another thread (here?Python list) we had someone frustrated about using an MS-Vista 'powered' machine and limited to 32-bits. We don't know the OP's circumstances. That said, loading an RDBMS, if (s)he doesn't already have one, is...) As you point-out, with memory more-commonly available, I've obtained significant speed improvements by moving relatively small, and particularly temporary, DB tables into MySQL's MEMORY storage (and with almost zero code-change/risk)! (so, it IS possible to teach old dogs new tricks) The key justification for moving to RDBMS would be "not the only selection mechanism". Whereas a Python dictionary (hash) offers speedy access to data based upon a single index, it is hard to beat the bug- and time-saving facility of a DB managing multiple indices/indexes. (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) So, using the OP's data-example, and assuming the 'columns' to be perhaps employment_date, name, and age; respectively: ['2019-01-19','Fred Flintstone',23], ['2019-02-01','Scooby doo', 99] - which Python (and pythonic - per OP's theme) structures and methods offer a relatively bug-unlikely solution to holding *multiple* indices into a base list (or other collection)? (alternately, maybe we should wait for the OP, and allow opportunity to complete the homework first?) (NB this may be veering OT, if the OP requires only the single access method, such as that illustrated earlier) -- Regards =dn ___ 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.
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. On 7/07/19 2:39 PM, mhysnm1...@gmail.com wrote: Hi all. 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. What I want to do is: Just because C has a construct does not imply that it does, nor even should, exist in another language! You're using Python because it is 'better', right? You are correct, Python does not use "pointers", and (a personal comment) I for one don't miss them and their many 'gotchas', eg out-by-one errors, preferring Python's constructs, eg for-each. That said, Python's sequences (data structures, eg strings and lists) do offer indices, slicing, and striding. So, it is quite possible to (relatively) address the first item in a list as list_item[ 0 ]. You can read about these (and many other delights) in the docs... I want to create different data structures such as dictionaries which contain specific list elements based upon specific keys. The original data structure could look like: Data = [ ['2019-01-19','Fred Flintstone',23], ['2019-02-01','Scooby doo', 99] ] Warning1: seem to be missing any identification of the "key" Warning2: the intro text talked about "dictionaries" (the correct word) but the code-snippet is describing nested lists The above structure does have 100's of elements. I want to reference specific lists within the above structure. Using the only method I know how: 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. Not the memory address of the nested list I am referencing. I am using a list within the key to permit multiple references to different nested lists from the original data structure. The end result of the structure for the dict could look like this (using example, not real output) Category['under-50'] = [ List1 pointer, List22 pointer, List52 pointer] I hope the above makes sense. How can this be done? I hope I've understood the description! One option would be to follow your line of thinking by turning the first data-structure into a dictionary (key-value) pairs, where the key is the character's age and the value is the inner list structure, previously outlined: { 23: ['2019-01-19','Fred Flintstone',23], 99: ['2019-02-01','Scooby doo', 99] } Then it would be possible to maintain the two lists, each containing keys for the relevant dict-elements: under_50 = [ 23 ] over_50 = [ 99 ] However, this would require that only one character be listed at a given age (dict keys must be unique), so another key might be a better choice! Another data structure you might consider is a "linked list". -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Was: Basic Question about Visualization for enduser
On 26/06/19 4:56 AM, Alan Gauld via Tutor wrote: 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) NB this is NOT intended as a discussion about the specifics of the OP's situation, nor of the OP personally! 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). However, such illustrative devices are often not what is used in the 'average' development department (assuming there is such a thing). Thus the training meets the knowledge-need (we hope) but not the requirements of practical application in the work-place. 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'? Your thoughts? -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] replacing a loop
Hi John, On 25/06/19 4:15 AM, johnf wrote: Hi folks, I have the following loop (actually repeated many times ) 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 where locDS is a tuple of dicts and a row is a dict. Since I use a lot of similar loops to populate many dropdown controls I started investigating the use of list comprehensions. But I can't figure out how to use them in this loop and wonder if it will improve the performance. The data is not very big - about a thousand rows - give or take. So what do you guys think? Just because this morning I needed something to kick-start my sleepy brain into being able to "think"... When most people think of 'multiples' in the context of list-comprehensions, they are talking about "nested-loops" - which are easy-enough (or at least, well-documented). However, I've often wondered about the multiple being on the 'left-hand side' of the equation/expression, and whether that is even possible? It is! Thus: python3 Python 3.6.8 (default, Mar 21 2019, 10:08:12) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux Type "help", "copyright", "credits" or "license" for more information. # build the data-structure (I hope) >>> d1 = { 'pkid':1, 'facility':'here' } >>> d2 = { 'pkid':2, 'facility':'there' } >>> d3 = { 'pkid':3, 'facility':'nowhere' } >>> locDS = ( d1, d2, d3 ) # let's check that the data-format is as-described? >>> type( locDS ) >>> for row in locDS: print( row ) ... {'pkid': 1, 'facility': 'here'} {'pkid': 2, 'facility': 'there'} {'pkid': 3, 'facility': 'nowhere'} # are we on the right track? # this is the original code and approach >>> loc_Choices = [ '' ] >>> locKeys = [ 0 ] >>> for row in locDS: ... loc_Choices.append( row[ 'facility' ] ) ... locKeys.append( row[ 'pkid' ] ) ... # which (I hope) shows the existing (and desired) results >>> print( loc_Choices ) ['', 'here', 'there', 'nowhere'] >>> print( locKeys ) [0, 1, 2, 3] # we can do the list inits, cf leaving it to the list comprehension >>> loc_Choices = [ '' ] >>> locKeys = [ 0 ] # but how might we achieve this using a (single) list comprehension? >>> [ [ loc_Choices.append( row[ 'facility' ] ), locKeys.append( row[ 'pkid' ] ) ] for row in locDS ] # in (proper) code, I'd format this over two lines (at least) # correct me if I'm wrong, but these o/ps will # 'disappear into the ether' when run within a pgm... [[None, None], [None, None], [None, None]] # leaving us with 'the proof of the pudding' >>> print( loc_Choices ) ['', 'here', 'there', 'nowhere'] >>> print( locKeys ) [0, 1, 2, 3] >>> Possible considerations: 1 some sample data would have eased the way/its lack leaves room for misinterpretation 2 a list comprehension normally executes more quickly than the traditional (long-form) multi-line code-block. However, it would be worth measuring that on your particular machine h/w and Py__version__. The other 'efficiency' is readability, but "elegance" is largely a matter of (personal) taste. So that ('mine-field') I leave to you... 3 taking the multi-line code-block and splitting it into TWO separate (consecutive) list comprehensions (one for loc_Choices and the other for locKeys) will most-likely be noticeably MORE 'expensive' 4 it is no matter if row/the locDS tuple dict-elements contain more key-value pairs 5 during code review, my colleagues would delight in criticising the choice of names such as locKeys - describing them as "JavaScript" (a 'dirty word' to some), loc_Choices as contrary to PEP-8 ("flake", or whatever), etc - despite that I have a little 'set' of abbreviations with which I do the same, eg locNR. YMMV! 6 however, this simple boy will venture criticism of the inconsistency in using the underline word-separator, eg locKeys but loc_Choices. Even more potential for confusion: locChoices and loc_Choices!? 7 consider the data structures (outside of our view, here). Assuming there are more elements in each 'loc dict', might it be just as easy to leave the two pertinent elements 'there' and iterate over locDS when actually applied. Alternately, perhaps it might be better to construct a dictionary with the 'pkid's as keys and the 'facility' as values for direct-access application? Not knowing how the structure(s) will be utilised makes this pure speculation! 8 any?every time "similar loops" are found, re-factor to a function (let's see if that philosophy kicks-off a retort or two...) 9 was it Knuth who described "premature optimisation" as "evil"? Certainly I'm a believer in 'make it work before you make it better'. So, (after all that!) what are we really achieving here? With such quantities, is it critical to 'save time'? If so, is this part of the code really the greatest time-sink? (and we're back to
Re: [Tutor] Follow-up on my removing elements from lists question.
On 15/06/19 9:35 PM, mhysnm1...@gmail.com wrote: This is a follow-up on my previous question for removing elements. Below is the code I am currently using. I am removing the elements at the end of the outer loop. The data structure goes along this: [ ['123123',[2019-2-18', 'transaction text', 'amount'], v ['123123',[2019-2-18', 'transaction text', 'amount'], ['123123',[2019-2-18', 'transaction text', 'amount'] ] The 2nd column where the transaction text I am modifying the content and using the end result of the built-up words as the string match as you will see in the code. This is all working fine. The last loop in the code I am trying to delete the elements in reverse order. This doesn't work. The length of the list reduces by 1. When it should have reduced by 42. Is the logic wrong? This is in Python 3.6 under windows 10. unknown_transactions.sort(key=lambda x: x[2]) while True: # re-initialise each time the current transaction text has been processed. for row in unknown_transactions: # remove common words from transactions which are not required. Such as 'WITHDRAWAL' and 'DEPOSIT'. line = regex_transaction(row[2]) # If the common words are not found, return a null and do not modify the transaction description. (from a very weak understanding of your previous question and the total code-base thus far) Consideration nr1: Write the code as comments first. Initially these will be at a fairly 'high level'. These comments can later be turned into function/method names, and more comments added within those. Wash, rinse, and repeat. The idea is to help re-state your thinking into Python code, and to structure the code into functional units. Even skilled Python-coders often find that this helps to keep the use-case foremost in-mind. Consideration nr2: (assuming that the total data-volume is easily RAM-resident) Rather than (appearing to) taking-in a 'group' of transactions and then select them according to ease/difficulty of 'translation', gradually removing/whittling the numbers down - hopefully to zero; why not consider adding another field to each record, which will note if it has already been processed (by whichever method) or conversely, which list-elements are yet to be completed? Thus each method of interpretation will first check the 'completed' field, and if not complete, apply the relevant analysis... Thus there is no concept of 'removal' and no danger of 'losing' anything! -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for some direction
On 18/05/19 4:15 AM, Alan Gauld via Tutor wrote: On 16/05/2019 04:17, Alex Kleider wrote: Alt-Tab and the X cut 'n paste mechanism provides enough integration between windows. I tried this (Ubuntu 18.4) and Alt-Tab cycles between terminal and browser but I can't make it cycle from one terminal to another. How do you do that? Sorry I've been busy, my mother passed away early this morning and we've been with her the last few days. Sympathies! FWIW I just use Alt-Tab. I'm on Linux Mint with Cinnamon. Yes, and Ctrl-Tab to switch windows within an application (Cinnamon on Fedora) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for some direction
On 13/05/19 6:13 AM, Alan Gauld via Tutor wrote: On 12/05/2019 10:15, David L Neil wrote: Interestingly, I split these into two - my laziness for running/testing is 'Save, Alt-Tab, Up-arrow, Enter' which would be 'ruined' by using the cmdLN for anything else. In a bash shell I use Ctr-R (for reverse search) and hit py to run the last python command. So for me its Alt-Tab, Cmd-R, py 2 characters extra and I get to use the OS for whatever I like in between... :-) Good one! -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for some direction
On 12/05/19 7:59 PM, Alan Gauld via Tutor wrote: On 12/05/2019 00:24, David L Neil wrote: "3 consoles": what is the purpose of each? (my first reaction stemmed from many editors including a built-in console) One for vim, yes - that 'replaces' the window/app running an IDE/GUI-based editor. * One for the Python interpreter * One for an OS shell used for running/testing the app plus any miscellaneous Unixy type things I need to do (sed/grep/awk/git etc). Interestingly, I split these into two - my laziness for running/testing is 'Save, Alt-Tab, Up-arrow, Enter' which would be 'ruined' by using the cmdLN for anything else. I might even 'raise' your bid, by adding a third terminal here - when needing to ssh into a VPS or other remote m/c. I'll usually also have a web browser for reading documentation if necessary, although that's mostly done in the interpreter using dir()/help(). +1 (to make it 'worse', I have two web browsers open - plain-Firefox for 'ordinary browsing' and the FF Development Edition for, well, development work. Yes, that habit probably came out of testing web stuff in multiple browsers, but now the DE is where I cruise the PSL docs, etc, as a de-facto separation-of-concerns in my mind. Alt-Tab and the X cut 'n paste mechanism provides enough integration between windows. Very handy! * > One for the Python interpreter * I also do this, all-the-time. My youngest/new grad colleague observing this last week, was most puzzled. He felt it was 'double-handling' because "most ideas could/should be hashed-out in TDD design" cf 'code experimentation'. Will be interesting to see if he takes-on the idea, or continues to tease 'the old boy'... (any comment if this is my failing to (fully) appreciate TDD philosophy?) I'm using Gnome Terminal under Fedora (Linux). This allows multiple terminals in tabs (and thus Ctrl-Tab rapid-switching). However, it irritates me that whilst I can set "profiles" for particular purposes; there does not seem to be a way to save a 'session'. Thus each time Terminal re-starts, I have to re-build each terminal, manually. (suggestions of other similar tools would be most welcome) I'm also conscious of advising the OP on IDEs/editors: in that I am currently using Sublime Text but find the "build" mechanism quite alien to Python (HTML, CSS, JS, etc - but not Rust!). Similarly, I need to learn more about using ST's built-in terminal (preferably at full-size rather than a few lines at the bottom of the editing window). That might cut-down on one concurrent terminal... (similarly, am open to suggestions for improving Python dev using ST) Thanks for your feedback! -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Collating date data from a csv file
Hi Dave, I also volunteer to do PAT safety testing during my "20% time". Clambering around Snowdonia as a boy, I eschewed* the Rheilffordd yr Wyddfa/SMR in favor of shanks' pony... * OK, I was made to...! For the good of my soul??? On 9/05/19 8:04 AM, Dave Hill wrote: I have a csv file which details the results of equipment tests, I carry out PAT testing as a volunteer at a heriatge railway in N. Wales. I want to extract how many items were tested on each test day. So far I have generated a List of test dates, but I am now stalled at how to efficiently count numbers tested on each date. Can I have a list of tuples, where one item is the date and the second the count? or is there a better construct? Thanks in advance, Dave For completeness, I have listed below an extract from a target file, where the 10 digit number is the UNIX timestamp 182 1515001232 Toaster 13 2000 1 183 1515001259 Contact Grill 13 2000 1 245 1515001367 3G Cube Adaptor 13 0 1 246 1515001396 13A IEC Lead 5 0 1 248 1515001415 Worktop Light 3 30 1 420 1515001440 Fly killer 0 0 1 424 1515001461 Dairy fridge 13 0 1 427 1513277293 Fire 13 0 1 429 1515001489 Toaster Avanti 13 0 1 When you say "target file", is this coming off the tester via a link cable to your PC, or are you capturing by hand to a spreadsheet? A tactic which many people 'miss' is that a workbook may contain multiple spreadsheets, and that the data on one spreadsheet may be auto-magically 'copied' onto another. Thus if the above is data coming off the PAT into one spreadsheet, I would immediately create a more meaningful sheet, 'for human consumption', which has column headings and converts (re-formats) the timestamp into a readable date (as suggested elsewhere), but is otherwise pretty-much a direct copy. We now have a sheet used for data capture/computer processing and something separate (and prettier) as a report/presentation for people. From the spec, above, we are only interested in the date. Remember that considering the whole timestamp only makes life confusing. So convert them (only) to dates. These can be strings because Python compares strings as easily as dates! The time component could be retained if sequence (of testing) might be important. The sad reality is that a daily count could be accomplished in either LO-Writer or MS-Excel. No reason why you shouldn't use Python though. (Assuming that the data appears in (forward or reverse) date sequence) Read-in the data sheet/CSV file, row-by-row, taking note of the date of the first data-entry, and starting to count from one. Then increment for each row where the date matches. When the dates don't match, report, reset the counter, and note the new date. How will you lay-out and present this report? Another spreadsheet? Screen? Paper? When you say "count numbers tested on each date", the above method will let you know a (single) daily total of tests-performed. Did you (also) mean that you want to track how many of tests were performed within categories of devices, eg how many toasters on the one day? In which case, further design consideration is required, eg which devices fit into which category and how to match "Toaster" with "Toaster Avanti"... -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for some direction
On 12/05/19 10:57 AM, Alan Gauld via Tutor wrote: On 11/05/2019 19:59, Cranky Frankie wrote: ... 1) For the IDE I'm most comfortable with Netbeans/Java, In that case use Netbeans. I use Netbeans myself when working with Java and have played with its Python implementation and its OK. Personally for serious Python work I just use vim and 3 consoles, but that's a matter of taste. but you should minimise the number of learning points and learning a new IDE while also learning OOP (and a GUI framework?) is just adding extra work. "3 consoles": what is the purpose of each? (my first reaction stemmed from many editors including a built-in console) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] self.name is calling the __set__ method of another class
Hi Arup, On 30/04/19 5:55 AM, Arup Rakshit wrote: class NonBlank: def __init__(self, storage_name): self.storage_name = storage_name def __set__(self, instance, value): if not isinstance(value, str): raise TypeError("%r must be of type 'str'" % self.storage_name) elif len(value) == 0: raise ValueError("%r must not be empty" % self.storage_name) instance.__dict__[self.storage_name] = value class Customer: name = NonBlank('name') email = NonBlank('email') def __init__(self, name, email, fidelity=0): self.name = name self.email = email self.fidelity = fidelity def full_email(self): return '{0} <{1}>'.format(self.name, self.email) if __name__ == '__main__': cus = Customer('Arup', 99) Running this code throws an error: Traceback (most recent call last): File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 25, in cus = Customer('Arup', 99) File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ self.email = email File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 7, in __set__ raise TypeError("%r must be of type 'str'" % self.storage_name) TypeError: 'email' must be of type 'str' Process terminated with an exit code of 1 Now I am not getting how the __set__() method from NonBlank is being called inside the __init__() method. Looks like some magic is going on under the hood. Can anyone please explain this how self.name and self.email assignment is called the __set__ from NonBlank? What is the name of this concept? Use the tools provided - follow the Traceback and interpret each step:- > cus = Customer('Arup', 99) means: instantiate a Customer object, which takes us to > def __init__(self, name, email, fidelity=0): where: - name is set to a string: 'Arup' - email is set to an *integer*: 99, and - fidelity is set to become another integer with a value of 0 (in the first instance) Ignoring name, we arrive at > self.email = email which *appears to be* the creation of an integer(!) within the cus Customer instance. However (the "magic") when the module was loaded into the Python interpreter self.email has already been defined as: > email = NonBlank('email') which means that: > def __init__(self, storage_name): > self.storage_name = storage_name made it (past tense!) an instance of the NonBlank object with a storage_name of email. (and with a __set__ method). So, returning to the Trace, specifically: >File "/Users/aruprakshit/python_playground/pycon2017/decorators_and_descriptors_decoded/customer.py", line 18, in __init__ > self.email = email what now happens is that the self.email instance of a NonBlank object receives the value passed-in as email (ie 99), and invokes the method: > def __set__(self, instance, value): In due course, we find that 99 is not an acceptable value: > if not isinstance(value, str): > raise TypeError("%r must be of type 'str'" % self.storage_name) and thus: > TypeError: 'email' must be of type 'str' > Process terminated with an exit code of 1 Crash! Of course it is a 'toy example' - when you could plug two 'is it a string' checks straight into Customer, why not keep-it-simple and do just that? - without the added abstraction on top of an abstraction! However, the author is illustrating a useful tool - should you find a situation where the 'checks' are much more involved or complex. (NB in addition to, not an alternative to, the discussions Steven has offered) Given previous conversations, I'm not surprised that you were mystified. The fact that I had to read it twice, and that the above explanation is NOT a 'straight line', indicates that there is probably a better (more simple) approach - and one which is MUCH more likely to be understood by our Python programming colleagues (possibly including our 'future selves'!) As Steven explained, this is a complex environment where only those with a good understanding of the meta abstractions would even want to play (IMHO). Perhaps you would be better served by actually writing some Python applications, and with such experience under-your-belt, adding these 'advanced knowledge' ideas at some later time, if/when needed?) Assuming use of a recent version of Python, you may like to solve this specific problem the same way you might in other programming languages: <<< typing — Support for type hints New in version 3.5. Note The typing module has been included in the standard library on a provisional basis. New features might be added and API may change even between minor releases if deemed necessary by the core developers. This module
Re: [Tutor] [OT] Problem with auto-complete package installation in Atom [Was: Re: After virtualenv, how to use ide]
apologize in advance! But that is okay. I am not a professional nor expert programmer myself. Because of this, I recall agonizing over the choice of code editor, or, possibly even worse, deciding whether to use an IDE or editor. This is a difficult decision about which many wax poetic and express their opinions with great emotion. But for professional programmers is a mere detail of very little import. Its almost as irrelevant as the choice of programming language. A professional uses the tools [sh]e is given(or dictated). Sure we all have our favourites (personally I favour a Unix-like OS using emacs/vim or Netbeans) but we use what we must. +1 The last time I had this conversation, in person, we were working on some HTML5 training and a colleague wanted to know how/why I edited something. I was using PyCharm, which he'd not seen before, and used a facility he had never seen, but which was also a little longer than one he would have used in his chosen-tool. Accordingly, he sought to 'educate' me... Having used many editors over the years, cough, mumble, decades; I suggest choosing one AND sticking with it for a long time, eg one year. Maybe as part of your new year celebrations you might choose another. In the meantime, I find it takes a while to stop trying to make 'this editor' look like the last one; and to learn how to best use the powers (and avoid the foibles) of this year's tool to actually improve one's productivity.. The fact remains, many of 'the big names' today offer many more facilities that one person will ever want to use, especially if working exclusively in one language! Perhaps the 80/20 rule applies? eg I've been using SublimeText for the last six+ months, and if I've more than scratched the surface, I'll be surprised. Even in my slightly-expanded-upon-the-vanilla installation, there are still lint-ing codes and sundry Python helps that bemuse me (and, obviously, I've simply ignored). But in my time I've alo used - CP/M and ed (or wordstar) {assembler, C, Pascal, Logo} - OS/9 and scred, (assembler, C, BASIC} - IBM MVS and ISPF, (COBOL, JCL, SQL} - Windows and VS (or Borland IDE.) {ASM, VB, Pascal, C++, Java) - DEC VAX/VMS and Edit/TPU {C, C++, DCL} How about MP/M? OS/360 etc before MVS? Extra respect for avoiding the PC-DOS/MS-DOS fandango. I find that the older I become, the longer it takes to re-wind all this nostalgia. I can't even remember the name of the editor I used on my slide-rule PC! Sorry for the rant but I get very depressed by some of the discussion that happens on programming fora, especially when there are so many genuinely interesting things to talk about! Agreed that it tends to be a newcomer's topic; and that one tends to use the same tools as the rest of the team; but it is an important consideration - I've never forgotten the first time I was asked (um, instructed) to change every instance of "xyz" in each source-code file in ... whereas today most professional-grade editors will achieve such in a single Find-Replace! (am not sure if that can be achieved with today's Windows-NotePad, but equally, I'd struggle to find a good reason for such an action 'today' and in Python!) -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Merge a dictionary into a string
On 18/03/19 6:05 AM, Mats Wichmann wrote: On 3/16/19 11:39 AM, Valerio Pachera wrote: Consider this: import collections d = OrderedDict(a='hallo', b='world') I wish to get a single string like this: 'a "hallo" b "world"' Notice I wish the double quote to be part of the string. In other words I want to wrap the value of a and b. So the question that comes to mind is "why"? I don't mean that in the negative sense as in you don't want to do that, but your use case may drive the choice of possible solutions. Reading the OP, I immediately recognised the problem - meantime others had responded and the f'string' suggestion seemed most apropos. Somewhat intrigued, and to answer the use-case question, I went looking in my personal collection of RDBMS routines and "snippets" (which have hardly been updated since Py2, excepting (IIRC) when MySQL's Connector-Python extended into dictionary-cursors). The Connector will automatically delimit field/colNMs passed within a variable collection ('escaping' practice, highly recommended!) - a SELECT clause (for example). However, such automation is not applied to similar appearing in other clauses. One of my helper-routines creates a comma-separated string by first surrounding columnNMs with back-ticks and then .join()ing. It's not rocket-surgery, but has been handy and import-ed many, many times. YAGNI: me being me [puffs-out chest in a most unattractive fashion], one of the function's optional arguments offers a choice of delimiter. Can't recall ever using it 'elsewhere' though. Thanks to the OP, and respondents making me think. Have added to my >=v3.6 Backlog... -- Regards =dn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor