From savageapple850 at gmail.com Wed May 1 08:18:21 2019 From: savageapple850 at gmail.com (Cravan) Date: Wed, 01 May 2019 20:18:21 +0800 Subject: [Tutor] Random ai win glitch Message-ID: <1F47C011-4248-4D86-AF1E-F0D59DBC6D88@gmail.com> Hi all, ??????????????? I?m a beginner using python3, and recently learnt oop in pygame. What I?m trying to create is a tug of war game, but somehow my console prints Ai wins even before the player crosses the middle line.Here is my code: newsettings.py: # Basic colours BLACK??? = (?? 0,?? 0,?? 0) WHITE??? = ( 255, 255, 255) GREEN??? = (?? 0, 255,?? 0) RED????? = ( 255,?? 0,?? 0) BLUE???? = (?? 0,?? 0, 255) YELLOW?? = ( 255, 255, 0) ORANGE?? = ( 255, 165, 0) PURPLE?? = ( 100, 0, 100) #settings WIDTH = 1024 HEIGHT = 768 FPS = 60 TITLE = "War of Tugs" BGCOLOR = GREEN spritesdata.py: import pygame as pg from os import path import os from newsettings import * size = (WIDTH, HEIGHT) screen = pg.display.set_mode(size) pg.display.init() game_folder = os.path.dirname(__file__) resources_folder = os.path.join(game_folder, 'resources') stickmanplayer_img = pg.image.load(os.path.join(resources_folder, 'stickmanplayer.png')).convert() stickmanai_img = pg.image.load(os.path.join(resources_folder, 'stickmanai.png')).convert() #https://www.shutterstock.com/image-vector/stick-figure-pulling-rope-coiled-1140317804?src=c7Vbr97B4rIRsJ9OYUVcLw-1-0 string = pg.sprite.Group() all_sprites = pg.sprite.Group() stickman_ai = pg.sprite.Group() stickman_player = pg.sprite.Group() AI_wins = 0 Player_wins = 0 class Player(pg.sprite.Sprite): ??? def __init__(self, game, x, y): ??????? self.groups = game.all_sprites ??????? pg.sprite.Sprite.__init__(self, self.groups) ??????? self.game = game ??????? self.image = stickmanplayer_img ??????? self.image.set_colorkey(BLACK) ??????? self.rect = self.image.get_rect() ??????? self.rect.x = x ??????? self.rect.y = y ??????? self.dx = 1 ??? def move_left(self,lol): ??????? if self.rect.left > 10 and lol.rect.x > 310: ??????????? self.rect.x -= 30 ??????????? lol.rect.x -= 30 ??????? else: ??????????? self.rect.left = 10 ??? def update(self): ??????? if self.rect.right < 770: ??????????? self.rect.x += self.dx class AI(pg.sprite.Sprite): ??? def __init__(self, game, x, y): ??????? self.groups = game.all_sprites ??????? pg.sprite.Sprite.__init__(self, self.groups) ??????? self.game = game ??????? self.image = stickmanai_img ??????? self.image.set_colorkey(BLACK) ??????? self.rect = self.image.get_rect() ??????? self.rect.x = x ??????? self.rect.y = y ??????? self.dx = 1 ??? def update(self): ??????? if self.rect.right < 1000: ??????????? self.rect.x += self.dx class String(pg.sprite.Sprite): ??? def __init__(self, game, x, y, height = 10, width = 320): ??????? self.game = game ??????? self.groups = game.all_sprites ??????? pg.sprite.Sprite.__init__(self, self.groups) ??????? self.height = height ??????? self.width = width ?? ?????self.surface = pg.Surface((2 * self.width, 2 * self.height)) ??????? self.surface.fill(YELLOW) ??????? self.surface.set_colorkey(YELLOW) ??????? pg.draw.line(self.surface, BLACK, [0,0], [self.width,0], self.height) ??????? self.image = self.surface ? ??????self.rect = self.image.get_rect() ??????? self.rect.x = x ??????? self.rect.y = y ??????? self.dx = 1 ??? def move_to_player(self): ??????? if self.rect.left > 100: ??????????? self.rect.x -= 30 ??????? else: ??????????? self.rect.left = 100 ??? def update(self): ??????? if self.rect.right < 1300: ??????????? self.rect.x += self.dx ??? def check_win(self,lol): ??????? global Player_wins ??????? global AI_wins ??????? if self.rect.right < lol.rect.x: ?????????? ?Player_wins += 1 ??????????? print("player wins") ??????? if self.rect.left > lol.rect.x: ??????????? AI_wins += 1 ??????????? print("ai wins") class Middle_Line(pg.sprite.Sprite): ??? def __init__(self, game, x): ??????? self.game = game ??????? self.groups = game.all_sprites ??????? pg.sprite.Sprite.__init__(self, self.groups) ??????? self.width = WIDTH + 100 ??????? self.height = HEIGHT ??????? self.surface = pg.Surface((2 * self.width, 2 * self.height)) ??????? self.surface.fill(WHITE) ??????? self.surface.set_colorkey(WHITE) ??????? pg.draw.line(self.surface, RED, [200, 0], [200, self.height], 5) ??????? self.image = self.surface ??????? self.rect = self.image.get_rect() ??????? self.rect.x = x main.py: import pygame as pg import sys import random import math import time from os import path from newsettings import * from spritesdata import * clock = pg.time.Clock() Player_wins = 0 AI_wins = 0 class Game: ??? def __init__(self): ??????? pg.init() ??????? Player_wins = 0 ??????? AI_wins = 0 ??????? self.screen = pg.display.set_mode((WIDTH, HEIGHT)) ??????? pg.display.set_caption(TITLE) ??????? self.clock = pg.time.Clock() ??????? self.time = pg.time.get_ticks() ??????? pg.key.set_repeat(500, 100) ??????? self.all_sprites = pg.sprite.Group() ??????? self.player = Player(self, 249, 384) ??????? self.ai = AI(self, 550, 430) ??????? self.middle_line = Middle_Line(self, 300) ??????? self.string = String(self, 350, 500) ??? def run(self): ??????? # game loop - set self.playing = False to end the game ??????? self.playing = True ??????? while self.playing: ??????????? self.dt = self.clock.tick(FPS) / 1000 ??????????? self.events() ??????????? self.update() ??????????? self.draw() ??? def quit(self): ??????? pg.quit() ??????? sys.exit() ??? def update(self): ??????? self.all_sprites.update() ??????? self.string.check_win(self.middle_line) ??????? self.all_sprites.update() ??? def draw(self): ??????? self.screen.fill(BGCOLOR) ??????? self.all_sprites.draw(self.screen) ???????font = pg.font.SysFont('Arial', 30, True, False) ??????? text = font.render("PLAYER WINS:" + str(Player_wins), True, BLACK) ??????? screen.blit(text, [50, 50]) ??????? font = pg.font.SysFont('Arial', 30, True, False) ??????? text = font.render("AI WINS:" + str(AI_wins), True, BLACK) ??????? screen.blit(text, [790, 50]) ??????? self.all_sprites.update() ??????? pg.display.flip() ??? def events(self): ??????? # catch all events here ??????? for event in pg.event.get(): ??????????? if event.type == pg.QUIT: ??????????????? self.quit() Apologise for the absence of > signs(im using atom From mikeramosherrera at yahoo.com.mx Wed May 1 15:08:00 2019 From: mikeramosherrera at yahoo.com.mx (Miguel Angel Ramos Herrera) Date: Wed, 1 May 2019 14:08:00 -0500 Subject: [Tutor] execute_from_command_line(sys.argv) Message-ID: <44vSb64cfkzndbm@mail.python.org> Hi, good afternoon? This line of code gives me an error Can not find sys.argv Appreciate if anybody can assist me in this item? Miguel angel From alan.gauld at yahoo.co.uk Wed May 1 19:37:52 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 2 May 2019 00:37:52 +0100 Subject: [Tutor] execute_from_command_line(sys.argv) In-Reply-To: <44vSb64cfkzndbm@mail.python.org> References: <44vSb64cfkzndbm@mail.python.org> Message-ID: On 01/05/2019 20:08, Miguel Angel Ramos Herrera via Tutor wrote: > This line of code gives me an error I assume you mean this line: execute_from_command_line(sys.argv) But that's not a line of standard Python so I can only assume that this is a function that you have defined yourself. But without seeing what the function does it's hard to debug it. However, the solution could simply be that you have not imported sys. You would need to call it with import sys execute_from_command_line(sys.argv) If that is not the solution then please post again with more detail (see below) > Can not find sys.argv That's not a standard python error message. Please always send the full error trace not a summary or partial message. There is a lot of important detail in the text. Please always try to show us both the actual code that produces an error as well as the full error text. And, if significant, the input and output data too. It may also help to tell us the Python version and OS. -- 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 ar at zeit.io Thu May 2 11:25:54 2019 From: ar at zeit.io (Arup Rakshit) Date: Thu, 2 May 2019 20:55:54 +0530 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <9c29679c-6354-f802-5e1b-85a3d16bb3d4@DancesWithMice.info> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <9c29679c-6354-f802-5e1b-85a3d16bb3d4@DancesWithMice.info> Message-ID: On 30/04/19 6:34 AM, David L Neil wrote: > 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?) Thanks David for suggesting this. I am going to do that, writing an web application using Python Flask. It feels more easier than battling with such frustrated abstractions. :) I don't know why python exposed so many things, and getter/setter goes wild in that respect. It was not that hard in the other languages(Ruby, JS). I think Python object model and how python interpreter executes code is the foundation. I'll try to hunt that slowly, because that's where I think I hit the wall everytime. Python doesn't work the way I am thinking it, and I am far behind of this. -- Thanks, Arup Rakshit From ar at zeit.io Thu May 2 11:28:13 2019 From: ar at zeit.io (Arup Rakshit) Date: Thu, 2 May 2019 20:58:13 +0530 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: <20190429234143.GG12773@ando.pearwood.info> References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <20190429181038.GE12773@ando.pearwood.info> <5f7b0791-7342-4fab-de2a-da00c69868a2@zeit.io> <20190429234143.GG12773@ando.pearwood.info> Message-ID: <7729c044-0320-a268-f434-98cd99c59cf0@zeit.io> On 30/04/19 5:11 AM, Steven D'Aprano wrote: > On Tue, Apr 30, 2019 at 12:47:02AM +0530, Arup Rakshit wrote: > >> I really didn't write that code by myself. The day I'll you will not see >> me here everyday :) . I was watching a PyCon video >> https://youtu.be/81S01c9zytE?t=8172 where the author used this code. But >> his explanation is not clear to me. The main problem is that the guy who >> was recorded it far away from the projector, so what speaker were >> showing there is not clear. So thought to ask here as usual. Because I >> felt so lost with this trick. > Okay, the short, SIMPLIFIED (and therefore inaccurate) summary of > descriptors: > > Descriptors are the "magic" used by Python whenever it does an attribute > lookup. When you do any sort of attribute lookup or assignment: > > x = spam.eggs > > spam.eggs = value > > Python looks at spam and spam's class for an attribute called "eggs", > and if that attribute is an object with a __set__ or __get__ method, it > calls that method: > > x = spam.eggs > => x = spam.eggs.__get__() > > spam.eggs = value > => spam.eggs.__set__(value) > > For the gory details of what *precisely* happens, see the Howto Guide: > > https://docs.python.org/3/howto/descriptor.html All answers in the thread with the howto link above helped me to understand this at least. I did't masters yet, but atleast now can reason about what is going on when I meet such code examples. > > > Python has a few common descriptors built in: > > - ordinary methods > - classmethod > - staticmethod > - property > > Apart from staticmethod, they're all pretty common in code. But writing > your own custom descriptors is fairly rare. I've only done it once, in > 25+ years of using Python. > > Thank you very much. -- Thanks, Arup Rakshit From mats at wichmann.us Thu May 2 12:10:23 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 2 May 2019 10:10:23 -0600 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <9c29679c-6354-f802-5e1b-85a3d16bb3d4@DancesWithMice.info> Message-ID: On 5/2/19 9:25 AM, Arup Rakshit wrote: > On 30/04/19 6:34 AM, David L Neil wrote: >> 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?) > > Thanks David for suggesting this. I am going to do that, writing an web > application using Python Flask. It feels more easier than battling with > such frustrated abstractions. :) I don't know why python exposed so many > things, and getter/setter goes wild in that respect. It was not thathard in the other languages(Ruby, JS). So let me make this comment: some of us (yeah, I'm guilty of this) like to expose the underlying details when talking about something. In my case, I do that because it's how I myself learn - if the details make beautiful sense, then the "normal way" complete with syntactic sugar doesn't feel like a mystery and I'm much happier. Does that mean everybody needs those details up front? Absolutely not. PyCon talks have a high risk of doing this - great material, usually, but they got accepted as talks because they are "revealing secrets". Attendees already knew how to use the feature but come back from the conference going "now I understand so much better" and so got their money's worth. In normal everyday use, getters and setters in Python are simple. (a) don't use them unless you actually need them, which is rarely - this isn't C++, Java, etc. where you're supposed to encapsulate everything. (b) if you do need them, "wrap" a regular attribute access in your class definition. So I wouldn't write this: class C: def __init__(self, x): self.__x = x def get_x(self): return self.__x def set_x(self, x): self.__x = x # But this: class C: def __init__(self, x): self.x = x Now if you later found that 'x' needed to be more complex (in this trivial example, x is only allowed to be between 0 and 100), we can add property decorators which provide some sort of encapsulation, but leaves the same API - so the retrofit doesn't break clients: class C: def __init__(self, x): self.x = x @property def x(self): return self.__x @x.setter def x(self, x): if x < 0: self.__x = 0 elif x > 100: self.__x = 100 else: self.__x = x From alan.gauld at yahoo.co.uk Thu May 2 12:11:17 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 2 May 2019 17:11:17 +0100 Subject: [Tutor] self.name is calling the __set__ method of another class In-Reply-To: References: <261784ED-A9DE-4D1C-B40D-D6D18E66392A@zeit.io> <9c29679c-6354-f802-5e1b-85a3d16bb3d4@DancesWithMice.info> Message-ID: On 02/05/2019 16:25, Arup Rakshit wrote: > such frustrated abstractions. :) I don't know why python exposed so many > things, Because by exposing it those few who do need to play with the internals can(*). Most of us just ignore it > Python doesn't work the way I am thinking it, and I am far behind of this. Most languages don't really work the way most users think they do, but Python allows you to discover that more easily than most. (Lisp and Smalltalk are other exceptions that expose their inner workings to anyone who cares to look) (*)One area where these meta-programming features are used is in frameworks, either web, GUI or networking. Django being one case in point where meta-classes are used effectively to make Python behave slightly differently to normal, which, in turn, makes web programming slightly easier... -- 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 anilduggirala at fastmail.fm Thu May 2 17:24:26 2019 From: anilduggirala at fastmail.fm (Anil Duggirala) Date: Thu, 02 May 2019 17:24:26 -0400 Subject: [Tutor] pip issue Message-ID: hello, I executed the pip3 install --user -r contrib/requirements/requirements.txt (I actually did sudo before that). I then interrupted the process with Ctrl-C. Now, when I execute the same command I get: Collecting aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) Could not find a version that satisfies the requirement aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) (from versions: ) No matching distribution found for aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) I suspect this has something to do with me interrupting the install process, because I interrupted it precisely when it was fetching the package that it can't find now. Can anyone please help me, Let me know if I should be asking this elsewhere. thanks, From cs at cskk.id.au Thu May 2 20:14:56 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 3 May 2019 10:14:56 +1000 Subject: [Tutor] pip issue In-Reply-To: References: Message-ID: <20190503001456.GA36379@cskk.homeip.net> On 02May2019 17:24, Anil Duggirala wrote: >I executed the pip3 install --user -r >contrib/requirements/requirements.txt (I actually did sudo before >that). Please don't use sudo for this. The notion "install" does not imply being root. The whole point of --user is to install packages in your personal account without troubling with the system packages. Doing that as root only installs the packages for root, generally a useless thing as you shouldn't be running normal stuff as root. Just do pip3 as yourself. >I then interrupted the process with Ctrl-C. Now, when I execute the >same command I get: >Collecting aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) > Could not find a version that satisfies the requirement aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) (from versions: ) >No matching distribution found for aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt (line 5)) That is odd, though I've seen this kind of complaint from pip before for some packages. Try this (as _yourself_, not as root): pip3 install --verbose --user 'aiorpcX<0.18,>=0.17.0' For me, this just worked. Also, the output includes the location where pip is installing stuff. You could always just clean that area out and retry. Also, try the --ignore-installed option and/or the --force-reinstall, which may cause pip3 to ignore any partial/damaged install and just do it all from scratch. >I suspect this has something to do with me interrupting the install process, because I interrupted it precisely when it was fetching the package that it can't find now. >Let me know if I should be asking this elsewhere. This is a fine place to ask this question. Cheers, Cameron Simpson From mats at wichmann.us Thu May 2 20:51:48 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 2 May 2019 18:51:48 -0600 Subject: [Tutor] pip issue In-Reply-To: <20190503001456.GA36379@cskk.homeip.net> References: <20190503001456.GA36379@cskk.homeip.net> Message-ID: <28a1be42-4ce3-eae8-539c-35b22aaead24@wichmann.us> On 5/2/19 6:14 PM, Cameron Simpson wrote: > On 02May2019 17:24, Anil Duggirala wrote: >> I executed the pip3 install --user -r >> contrib/requirements/requirements.txt (I actually did sudo before that). > > Please don't use sudo for this. The notion "install" does not imply > being root. > The whole point of --user is to install packages in your personal > account without troubling with the system packages. Doing that as root > only installs the packages for root, generally a useless thing as you > shouldn't be running normal stuff as root. > > Just do pip3 as yourself. also, despite the large volume of old text that says otherwise, it's better not to invoke pip as a command, but as a module. Thus, instead of pip3 install --user blah do: python3 -m pip install --user blah assuming you wanted it to work for "python3". the latter way makes sure the package ends up in a place that matches the Python you're going to use - and you are going to use this code from another Python program, no? (that change is not going to fix your problem, it's just general advice). Most Linux systems these days have several Python versions, and the more you use Python the more likely it is you got something that set up another Python version (virtualenv, bundled Python, etc). Mac users often have two: the system one, and the one you actually use for your own work. Even Windows users end up with several Pythons, e.g. one you installed, one that got installed with Visual Studio, etc. From mhysnm1964 at gmail.com Fri May 3 08:07:24 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 3 May 2019 22:07:24 +1000 Subject: [Tutor] Finding unique strings. Message-ID: <002001d501a8$c6d46740$547d35c0$@gmail.com> All, I have a list of strings which has been downloaded from my bank. I am trying to build a program to find the unique string patterns which I want to use with a dictionary. So I can group the different transactions together. Below are example unique strings which I have manually extracted from the data. Everything after the example text is different. I cannot show the full data due to privacy. WITHDRAWAL AT HANDYBANK PAYMENT BY AUTHORITY WITHDRAWAL BY EFTPOS WITHDRAWAL MOBILE DEPOSIT ACCESSPAY Note: Some of the entries, have an store name contained in the string towards the end. For example: WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 Thus I want to extract the KMART as part of the unique key. As the shown example transaction always has a number. I was going to use a test condition for the above to test for the number. Then the next word would be added to the string for the key. I tried to use dictionaries and managed to get unique first words. But got stuck at this point and could not work out how to build a unique keyword with multiple words. I hope someone can help. Sean From anilduggirala at fastmail.fm Fri May 3 12:11:28 2019 From: anilduggirala at fastmail.fm (Anil Duggirala) Date: Fri, 03 May 2019 11:11:28 -0500 Subject: [Tutor] pip issue In-Reply-To: <20190503001456.GA36379@cskk.homeip.net> References: <20190503001456.GA36379@cskk.homeip.net> Message-ID: <1556899888.4080.2.camel@fastmail.fm> On Fri, 2019-05-03 at 10:14 +1000, Cameron Simpson wrote: > On 02May2019 17:24, Anil Duggirala wrote: > > I executed the pip3 install --user -r? > > contrib/requirements/requirements.txt (I actually did sudo before? > > that). > > Please don't use sudo for this. The notion "install" does not imply? > being root. That is actually why I interrupted the process, because I remembered that you're not supposed to do pip as sudo or 'install' anything python as sudo. > Try this (as _yourself_, not as root): > > ? pip3 install --verbose --user 'aiorpcX<0.18,>=0.17.0' I tried this and got a lot of messages like: The package https://files.pythonhosted.org/packages/60/1c/dd77ef44387e9 c51d845140cc46a27049effc04895e02f53a1006754d510/aiorpcX-0.1-py3-none- any.whl#sha256=c6fcb4bce3eb82b9bba2d80b1c57cf3e2498462b2bc8c646a1b94263 9a0d86eb (from https://pypi.org/simple/aiorpcx/) (requires- python:>=3.6) is incompatible with the pythonversion in use. Acceptable python versions are:>=3.6 And then: The package https://files.pythonhosted.org/packages/fd/2e/7d9f0dd1a8c30 8bdc7cbda32859e9b1171768b8f68c124527da83cd4f978/aiorpcX- 0.17.0.tar.gz#sha256=13ccc8361bc3049d649094b69aead6118f6deb5f1b88ad7721 1be85c4e2ed792 (from https://pypi.org/simple/aiorpcx/) (requires- python:>=3.6) is incompatible with the pythonversion in use. Acceptable python versions are:>=3.6 ? Could not find a version that satisfies the requirement aiorpcX<0.18,>=0.17.0 (from versions: ) Cleaning up... No matching distribution found for aiorpcX<0.18,>=0.17.0 Exception information: Traceback (most recent call last): ? File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main ????status = self.run(options, args) ? File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in run ????wb.build(autobuilding=True) ? File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build ????self.requirement_set.prepare_files(self.finder) ? File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_files ????ignore_dependencies=self.ignore_dependencies)) ? File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 554, in _prepare_file ????require_hashes ? File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 278, in populate_link ????self.link = finder.find_requirement(self, upgrade) ? File "/usr/lib/python3/dist-packages/pip/index.py", line 514, in find_requirement ????'No matching distribution found for %s' % req pip.exceptions.DistributionNotFound: No matching distribution found for aiorpcX<0.18,>=0.17.0 > Also, try the --ignore-installed option and/or the --force- > reinstall,? > which may cause pip3 to ignore any partial/damaged install and just > do? > it all from scratch. I also tried these, and got the same (or very similar) outputs, with no avail. Please tell me where I screwed up. I think I could learn to program in Python, but learning about the packaging and modules and using them, requires a lot more time, I think. thank you Cameron, From rba2124 at gmail.com Fri May 3 15:06:08 2019 From: rba2124 at gmail.com (Roger B. Atkins) Date: Fri, 3 May 2019 12:06:08 -0700 Subject: [Tutor] Finding unique strings. In-Reply-To: <002001d501a8$c6d46740$547d35c0$@gmail.com> References: <002001d501a8$c6d46740$547d35c0$@gmail.com> Message-ID: It would probably make things easier if you specified your operating system, Python version, data file type and location. Typically, the bank info would be downloadable as a CSV (comma separated value) file. Assuming that to be the case, and assuming you are using Windows, and assuming Python 3, and assuming you are downing loading your data to a file stored on your computer and running your reader program on the data file, you might start with something like: #! python 3 import csv deposits, atm, temp = [], [], [] # initializes column headings to empty lists. filename = input('Enter file name: ') with open("C:\\PyScripts\\" + filename, 'r') as f: contents = csv.reader(f) for row in contents: ... You would substitute your own column names in place of 'deposits, atm' and substitute your file path information in place of "C:\\PyScripts\\". The first row of the csv file will contain your column headers. Therefore, as you read through the first row in the file, row[0] would be the first header, row[1] would be the second header and so forth. As you read the second row, row[0] would be the first value (data element) under the first column header, row[1] would be the first value under the second column header and so forth. For example, if you were searching for deposits, assuming 'Deposits' is the 3rd column header, you could use code such as deposits += row[2] to add deposit values to your list of deposits. Such code would be indented under 'for row in contents' to make it part of the 'for' loop. Hopefully this will give you some ideas to get you started. to On Fri, May 3, 2019 at 5:40 AM wrote: > > All, > > > > I have a list of strings which has been downloaded from my bank. I am trying > to build a program to find the unique string patterns which I want to use > with a dictionary. So I can group the different transactions together. Below > are example unique strings which I have manually extracted from the data. > Everything after the example text is different. I cannot show the full data > due to privacy. > > > > WITHDRAWAL AT HANDYBANK > > PAYMENT BY AUTHORITY > > WITHDRAWAL BY EFTPOS > > WITHDRAWAL MOBILE > > DEPOSIT ACCESSPAY > > > > Note: Some of the entries, have an store name contained in the string > towards the end. For example: > > > > WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 > > > > Thus I want to extract the KMART as part of the unique key. As the shown > example transaction always has a number. I was going to use a test condition > for the above to test for the number. Then the next word would be added to > the string for the key. > > > > I tried to use dictionaries and managed to get unique first words. But got > stuck at this point and could not work out how to build a unique keyword > with multiple words. I hope someone can help. > > > > > > Sean > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From breamoreboy at gmail.com Fri May 3 17:35:12 2019 From: breamoreboy at gmail.com (Mark Lawrence) Date: Fri, 3 May 2019 22:35:12 +0100 Subject: [Tutor] Finding unique strings. In-Reply-To: <002001d501a8$c6d46740$547d35c0$@gmail.com> References: <002001d501a8$c6d46740$547d35c0$@gmail.com> Message-ID: On 03/05/2019 13:07, mhysnm1964 at gmail.com wrote: > All, > > I have a list of strings which has been downloaded from my bank. I am trying > to build a program to find the unique string patterns which I want to use > with a dictionary. So I can group the different transactions together. Below > are example unique strings which I have manually extracted from the data. > Everything after the example text is different. I cannot show the full data > due to privacy. > > WITHDRAWAL AT HANDYBANK > > PAYMENT BY AUTHORITY > > WITHDRAWAL BY EFTPOS > > WITHDRAWAL MOBILE > > DEPOSIT ACCESSPAY > > Note: Some of the entries, have an store name contained in the string > towards the end. For example: > > WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 > > Thus I want to extract the KMART as part of the unique key. As the shown > example transaction always has a number. I was going to use a test condition > for the above to test for the number. Then the next word would be added to > the string for the key. > I tried to use dictionaries and managed to get unique first words. But got > stuck at this point and could not work out how to build a unique keyword > with multiple words. I hope someone can help. > > Sean > Please check out https://docs.python.org/3/library/collections.html#collections.defaultdict as I think it's right up your street. Examples are given at the link :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at yahoo.co.uk Fri May 3 18:40:40 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 3 May 2019 23:40:40 +0100 Subject: [Tutor] pip issue In-Reply-To: <1556899888.4080.2.camel@fastmail.fm> References: <20190503001456.GA36379@cskk.homeip.net> <1556899888.4080.2.camel@fastmail.fm> Message-ID: On 03/05/2019 17:11, Anil Duggirala wrote: >> Try this (as _yourself_, not as root): >> >> ? pip3 install --verbose --user 'aiorpcX<0.18,>=0.17.0' > > I tried this and got a lot of messages like: ... > 9a0d86eb (from https://pypi.org/simple/aiorpcx/) (requires- > python:>=3.6) is incompatible with the pythonversion in use. Acceptable > python versions are:>=3.6 Ok, so it clearly says you need a Python version greater than or equal to 3.6. Which version of Python are you using? > Please tell me where I screwed up. I think I could learn to program in > Python, but learning about the packaging and modules and using them, > requires a lot more time, I think. Can you clarify your current status since that will help us provide suitable solutions. Can you already program in any other language? (If so which?) Or are you a complete beginner programmer? Normally when learning a language it's best to start with the basics which don't require installing third party libraries. Is there some specific task you need this library for? Is there not an existing python distribution that includes the library already - like Enthought or Anaconda say? Before trying to solve the problem the hard way it might be worth first checking if a simpler alternative exists? -- 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 cs at cskk.id.au Fri May 3 18:43:59 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 4 May 2019 08:43:59 +1000 Subject: [Tutor] Finding unique strings. In-Reply-To: <002001d501a8$c6d46740$547d35c0$@gmail.com> References: <002001d501a8$c6d46740$547d35c0$@gmail.com> Message-ID: <20190503224359.GA47668@cskk.homeip.net> On 03May2019 22:07, Sean Murphy wrote: >I have a list of strings which has been downloaded from my bank. I am >trying >to build a program to find the unique string patterns which I want to use >with a dictionary. So I can group the different transactions together. Below >are example unique strings which I have manually extracted from the data. >Everything after the example text is different. I cannot show the full data >due to privacy. > >WITHDRAWAL AT HANDYBANK >PAYMENT BY AUTHORITY >WITHDRAWAL BY EFTPOS >WITHDRAWAL MOBILE >DEPOSIT ACCESSPAY > >Note: Some of the entries, have an store name contained in the string >towards the end. For example: > >WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 > >Thus I want to extract the KMART as part of the unique key. As the shown >example transaction always has a number. I was going to use a test condition >for the above to test for the number. Then the next word would be added to >the string for the key. [...] I'm assuming you're handed the text as one string, for example this: WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 I'm assuming is a single column from a CSV of transactions. I've got 2 observations: 1: For your unique key, if it is a string (it needn't be), you just need to put the relevant parts into your key. FOr the above, perhaps that might be: WITHDRAWAL 0304479 KMART or: WITHDRAWAL KMART 1075 etc depending on what the relevant parts are. 2: To pull out the relevant words from the description I would be inclined to do a more structured parse. Consider something like the following (untested): # example data desc = 'WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09' # various things which may be recognised method = None terminal = None vendor = None vendor_site = None # inspect the description words = desc.split() flavour = desc.pop(0) # "WITHDRAWAL" etc word0 = desc.pop(0) if word0 in ('BY', 'AT'): method = word0 + ' ' + desc.pop(0) # "BY EFTPOS" elif word0 in ('MOBILE', 'ACCESSPAY'): method = word0 word0 = words.pop(0) if word0.isdigit(): # probably really part of the "BY EFTPOS" description terminal = word0 word0 = words.pop(0) vendor = word0 word0 = words.pop(0) if word0.isdigit(): vendor_site = word0 word0 = words.pop(0) # ... more dissection ... # assemble the key - include only the items that matter # eg maybe leave out terminal and vendor_site, etc key = (flavour, method, terminal, vendor, vendor_site) This is all rather open ended, and totally dependent on your bank's reporting habits. Also, it needs some length checks: words.pop(0) will raise an exception when "words" is empty, as it will be for the shorter descriptions at some point. The important point is to get a structured key containing just the relevant field values: being assembled as a tuple from strings (immutable hashable Python values) it is usable as a dictionary key. For more ease of use you can make the key a namedtuple: from collections import defaultdict, namedtuple ........ KeyType = namedtuple('KeyType', 'flavour method vendor') transactions = defaultdict(list) ........ loop over the CSV data ... key = KeyType(flavour, method, vendor) transactions[key].append(transcaction info here...) which gets you a dictionary "transactions" containing lists of transaction record (in whatever form you make them, when might be simply the row from the CSV data as a first cut). The nice thing about a namedtuple is that the values are available as attributes: you can use "key.flavour" etc to inspect the tuple. Cheers, Cameron Simpson From nathan-tech at hotmail.com Fri May 3 19:45:08 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Fri, 3 May 2019 23:45:08 +0000 Subject: [Tutor] don't steel my code Mister user Message-ID: Hi there, Hope you like the subject, I was feeling inventive. my question today concerns compilation. I hear this a lot from the communities I hang around in, and it is something I wonder about often. There are tools like py2exe and pyinstaller that are able to compile your python code into .exe format. but why bother? Lets use a metaphorical example. Lets say I create a program called awesomesauce. In awesomesauce, you boot it up, it checks your user key and sends that up to a server which says if you've paid for the product. Now then, mister user comes along. He decompiles it with pyinstaller ("Which I'm told is easy"), removes the check, and has himself a free product. So how are people doing it? I'm told that many commercial, business, and game-world programs are made in python, and are pay to use, or are yet to be source code stolen, but how? I'd appreciate any tips you may have. Nate From alan.gauld at yahoo.co.uk Sat May 4 07:46:33 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 May 2019 12:46:33 +0100 Subject: [Tutor] don't steel my code Mister user In-Reply-To: References: Message-ID: On 04/05/2019 00:45, nathan tech wrote: > There are tools like py2exe and pyinstaller that are able to compile > your python code into .exe format. > > but why bother? It's easier and more convenient to distribute a single .exe file than a swathe of individual .py or .pyc files. It also removes any potential issues around the python interpreter version. > Lets say I create a program called awesomesauce. > > Now then, mister user comes along. > > He decompiles it with pyinstaller ("Which I'm told is easy"), removes > the check, and has himself a free product. Never consider compilation a security feature, it isn't. Regardless of the language not just Python. A skilled technician can hack the binary if necessary. Compilation is simply a distribution feature that makes life easier for both the distributor and the recipient. -- 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 mhysnm1964 at gmail.com Sat May 4 04:48:43 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 4 May 2019 18:48:43 +1000 Subject: [Tutor] Finding unique strings. In-Reply-To: References: <002001d501a8$c6d46740$547d35c0$@gmail.com> Message-ID: <012901d50256$2fe1ae20$8fa50a60$@gmail.com> Mark and all, Thanks for the link to the different dictionaries. The book which was recommended I have already got and read which doesn't answer my question. The structure of the CSV file is: Account no, date, transaction description (what I am trying to build unique keys from), credit, debit, serial and transaction type. I have already loaded the cSV file into a list. Thus why I did not show any code. I will review the page provided and if I have more questions which are going to e more than likely. I will come back using the same bat channel. Sean -----Original Message----- From: Tutor On Behalf Of Mark Lawrence Sent: Saturday, 4 May 2019 7:35 AM To: tutor at python.org Subject: Re: [Tutor] Finding unique strings. On 03/05/2019 13:07, mhysnm1964 at gmail.com wrote: > All, > > I have a list of strings which has been downloaded from my bank. I am > trying to build a program to find the unique string patterns which I > want to use with a dictionary. So I can group the different > transactions together. Below are example unique strings which I have manually extracted from the data. > Everything after the example text is different. I cannot show the full > data due to privacy. > > WITHDRAWAL AT HANDYBANK > > PAYMENT BY AUTHORITY > > WITHDRAWAL BY EFTPOS > > WITHDRAWAL MOBILE > > DEPOSIT ACCESSPAY > > Note: Some of the entries, have an store name contained in the string > towards the end. For example: > > WITHDRAWAL BY EFTPOS 0304479 KMART 1075 CASTLE HILL 24/09 > > Thus I want to extract the KMART as part of the unique key. As the > shown example transaction always has a number. I was going to use a > test condition for the above to test for the number. Then the next > word would be added to the string for the key. > I tried to use dictionaries and managed to get unique first words. But > got stuck at this point and could not work out how to build a unique > keyword with multiple words. I hope someone can help. > > Sean > Please check out https://docs.python.org/3/library/collections.html#collections.defaultdict as I think it's right up your street. Examples are given at the link :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From anilduggirala at fastmail.fm Sat May 4 12:01:56 2019 From: anilduggirala at fastmail.fm (Anil Duggirala) Date: Sat, 04 May 2019 11:01:56 -0500 Subject: [Tutor] pip issue In-Reply-To: References: <20190503001456.GA36379@cskk.homeip.net> <1556899888.4080.2.camel@fastmail.fm> Message-ID: <1556985716.1698.5.camel@fastmail.fm> > > 9a0d86eb (from https://pypi.org/simple/aiorpcx/) (requires- > > python:>=3.6) is incompatible with the pythonversion in use. > > Acceptable > > python versions are:>=3.6 > > Ok, so it clearly says you need a Python version greater > than or equal to 3.6. Which version of Python are you using? I am running python3 version 3.5.3. That is an incredible coincidence then, that the problem arose specifically when I interrupted an install process. I thought that was it. Thanks, that clears it up. Debian loves to have older software as default. > Can you clarify your current status since that will help > us provide suitable solutions. > Normally when learning a language it's best to start with > the basics which don't require installing third party > libraries. Is there some specific task you need this > library for? Actually. I am not planning to do any coding relating to this. I am just looking to install a piece of software, that required installing some dependencies. Thanks very much. This will make my move to Ubuntu happen earlier I think, From nathan-tech at hotmail.com Sat May 4 10:35:53 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Sat, 4 May 2019 14:35:53 +0000 Subject: [Tutor] don't steel my code Mister user In-Reply-To: References: Message-ID: It has to be said, after extensive research, and responses here, it seems python was just not designed to be a commercial product. Licenses are all well and good, but if you're hacking a product, you're just not going to be stopped by a lisence. Furthering to that, if I ever sold products it would be ?5, or $7, and 7 bucks just isn't worth all the effort to make python difficult to hack. Nothing is impossible, but, deterring the average user just for $7? Not worth it. Thanks anyway guys. Nate On 04/05/2019 12:46, Alan Gauld via Tutor wrote: > On 04/05/2019 00:45, nathan tech wrote: > >> There are tools like py2exe and pyinstaller that are able to compile >> your python code into .exe format. >> >> but why bother? > It's easier and more convenient to distribute a single .exe > file than a swathe of individual .py or .pyc files. It also > removes any potential issues around the python interpreter > version. > >> Lets say I create a program called awesomesauce. >> >> Now then, mister user comes along. >> >> He decompiles it with pyinstaller ("Which I'm told is easy"), removes >> the check, and has himself a free product. > Never consider compilation a security feature, it isn't. > Regardless of the language not just Python. A skilled > technician can hack the binary if necessary. > > Compilation is simply a distribution feature that makes > life easier for both the distributor and the recipient. > From alan.gauld at yahoo.co.uk Sat May 4 17:50:42 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 May 2019 22:50:42 +0100 Subject: [Tutor] don't steel my code Mister user In-Reply-To: References: Message-ID: On 04/05/2019 15:35, nathan tech wrote: > It has to be said, after extensive research, and responses here, it > seems python was just not designed to be a commercial product. That depends. Python wasn't designed to be a commercial product in that it is an open source programming language and interpreter and so is free and inherently non-commercial(*). Can it produce commercial products? Of course it can and has. It is no different to any other similar development environment such as Visual Basic, PHP, even JAVA or Lisp or Smalltalk. > Licenses are all well and good, but if you're hacking a product, you're > just not going to be stopped by a lisence. True, but you can hack any product regardless of the language, even C++ or assembler can be hacked. The vast majority of users don't have the skills nor the time nor the inclination. And, if you can catch them, the license allows you to sue... > Furthering to that, if I ever sold products it would be ?5, or $7, and 7 > bucks just isn't worth all the effort to make python difficult to hack. 7 bucks isn't worth building a commercial product, unless you are sure you will sell 100's of thousands. And 7 bucks is also not worth the time and effort of hacking anything! But there are commercial products that sell for 100s of dollars that are written, in part at least, in Python. > Nothing is impossible, but, deterring the average user just for $7? Not > worth it. A license is cheap to produce and deters the "average user". Very few users will know how to hack code of any kind, and even those that do will have better things to do with their time than try to save 7 bucks! The real question is whether you can produce something that is worth $7 to your customers. If it is they will buy it. If not they will look elsewhere, they won't try to decompile it and disable the protection - assuming you installed any. If your software is worth, say, $700 then possibly they might think about spending time getting round the license. Then it might be worth some minor effort on protection. but not much because if they really want to they can reverse engineer it regardless. That's the rules and reality of commercial software. The value lies in always being one step better than the guys who are following. That's how Adobe, et al maintain their lead and why they keep issuing updates! (*)Even open source can be commercial if you build a support plan around it. Red Hat and Cygnus are good examples of that strategy. Selling support for opensource software can work. -- 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 nathan-tech at hotmail.com Sat May 4 21:28:47 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Sun, 5 May 2019 01:28:47 +0000 Subject: [Tutor] don't steel my code Mister user In-Reply-To: References: Message-ID: Hello everyone, Again, thank you for your replies. I've always done free products up to the one I am developing now, and, having considered the things you guys have said, will continue to do so. Part of it is a confidence thing, I really don't think my products are worth 7, let alone 700 bucks, the other is I'm a sucker for helping as many people as I can. Anyway, staying on topic here, I really appreciate all of you taking the time to give me advice and help, thank you very much :) Thanks Nate On 04/05/2019 22:50, Alan Gauld via Tutor wrote: > On 04/05/2019 15:35, nathan tech wrote: >> It has to be said, after extensive research, and responses here, it >> seems python was just not designed to be a commercial product. > That depends. Python wasn't designed to be a commercial product > in that it is an open source programming language and interpreter > and so is free and inherently non-commercial(*). > > Can it produce commercial products? Of course it can and has. > It is no different to any other similar development environment > such as Visual Basic, PHP, even JAVA or Lisp or Smalltalk. > >> Licenses are all well and good, but if you're hacking a product, you're >> just not going to be stopped by a lisence. > True, but you can hack any product regardless of the language, > even C++ or assembler can be hacked. The vast majority of users > don't have the skills nor the time nor the inclination. And, > if you can catch them, the license allows you to sue... > >> Furthering to that, if I ever sold products it would be ?5, or $7, and 7 >> bucks just isn't worth all the effort to make python difficult to hack. > 7 bucks isn't worth building a commercial product, unless you are sure > you will sell 100's of thousands. And 7 bucks is also not worth the > time and effort of hacking anything! But there are commercial products > that sell for 100s of dollars that are written, in part at least, in Python. > >> Nothing is impossible, but, deterring the average user just for $7? Not >> worth it. > A license is cheap to produce and deters the "average user". > Very few users will know how to hack code of any kind, and > even those that do will have better things to do with > their time than try to save 7 bucks! > > The real question is whether you can produce something > that is worth $7 to your customers. If it is they will > buy it. If not they will look elsewhere, they won't try to > decompile it and disable the protection - assuming you > installed any. > > If your software is worth, say, $700 then possibly they > might think about spending time getting round the license. > Then it might be worth some minor effort on protection. > but not much because if they really want to they can > reverse engineer it regardless. That's the rules and > reality of commercial software. > > The value lies in always being one step better than > the guys who are following. That's how Adobe, et al > maintain their lead and why they keep issuing updates! > > (*)Even open source can be commercial if you build a > support plan around it. Red Hat and Cygnus are good > examples of that strategy. Selling support for > opensource software can work. > From mats at wichmann.us Sun May 5 10:10:59 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 5 May 2019 08:10:59 -0600 Subject: [Tutor] don't steel my code Mister user In-Reply-To: References: Message-ID: <431e8bfd-81d3-8ee5-bf6f-f3bdcb1b2838@wichmann.us> On 5/4/19 8:35 AM, nathan tech wrote: > It has to be said, after extensive research, and responses here, it > seems python was just not designed to be a commercial product. > > Licenses are all well and good, but if you're hacking a product, you're > just not going to be stopped by a lisence. > > Furthering to that, if I ever sold products it would be ?5, or $7, and 7 > bucks just isn't worth all the effort to make python difficult to hack. > > Nothing is impossible, but, deterring the average user just for $7? Not > worth it. > > Thanks anyway guys. > > Nate A few more comments to add, this is philosophical and business model territory, not coding, thus not necessarily the space we here are good at :) As Alan observed, the purpose of "compile" in Python is not to obfuscate, it is to improve the chances that the program works right as delivered without running into dependency and path problems. (especially on Windows, a tricky target anyway and you should have limited confidence any given system has Python installed) There are people who have been working for years on "protecting" code written in Python. Some claim to have technologies with very good success. Personally, I tend to take those claims with a grain of salt, and won't provide any links; some internet searching should turn up some ideas. If you feel you have secrets to protect, or indeed simply want to prevent people from using your code without paying there are several different general approaches: Licensing means - tell people they can't do that. Technical means - the use of license keys, code obfuscation (*), etc. Software as a service - put the "meat" of your technology as a web service, give the application the user runs away for free but use some sort of API/license key scheme to restrict who can actually use the service. Better value - just make it not worth the time for people to cheat - your product is so cheap the work to steal it costs more; you keep adding new value such that reverse-engineers have to start over frequently, etc. There's a page on the Python wiki about this, which really doesn't have that much information, but I will include anyway: https://wiki.python.org/moin/Asking%20for%20Help/How%20do%20you%20protect%20Python%20source%20code%3F * Note on code obfuscation: people can do surprisingly sophisticated things here. One approach that has been used is to change the bytecode emitted so it can only be read by a custom interpreter which you supply. Dropbox did this. Of course, researchers figured out how to decode it anyway. I didn't realize this was so old, time passes... https://developers.slashdot.org/story/13/08/28/0048238/researchers-reverse-engineer-dropbox-cracking-heavily-obfuscated-python-app This stuff definitely falls in the catgeory of "is it worth doing all this to protect an inexpensive app"? From contact.zelphy at gmail.com Wed May 8 07:36:57 2019 From: contact.zelphy at gmail.com (Zelphy) Date: Wed, 8 May 2019 13:36:57 +0200 Subject: [Tutor] Can't reinstall pip for Python 3.7.3 Message-ID: Dear tutor members, After spending a lot of time on forums trying to fix this problem, I come here hoping for a solution to my problem. In fact, after upgrading my version of Python ( I was on 3.6.5 and then on 3.7.3 ) I wanted to reinstall pip ( mainly in order to download Pygame ) But, after downloading Python and the get-pip.py file, when I open it ( and it do the same thing every time ) the command prompt tell me "successfully uninstalled pip-19.1.1 and then Python stop to work. [image: Pip install.PNG] I checked the Path but when I open the Script folder, this folder is empty. But as I remember, normally there is, in this folder, a pip.exe and some other things. [image: Path.PNG] [image: path Environment variables.PNG] I also linked here some screenshots that might help. Btw I'm not a native english speaker so I've may done some mistakes and I'm sorry about that. Hopping to have a solution to this problem, Best regards Garanti sans virus. www.avast.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From mats at wichmann.us Wed May 8 11:19:57 2019 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 8 May 2019 09:19:57 -0600 Subject: [Tutor] Can't reinstall pip for Python 3.7.3 In-Reply-To: References: Message-ID: <6a3d9a84-d759-c2ed-3821-63b81660487c@wichmann.us> On 5/8/19 5:36 AM, Zelphy wrote: > Dear tutor members, > > After spending a lot of time on forums trying to fix this problem, I come > here hoping for a solution to my problem. > In fact, after upgrading my version of Python ( I was on 3.6.5 and then on > 3.7.3 ) I wanted to reinstall pip ( mainly in order to download Pygame ) > But, after downloading Python and the get-pip.py file, when I open it ( and > it do the same thing every time ) the command prompt tell me "successfully > uninstalled pip-19.1.1 and then Python stop to work. > [image: Pip install.PNG] > I checked the Path but when I open the Script folder, this folder is empty. > But as I remember, normally there is, in this folder, a pip.exe and some > other things. > > [image: Path.PNG] > > [image: path Environment variables.PNG] > > I also linked here some screenshots that might help. Screenshots don't usually make it through the mailing list software. When installing Python on Windows, you don't need to install pip, so don't fiddle with the get-pip instructions - it looks like you had it, then it was uninstalled but not reinstalled. Just avoid that. If you do a fresh install and check, it should all be there. The only thing is that the Scripts directory doesn't go into your environment path, even if you said to add the path for Python. Instead of fiddling with that, and assuming you installed the python launcher (which is a good idea), invoke pip as a module instead of trying to use pip.exe. Here's to check if it's working at all: py -m pip --version here's to install something: py -m pip install MyFavoritePackage If you then need to _upgrade_ pip, you would do it this way: py -m pip install --upgrade pip From dave at the-hills.org.uk Wed May 8 16:04:49 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Wed, 8 May 2019 21:04:49 +0100 Subject: [Tutor] Collating date data from a csv file Message-ID: 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 From cs at cskk.id.au Wed May 8 23:08:46 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 9 May 2019 13:08:46 +1000 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: Message-ID: <20190509030846.GA96188@cskk.homeip.net> On 08May2019 21:04, 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? Not as such, because you can't modify a tuple (so you can't update the count part). But you could use a 2 element list. >or is there a better construct? Oh definitely. The easiest thing would be a defaultdict(int). Example: from collections import defaultdict ... by_date = defaultdict(int) for row in csvdata: timestamp = row[1] # based on your example data # get the date from the timestamp date = ... by_date[date] += 1 A defaultdict is a dict which magicly makes missing elements when they get access, using a factory function you supply. Here we're using "int" as that factory, as int() returns zero. I presume you've got the timestamp => date conversion sorted? Cheers, Cameron Simpson From robertvstepp at gmail.com Wed May 8 23:29:56 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 May 2019 22:29:56 -0500 Subject: [Tutor] Collating date data from a csv file In-Reply-To: <20190509030846.GA96188@cskk.homeip.net> References: <20190509030846.GA96188@cskk.homeip.net> Message-ID: On Wed, May 8, 2019 at 10:09 PM Cameron Simpson wrote: > > A defaultdict is a dict which magicly makes missing elements when they > get access, using a factory function you supply. Here we're using "int" > as that factory, as int() returns zero. Is int() guaranteed to always return zero as Python versions progress? More importantly, perhaps, where would I go to look to find the answer to this question myself? -- boB From robertvstepp at gmail.com Wed May 8 23:46:13 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 8 May 2019 22:46:13 -0500 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: <20190509030846.GA96188@cskk.homeip.net> Message-ID: On Wed, May 8, 2019 at 10:29 PM boB Stepp wrote: > > On Wed, May 8, 2019 at 10:09 PM Cameron Simpson wrote: > > > > A defaultdict is a dict which magicly makes missing elements when they > > get access, using a factory function you supply. Here we're using "int" > > as that factory, as int() returns zero. > > Is int() guaranteed to always return zero as Python versions progress? > More importantly, perhaps, where would I go to look to find the > answer to this question myself? It must be time to go to bed! Somehow my eyes missed the int() return behavior at https://docs.python.org/3/library/functions.html#int my first time through. Sorry for the noise! > boB -- boB From __peter__ at web.de Thu May 9 03:53:47 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 May 2019 09:53:47 +0200 Subject: [Tutor] Collating date data from a csv file References: <20190509030846.GA96188@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > On 08May2019 21:04, 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? > > Not as such, because you can't modify a tuple (so you can't update the > count part). But you could use a 2 element list. > >>or is there a better construct? > > Oh definitely. The easiest thing would be a defaultdict(int). Example: > > from collections import defaultdict > ... > by_date = defaultdict(int) > for row in csvdata: > timestamp = row[1] # based on your example data > # get the date from the timestamp > date = ... > by_date[date] += 1 > > A defaultdict is a dict which magicly makes missing elements when they > get access, using a factory function you supply. Here we're using "int" > as that factory, as int() returns zero. While this is easily adaptable if you want to keep more data... by_date = defaultdict(list) # rows grouped by date for row in csvdata: date = ... by_date[date].append(row) ... for the simple case there is also collections.Counter: def get_date(row): return datetime.datetime.fromtimestamp(int(row[1])).date() by_date = collections.Counter(map(get_date, csvdata)) # (date, freq) pairs ordered by frequency: print(by_date.most_common()) > > I presume you've got the timestamp => date conversion sorted? > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jf_byrnes at comcast.net Thu May 9 11:52:14 2019 From: jf_byrnes at comcast.net (Jim) Date: Thu, 9 May 2019 10:52:14 -0500 Subject: [Tutor] Consequences of removing python3-venv Message-ID: My Linux mint18 system recently wanted to up date python 3.6. When I clicked install I got the following message: The following 2 packages will be removed: python3-dev python3-venv I have 2 virtual environments installed: python 3.5.2 & 3.6.7. Listing the various pythons I have installed shows: jfb at jims-mint18 ~ $ ls -ls /usr/bin/python* 0 lrwxrwxrwx 1 root root 9 Nov 23 2017 /usr/bin/python -> python2.7 0 lrwxrwxrwx 1 root root 9 Nov 23 2017 /usr/bin/python2 -> python2.7 3412 -rwxr-xr-x 1 root root 3492656 Nov 12 13:46 /usr/bin/python2.7 0 lrwxrwxrwx 1 root root 9 Jan 9 2017 /usr/bin/python3 -> python3.5 4360 -rwxr-xr-x 2 root root 4464368 Nov 12 10:27 /usr/bin/python3.5 0 lrwxrwxrwx 1 root root 33 Nov 12 10:27 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config 4360 -rwxr-xr-x 2 root root 4464368 Nov 12 10:27 /usr/bin/python3.5m 0 lrwxrwxrwx 1 root root 34 Nov 12 10:27 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config 4500 -rwxr-xr-x 2 root root 4604416 Oct 25 2018 /usr/bin/python3.6 0 lrwxrwxrwx 1 root root 33 Oct 25 2018 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config 4500 -rwxr-xr-x 2 root root 4604416 Oct 25 2018 /usr/bin/python3.6m 0 lrwxrwxrwx 1 root root 34 Oct 25 2018 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config 0 lrwxrwxrwx 1 root root 16 Mar 23 2016 /usr/bin/python3-config -> python3.5-config 0 lrwxrwxrwx 1 root root 10 Jan 9 2017 /usr/bin/python3m -> python3.5m 0 lrwxrwxrwx 1 root root 17 Mar 23 2016 /usr/bin/python3m-config -> python3.5m-config So will allowing the update harm my virtual environments? I really don't want to reinstall them. thanks, Jim From dave at the-hills.org.uk Thu May 9 09:06:43 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Thu, 9 May 2019 14:06:43 +0100 Subject: [Tutor] Collating date data from a csv file In-Reply-To: <20190509030846.GA96188@cskk.homeip.net> References: <20190509030846.GA96188@cskk.homeip.net> Message-ID: Thank you, I now have defaultdict(, {736697: 10, 736677: 14, 736980: 9, 737109: 50, 736919: 15, 736652: 19, 736502: 14, 736710: 2, 736674: 6, 736672: 5, 736933: 2, 736932: 6, 736658: 7, 736671: 5, 736499: 6, 736707: 4, 737181: 4, 736686: 2, ... where the first number is the proleptic Gregorian ordinal of the date 8-) ( I had to look up what proleptic meant) This means I can access the elements by the ordinal of the date, for later processing, and extraction to a spreadsheet Dave On 09/05/2019 04:08, Cameron Simpson wrote: > On 08May2019 21:04, 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? > > Not as such, because you can't modify a tuple (so you can't update the > count part). But you could use a 2 element list. > >> or is there a better construct? > > Oh definitely. The easiest thing would be a defaultdict(int). Example: > > ?from collections import defaultdict > ?... > ?by_date = defaultdict(int) > ?for row in csvdata: > ?? timestamp = row[1]? # based on your example data > ?? # get the date from the timestamp > ?? date = ... > ?? by_date[date] += 1 > > A defaultdict is a dict which magicly makes missing elements when they > get access, using a factory function you supply. Here we're using > "int" as that factory, as int() returns zero. > > I presume you've got the timestamp => date conversion sorted? > > Cheers, > Cameron Simpson From alan.gauld at yahoo.co.uk Thu May 9 12:51:38 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 9 May 2019 17:51:38 +0100 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: <20190509030846.GA96188@cskk.homeip.net> Message-ID: On 09/05/2019 14:06, Dave Hill wrote: > Thank you, I now have > > defaultdict(, {736697: 10, 736677: 14, 736980: 9, 737109: > 50, 736919: 15, 736652: 19, 736502: 14, 736710: 2, 736674: 6, 736672: 5, > 736933: 2, 736932: 6, 736658: 7, 736671: 5, 736499: 6, 736707: 4, > 737181: 4, 736686: 2, ... > > where the first number is the proleptic Gregorian ordinal of the date > 8-) ( I had to look up what proleptic meant) > > This means I can access the elements by the ordinal of the date, for > later processing, and extraction to a spreadsheet It might be easier to use a DateTime object instead of the ordinal. It might make printing the results easier later on. You'll find it in the datetime module... -- 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 Thu May 9 13:23:14 2019 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 9 May 2019 11:23:14 -0600 Subject: [Tutor] Consequences of removing python3-venv In-Reply-To: References: Message-ID: On 5/9/19 9:52 AM, Jim wrote: > My Linux mint18 system recently wanted to up date python 3.6. When I > clicked install I got the following message: > > The following 2 packages will be removed: > python3-dev > python3-venv > > I have 2 virtual environments installed: python 3.5.2 & 3.6.7. > > Listing the various pythons I have installed shows: > > jfb at jims-mint18 ~ $ ls -ls /usr/bin/python* > ?? 0 lrwxrwxrwx 1 root root?????? 9 Nov 23? 2017 /usr/bin/python -> > python2.7 > ?? 0 lrwxrwxrwx 1 root root?????? 9 Nov 23? 2017 /usr/bin/python2 -> > python2.7 > 3412 -rwxr-xr-x 1 root root 3492656 Nov 12 13:46 /usr/bin/python2.7 > ?? 0 lrwxrwxrwx 1 root root?????? 9 Jan? 9? 2017 /usr/bin/python3 -> > python3.5 > 4360 -rwxr-xr-x 2 root root 4464368 Nov 12 10:27 /usr/bin/python3.5 > ?? 0 lrwxrwxrwx 1 root root????? 33 Nov 12 10:27 > /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config > 4360 -rwxr-xr-x 2 root root 4464368 Nov 12 10:27 /usr/bin/python3.5m > ?? 0 lrwxrwxrwx 1 root root????? 34 Nov 12 10:27 > /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config > 4500 -rwxr-xr-x 2 root root 4604416 Oct 25? 2018 /usr/bin/python3.6 > ?? 0 lrwxrwxrwx 1 root root????? 33 Oct 25? 2018 > /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config > 4500 -rwxr-xr-x 2 root root 4604416 Oct 25? 2018 /usr/bin/python3.6m > ?? 0 lrwxrwxrwx 1 root root????? 34 Oct 25? 2018 > /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config > ?? 0 lrwxrwxrwx 1 root root????? 16 Mar 23? 2016 /usr/bin/python3-config > -> python3.5-config > ?? 0 lrwxrwxrwx 1 root root????? 10 Jan? 9? 2017 /usr/bin/python3m -> > python3.5m > ?? 0 lrwxrwxrwx 1 root root????? 17 Mar 23? 2016 > /usr/bin/python3m-config -> python3.5m-config > > So will allowing the update harm my virtual environments? > > I really don't want to reinstall them. venv shouldn't need to be a separate package, since it's considered "part of Python3", but I don't know what they're thinking exactly over on the debian side. Maybe it's only for the command, and the module is part of the regular distribution? My pyvenv script says not to use it: WARNING: the pyenv script is deprecated in favour of `python3.7 -m venv` In any case, I believe python3-venv is a suitable virtual package, such that it makes sure the one for the right Python exists - as long as it's not trying to remove python3-venv you should be okay. Or, maybe Mint have decided to actually remove the command and you'll have to fall back to calling it as a module as noted above. Removing the command won't remove your virtualenvs. But, the Python upgrade may cause some of your virtual environments might break, and you'll have to refresh them - if they point to, rather than duplicate, system stuff which is changing version. There's a --upgrade option. So tread somewhat carefully. From cranky.frankie at gmail.com Sat May 11 14:59:49 2019 From: cranky.frankie at gmail.com (Cranky Frankie) Date: Sat, 11 May 2019 14:59:49 -0400 Subject: [Tutor] Looking for some direction Message-ID: I'm a long time IT professional trying to teach myself object-oriented programming. As such I want to build a traditional PC app using MVC (Model - View - Controller) architecture. Just want to make sure I'm heading about this in the best way so I'm looking for some direction. For the Model or persistence layer I want to use SQLite. For the View or GUI I want to use wxPython. For the Controller I want to of course use Python. I'm also planning on using Git for source control. 1) For the IDE I'm most comfortable with Netbeans/Java, but I'm forcing myself to try and get comfortable with PyCharm. Is it worth sticking it out with PyCharm, or should I go with the Python module in Netbeans? Or is there another IDE I should look at? 2) For wxPython I'm finding a lot of the documentation is outdated. Is this book any good: http://www.blog.pythonlibrary.org/2019/05/08/creating-gui-applications-with-wxpython-now-available/ Or is there a better book/course/website I should be working with? Or is there a better grahpics framework at this point for a traditional desktop app? 3) For the O-O part, I'm comfortable with Inheritance and Composition. Do I need to worry about any of the more advanced design patterns? This app will be for vehicle ownership - tracking maintenance, etc. Nothing fancy. 4) I plan to write my Use Case in Libre Office Write. For the UML diagrams, is there a simple app to handle that? I don't need it to generate code but I'd like to have a nice class diagram if possible. 5) I'm really having trouble envisioning the GUI screens. How does one get a handle on that? Just draw the screens on paper, or what? Any ideas very much appreciated. -- Frank L. "Cranky Frankie" Palmeri, Risible Riding Raconteur & Writer "I never lose. I either win or I learn" - Nelson Mandela From alan.gauld at yahoo.co.uk Sat May 11 18:57:43 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 11 May 2019 23:57:43 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: On 11/05/2019 19:59, Cranky Frankie wrote: > I'm a long time IT professional trying to teach myself object-oriented > programming. As such I want to build a traditional PC app using MVC (Model > - View - Controller) architecture. One important thing to realoze about MVC is that there are very many interpretations of it. But the original, and purest in OOP terms, is the Smalltalk version which is all about building GUIs. > For the Model or persistence layer I want to use SQLite. Thats fine but I;d point out that in good OOP the model hould be more than persistence. The model is the core object including all the core behaviour of the application. That is to say the "business logic and behaviour" as opposed to things pertaining to the presentation and usability of the app. > For the View or GUI I want to use wxPython. And again the view should not be "the GUI" it should only be the display element of the model. The view is responsible for displaying an aspect of the model. And there may be several views of the same model. (For example thee may be a graphical view, a form view and a tabular view - with the latter technically being associated with the model class rather than any singular model instance) > For the Controller I want to of course use Python. I'm kind of assuming you'd use Python for all three. Of course if it was a web based app you may use HTML and Javacript for the view/controller part. But python would be a good choice for the model in all cases. > 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. > 2) For wxPython I'm finding a lot of the documentation is outdated. One of the penalties of open source development is that the software moves faster than authors can keep up! > there a better grahpics framework at this point for a traditional desktop > app? I've used wxwidgets, Tkinter and played with Side/PyQt wxPython is powerful enough for ost things, has a good range of widgets and fits OOP well without forcing a particular MVC style on you. (that may not be a good thing depending on your views. Its certainly much less formulaic in its MVC approach that Java GUIs like Swing and FX where MVC is taken to mad extremes.) > 3) For the O-O part, I'm comfortable with Inheritance and Composition. Do I > need to worry about any of the more advanced design patterns? This app will > be for vehicle ownership - tracking maintenance, etc. Nothing fancy. Inheritance and composition are primarily implementation tricks. The core of OOP is identifying where the functionality sits. Which objects are responsible for which pieces of the jigsaw. The way functions are divided between object methods tends to be quite different to the way you'd do it in a procedural (or functional) approach. Look out for cases where you are trying to fetch data from another object to process it and stop. Ask whether the other object should be doing that bit of processing. Remember that objects do it to themselves. Coming from a Java background resist the urge to write getters and setters. You shouldn't need them most of the time. If other objects are getting and setting your internal data something is probably wrong with your design. > 4) I plan to write my Use Case in Libre Office Write. For the UML diagrams, > is there a simple app to handle that? I don't need it to generate code but > I'd like to have a nice class diagram if possible. I wouldn't worry about UML class diagrams overmuch, they are mainly useful when you get into big apps with dozens (or hundreds or thousands) of model classes. Most desktop apps based on Sqlite only have a dozen or less models and maybe 20-30 classes in total. You don't need UML for that, only CRC cards - which can be in a spreadsheet. If you really want to draw UML Powerpoint will work just fine for starters. A full blown Case tool only pays dividends when you need to make classes available o other projects for reuse or want the tool to identify gaps and inconsistencies in class/method definitions. If you still want to go down the CASE tool route there are add-ons for Eclipse and visual Studio and I suspect for Netbeans too. Google is your friend. > 5) I'm really having trouble envisioning the GUI screens. How does one get > a handle on that? Just draw the screens on paper, or what? Personally that's what I do, keeping them deliberately vague. Whats much more important is getting your wire-frames joined together to show the workflow across windows (the UX design). A state chart can often be useful here too. Managing that workflow/state is what your controllers should be doing. No business logic but all the user flows and GUI rules. The controllers control the usecases not the models. (Jacobsen suggested one controller per usecase, I don't like to be that prescriptive but certainly there should be fewer controllers than there are views and models) > Any ideas very much appreciated. HTH. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From cs at cskk.id.au Sat May 11 18:59:26 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 12 May 2019 08:59:26 +1000 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: <20190511225926.GA4069@cskk.homeip.net> On 11May2019 14:59, Cranky Frankie wrote: >I'm a long time IT professional trying to teach myself object-oriented >programming. As such I want to build a traditional PC app using MVC (Model >- View - Controller) architecture. Just want to make sure I'm heading about >this in the best way so I'm looking for some direction. > >For the Model or persistence layer I want to use SQLite. That's reasonable. I'm trusting you're aware of SQL injection issues and will be constructing your SQL with '?' parameter placeholders to avoid this? >For the View or >GUI I want to use wxPython. For the Controller I want to of course use >Python. I'm also planning on using Git for source control. All reasonable. >1) For the IDE I'm most comfortable with Netbeans/Java, but I'm forcing >myself to try and get comfortable with PyCharm. Is it worth sticking it out >with PyCharm, or should I go with the Python module in Netbeans? Or is >there another IDE I should look at? I haven't an opinion about IDEs except that unless the IDE doesn't work with Python for whatever reason, there's no reason to switch IDEs unless you want to learn a specific new IDE. Several of us don't really use IDEs and just use editors and terminal windows, though that is less common in the Windows world. Just use what already works unless there's some feature elsewhere which you need. [...] >3) For the O-O part, I'm comfortable with Inheritance and Composition. >Do I >need to worry about any of the more advanced design patterns? This app will >be for vehicle ownership - tracking maintenance, etc. Nothing fancy. I wouldn't worry. Python mostly does inheritance; I suppose mixin classes (inherits) are like composition. The biggest change you'll find coming from Java is likely the typing: Python _values_ are strongly typed, but the variables are not - they can refer to a value of any type. And there aren't really Java interfaces. However, there are lint tools to look for issues like this. Oh yes: more than one class per Python source file - group them by package/module/topic. Cheers, Cameron Simpson From PyTutor at danceswithmice.info Sat May 11 18:12:28 2019 From: PyTutor at danceswithmice.info (DL Neil) Date: Sun, 12 May 2019 10:12:28 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: Cranky, It is a little difficult to answer your question. First impression is that your experience gives you good ideas of how to solve the problem. Some of them may not suit the Python environment as well as (say) that of PHP/MSFT. So, a learning opportunity there too. Second impression is that there are a lot of moving parts. How many of them do you know well, and how many are learning-targets? Trying to tackle 'too much' that is 'new' may result in heavy frustration. (no mention of skill-levels, so 'just sayin') The web-ref is not a book, but a *simple* article. It will serve, as long as you stick within the narrow scope of that article. The instant you deviate or customise, you're 'on your own'. For this reason, my recommendation is always a (proper/full) book*, if at all possible. You speak of MVC, so one (of many) Python web framework is Mig Grinberg's "Flask". Apart from his own, there are several other books which cover this subject. Following such a text will enable you to start-out on 'toy examples' and then gather expertise. Likely, you will re-shape this project whilst learning. * I used the term "book" but the last person to whom I recommended such couldn't afford the cost, and these days there are many excellent on-line and equally structured alternatives, eg Python courses on Coursera.org and edX.org (both 'freemium' offerings) - IIRC(?) Flask is available on Lynda (LinkedIN). That said, let's try responding to each of your points:- On 12/05/19 6:59 AM, Cranky Frankie wrote: > I'm a long time IT professional trying to teach myself object-oriented > programming. As such I want to build a traditional PC app using MVC (Model > - View - Controller) architecture. Just want to make sure I'm heading about > this in the best way so I'm looking for some direction. > > For the Model or persistence layer I want to use SQLite. For the View or > GUI I want to use wxPython. For the Controller I want to of course use > Python. I'm also planning on using Git for source control. > > 1) For the IDE I'm most comfortable with Netbeans/Java, but I'm forcing > myself to try and get comfortable with PyCharm. Is it worth sticking it out > with PyCharm, or should I go with the Python module in Netbeans? Or is > there another IDE I should look at? To my observation an IDE is less important in Python (than for highly structured, strongly typed, compiled, ... languages). My advice is, once you've chosen and found that it works, do NOT switch editor/IDE in anything less than one year. Under the 80-20 'rule', we use a basic few editing 'features' to do most of our work. Thus, it stands to reason that it will take periods of 'real time' before you decide 'there must be an easier way' to achieve some functionality and go looking in the editor's menus/help/etc to see if/what it might be! The other aspect is that 'religious wars' are fought over "which is the best editor/IDE" style questions. If you believe 'everyone' you'll 'jump' so often that you'll never figure-out 'which'. So, given that PyCharm is purpose-built, has a good reputation, and you have already used it: "if it ain't broke, why fix it"? (NB I don't use it, but have in the past) > 2) For wxPython I'm finding a lot of the documentation is outdated. Is this > book any good: > > http://www.blog.pythonlibrary.org/2019/05/08/creating-gui-applications-with-wxpython-now-available/ > > Or is there a better book/course/website I should be working with? Or is > there a better grahpics framework at this point for a traditional desktop > app? - attempted to tackle in 'strategy' comments, above > 3) For the O-O part, I'm comfortable with Inheritance and Composition. Do I > need to worry about any of the more advanced design patterns? This app will > be for vehicle ownership - tracking maintenance, etc. Nothing fancy. At risk of creating a dichotomy, whilst there are books which attempt to 'translate' the original gang-of-four patterns (and more) into 'Python', none has really impressed. "Pythonista" talk of "pythonic" solutions. Personal observations when learning Python (as if I'm not still...) included the need to desist from trying to solve a problem in xyz-other-language and 'translate' that to Python, but to learn how Python's construction enables its own solution-approach - sometimes quite distinctive. My favorite simple example of this is that many other languages offer do/for-loops. However Python's construct should be called for-each because it does not manage an index, eg loop-around doing something with array[index]; but instead/additionally "iterates" over a "collection", eg for item in list: ... Which also disposes of the need to learn the GoF iterator pattern as a "pattern" - but does not excuse you from understanding the "idiom". Once again, such are best (IMHO) learned from the cohesive and comprehensive coverage of a decent Python book*, cf numerous and unrelated web/blog/etc entries. > 4) I plan to write my Use Case in Libre Office Write. For the UML diagrams, > is there a simple app to handle that? I don't need it to generate code but > I'd like to have a nice class diagram if possible. There are many UML tools. Most operating at an higher level and understanding the abstraction/components, thus better than using LibreOffice tools to 'roll your own' as 'pretty pictures' (without the tool understanding their meaning)! > 5) I'm really having trouble envisioning the GUI screens. How does one get > a handle on that? Just draw the screens on paper, or what? Yes! Paper, whiteboard, chalkboard, glass, ... It is a very iterative process, even after?especially after delivering the first MVP! > Any ideas very much appreciated. The MVP system design/project management ideas of iterative development repeats the theme of progressive-learning outlined earlier. Great things start from small-beginnings. Don't "bite off more than you can chew". Keep things manageable, reign-in your expectations, and thus help yourself to maintain your enthusiasm over the life of the project... All the best, -- Regards =dn From marc.tompkins at gmail.com Sat May 11 19:48:12 2019 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sat, 11 May 2019 16:48:12 -0700 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: On Sat, May 11, 2019 at 12:05 PM Cranky Frankie wrote: > 2) For wxPython I'm finding a lot of the documentation is outdated. I'm a fan of wxPython myself, for a number of reasons - it suits the way I think, and the applications it generates look native to the platform they're running on, as opposed to the way Java apps always seem look a little odd whether you run them on Windows or Mac. wxPython is a Python wrapper around the wxWidgets library (written in C, I believe); any time I've found the wxPython documentation lacking, the wxWidgets docs filled in the gaps. But the best documentation of all is the wxPython demo app; it contains working examples - with modifiable source code - for nearly every wxPython control, plus a few user-contributed extra controls. There's also an excellent mailing list (wxpython-users at googlegroups.com) which is a great place to get help. The downsides: - there's no (working) WYSIWYG code generation tool - wxGlade is so bad it's worse than nothing - layout is controlled by sizers in a model that might be different from what you're used to; failure modes if/when you screw up are surprising and nonintuitive. (Not disastrous, just confusing.) Make sure to include the Layout Inspection Tool while you're codebashing, and comment it out for production! Welcome to Python (and wxPython, I hope) - enjoy! From jugurtha.hadjar at gmail.com Sun May 12 01:02:58 2019 From: jugurtha.hadjar at gmail.com (Jugurtha Hadjar) Date: Sun, 12 May 2019 06:02:58 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: <37c5da8f-8759-ad77-fc04-ed07ffe1a6a5@gmail.com> Hi, It seems you have a good overall idea on how you would tackle the project. First, have you talked to the people you are trying to build the application for, or are you the very people you're building this for? If you're building this for other people, try an figure out common scenarios on how they're doing things. As developers, jumping to code is tempting but I think it comes at an unnecessarily high price we pay later. Second, a document that explains what your application does in the present tense to show these people might come in handy based on your users telling you what they do daily. Written before code. "AutoAwesome allows you to track the vehicles in your fleet. You can easily [insert what the end users have described] in a list of features." That document will elicit reactions and feedback. Again, it's easy to have fantasies about how broken their workflow is and think we know better and we can just code away and build the perfect product that will inspire awe when finally revealed. I get the sense that you might be lucky enough to know exactly what the needs are, but in case it's not for you, talking to users is important. On 5/11/19 7:59 PM, Cranky Frankie wrote: > For the Model or persistence layer I want to use SQLite. For the View or > GUI I want to use wxPython. For the Controller I want to of course use > Python. I'm also planning on using Git for source control. If you're using SQL, SQLAlchemy for the ORM (Object Relational Mapper) might be worth checking out. One benefit is allowing you to avoid writing raw, possibly unsafe queries. However, if you are comfortable writing SQL, then by all means just start and ship a first working version 0. A first working version in the hands of users will get you feedback on what really matters which neither you nor us can think of right now. One thing to do a bit later would be to decouple the persistence layer from your application. i.e: not make assumptions on which persistence technology you're using in your application. For example, in one project, we could save to a database or to Elastic interchangeably without the "application" changing because all the parts that are "specific" to the persistence technology used were contained in the appropriate, specific, module. Again, this all doesn't really matter right now. What matters is building an application in which you can go through one simple scenario from start to finish by the end the week.. > 1) For the IDE I'm most comfortable with Netbeans/Java, but I'm forcing > myself to try and get comfortable with PyCharm. Is it worth sticking it out > with PyCharm, or should I go with the Python module in Netbeans? Or is > there another IDE I should look at? I'd recommend to work with the IDE you're most comfortable with to produce the application. If you want to try another IDE, you can play with PyCharm when you're not working on your application. The goal here is to have a working product. Your users don't care which IDE you've developed the application with. Yes, tools are important up to the point where focusing on them prevents you from shipping the product you were trying to optimize the tools to ship in the first place. > 2) For wxPython I'm finding a lot of the documentation is outdated. Is this > book any good: > > http://www.blog.pythonlibrary.org/2019/05/08/creating-gui-applications-with-wxpython-now-available/ > > Or is there a better book/course/website I should be working with? Or is > there a better grahpics framework at this point for a traditional desktop > app? > One thing to think about is having the core of your application decoupled from the technology you use for persistence, view, or other things. For example, if you have to create a user, format addresses, parse text, etc. You can have functions that do each part that are blind to whether you're using wxPython or PyQt and only understand text. You can then use these functions by importing them whether you're building a web application, a Terminal User Interface, or a desktop application since they don't make assumptions. In other words, having utility functions you can import from different parts of the projects, or from other projects, that you import is better than having say parsing done by a method in a wxPython widget. You can then concentrate on the functionality and not bother with the "viewing" technology by building a console application first. Show it to the users where they could simply input text and you could display the results on a terminal, *then*, worry about the specificity. Again, the goal is to have software that *does* something, then make it look good, unless the end state itself is looking good. > 3) For the O-O part, I'm comfortable with Inheritance and Composition. Do I > need to worry about any of the more advanced design patterns? This app will > be for vehicle ownership - tracking maintenance, etc. Nothing fancy. Not worrying, but keeping the goal in mind: a first version of the application by the end of the week. The "Nothing fancy" reminds me of Linus Torvalds' email: """ I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. """ > 4) I plan to write my Use Case in Libre Office Write. > I think using plain text (like Markdown) would be better, instead of binary format. This way you can use version control to track changes on the documents and see diffs. I'd also recommend using GitHub or GitLab (they have free private repos). You can use that to have your code somewhere else than your laptop just in case and treat it like a real project with issues, deadlines, milestones, branches, etc. You can have issue templates (create your own) for features and bugs. You can also have pages/static site for your project generated automatically that can serve as a pitch or documentation that's written as the project matures, instead of as an afterthought. Check out `mkdocs`, or `gitbook`. I switched from gitbook to mkdocs. It's also useful to document assumptions and rationale. Often times, you write something and no one remembers why it was implemented. Did the user ask for it or did we just guess? Documenting rationale is very useful because you'll know why you implemented a feature, and why you got rid of something. > For the UML diagrams, > is there a simple app to handle that? I don't need it to generate code but > I'd like to have a nice class diagram if possible. Have a look at `pyreverse`. You can write the code and then point pyreverse on the directory, and it will generate the diagram for you to a nice document (pdf, png, svg, etc...). > 5) I'm really having trouble envisioning the GUI screens. How does one get > a handle on that? Just draw the screens on paper, or what? Simplest use case working by the end of the week. > Any ideas very much appreciated. > Recap: - Set up a project on GitLab or GitHub or something to save progress - Talk to users and eke out use cases if you haven't already done so - Write a simple document that describes what the product does (don't use *will*) - Show it around and see if you got it right. There ought to be a priority use case - Create issues on GitHub or GitLab for the functionality explaining why you're writing it - Keep functions dumb (your phone number parsing shouldn't care about wxPython or Qt) - Console first. Validate usefulness. First spreadsheet wasn't Excel. - Work on the branch/fork, etc... - Show your work. Tweak. Merge. - Next issue. There's a problem to be solved. Every day we forget that is one day too many of suffering for the user. From PyTutor at DancesWithMice.info Sat May 11 19:24:11 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 12 May 2019 11:24:11 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> 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 From PyTutor at DancesWithMice.info Sat May 11 23:20:58 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 12 May 2019 15:20:58 +1200 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: Message-ID: 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 From alan.gauld at yahoo.co.uk Sun May 12 03:59:16 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 12 May 2019 08:59:16 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: 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, 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). I'll usually also have a web browser for reading documentation if necessary, although that's mostly done in the interpreter using dir()/help(). Alt-Tab and the X cut 'n paste mechanism provides enough integration between windows. -- 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 ar at zeit.io Sun May 12 09:04:03 2019 From: ar at zeit.io (Arup Rakshit) Date: Sun, 12 May 2019 18:34:03 +0530 Subject: [Tutor] Local variable look up outside the function and method Message-ID: In the following the function, x is reachable outside the scope of foo function. In [1]: x = 10 In [2]: def foo(): ...: return x ...: In [3]: print(foo()) 10 But why it is not the case when the look up happens inside a instance method of a class? In [1]: class Foo: ...: x = 10 ...: def bar(self): ...: return x ...: In [2]: Foo().bar() --------------------------------------------------------------------------- NameError Traceback (most recent call last) in ----> 1 Foo().bar() in bar(self) 2 x = 10 3 def bar(self): ----> 4 return x NameError: name 'x' is not defined I figured I have to access it like: In [1]: class Foo: ...: x = 10 ...: def bar(self): ...: return self.__class__.x ...: ...: print(Foo().bar()) 10 Just want to know how these search happens in 2 contexts. Thanks, Arup Rakshit ar at zeit.io From alan.gauld at yahoo.co.uk Sun May 12 09:45:56 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 12 May 2019 14:45:56 +0100 Subject: [Tutor] Local variable look up outside the function and method In-Reply-To: References: Message-ID: On 12/05/2019 14:04, Arup Rakshit wrote: The first case x is a global variable - which in Python really means module level. In the second case you have placed it inside Foo so you need to specify that that is where it is located. Classes encapsulate their own methods and attributes, that is one of their purposes. and just as you can't access a method without preceding it with self similarly you can't access an attribute without providing the object or class reference. > I figured I have to access it like: > > In [1]: class Foo: > ...: x = 10 > ...: def bar(self): > ...: return self.__class__.x Remember that any time you find yourself using dunder techniques ask if there is an easier way. There usually is... You can just use return self.x or, possibly better: return Foo.x You know x is declared in the context of Foo so refer to it directly. The self approach runs the risk of an instance attribute of the same name being created and overriding the class attribute. (But sometimes that's what you want.) If you had an inheritance hierarchy where x was defined in multiple places the .__class__ approach would work better (probably even be needed). But in most cases these kinds of variables are defined once in the class and you are safe to access it by name - and it makes the code more readable and unambiguous. -- 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 martin at linux-ip.net Sun May 12 10:12:01 2019 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 12 May 2019 07:12:01 -0700 Subject: [Tutor] Local variable look up outside the function and method In-Reply-To: References: Message-ID: Hello Arup, > In the following the function, x is reachable outside the scope of foo function. > In [1]: x = 10 > In [2]: def foo(): > ...: return x > In [3]: print(foo()) > 10 > But why it is not the case when the look up happens inside a > instance method of a class? > > In [1]: class Foo: > ...: x = 10 > ...: def bar(self): > ...: return x > In [2]: Foo().bar() > --------------------------------------------------------------------------- > NameError Traceback (most recent call last) > in > ----> 1 Foo().bar() > > in bar(self) > 2 x = 10 > 3 def bar(self): > ----> 4 return x > > NameError: name 'x' is not defined > > I figured I have to access it like: > > In [1]: class Foo: > ...: x = 10 > ...: def bar(self): > ...: return self.__class__.x > ...: > ...: print(Foo().bar()) > 10 > > Just want to know how these search happens in 2 contexts. Probably the following is the best section of the documentation to read: https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables Here's also a little sample that may help. By the way, I wouldn't normally be doing things like "unset self.x" and creating those peculiar methods, but I thought it might be instructive in this case. This is using the same sort of technique you were with your class called Foo. (I don't know all of the reasons why to inherit from object, but I usually do...) class Foo(object): x = 10 # -- class variable def __init__(self): self.y = 20 # -- instance variable named 'y' def setinstancex(self, x): self.x = x def removeinstancex(self): del self.x def report(self): return (self.x, self.y) if __name__ == '__main__': f = Foo() print('# -- f.x retrieves class value, f.y instance value') print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x)) print() f.setinstancex(3.14159) print('# -- f.x retrieves instance value, f.y instance value') print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x)) print() print('# -- warning, we are about to change the value for Foo.x') print() Foo.x = 6.022141e-23 print('# -- f.x retrieves class value, f.y instance value') print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x)) print() f.removeinstancex() print('# -- f.x retrieves class value, f.y instance value') print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x)) print() Good luck, -Martin -- Martin A. Brown http://linux-ip.net/ From PyTutor at DancesWithMice.info Sun May 12 05:15:52 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 12 May 2019 21:15:52 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: 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 From dave at the-hills.org.uk Sun May 12 06:27:42 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Sun, 12 May 2019 11:27:42 +0100 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: Message-ID: Hi David, Firstly, thank you for your reply. One condition of my agreeing to undertake PAT was that I got a PAT machine that stored the data, as I am better at programming than paperwork! I have a Megger PAT 420 which provides a data backup to a USB stick, and thence transfer to my laptop, which is network connected a dual HDD system (Buffalo), which automatically copies the main HDD to the slave HDD. I found out by accident that the Megger PAT 420 data backup is actually an SQLite database, so that is my route for access. Having played with Python on Raspberry Pi's, I thought I would explore Python for data processing, and now, I have a set of programs which extract SQLite, to 'csv' then collate/process this data and produce a multi-page ODS spreadsheet document, which lists tests by location. I also have an application which extracts appliances requiring testing within a? +/- 30 day window, so I have a target for testing. My biggest problem is locating kit, and keeping up with removals/disposals and new acquisitions, but the guys are getting a bit better at communication! I thought it would be useful to the 'management' to have a handle on progress, and stats on tested/new/disposed, etc, hence the latest question. I convert the datetimestamp to Gregorian ordinal date, as this is easier to use in accessing the resulting 'defaultdict', do the counting and then convert the date back to something comprehensible for writing to an ODS spreadsheet. Having seen todays posts I am going to look at wxPython, as a front-end (and possibly display?) Thank you for your consideration Dave On 12/05/2019 04:20, David L Neil wrote: > 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"... > From matthew.polack at htlc.vic.edu.au Sun May 12 07:31:45 2019 From: matthew.polack at htlc.vic.edu.au (Matthew Polack) Date: Sun, 12 May 2019 21:31:45 +1000 Subject: [Tutor] What is this code doing? What is it? Message-ID: We're beginners trying to learn Python and have this sample code: https://github.com/PySimpleGUI/PySimpleGUI/blob/master/ProgrammingClassExamples/Win10%20versions/2a.%20PSG%20(checkbox%20and%20radiobuttons)%20-%20Copy.py There is a section of code that has this line:: result = str(' Cost: ' + '${:.2f}'.format(cost)) But I don't understamd what the curly brace part is actually doing: {:} '${:.2f}' I think the 2f means to use 2 decimal places...but what does the rest of this do? ..curly braces apparenly are for dictionaries...but I don't get how this is a dictionary..or what this {:} command is actually doing? Thanks for any clues or links to an easy to understand tutorial or something on this feature. - Matt -- **Disclaimer: *Whilst every attempt has been made to ensure that material contained in this email is free from computer viruses or other defects, the attached files are provided, and may only be used, on the basis that the user assumes all responsibility for use of the material transmitted. This email is intended only for the use of the individual or entity named above and may contain information that is confidential and privileged. If you are not the intended recipient, please note that any dissemination, distribution or copying of this email is strictly prohibited. If you have received this email in error, please notify us immediately by return email or telephone +61 3 5382 2529**?and destroy the original message.* From nathan-tech at hotmail.com Sun May 12 08:26:10 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Sun, 12 May 2019 12:26:10 +0000 Subject: [Tutor] cython and threads, and feedparser Message-ID: Hello! After a day of various attempts yesterday, I managed to get cython installed on my windows 10 machine. Allow me to prefix this by saying, if there is somewhere else I should put this, II'M SORRY! So I ran cython on my python project, and it worked fine, there was one error about an int, which I have put at the end of this document. In summary though I don't think it has any relevance, might be me though. Here's the problem. In my program, when you load a new feed, the program launches a separate thread for using feedparser to download the rss feed, like this: def loadfeed(feed): ?# do the feed loading code here ?# play a sound to say the feed was loaded the loadfeed function is called through the threading module. I put some print statements in, to establish what is going on in cython. According to what I see, it launches the thread fine, it starts, and then it gets to this line: ??? newfeed=feedparser.parse(url) And just, stops. I admit, it could be a speed problem, as I've not had the patients to let it run past about a minute, but in the normal, uncythonised python code, it runs in between 1 and 5 seconds, based on size of feed. It doesn't error, it just acts like it has frozen. The thread doesn't disappear, because the little status checker I have set up to keep an eye on threads within the program says it is still running, it just... seems not to. I know this because, after the line above, I then have: ??? print '2' Which it never does. I was wondering if there is a common cython... thing, I have missed here, where you have to compile feedparser separately? not at all? or something. My worry is not speed, in this instance, 1 to 5 seconds is fine, all things considered and it being in a separate thread... I'm unworried at those timings. My aim with cython was a simple, but nice enough, layer of protection That being said, in my research I had read about the gil? Or something similar to do with only one python thread running at any one time in cython? I admit to not understanding this, though. Any explanations anyone can offer here would be greatly appreciated. Thank you very much. A confused Nate. Compilation: running build_ext cythoning rss.py to rss.c building 'rss' extension C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\python2732\include -Ic:\python2732\PC /Tcrss.c /Fobuild\temp.win32-2.7\Release\rss.obj /openmp rss.c rss.c(6931) : warning C4047: '=' : 'int' differs in levels of indirection from 'PyObject *' C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:c:\python2732\libs /LIBPATH:c:\python2732\PCbuild /LIBPATH:c:\python2732\PC\VS9.0 /EXPORT:initrss build\temp.win32-2.7\Release\rss.obj "/OUT:C:\Users\natha\Dropbox\coding projects\python\luna rss\rss.pyd" /IMPLIB:build\temp.win32-2.7\Release\rss.lib /MANIFESTFILE:build\temp.win32-2.7\Release\rss.pyd.manifest ?? Creating library build\temp.win32-2.7\Release\rss.lib and object build\temp.win32-2.7\Release\rss.exp C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\WinSDK\Bin\mt.exe -nologo -manifest build\temp.win32-2.7\Release\rss.pyd.manifest "-outputresource:C:\Users\natha\Dropbox\coding projects\python\luna rss\rss.pyd;2" From alan.gauld at yahoo.co.uk Sun May 12 14:13:19 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 12 May 2019 19:13:19 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: 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... :-) -- 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 alan.gauld at yahoo.co.uk Sun May 12 14:17:53 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 12 May 2019 19:17:53 +0100 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: Message-ID: On 12/05/2019 11:27, Dave Hill wrote: > I found out by accident that the Megger PAT 420 data backup is actually > an SQLite database, so that is my route for access. Having played with > Python on Raspberry Pi's, I thought I would explore Python for data > processing, and now, I have a set of programs which extract SQLite, to > 'csv' then collate/process this data and produce a multi-page ODS > spreadsheet document, which lists tests by location. I also have an > application which extracts appliances requiring testing within a? +/- 30 > day window, so I have a target for testing. You could do it all in native SQLite SQL of course. You can tell sqlite to output its results in Excel CSV format and to a file rather than (or in addition to) stdout. So you can write a standard query and have it generate your Excel readable file directly. You can then automate that as a batch job in the OS... Assuming you run the same reports regularly. Much as I love Python sometimes the native tools are even better... -- 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 alan.gauld at yahoo.co.uk Sun May 12 14:25:12 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 12 May 2019 19:25:12 +0100 Subject: [Tutor] What is this code doing? What is it? In-Reply-To: References: Message-ID: On 12/05/2019 12:31, Matthew Polack wrote: > result = str(' Cost: ' + '${:.2f}'.format(cost)) > > But I don't understamd what the curly brace part is actually doing: > ..curly braces apparenly are for dictionaries...but I don't get how this is > a dictionary..or what this {:} command is actually doing? Curly braces inside a string are nothing to do with dictionaries or sets(which also use them). They are placeholders for inserted data using the format() string method. Search the docs for string formatting, you will find a page on the subject with many examples. (or you could read the "Simple Sequences" topic in my tutorial - about half way down - see below) There is another, older style of formatting that uses % characters instead of {}. You will see both styles used regularly in Python code. Some folks prefer one, some the other. They are both very powerful ways of constructing output strings with data inserted. {} and format() has a few extra tricks once you get into advanced uses, but % style does most of the same things (and has the advantage of being used in other languages too so you only need to remember one style!). -- 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 robertvstepp at gmail.com Sun May 12 17:46:23 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 12 May 2019 16:46:23 -0500 Subject: [Tutor] Local variable look up outside the function and method In-Reply-To: References: Message-ID: On Sun, May 12, 2019 at 8:05 AM Arup Rakshit wrote: > > In the following the function, x is reachable outside the scope of foo function. > > In [1]: x = 10 > > In [2]: def foo(): > ...: return x > ...: > > In [3]: print(foo()) > 10 To what the others have said I wish to point out that if x is reassigned _inside the function_ x's scope is now local inside the function while x also still has a module scope. Consider: 3.6.8: x = 10 3.6.8: def foo(): ... x = 5 ... print("In foo x =", x) ... 3.6.8: foo() In foo x = 5 3.6.8: print("In module scope, x =", x) In module scope, x = 10 boB From robertvstepp at gmail.com Sun May 12 18:19:02 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 12 May 2019 17:19:02 -0500 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On Sun, May 12, 2019 at 1:05 PM David L Neil wrote: > 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 may be mistaken, but I think that a terminal multiplexer like tmux (https://github.com/tmux/tmux/wiki) is capable of session management. I have no personal use of tmux, but have been intrigued enough about others referring to it that eventually I will get around to seriously checking it out. -- boB From robertvstepp at gmail.com Sun May 12 18:56:16 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 12 May 2019 17:56:16 -0500 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On Sun, May 12, 2019 at 5:19 PM boB Stepp wrote: > > On Sun, May 12, 2019 at 1:05 PM David L Neil > wrote: > > > 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 may be mistaken, but I think that a terminal multiplexer like tmux > (https://github.com/tmux/tmux/wiki) is capable of session management. > I have no personal use of tmux, but have been intrigued enough about > others referring to it that eventually I will get around to seriously > checking it out. Actually, tmux is starting to look more and more interesting. David, I think you might this helpful. I am currently looking at an introduction to tmux by a Stack Overflow developer. This article is at https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ (There was a link to this on the tmux wiki I sent out a link to earlier.) I think I may start playing around with this! -- boB From cs at cskk.id.au Sun May 12 19:10:55 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 13 May 2019 09:10:55 +1000 Subject: [Tutor] Looking for some direction In-Reply-To: References: Message-ID: <20190512231055.GA92810@cskk.homeip.net> On 12May2019 17:19, boB Stepp wrote: >On Sun, May 12, 2019 at 1:05 PM David L Neil > wrote: >> 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 may be mistaken, but I think that a terminal multiplexer like tmux >(https://github.com/tmux/tmux/wiki) is capable of session management. >I have no personal use of tmux, but have been intrigued enough about >others referring to it that eventually I will get around to seriously >checking it out. Tmux is great, but I mostly use it for persistent sessions. It has facilities for running multiple panes in one terminal, which could be useful for remote sessions; locally I just use multiple panes in my terminal emulator, and in fact just multiple local panes connected to remote sessions anyway. I do use tmux panes in email though: I invoke mutt in a tmux session and replies start in a subpane, with mutt's index view in the upper (smaller) pane. But otherwise it is almost all persistent (and named) sessions. Now, I am spoilt: on a Mac I have access to iTerm3, which does: multiple tabs _and multiple panes and subpanes. My usual coding layout is a terminal in the left half of the screen running vim (with, usually, two _vim_ windows in it, vertically split). Often, that window gets a horizontal split, with a short and wide terminal pane below the editors, where I have a shell. The right hand half is antoher terminal, usually split intovarious panes, often 2 more evenly split. FOr shells and Python interpreters. But a really good terminal emulator is an outstanding tool. And iTerm3 seems to outshine everything else (Mac only alas - I'd like to find a food X11 equivalent - everything I've tried is deficient). Tabs and multiple panes is hugely flexible. It also has a toggle keystroke to expand a particular pane to the full window for when you want lots of text (diffs, commiting, etc). So unlike Alan, since it is all one Mac app (iTerm3), there's no Alt-TAB to switch apps, and since it does focus-follows-mouse cut/paste is very fast (iTerm3 has a mode like most X11 apps: select implicitly copies, so no Cmd-C copy keystroke for me either). Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Sun May 12 19:12:05 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 13 May 2019 00:12:05 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On 12/05/2019 23:19, boB Stepp wrote: > I may be mistaken, but I think that a terminal multiplexer like tmux > (https://github.com/tmux/tmux/wiki) is capable of session management. > I have no personal use of tmux, but have been intrigued enough about > others referring to it that eventually I will get around to seriously > checking it out. Me too. its been on my "must check that out" list for years! But I really must.... -- 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 eryksun at gmail.com Sun May 12 19:18:37 2019 From: eryksun at gmail.com (eryk sun) Date: Sun, 12 May 2019 18:18:37 -0500 Subject: [Tutor] What is this code doing? What is it? In-Reply-To: References: Message-ID: On 5/12/19, Alan Gauld via Tutor wrote: > > They are both very powerful ways of constructing output strings with > data inserted. {} and format() has a few extra tricks once you get > into advanced uses, but % style does most of the same things (and > has the advantage of being used in other languages too so you only > need to remember one style!). IMHO, given the choice of learning only one, learn the newer curly-brace string formatting system. It's extensible since it's based on a __format__ special method that a type (i.e. class) can override. Take the Decimal type, for instance. By default it supports 28 digits of precision. For example: >>> from decimal import Decimal >>> n = Decimal('0.12340567891234056789123405669') * 10 >>> n Decimal('1.234056789123405678912340567') Decimal implements the __format__ special method with its own implementation of the "f" format specifier, which faithfully renders the value as a string. >>> '{:0.27f}'.format(n) '1.234056789123405678912340567' We can also use this directly with the built-in format() function in Python 3: >>> format(n, '0.27f') '1.234056789123405678912340567' On the other hand, if we use a string's percent formatting, its "f" format specifier has a fixed implementation that first converts a number to a Python float, as opposed to delegating the string conversion to the object itself. >>> float(n) 1.2340567891234058 >>> '%0.27f' % n '1.234056789123405772912178691' As you can see in the formatted output, everything after "1.234056789123405" is wrong. This is because a CPython float is implemented as a C double-precision value, which only has about 16 decimal digits of precision. From bghancock at vivaldi.net Tue May 14 01:20:18 2019 From: bghancock at vivaldi.net (Ben Hancock) Date: Mon, 13 May 2019 22:20:18 -0700 Subject: [Tutor] Collating date data from a csv file In-Reply-To: References: Message-ID: <20190514052018.GA5816@gorilla> On Sun, May 12, 2019 at 07:17:53PM +0100, Alan Gauld via Tutor wrote: >On 12/05/2019 11:27, Dave Hill wrote: > >> I found out by accident that the Megger PAT 420 data backup is actually >> an SQLite database, so that is my route for access. Having played with >> Python on Raspberry Pi's, I thought I would explore Python for data >> processing, and now, I have a set of programs which extract SQLite, to >> 'csv' then collate/process this data and produce a multi-page ODS >> spreadsheet document, which lists tests by location. I also have an >> application which extracts appliances requiring testing within a? +/- 30 >> day window, so I have a target for testing. > >You could do it all in native SQLite SQL of course. >You can tell sqlite to output its results in Excel >CSV format and to a file rather than (or in addition to) >stdout. > >So you can write a standard query and have it generate >your Excel readable file directly. > >You can then automate that as a batch job in the OS... >Assuming you run the same reports regularly. > >Much as I love Python sometimes the native tools are >even better... > I wonder if `pandas` would also be a helpful library for what you're describing? It has a `pandas.read_sql` function to read data from a SQL database into a pandas dataframe, which you then can manipulate: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html Good luck! -- Ben Hancock https://elwha1.github.io From dave at the-hills.org.uk Tue May 14 04:00:34 2019 From: dave at the-hills.org.uk (Dave Hill) Date: Tue, 14 May 2019 09:00:34 +0100 Subject: [Tutor] Collating date data from a csv file In-Reply-To: <20190514052018.GA5816@gorilla> References: <20190514052018.GA5816@gorilla> Message-ID: <91328d83-b88d-cca5-1e15-7616aecdd6fa@the-hills.org.uk> Something else to have a look at, but it might have to wait for the longer winter nights :-) On 14/05/2019 06:20, Ben Hancock via Tutor wrote: > On Sun, May 12, 2019 at 07:17:53PM +0100, Alan Gauld via Tutor wrote: >> On 12/05/2019 11:27, Dave Hill wrote: >> >>> I found out by accident that the Megger PAT 420 data backup is actually >>> an SQLite database, so that is my route for access. Having played with >>> Python on Raspberry Pi's, I thought I would explore Python for data >>> processing, and now, I have a set of programs which extract SQLite, to >>> 'csv' then collate/process this data and produce a multi-page ODS >>> spreadsheet document, which lists tests by location. I also have an >>> application which extracts appliances requiring testing within a? >>> +/- 30 >>> day window, so I have a target for testing. >> >> You could do it all in native SQLite SQL of course. >> You can tell? sqlite to output its results in Excel >> CSV format and to a file rather than (or in addition to) >> stdout. >> >> So you can write a standard query and have it generate >> your Excel readable file directly. >> >> You can then automate that as a batch job in the OS... >> Assuming you run the same reports regularly. >> >> Much as I love Python sometimes the native tools are >> even better... >> > > I wonder if `pandas` would also be a helpful library for what you're > describing? It has a `pandas.read_sql` function to read data from a > SQL database into a pandas dataframe, which you then can manipulate: > > https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html > > > > Good luck! > > -- > Ben Hancock > https://elwha1.github.io > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From s.molnar at sbcglobal.net Wed May 15 08:16:02 2019 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Wed, 15 May 2019 08:16:02 -0400 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't Message-ID: <5CDC0302.2020608@sbcglobal.net> I am writing scripts to semi-automate some of my Quantum Chemistry software and have encountered a problem that has me baffled. The two scripts have the same form, the only difference being the commands. One script works, the other bombs. The script that works is: #!/usr/bin/env python # -*- coding: utf-8 -*- """ calc_pdbqt Created on Mon May 13 09:50:54 2019 copyrignt (c) 2019 Stephen P. Molnar, Ph.D. All rights reserved """ import subprocess with open('ligand') as infile: ligand = infile.read().strip().split() for nvar in ligand: command = ["./pythonsh", "./prepare_ligand4.py", "-l", nvar + ".mol2", "-o", nvar + ".pdbqt" ] proc = subprocess.Popen(command, stdout=subprocess.PIPE) out, err = proc.communicate() print(out) print(err) if proc.returncode: print ('Subprocess FAILED:', proc.command) while the script that bombs is: #!/usr/bin/env python # -*- coding: utf-8 -*- """ calc_pdf4 Created on Mon May 13 09:50:54 2019 copyrignt (c) 2019 Stephen P. Molnar, Ph.D. All rights reserved """ import subprocess with open('ligand') as infile: ligand = infile.read().strip().split() for nvar in ligand: command = ["./pythonsh", "./prepare_pdf4.py", "-l", nvar + ".pdbqt", "-ro", nvar + ".dpf" ] proc = subprocess.Popen(command, stdout=subprocess.PIPE) out, err = proc.communicate() print(out) print(err) if proc.returncode: print ('Subprocess FAILED:', proc.command) The errors are: runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands') b'' None /sdc1/Apps/MGLTools2-1.1/bin/python: can't open file './prepare_pdf4.py': [Errno 2] No such file or directory Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands') File "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 824, in runfile execfile(filename, namespace) File "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", line 23, in print ('Subprocess FAILED:', proc.command) AttributeError: 'Popen' object has no attribute 'command' I have attached the two scripts that are called and a test input file. Googling has not helped me to find a solution and I would greatly appreciate your assistance. Thanks in advance. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528(c) Skype: smolnar1 From cs at cskk.id.au Wed May 15 19:21:41 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 16 May 2019 09:21:41 +1000 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: <5CDC0302.2020608@sbcglobal.net> References: <5CDC0302.2020608@sbcglobal.net> Message-ID: <20190515232141.GA12077@cskk.homeip.net> On 15May2019 08:16, Stephen P. Molnar wrote: >I am writing scripts to semi-automate some of my Quantum Chemistry >software and have encountered a problem that has me baffled. The two >scripts have the same form, the only difference being the commands. As you say, the scripts are the same but for that tiny difference. So let's look: >for nvar in ligand: > command = ["./pythonsh", "./prepare_ligand4.py", [...] > command = ["./pythonsh", "./prepare_pdf4.py", [...] >The errors are: > >runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', >wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands') >b'' >None >/sdc1/Apps/MGLTools2-1.1/bin/python: can't open file >'./prepare_pdf4.py': [Errno 2] No such file or directory So: first issue: your prepare_pdf4.py file is missing. Is it really missing or are you in the wrong working directory? Might its filename be mistyped? BTW, you shouldn't need the leading "./" on the .py filenames: harmless but also useless. If you copied that usage from the "./pythonsh" incantation, the circumstances are different. You probably need the "./pythonsh" to execute it directly from the current directory, which is not in your $PATH (and indeed it should not be - that way lies subversion). The .py files are just data and do not need this. Then, the second part. This is a totally separate error from your script. Jumping to the bottom... >Traceback (most recent call last): [...] > File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", >line 23, in > print ('Subprocess FAILED:', proc.command) > >AttributeError: 'Popen' object has no attribute 'command' Because the prepare_pdf.py file is missing, for whatever reason, your "if proc.returncode" fire. And in that code you access "proc.command", which isn't defined. In the former script it doesn't try to execute this line because pythonsh succeeded. Looking at the subprocess docs, I think that is spelled "cmd", not "command". >I have attached the two scripts that are called and a test input file. This is a text only list. Attachments are dropped before your message goes out. Fortunately you have enough information in the main body of the message. >Googling has not helped me to find a solution [...] 1: I recommend duckduckgo instead of Google for privacy/tracking reasons. 2: When I search, I tend to find people with the same problem, not necessarily people with answers. But this question might be hard to get good hits on because the problem lies in a spelling error, which others may not have made that way. Cheers, Cameron Simpson From steve at pearwood.info Wed May 15 20:12:02 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 16 May 2019 10:12:02 +1000 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: <5CDC0302.2020608@sbcglobal.net> References: <5CDC0302.2020608@sbcglobal.net> Message-ID: <20190516001202.GS5010@ando.pearwood.info> Hi Stephen, My responses are interleaved with your comments below. Please excuse the length of my post, but your problem is a complicated one. On Wed, May 15, 2019 at 08:16:02AM -0400, Stephen P. Molnar wrote: > I am writing scripts to semi-automate some of my Quantum Chemistry > software and have encountered a problem that has me baffled. The two > scripts have the same form, the only difference being the commands. One > script works, the other bombs. A note on terminology: to programmers, the terminology "bombs" usually refers to a fatal application crash detected by the operating system: https://en.wikipedia.org/wiki/Bomb_%28icon%29 whereas what you are experiencing is merely an exception with a traceback displayed. The difference is that a "bomb" (or a "crash") is a serious bug in the application (Python), while an exception is actually a sign that Python is working correctly. The bottom line is that there's not much you can do about a bomb except stop running the script, whereas an exception means there's a bug in your code you can fix. > The script that works is: [...] Thanks for the careful and detailed samples of your code, but in general unless you are paying a consultant to to the work for you, it is best to try to narrow down the samples to the smallest amount that demonstrates the problem. We're volunteers, donating our time for free, and often with very limited time to offer. Every extra line of code you present us with discourages us a little more from tackling your problem, so the smaller the amount of code, the more likely we are to help. For a better explanation, you might like to read this: http://www.sscce.org/ It is written for Java programmers but applies to any language. > #!/usr/bin/env python This line tells a Unix system to run the script using "python", which is probably going to be Python 2. But I see from your code below that you are actually running Python 3 code. So this is a potential problem. I cannot tell if it is *actually* a problem right now, or will only become one in the future. You could try replacing the word "python" with "python3", but you should run these commands at the Unix shell first: env python env python3 and see what they say. Another problem is that both of your scripts call out to an external script `pythonsh` which we cannot see, so there's no way of knowing what that is doing. The second script refers to yet another Python script `prepare_pdf4.py` but the output says this file doesn't exist: can't open file './prepare_pdf4.py': [Errno 2] No such file or directory So that's a problem you need to fix. I have no idea if that will fix everything, or just part of the issue. > while the script that bombs is: [...] > Traceback (most recent call last): > > File "", line 1, in > runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', > wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands') > > File > "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", > line 824, in runfile > execfile(filename, namespace) This tells me that you are running your code via the Spyder IDE. That means you have *at least* five and maybe six components involved: - the Python interpreter - which runs the Spyder IDE - which runs your script - which runs the mystery file pythonsh; - which does something (runs it?) with the prepare_ligands4.py file or the (missing) prepare_pdf4.py file; - which does who knows what. When having problems debugging this code, it helps to simplify the problem by cutting Spyder out of the chain. If you run your script directly, using a command like python3 path/to/the/script.py does the error go away? I suspect not, but you can keep that in mind for future debugging. > File > "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", > line 110, in execfile > exec(compile(f.read(), filename, 'exec'), namespace) > > File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", line > 23, in > print ('Subprocess FAILED:', proc.command) > > AttributeError: 'Popen' object has no attribute 'command' That error message is completely correct: Popen objects don't have an attribute called "command". You are tring to print your own error message, referring to "proc.command" but there is no such thing. What are you trying to do? You need to find another way to do it. -- Steven From john at netcore.com.au Wed May 15 21:07:08 2019 From: john at netcore.com.au (John Collins) Date: Thu, 16 May 2019 11:07:08 +1000 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: <20190516001202.GS5010@ando.pearwood.info> References: <5CDC0302.2020608@sbcglobal.net> <20190516001202.GS5010@ando.pearwood.info> Message-ID: <3d6b2ce2-9416-afa2-9925-6972806c7689@netcore.com.au> Probably not applicable, but before deleting the original post, I distinctly remember seeing in amongst the text of the post, ".pfd" as part of a larger string of text, and as distinct from ".pdf". I wondered then if the OP's problem might be a typographical error? I am a student not a tutor - this is wild speculation! Best Regards, John Collins. From akleider at sonic.net Wed May 15 23:17:14 2019 From: akleider at sonic.net (Alex Kleider) Date: Wed, 15 May 2019 20:17:14 -0700 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On 2019-05-12 00:59, 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, > 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). > > I'll usually also have a web browser for reading > documentation if necessary, although that's mostly > done in the interpreter using dir()/help(). > > 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? Alex From oscar.j.benjamin at gmail.com Thu May 16 06:35:23 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 16 May 2019 11:35:23 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On Thu, 16 May 2019 at 04:30, Alex Kleider wrote: > > On 2019-05-12 00:59, Alan Gauld via Tutor wrote: > > On 12/05/2019 00:24, David L Neil 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? I don't have Ubuntu 18.04 here to test but from memory there is a setting (enabled by default) so that alt-tab switches between applications but to switch between windows within an application you can use alt-~ (That's alt with the tilde key: ~). That's assuming you have multiple terminal windows. If on the other hand you multiple terminal tabs within one window then you can use ctrl-pgup/pgdn or alt-1/2/3 to switch between tabs. -- Oscar From akleider at sonic.net Thu May 16 11:17:30 2019 From: akleider at sonic.net (Alex Kleider) Date: Thu, 16 May 2019 08:17:30 -0700 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On 2019-05-16 03:35, Oscar Benjamin wrote: > On Thu, 16 May 2019 at 04:30, Alex Kleider wrote: >> >> On 2019-05-12 00:59, Alan Gauld via Tutor wrote: >> > On 12/05/2019 00:24, David L Neil 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? > > I don't have Ubuntu 18.04 here to test but from memory there is a > setting (enabled by default) so that alt-tab switches between > applications but to switch between windows within an application you > can use alt-~ (That's alt with the tilde key: ~). It works! Thank you very much. a From s.molnar at sbcglobal.net Thu May 16 08:43:07 2019 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 16 May 2019 08:43:07 -0400 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: <20190516001202.GS5010@ando.pearwood.info> References: <5CDC0302.2020608@sbcglobal.net> <20190516001202.GS5010@ando.pearwood.info> Message-ID: <5CDD5ADB.7040604@sbcglobal.net> Thanks for your reply. I am most appreciative of your detailed response. Please see below for my comments. On 05/15/2019 08:12 PM, Steven D'Aprano wrote: > Hi Stephen, > > My responses are interleaved with your comments below. Please excuse the > length of my post, but your problem is a complicated one. > > > On Wed, May 15, 2019 at 08:16:02AM -0400, Stephen P. Molnar wrote: >> I am writing scripts to semi-automate some of my Quantum Chemistry >> software and have encountered a problem that has me baffled. The two >> scripts have the same form, the only difference being the commands. One >> script works, the other bombs. > A note on terminology: to programmers, the terminology "bombs" usually > refers to a fatal application crash detected by the operating system: > > https://en.wikipedia.org/wiki/Bomb_%28icon%29 > > whereas what you are experiencing is merely an exception with a > traceback displayed. The difference is that a "bomb" (or a "crash") is a > serious bug in the application (Python), while an exception is actually > a sign that Python is working correctly. > > The bottom line is that there's not much you can do about a bomb except > stop running the script, whereas an exception means there's a bug in > your code you can fix. > My use of computers in my scientific work goes back to the early 1960's as a graduate student when I took an introductory course in FORTRAN II. I am a Chemist and not a professional programmer, although I occasionally write a FORTRAN program, thanks to Linux and gfortran. Although retired, I am still maintaining a computational chemistry research program. So, substitute 'bug' for 'bomb' in this thread. >> The script that works is: > [...] > > Thanks for the careful and detailed samples of your code, but in general > unless you are paying a consultant to to the work for you, it is best to > try to narrow down the samples to the smallest amount that demonstrates > the problem. We're volunteers, donating our time for free, and often > with very limited time to offer. Every extra line of code you present us > with discourages us a little more from tackling your problem, so the > smaller the amount of code, the more likely we are to help. > > For a better explanation, you might like to read this: > > http://www.sscce.org/ > > It is written for Java programmers but applies to any language. > > > >> #!/usr/bin/env python > This line tells a Unix system to run the script using "python", which is > probably going to be Python 2. But I see from your code below that you > are actually running Python 3 code. So this is a potential problem. I > cannot tell if it is *actually* a problem right now, or will only become > one in the future. > > You could try replacing the word "python" with "python3", but you should > run these commands at the Unix shell first: > > env python > env python3 > > and see what they say. > > Another problem is that both of your scripts call out to an external > script `pythonsh` which we cannot see, so there's no way of knowing what > that is doing. Pythonsh is a bash script, a part of MGL_Tools (http://mgltools.scripps.edu/) which is the source of prepare_ligand4.py and prepare_dpf4.py which I am attempting to implement in my two scripts. MGL_Tools is a gui for AutoDock which, in turn I have been using for at least 25 years now. (Both MGL_Tools and AutoDock are open source and have been widely used.) The function of pythonsh is to establish an environment for the use of python2 and to set a number of paths.. If I invoke pythonsh in a bash shell I get: comp at AbNormal:/sdc1/Apps/Models/1-NerveAgents/Scripts$ ./pythonsh setting PYTHONHOME environment Python 2.5.6 (r256:88840, Nov 6 2012, 15:29:26) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> > The second script refers to yet another Python script > `prepare_pdf4.py` but the output says this file doesn't exist: > > can't open file > './prepare_pdf4.py': [Errno 2] No such file or directory > > > So that's a problem you need to fix. I have no idea if that will fix > everything, or just part of the issue. > > The problem may not be the invocation of prepare_dpf4.py, because: comp at AbNormal:/sdc1/Apps/Models/1-NerveAgents/Ligands$ ./pythonsh ./prepare_dpf4.py prepare_dpf4.py: ligand and receptor filenames must be specified. Usage: prepare_dpf4.py -l pdbqt_file -r pdbqt_file -l ligand_filename -r receptor_filename Optional parameters: [-o output dpf_filename] [-i template dpf_filename] [-x flexres_filename] [-p parameter_name=new_value] [-k list of parameters to write] [-e write epdb dpf ] [-v] verbose output [-L] use local search parameters [-S] use simulated annealing search parameters [-s] seed population using ligand's present conformation Prepare a docking parameter file (DPF) for AutoDock4. The DPF will by default be _.dpf. This may be overridden using the -o flag. Then: /sdc1/Apps/Models/1-NerveAgents/Ligands$ ./pythonsh ./prepare_dpf4.py -l VM.pdbqt -ro apo-1acl.pdbqt which results in: /sdc1/Apps/Models/1-NerveAgents/Ligands$ l *.dpf -rw-r--r-- 1 comp comp 2892 May 16 08:20 VM_o.dpf Now, the only problem here is that the file name should be 'VM.dpf' not 'VM_o.dpf'. However, I'll worry about that later. incidentally, the contents of the file are correct. while the script that bombs is: > [...] >> Traceback (most recent call last): >> >> File "", line 1, in >> runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', >> wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands') >> >> File >> "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", >> line 824, in runfile >> execfile(filename, namespace) > This tells me that you are running your code via the Spyder IDE. That > means you have *at least* five and maybe six components involved: > > > - the Python interpreter > - which runs the Spyder IDE > - which runs your script > - which runs the mystery file pythonsh; > - which does something (runs it?) with the prepare_ligands4.py > file or the (missing) prepare_pdf4.py file; > - which does who knows what. Yes, I do use Spyder. I like the Variable Explorer feature. However, I get the same results when using Geany or the command line. > When having problems debugging this code, it helps to simplify the > problem by cutting Spyder out of the chain. If you run your script > directly, using a command like > > python3 path/to/the/script.py > > does the error go away? I suspect not, but you can keep that in mind for > future debugging. > > > >> File >> "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", >> line 110, in execfile >> exec(compile(f.read(), filename, 'exec'), namespace) >> >> File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", line >> 23, in >> print ('Subprocess FAILED:', proc.command) >> >> AttributeError: 'Popen' object has no attribute 'command' Yes, I will grant the error 'AttributeError: 'Popen' object has no attribute 'command' ' i s there, but, if I run the other script with the same commands, I get: . . . None None None None ------------------ (program exited with code: 0) Press return to continue with the Geany IDE, and the corresponding .pdbqt from .mol2 files are generated. This is the mayor reason for my confusion and frustration! > That error message is completely correct: Popen objects don't have an > attribute called "command". > > You are tring to print your own error message, referring to > "proc.command" but there is no such thing. What are you trying to do? > You need to find another way to do it. > > If this is the case, why: 17.1.4.5. Replacing |os.popen()| , |os.popen2()| , |os.popen3()| pipe = os.popen("cmd", 'r', bufsize) ==> pipe = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE).stdout as a result of a web search? Just what is "cmd"? Finally, my apologies for not hewing to the tenants of: http://www.sscce.org, and being rather verbose.. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528(c) Skype: smolnar1 From alan.gauld at yahoo.co.uk Fri May 17 12:15:05 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 17 May 2019 17:15:05 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: 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. FWIW I just use Alt-Tab. I'm on Linux Mint with Cinnamon. -- 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 oscar.j.benjamin at gmail.com Fri May 17 12:33:28 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 17 May 2019 17:33:28 +0100 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On Fri, 17 May 2019 at 17:17, Alan Gauld via Tutor wrote: > > Sorry I've been busy, my mother passed away early this morning > and we've been with her the last few days. Really sorry to hear that Alan. I hope you're holding up okay. (Obviously don't feel the need to answer python-tutor questions here right now if you'd rather not...) All the best, Oscar From marcus.luetolf at bluewin.ch Sat May 18 04:20:20 2019 From: marcus.luetolf at bluewin.ch (=?iso-8859-1?Q?marcus_l=FCtolf?=) Date: Sat, 18 May 2019 10:20:20 +0200 Subject: [Tutor] simple question about scope Message-ID: <003701d50d52$888f4df0$99ade9d0$@bluewin.ch> Dear experts in learning the principles of Python I came across scope in the control structure's section. There I read the notion that variables createted inside a control structute can't be seen or accessed from outside that structure, Python would raise a Name Error. However in for loops - also control structures - like in this simple example: for i in range(1,11): sum = 0 sum += i print(sum) the print function "returns" the last number of range as value of the variable sum created inside this for loop. It should have raised a Name Error instead. Could somebody please explain this discrepancy? Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From Richard at Damon-Family.org Fri May 17 22:14:12 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Fri, 17 May 2019 22:14:12 -0400 Subject: [Tutor] Case Insensitive Globing Message-ID: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> I am working on a program to process some files created by an old windows program that created it files with varying case with a python program. Using glob.glob on Windows seems to ignore the case, and find all the matching files. The same directory, running the same program under Mac OS X, which also is a case insensitive file system, is only files that match the case of the glob, and is missing many of the files that were found under windows. Is there an easy was to make glob match files as a case insensitive manner? Or a simple way to do this with something else. I am trying to do something like: ? for file in glob.glob(pattern): processfile(file) -- Richard Damon From alan.gauld at yahoo.co.uk Sat May 18 06:33:49 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 18 May 2019 11:33:49 +0100 Subject: [Tutor] simple question about scope In-Reply-To: <003701d50d52$888f4df0$99ade9d0$@bluewin.ch> References: <003701d50d52$888f4df0$99ade9d0$@bluewin.ch> Message-ID: On 18/05/2019 09:20, marcus l?tolf wrote: > in learning the principles of Python I came across scope in the > control structure's section. > There I read the notion that variables createted inside a > control structute can't be seen or accessed from outside that > structure, Python would raise a Name Error. I don't know which tutorial you are reading but that's just wrong. Names created inside functions are local to that function. Names created inside a module are local to that module (and rather confusingly referred to as global scope) Names created inside a class are local to that class. Names created inside a generator expression are local to that expression. That's pretty much all you need to know about python scoping. > However in for loops - also control structures - like in this > simple example: Control structures like for/while/if-else etc have no influence no name visibility. -- 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 alan.gauld at yahoo.co.uk Sat May 18 06:52:29 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 18 May 2019 11:52:29 +0100 Subject: [Tutor] Case Insensitive Globing In-Reply-To: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> Message-ID: On 18/05/2019 03:14, Richard Damon wrote: > The same directory, running the same program under Mac OS X, which also > is a case insensitive file system, That is your mistake. Darwin, the core of the MacOS X system is a version of BSD Unix and like all Unix OS is very much case sensitive. Some of the GUI tools in MacOS X may work as if they were case insensitive (presumably for backwards compatibility to MacOS 9) but the underlying OS is case sensitive and all the Terminal type tools and commands, including Python, follow suit. > Is there an easy was to make glob match files as a case insensitive manner? Depends on what you mean by easy :-) You could include both upper and lower case letters in the search: glob.glob("[aA][mM][iI][xX][eE][dD][nN][aA][mM][eE]") And you could write a helper function to generate the search strings. But personally I'd probably just use listdir and compare with my search string expressed as a regex. for fname in os.listdir('.'): if re.match("aregex", fname) ... Another option might be to use fnmatch against the uppercase version of the name for fname in os.listdir('.'): if fnmatch(pattern, fname.upper()) ... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Sat May 18 09:39:15 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 18 May 2019 07:39:15 -0600 Subject: [Tutor] simple question about scope In-Reply-To: <003701d50d52$888f4df0$99ade9d0$@bluewin.ch> References: <003701d50d52$888f4df0$99ade9d0$@bluewin.ch> Message-ID: On 5/18/19 2:20 AM, marcus l?tolf wrote: > Dear experts > > in learning the principles of Python I came across scope in the > control structure's section. > There I read the notion that variables createted inside a > control structute can't be seen or accessed from outside that > structure, Python would raise a Name Error. > > However in for loops - also control structures - like in this > simple example: > > for i in range(1,11): > sum = 0 > sum += i > print(sum) > > the print function "returns" the last number of range as > value of the variable sum created inside this for loop. > It should have raised a Name Error instead. > Could somebody please explain this discrepancy? in addition to Alan's comment, in your loop you are resetting sum to zero each time through, which is why at the end of the loop sum is the same as the finishing value of i, not the sum of the values... From mats at wichmann.us Sat May 18 09:59:28 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 18 May 2019 07:59:28 -0600 Subject: [Tutor] Case Insensitive Globing In-Reply-To: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> Message-ID: <78442cf6-74df-db89-76e3-fdfc3b23d3c3@wichmann.us> On 5/17/19 8:14 PM, Richard Damon wrote: > I am working on a program to process some files created by an old > windows program that created it files with varying case with a python > program. > > Using glob.glob on Windows seems to ignore the case, and find all the > matching files. > > The same directory, running the same program under Mac OS X, which also > is a case insensitive file system, is only files that match the case of > the glob, and is missing many of the files that were found under windows. > > Is there an easy was to make glob match files as a case insensitive manner? > > Or a simple way to do this with something else. > > I am trying to do something like: > > ? for file in glob.glob(pattern): processfile(file) > here's a little snippet I've used in the past - uses fnmatch to help translate the pattern into a regular expression: import os, re, fnmatch def findfiles(pattern, path='.'): rule = re.compile(fnmatch.translate(pattern), re.IGNORECASE) return [name for name in os.listdir(path) if rule.match(name)] From marcus.luetolf at bluewin.ch Sat May 18 09:59:56 2019 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Sat, 18 May 2019 15:59:56 +0200 Subject: [Tutor] simple question about scope SOLVED Message-ID: <000401d50d81$f9ac2b50$ed0481f0$@bluewin.ch> Many thanks for this quick answer. I unfortunatly misread the tutorial (edx Course CS1301xII, Computing in Python II: Control Structures) concerning scope insofar as a NameError arises if a variable is accessed which was not created inside the control structure. Marcus. -----Urspr?ngliche Nachricht----- Von: Tutor [mailto:tutor-bounces+marcus.luetolf=bluewin.ch at python.org] Im Auftrag von Alan Gauld via Tutor Gesendet: Samstag, 18. Mai 2019 12:34 An: tutor at python.org Betreff: Re: [Tutor] simple question about scope On 18/05/2019 09:20, marcus l?tolf wrote: > in learning the principles of Python I came across scope in the > control structure's section. > There I read the notion that variables createted inside a control > structute can't be seen or accessed from outside that structure, > Python would raise a Name Error. I don't know which tutorial you are reading but that's just wrong. Names created inside functions are local to that function. Names created inside a module are local to that module (and rather confusingly referred to as global scope) Names created inside a class are local to that class. Names created inside a generator expression are local to that expression. That's pretty much all you need to know about python scoping. > However in for loops - also control structures - like in this simple > example: Control structures like for/while/if-else etc have no influence no name visibility. -- 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 --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From Richard at Damon-Family.org Sat May 18 07:31:49 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Sat, 18 May 2019 07:31:49 -0400 Subject: [Tutor] [SPAM?] Re: Case Insensitive Globing In-Reply-To: References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> Message-ID: On 5/18/19 6:52 AM, Alan Gauld via Tutor wrote: > On 18/05/2019 03:14, Richard Damon wrote: > >> The same directory, running the same program under Mac OS X, which also >> is a case insensitive file system, > That is your mistake. Darwin, the core of the MacOS X system > is a version of BSD Unix and like all Unix OS is very much > case sensitive. > > Some of the GUI tools in MacOS X may work as if they were case > insensitive (presumably for backwards compatibility to MacOS 9) > but the underlying OS is case sensitive and all the Terminal > type tools and commands, including Python, follow suit. Ok, I guess I have gotten used to the GUI tools and their behavior, that would explain why glob works the way it does. >> Is there an easy was to make glob match files as a case insensitive manner? > Depends on what you mean by easy :-) > You could include both upper and lower case letters in the search: > > glob.glob("[aA][mM][iI][xX][eE][dD][nN][aA][mM][eE]") > > And you could write a helper function to generate the search strings. > > But personally I'd probably just use listdir and compare with > my search string expressed as a regex. > > for fname in os.listdir('.'): > if re.match("aregex", fname) > ... > > Another option might be to use fnmatch against the uppercase > version of the name > > for fname in os.listdir('.'): > if fnmatch(pattern, fname.upper()) > ... > > HTH I was hoping for something simpler, but I guess an explicit directory search isn't that bad. Thanks. -- Richard Damon From ar at zeit.io Sat May 18 12:21:39 2019 From: ar at zeit.io (Arup Rakshit) Date: Sat, 18 May 2019 21:51:39 +0530 Subject: [Tutor] How arguments to the super() function works? Message-ID: I am writing an Flask app following a book, where a piece of python concept I am not getting how it works. Code is: class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') def __init__(self, **kwargs): super(Role, self).__init__(**kwargs) if self.permissions is None: self.permissions = 0 Here, why super(Role, self).__init__(**kwargs) is used instead of super().__init__(**kwargs) ? What that Role and self argument is instructing the super() ? Thanks, Arup Rakshit ar at zeit.io From alan.gauld at yahoo.co.uk Sat May 18 15:13:44 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 18 May 2019 20:13:44 +0100 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: References: Message-ID: On 18/05/2019 17:21, Arup Rakshit wrote: > class Role(db.Model): > > def __init__(self, **kwargs): > super(Role, self).__init__(**kwargs) > > Here, why super(Role, self).__init__(**kwargs) is used instead > of super().__init__(**kwargs) ? I suspect you are reading an older tutorial. It used to be the case that you had to provide the class and instance values to super() but that's no longer needed in the latest versions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at gmail.com Sat May 18 19:16:37 2019 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sun, 19 May 2019 00:16:37 +0100 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: References: Message-ID: On 18/05/2019 17:21, Arup Rakshit wrote: > I am writing an Flask app following a book, where a piece of python concept I am not getting how it works. Code is: > > class Role(db.Model): > __tablename__ = 'roles' > id = db.Column(db.Integer, primary_key=True) > name = db.Column(db.String(64), unique=True) > default = db.Column(db.Boolean, default=False, index=True) > permissions = db.Column(db.Integer) > users = db.relationship('User', backref='role', lazy='dynamic') > > def __init__(self, **kwargs): > super(Role, self).__init__(**kwargs) > if self.permissions is None: > self.permissions = 0 > > Here, why super(Role, self).__init__(**kwargs) is used instead of super().__init__(**kwargs) ? What that Role and self argument is instructing the super() ? > > Thanks, > > Arup Rakshit > ar at zeit.io Please check this https://www.youtube.com/watch?v=EiOglTERPEo out. If that doesn't answer your question please ask again. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Sat May 18 20:13:13 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 May 2019 10:13:13 +1000 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: References: Message-ID: <20190519001313.GA4221@ando.pearwood.info> On Sat, May 18, 2019 at 09:51:39PM +0530, Arup Rakshit wrote: > Here, why super(Role, self).__init__(**kwargs) is used instead of > super().__init__(**kwargs) ? What that Role and self argument is > instructing the super() ? The Role and self arguments are the owning class and current instance. They are always required, but in Python 2 you needed to provide them yourself, and it was error-prone. So in Python 3, the interpreter was modified to do the right thing when super is called with no arguments. super() still accepts manual arguments, and in fact the signature is quite complex: class super(object) | super() -> same as super(__class__, ) | super(type) -> unbound super object | super(type, obj) -> bound super object; requires isinstance(obj, type) | super(type, type2) -> bound super object; requires issubclass(type2, type) but any use other than plain old super(Class, self) in Python 2 or super() in Python 3 is considered rather advanced. -- Steven From steve at pearwood.info Sat May 18 20:37:56 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 May 2019 10:37:56 +1000 Subject: [Tutor] Case Insensitive Globing In-Reply-To: References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> Message-ID: <20190519003755.GB4221@ando.pearwood.info> On Sat, May 18, 2019 at 11:52:29AM +0100, Alan Gauld via Tutor wrote: > On 18/05/2019 03:14, Richard Damon wrote: > > > The same directory, running the same program under Mac OS X, which also > > is a case insensitive file system, > > That is your mistake. Darwin, the core of the MacOS X system > is a version of BSD Unix and like all Unix OS is very much > case sensitive. That's not quite right -- case sensitivity of the OS isn't important, case sensitivity of the *file system* is. And the standard file system on Mac OS, HFS+, defaults to case-preserving but case-insensitive. (There is an option to turn case-sensitivity off, but hardly anyone uses it because too many applications break.) https://stackoverflow.com/questions/4706215/mac-os-x-how-to-determine-if-filesystem-is-case-sensitive https://apple.stackexchange.com/questions/71357/how-to-check-if-my-hd-is-case-sensitive-or-not That means that, like Windows file systems FAT and NTFS, file names are case-insensitive: files "Foo", "foo" and "FOO" are all considered the same. But unlike Windows, the file system preserves the case of the file as you created it, so if you created it as "foO" that's how it will be recorded on the disk rather than normalizied to "foo". Fun fact: even NTFS supports a case-sensitive mode! But again, hardly anyone uses it. Likewise, if you are using Samba on Unix/Linux, you can have case-insensitive file operations on an underlying case-sensitive file system. > Some of the GUI tools in MacOS X may work as if they were case > insensitive (presumably for backwards compatibility to MacOS 9) > but the underlying OS is case sensitive and all the Terminal > type tools and commands, including Python, follow suit. I don't think that is correct: https://apple.stackexchange.com/questions/22297/is-bash-in-osx-case-insensitive -- Steven From steve at pearwood.info Sun May 19 01:21:09 2019 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 May 2019 15:21:09 +1000 Subject: [Tutor] Case Insensitive Globing In-Reply-To: <20190519003755.GB4221@ando.pearwood.info> References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: <20190519052109.GD4221@ando.pearwood.info> On Sun, May 19, 2019 at 10:37:56AM +1000, Steven D'Aprano wrote: > That's not quite right -- case sensitivity of the OS isn't important, > case sensitivity of the *file system* is. And the standard file system > on Mac OS, HFS+, defaults to case-preserving but case-insensitive. > > (There is an option to turn case-sensitivity off, but hardly anyone uses > it because too many applications break.) Oops, I meant to say there is an option to turn case-sensitivity ON, but hardly anyone uses it. By the way, Linux users can experiment with case-insensitive file systems provided they have privileges to mount file systems. Here's an example. At the shell: # allocate 100K for a (tiny) file system dd if=/dev/zero of=fat.fs bs=1024 count=100 # format as FAT-12 with an optional label /sbin/mkfs.vfat -n "FAT image" fat.fs # mount it and create some files sudo mount -o loop fat.fs /mnt sudo touch /mnt/{lower.txt,UPPER.TXT,mIxEd.TxT} Now you have a file system and some empty files to experiment with. -- Steven From ar at zeit.io Sun May 19 02:28:20 2019 From: ar at zeit.io (Arup Rakshit) Date: Sun, 19 May 2019 11:58:20 +0530 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: References: Message-ID: <490D376B-B539-4CA3-BCCB-F5130F9C0BBE@zeit.io> > On 19-May-2019, at 4:46 AM, Mark Lawrence wrote: > > On 18/05/2019 17:21, Arup Rakshit wrote: >> I am writing an Flask app following a book, where a piece of python concept I am not getting how it works. Code is: >> class Role(db.Model): >> __tablename__ = 'roles' >> id = db.Column(db.Integer, primary_key=True) >> name = db.Column(db.String(64), unique=True) >> default = db.Column(db.Boolean, default=False, index=True) >> permissions = db.Column(db.Integer) >> users = db.relationship('User', backref='role', lazy='dynamic') >> def __init__(self, **kwargs): >> super(Role, self).__init__(**kwargs) >> if self.permissions is None: >> self.permissions = 0 >> Here, why super(Role, self).__init__(**kwargs) is used instead of super().__init__(**kwargs) ? What that Role and self argument is instructing the super() ? >> Thanks, >> Arup Rakshit >> ar at zeit.io > > Please check this https://www.youtube.com/watch?v=EiOglTERPEo out. If that doesn't answer your question please ask again. > Hello Mark, Thanks for sharing the link. Just finished the talk. Little better now about how super uses the MRO. class Dad: def can_i_take_your_car(self): print("No...") class Mom(Dad): def can_i_take_your_car(self): print("Asking your dad...") class Victor(Mom, Dad): def can_i_take_your_car(self): print("Asking mom...") class Pinki(Mom, Dad): def can_i_take_your_car(self): print("I need it today..") class Debu(Pinki, Victor): def can_i_take_your_car(self): super(Victor, self).can_i_take_your_car() In this piece of code: print(Debu().can_i_take_your_car()) Why the above call prints "Asking your dad?? but not " print("Asking mom??)? although can_i_take_your_car is defined inside the class Victor ? help(Debu) #prints Help on class Debu in module __main__: class Debu(Pinki, Victor) | Method resolution order: | Debu | Pinki | Victor | Mom | Dad | builtins.object | | Methods defined here: | | can_i_take_your_car(self) | | ---------------------------------------------------------------------- | Data descriptors inherited from Dad: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) Last question: Why duplicate PEPs for the same thing https://www.python.org/dev/peps/pep-0367/ and https://www.python.org/dev/peps/pep-3135/#specification ? Which one to read when such duplicate exists? > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Thanks, Arup Rakshit ar at zeit.io From PyTutor at DancesWithMice.info Sat May 18 22:03:15 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 19 May 2019 14:03:15 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: <57fa8410-cf93-a5c5-5f11-d4f15010a9e5@DancesWithMice.info> 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 From PyTutor at DancesWithMice.info Sat May 18 22:07:39 2019 From: PyTutor at DancesWithMice.info (David L Neil) Date: Sun, 19 May 2019 14:07:39 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: <6b052d3d-11f0-f576-90f5-64b5d3efbfe4@DancesWithMice.info> 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 From PyTutor at danceswithmice.info Sat May 18 22:39:35 2019 From: PyTutor at danceswithmice.info (DL Neil) Date: Sun, 19 May 2019 14:39:35 +1200 Subject: [Tutor] Looking for some direction In-Reply-To: References: <5e428d08-b1d8-b4f5-30c3-2e92e3a5f0b3@DancesWithMice.info> Message-ID: On 13/05/19 10:56 AM, boB Stepp wrote: > On Sun, May 12, 2019 at 5:19 PM boB Stepp wrote: >> On Sun, May 12, 2019 at 1:05 PM David L Neil >> wrote: >>> 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 may be mistaken, but I think that a terminal multiplexer like tmux >> (https://github.com/tmux/tmux/wiki) is capable of session management. >> I have no personal use of tmux, but have been intrigued enough about >> others referring to it that eventually I will get around to seriously >> checking it out. > > Actually, tmux is starting to look more and more interesting. David, > I think you might this helpful. I am currently looking at an > introduction to tmux by a Stack Overflow developer. This article is > at https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ > (There was a link to this on the tmux wiki I sent out a link to > earlier.) I think I may start playing around with this! Thanks Bob. Am taking it for a spin... -- Regards =dn From PyTutor at danceswithmice.info Sat May 18 23:58:22 2019 From: PyTutor at danceswithmice.info (DL Neil) Date: Sun, 19 May 2019 15:58:22 +1200 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: <5CDC0302.2020608@sbcglobal.net> References: <5CDC0302.2020608@sbcglobal.net> Message-ID: Stephen, On 16/05/19 12:16 AM, Stephen P. Molnar wrote: > I am writing scripts to semi-automate some of my Quantum Chemistry > software and have encountered a problem that has me baffled. The two > scripts have the same form, the only difference being the commands. One > script works, the other bombs. Blast from the past! I thought I'd finished with nerve agents, receptors, and ligands back in 1990-1. The memories have not improved with age! Taking the first question, ie 'two scripts which appear identical but are not':- Nothing 'popped' during a quick visual scan. Linux (which it is assumed you are using) has two basic built-in commands/pgms: diff and cmp (difference and compare). For occasions when I want to compare directories first, and probably contained-files thereafter, I use "Meld". As to the second: +1 to removing the extra moving-parts, like Spyder, and running directly from the cmdLN (simplify, simplify). Even though accuracy and precision are important, this is the Tutor list, so don't worry too much about the terminology. Most of us, young or old will have recognised what you meant. Whilst I can't recall the last time I used the term "bomb", I'm guessing that "abend" wouldn't pass muster either... -- Regards =dn From eryksun at gmail.com Sun May 19 08:43:13 2019 From: eryksun at gmail.com (eryk sun) Date: Sun, 19 May 2019 07:43:13 -0500 Subject: [Tutor] Case Insensitive Globing In-Reply-To: <20190519003755.GB4221@ando.pearwood.info> References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: On 5/18/19, Steven D'Aprano wrote: > > That means that, like Windows file systems FAT and NTFS, file names are > case-insensitive: files "Foo", "foo" and "FOO" are all considered the > same. But unlike Windows, the file system preserves the case of the file > as you created it, so if you created it as "foO" that's how it will be > recorded on the disk rather than normalizied to "foo". NTFS and FAT32 are case preserving. > Fun fact: even NTFS supports a case-sensitive mode! But again, hardly > anyone uses it. There's nothing inherent in the design of NTFS that prevents case-sensitive names in a directory. It's strictly a function of the OS and filesystem driver. On non-Windows platforms, an NTFS driver can create names that differ only in case. An example is the Linux ntfs-3g driver, which allows this unless the "windows_names" option is set. In a Windows system, the NT object namespace is case sensitive, and overriding a create or open to be case insensitive requires the flag OBJ_CASE_INSENSITIVE. The Windows API uses this flag by default in the file and registry APIs. However, beginning with Windows XP, the kernel takes it a step further. Regardless of the user-mode subsytem, it forces this flag for object types that are defined as case insensitive, such as Object Manager "Directory" and "SymbolicLink" objects (used in the root object namespace, akin to the root filesystem in Unix), Configuration Manager "Key" objects (registry), and I/O manager "Device" and "File" objects. Prior to Windows XP, it was possible to force a CreateFile or FindFirstFileEx call to be case sensitive, respectively via the flags FILE_FLAG_POSIX_SEMANTICS and FIND_FIRST_EX_CASE_SENSITIVE. Internally, this is implemented by *omitting* the OBJ_CASE_INSENSITIVE flag that the Windows API usually includes for device and file operations. This didn't change in XP, but, as mentioned above, it's impotent now since the kernel itself adds the flag for case-insensitive object types. The only way to change this is to restart the system after zeroing the "obcaseinsensitive" value in the "Session Manager\kernel" registry key. That's not tenable in practice, so we have to just accept that device and file names are case insensitive. That said, in Windows 10, the Windows Subsystem for Linux requires a way to change the filesystem default to case sensitive for individual directories. If a directory is case sensitive, then create, open, and control operations in or on it always ignore OBJ_CASE_INSENSITIVE. This directory attribute can be queried and set with a new file information class in the NT API named "FileCaseSensitiveInformation", or on the command line via `fsutil file queryCaseSensitiveInfo ` and `fsutil file setCaseSensitiveInfo [enable|disable]`. From eryksun at gmail.com Sun May 19 09:15:59 2019 From: eryksun at gmail.com (eryk sun) Date: Sun, 19 May 2019 08:15:59 -0500 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: <490D376B-B539-4CA3-BCCB-F5130F9C0BBE@zeit.io> References: <490D376B-B539-4CA3-BCCB-F5130F9C0BBE@zeit.io> Message-ID: On 5/19/19, Arup Rakshit wrote: > > class Dad: > def can_i_take_your_car(self): > print("No...") > > class Mom(Dad): > def can_i_take_your_car(self): > print("Asking your dad...") > > class Victor(Mom, Dad): > def can_i_take_your_car(self): > print("Asking mom...") > > class Pinki(Mom, Dad): > def can_i_take_your_car(self): > print("I need it today..") > > class Debu(Pinki, Victor): > def can_i_take_your_car(self): > super(Victor, self).can_i_take_your_car() > > In this piece of code: > > print(Debu().can_i_take_your_car()) > > Why the above call prints "Asking your dad?? but not " print("Asking mom??)? > although can_i_take_your_car is defined inside the class Victor ? Victor comes after Pinki in the MRO: >>> super(Debu, Debu()).can_i_take_your_car() I need it today.. >>> super(Pinki, Debu()).can_i_take_your_car() Asking mom... >>> super(Victor, Debu()).can_i_take_your_car() Asking your dad... >>> super(Mom, Debu()).can_i_take_your_car() No... > Last question: Why duplicate PEPs for the same thing > https://www.python.org/dev/peps/pep-0367/ and > https://www.python.org/dev/peps/pep-3135/#specification ? Which one to read > when such duplicate exists? See https://www.python.org/dev/peps/pep-0367/#numbering-note and https://www.python.org/dev/peps/pep-3135/#numbering-note. From mats at wichmann.us Sun May 19 11:45:34 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 19 May 2019 09:45:34 -0600 Subject: [Tutor] How arguments to the super() function works? In-Reply-To: <490D376B-B539-4CA3-BCCB-F5130F9C0BBE@zeit.io> References: <490D376B-B539-4CA3-BCCB-F5130F9C0BBE@zeit.io> Message-ID: On 5/19/19 12:28 AM, Arup Rakshit wrote: > >> On 19-May-2019, at 4:46 AM, Mark Lawrence wrote: >> >> On 18/05/2019 17:21, Arup Rakshit wrote: >>> I am writing an Flask app following a book, where a piece of python concept I am not getting how it works. Code is: >>> class Role(db.Model): >>> __tablename__ = 'roles' >>> id = db.Column(db.Integer, primary_key=True) >>> name = db.Column(db.String(64), unique=True) >>> default = db.Column(db.Boolean, default=False, index=True) >>> permissions = db.Column(db.Integer) >>> users = db.relationship('User', backref='role', lazy='dynamic') >>> def __init__(self, **kwargs): >>> super(Role, self).__init__(**kwargs) >>> if self.permissions is None: >>> self.permissions = 0 >>> Here, why super(Role, self).__init__(**kwargs) is used instead of super().__init__(**kwargs) ? What that Role and self argument is instructing the super() ? >>> Thanks, >>> Arup Rakshit >>> ar at zeit.io >> >> Please check this https://www.youtube.com/watch?v=EiOglTERPEo out. If that doesn't answer your question please ask again. >> > > > Hello Mark, > > Thanks for sharing the link. Just finished the talk. Little better now about how super uses the MRO. > > class Dad: > def can_i_take_your_car(self): > print("No...") > > class Mom(Dad): > def can_i_take_your_car(self): > print("Asking your dad...") > > class Victor(Mom, Dad): > def can_i_take_your_car(self): > print("Asking mom...") > > class Pinki(Mom, Dad): > def can_i_take_your_car(self): > print("I need it today..") > > class Debu(Pinki, Victor): > def can_i_take_your_car(self): > super(Victor, self).can_i_take_your_car() > > In this piece of code: > > print(Debu().can_i_take_your_car()) > > Why the above call prints "Asking your dad?? but not " print("Asking mom??)? although can_i_take_your_car is defined inside the class Victor ? Because that's what you asked for? in Debu you asked to call the method through the object obtained by calling super on Victor, and that resolves to Mom per the MRO. help() gets that for you, but you can also check it programmatically: print(Victor.__mro__) (, , , ) you can also print the method it would resolve to, change class Debu to: class Debu(Pinki, Victor): def can_i_take_your_car(self): print(super(Victor, self).can_i_take_your_car) # note this is not a call super(Victor, self).can_i_take_your_car() and you can see how it resolves: > Asking your dad... From 2212bijaya at gmail.com Sun May 19 02:59:46 2019 From: 2212bijaya at gmail.com (bijaya dalei) Date: Sun, 19 May 2019 12:29:46 +0530 Subject: [Tutor] Query about python recipies for practices Message-ID: Hii, Good morning. I am a new user of python programming language. I have a small query on "where to get python recepies for practices".plz suggest.Thanks. From alan.gauld at yahoo.co.uk Sun May 19 14:06:33 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 May 2019 19:06:33 +0100 Subject: [Tutor] Two Scripts, Same Commands, One Works, One Doesn't In-Reply-To: References: <5CDC0302.2020608@sbcglobal.net> Message-ID: On 19/05/2019 04:58, DL Neil wrote: > last time I used the term "bomb", I'm guessing that "abend" wouldn't > pass muster either... Gosh, I haven't seen a reference to abend in 20 years. I'd almost managed to erase the memory... :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun May 19 14:19:31 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 19 May 2019 19:19:31 +0100 Subject: [Tutor] Case Insensitive Globing In-Reply-To: <20190519003755.GB4221@ando.pearwood.info> References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: On 19/05/2019 01:37, Steven D'Aprano wrote: > That's not quite right -- case sensitivity of the OS isn't important, > case sensitivity of the *file system* is. And the standard file system > on Mac OS, HFS+, defaults to case-preserving but case-insensitive. > > (There is an option to turn case-sensitivity off, but hardly anyone uses > it because too many applications break.) Oops, my mistake. Almost all my MacOS knowledge is based on my ancient iBook. Having had a look, it appears it's formatted in UFS rather than HFS+ so I assume I must have done something to force that when I first installed the system back in 2001... It certainly appears to be case sensitive but life is too short for me to do extensive testing right now! > Fun fact: even NTFS supports a case-sensitive mode! But again, hardly > anyone uses it. Hmm, odd. My NTFS filesystems on Windows all appear to be case sensitive. For example I have a photo editor that saves its files with a jpg extension but the files from my camera all end in JPG. So I always end up with two copies - the original file and the edited version. I'm not aware of having done anything to cause that. More investigation required I think... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From eryksun at gmail.com Sun May 19 18:36:38 2019 From: eryksun at gmail.com (eryk sun) Date: Sun, 19 May 2019 17:36:38 -0500 Subject: [Tutor] Case Insensitive Globing In-Reply-To: References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: On 5/19/19, Alan Gauld via Tutor wrote: > > Hmm, odd. My NTFS filesystems on Windows all appear to be case > sensitive. For example I have a photo editor that saves its files > with a jpg extension but the files from my camera all end in JPG. > So I always end up with two copies - the original file and the > edited version. > > I'm not aware of having done anything to cause that. > More investigation required I think... Maybe you have Explorer configured to hide file extensions, and you have one file named "filename.JPG" and another named "filename.jpg.JPG". On a related note, the new support for case-sensitive directories in Windows 10 can lead to an issue when running commands. Shells use the PATHEXT environment variable to supply a list of default extensions when searching PATH for a command. These are usually upper case, so if we run "script" and it searches for "script.PY", it won't find "script.py" in a case-sensitive directory. The same applies to Python's shutil.which(). Regarding this topic, glob.py and fnmatch.py are unreliable in Windows for case-sensitive NTFS directories. Sometimes they rely on regular expressions and os.path.normcase (for ntpath.normcase, this replaces slash with backslash and converts to lowercase), and sometimes they rely on stat-based functions such as os.path.lexists, which will be case sensitive. From alan.gauld at yahoo.co.uk Mon May 20 04:49:57 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 20 May 2019 09:49:57 +0100 Subject: [Tutor] Case Insensitive Globing In-Reply-To: References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: On 19/05/2019 19:19, Alan Gauld via Tutor wrote: > Hmm, odd. My NTFS filesystems on Windows all appear to be case > sensitive. For example I have a photo editor that saves its files > with a jpg extension but the files from my camera all end in JPG. > So I always end up with two copies - the original file and the > edited version. Amazing how you forget the simple things over time. I forgot I had moved all my photos onto my NAS box and then mounted that in my pictures library under Windows 10. Because you access them via the Library feature I'd completely forgotten they were on the NAS. So the filesystem is really ext3 and I'm assuming that's why the double names exist... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Mon May 20 07:40:56 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 20 May 2019 12:40:56 +0100 Subject: [Tutor] Case Insensitive Globing In-Reply-To: References: <2b3a7cb2-3b02-5c0c-c831-d36149dced92@Damon-Family.org> <20190519003755.GB4221@ando.pearwood.info> Message-ID: On 20/05/2019 09:49, Alan Gauld via Tutor wrote: > On 19/05/2019 19:19, Alan Gauld via Tutor wrote: > ... >> So I always end up with two copies - the original file and the >> edited version. > I forgot I had moved all my photos onto my NAS box > and then mounted that in my pictures library under > Windows 10. On closer study it was slightly more complex. In fact, the errant editor is actually a Linux utility program that I only rarely use but which uses the same NAS folder as is used by the Windows box(obviously, that's why it's on the NAS!) Now, because I do 90% of my photo editing on Windows (Affinity Photo really is superb!), I only noticed the duplicate file names there and so assumed it was a Windows thing. So,to summarize: I copy the files from the camera to the (Linux based) NAS. I edit the file on my Linux PC (which saves with the .jpg extension) Then I open the folder for serious editing on Windows and see two filenames differing only in the case of the extension. No NTFS involved. Just memory loss from a befuddled senior... :-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From rsalawani at hotmail.com Mon May 20 15:43:49 2019 From: rsalawani at hotmail.com (Rahul Alawani) Date: Mon, 20 May 2019 19:43:49 +0000 Subject: [Tutor] A Python Newbie Requesting help with Lambdas, Filters, and Maps Message-ID: Hello Tutors! I am an ambitious Python newbie without any CS background!? I am currently taking an online 30-hour/36-lecture Python course which I am enjoying a lot.? I am religiously answering the quizzes and solving all the exercise problems (usually short and straightforward).? So far, I have learned introductory topics such as data types, lists/dictionaries/tuples/sets, conditional statements, for and while loops, functions, and apparently uncommonly used lambdas (along with maps and filters). I know that my real, long battle lies ahead. Almost midway through the course, with the latest course content on lambdas, filters, maps, min/max built-in functions and created a long problem statement for an interesting self-exercise.? However, I might have gotten a tad bit too ambitious and need help. Below is my long problem statement: Please ignore the details regarding some ultra-popular Bollywood song artists and only a fraction of their most popular song titles. You can replace the artists' names and their Bollywood song titles with western artists' names and their respective song titles if the current names and titles are too distracting. I would appreciate it if you could simply focus on the nature of the data to be used and the multi-part problem. Those who might be willing to help, it would be easier if you copy and paste the entire code below into a Python 3 text editor such as Sublime Text (which I am using). Also, I have added my personal comments on problems which I have attempted so far and either solved completely or have come dangerously close to solving. I am not looking for a spoon-fed solution to every single unsolved problem. Just something that would push me over to the next step would suffice. I know conceptually how I would solve each of these problems anyone can solve them in their mind. What I'm struggling the most with is the syntax and order of certain functions when solving. Perhaps I am not considering some intermediate code steps that are required and/or helpful. Please note that the primary objective is to solve these problems using lambdas, filters, maps, etc. and NOT for or while loops or list comprehensions. Any help will be greatly appreciated with heartfelt thanks and email fist-bumps. Thank you for your time and consideration. Regards, Rahul A. So here it goes......................... # This function accepts a list of several dictionaries that contain song artists' names and their song titles and # returns the following outputs in tuples (remember to return a tuple for each output!): # 1. The highest number of songs sung by any artist # 2. The name of the artist with the highest number of songs # 3. A tuple containing the name of the artist and their number of songs sung # 4. The longest song title in the entire song list # 5. The name of the artist with the longest song title # 6. A tuple containing the name of the artist w/ longest song title and their longest song title # 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title # 8. A list of artists and their number of songs sung sorted (default ascending order) by the number of song titles # 9. A list of artists and their longest song titles sorted in descending order of the length of the song title # 9a. Similar list as original but longest title listed first and shortest title listed last for each artist # 9b. BONUS: same as 9a above with artists sorted in default ascending order. Thus the artists will be listed # in regular alphabetical order, but their song titles will listed in the descending order of title length songmix = [ {"artist":"Rafi","titles":["Pukarta chala hoon main","Aapke haseen rukh pe","Thaheriye hosh main aaloon","Woh jab yaad aaye","Deewana hua badal","Ehsan tera hoga mujhpar"]}, {"artist":"Asha","titles":["Aja aja main hun pyar tera","Dil cheez kya hai","Aaiye meherban","Aao huzur tum ko","In aankhon ki masti mein","Paan khaye saiyan humaro"]}, {"artist":"Suman","titles":["Rahete kabhi jinke dil mein","Na tum hamen jano","Jo hum pe guzarti hai","Rahe na rahe hum","Aajkal tere mere pyar ke","Tujhe dekha tujhe chaha","Parbaton ke pedon par","Tumne pukara aur"]}, {"artist":"Kishor","titles":["Beqarar dil","Nile nile ambar pe","Muqaddar ka sikandar","Mere mehboob kayamat hogi","Mere sapno ki rani kab","Pyar diwana hota hai","O mere dil ke chain","Yeh shaam mastani","Pal pal dil ke paas"]}, {"artist":"Lata","titles":["Lag ja gale","Tera jana dil ke armanon ka","Tera mera pyar amar","Yoon hasraton ke daag","Awaz deke hamen tum bulao","Mujhe kitana pyar hai tumse","Mausam hai aashiqana","Tujhe dekha to jana sanam","Salam-e ishq meri jaan","Yeh dil aur unki"]}, {"artist":"Hemant","titles":["Tumhe yaad hoga","Tum pukar lo","Jane wow kaise log the","Neend na mujhko aye","Beqarar karke humein",]}, {"artist":"Talat","titles":["Jalte hain jisake liye","Jayen to jayen kahan"]}, {"artist":"Manna","titles":["Zindagi kaisi hai paheli haye","Poochho na kaise maine rain bitayee","Laga chunari mein daag"]}, {"artist":"Alka","titles":["Akele hain to kya gam hai","Jane kyun log pyar karte hain","Kuchh kuchh hota hai","Pardesi pardesi jana nahi"]} ] # 1. The highest number of songs sung by any artist # def most_titles (songmix): # return len(max(songmix, key=lambda num: len(num['titles']))['titles']) # 1. Most titles # print (f"Max song count for any artist", most_titles (songmix)) # !!! # Gives CORRECT answer (10) # 2. The name of the artist with the highest number of songs # def artist_most_titles (songmix): # return max(songmix, key=lambda num: len(num['titles']))['artist'] # 2. Artist w/ most titles # print (f"The artist with max song count is", artist_most_titles (songmix)) # Gives CORRECT answer (Lata) # 3. A tuple containing the name of the artist w/ most titles and their number of songs sung # def artist_most_titles (songmix): # artist_titles = (max(songmix, key=lambda num: len(num['titles']))['artist'], len(max(songmix, key=lambda num: len(num['titles']))['titles'])) # return artist_titles # print (artist_most_titles (songmix)) # Gives CORRECT pair as tuple ('Lata', 10) # print (f"Artist, {artist_titles[0]}, has the highest song titles at, {artist_titles[1]}") # ERROR: Does NOT recognize artist_titles! # 4. The longest song title in the entire songs list # Sincen now we have to select the actual longest title, we likely need to use 'filter', 'map', and 'max' function all in one! def longest_title (songmix): temp = max(songmix, key=lambda num: len(max(num['titles']))) return list(map(lambda x: max(songmix, key=lambda num: len(max(num['titles'])))['artist'], filter(lambda title: max(len('title')), temp))) # DON'T MESS WITH # return max(songmix, key=lambda num: len(max(num['titles']))) # 4. Longest title print (longest_title (songmix)) # !!! Returns the entire record {...} for the correct artist ('Manna') (but NOT actual song title) # 5. The name of the artist with the longest song title # def artist_longest_title (songmix): # return max(songmix, key=lambda num: len(max(num['titles'])))['artist'] # 5. Artist w/ longest title # print (f"The artist with longest song title is", artist_longest_title (songmix)) # !!! Gives CORRECT answer (Manna) # 6. A tuple containing the name of the artist w/ longest song title and their longest song title # 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title # 8. A list of artists and their number of songs sung sorted (default ascending order) by the number of song titles # 9. A list of artists and their longest song titles sorted in descending order of the length of the song title # 9a. Similar list as original but longest title listed first and shortest title listed last for each artist # 9b. Bonus: same as 9a above with artists sorted in default ascending order. Thus the artists will be listed # in regular alphabetical order, but their song titles will listed in the descending order of title length From alan.gauld at yahoo.co.uk Tue May 21 08:24:28 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 21 May 2019 13:24:28 +0100 Subject: [Tutor] A Python Newbie Requesting help with Lambdas, Filters, and Maps In-Reply-To: References: Message-ID: On 20/05/2019 20:43, Rahul Alawani wrote: > ...created a long problem statement for an interesting self-exercise.? > However, I might have gotten a tad bit too ambitious and need help. That's fine and we are here to help. However.... We are all volunteers and most of us have busy lives so don't really have time to go digging deep into complex problem statements or wading through long code listings. So it is best if you can reduce your post to specific questions (only one or two per post) with trimmed down code to show the specific issues you need help with. That is far more likely to get a reply than a show everything approach. Sometimes we might ask to see everything because we need the wider picture but usually a function or two will be enough. Just be sure to post the actual code that gives the error(if there is one) otherwise it's just confusing! Oh yes, and always post the entire error message if there is one, don't summarize or send just the last line. Onward... > I would appreciate it if you could simply focus on the nature > of the data to be used and the multi-part problem. > Those who might be willing to help, it would be easier > if you copy and paste the entire code below That's a big ask. How confident are we that your code won't damage our environment? Running someone else's unknown code is a lot like running some unknown binary, just to see what it does! A bit scary and a huge act of trust. > problems anyone can solve them in their mind. > What I'm struggling the most with is the syntax and order of > certain functions when solving. It's probably best to tackle these one by one and send the problem code here for us to look at rather than ask us to tackle it all at once. > Please note that the primary objective is to solve these problems > using lambdas, filters, maps, etc. and NOT for or while loops > or list comprehensions. That's fair enough except for the last one. List comprehensions (and the more general generator expressions) explicitly replace map and filter in many cases being more concise, faster and, once you get used to them easier to read. So I would treat LC (and GE) as being part of the same idiom set as map/reduce/filter etc. You might find the functional programming topic in my tutorial a useful resource too (see link below) > So here it goes......................... > > # This function accepts a list of several dictionaries that contain song artists' names and their song titles and > # returns the following outputs in tuples (remember to return a tuple for each output!): > # 1. The highest number of songs sung by any artist > # 2. The name of the artist with the highest number of songs > # 3. A tuple containing the name of the artist and their number of songs sung > # 4. The longest song title in the entire song list > # 5. The name of the artist with the longest song title > # 6. A tuple containing the name of the artist w/ longest song title and their longest song title > # 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title > # 8. A list of artists and their number of songs sung sorted (default ascending order) by the number of song titles > # 9. A list of artists and their longest song titles sorted in descending order of the length of the song title > # 9a. Similar list as original but longest title listed first and shortest title listed last for each artist > # 9b. BONUS: same as 9a above with artists sorted in default ascending order. Thus the artists will be listed > # in regular alphabetical order, but their song titles will listed in the descending order of title length > > songmix = [ > {"artist":"Rafi","titles":["Pukarta chala hoon main","Aapke haseen rukh pe","Thaheriye hosh main aaloon","Woh jab yaad aaye","Deewana hua badal","Ehsan tera hoga mujhpar"]}, > {"artist":"Asha","titles":["Aja aja main hun pyar tera","Dil cheez kya hai","Aaiye meherban","Aao huzur tum ko","In aankhon ki masti mein","Paan khaye saiyan humaro"]}, > {"artist":"Suman","titles":["Rahete kabhi jinke dil mein","Na tum hamen jano","Jo hum pe guzarti hai","Rahe na rahe hum","Aajkal tere mere pyar ke","Tujhe dekha tujhe chaha","Parbaton ke pedon par","Tumne pukara aur"]}, > {"artist":"Kishor","titles":["Beqarar dil","Nile nile ambar pe","Muqaddar ka sikandar","Mere mehboob kayamat hogi","Mere sapno ki rani kab","Pyar diwana hota hai","O mere dil ke chain","Yeh shaam mastani","Pal pal dil ke paas"]}, > {"artist":"Lata","titles":["Lag ja gale","Tera jana dil ke armanon ka","Tera mera pyar amar","Yoon hasraton ke daag","Awaz deke hamen tum bulao","Mujhe kitana pyar hai tumse","Mausam hai aashiqana","Tujhe dekha to jana sanam","Salam-e ishq meri jaan","Yeh dil aur unki"]}, > {"artist":"Hemant","titles":["Tumhe yaad hoga","Tum pukar lo","Jane wow kaise log the","Neend na mujhko aye","Beqarar karke humein",]}, > {"artist":"Talat","titles":["Jalte hain jisake liye","Jayen to jayen kahan"]}, > {"artist":"Manna","titles":["Zindagi kaisi hai paheli haye","Poochho na kaise maine rain bitayee","Laga chunari mein daag"]}, > {"artist":"Alka","titles":["Akele hain to kya gam hai","Jane kyun log pyar karte hain","Kuchh kuchh hota hai","Pardesi pardesi jana nahi"]} > ] > > # 1. The highest number of songs sung by any artist > # def most_titles (songmix): > # return len(max(songmix, key=lambda num: len(num['titles']))['titles']) # 1. Most titles > # print (f"Max song count for any artist", most_titles (songmix)) # !!! # Gives CORRECT answer (10) > > # 2. The name of the artist with the highest number of songs > # def artist_most_titles (songmix): > # return max(songmix, key=lambda num: len(num['titles']))['artist'] # 2. Artist w/ most titles > # print (f"The artist with max song count is", artist_most_titles (songmix)) # Gives CORRECT answer (Lata) > > # 3. A tuple containing the name of the artist w/ most titles and their number of songs sung > # def artist_most_titles (songmix): > # artist_titles = (max(songmix, key=lambda num: len(num['titles']))['artist'], len(max(songmix, key=lambda num: len(num['titles']))['titles'])) > # return artist_titles Shouldn't this just use the first two functions? def artist_and_most_titles (songmix): return(artist_most_titles(songmix), most_titles(songmix)) The whole point of defining functions is that you can reuse them elsewhere... > # print (artist_most_titles (songmix)) # Gives CORRECT pair as tuple ('Lata', 10) > # print (f"Artist, {artist_titles[0]}, has the highest song titles at, {artist_titles[1]}") # ERROR: Does NOT recognize artist_titles! artist_titles is a variable you created inside the function. It is not seen outside the function. The return statement returns the value not the name. So you need to store the result if you want to reference it multiple times: result = artist_most_titles(songmix) print(....result[0]) print(...result[1]) I've run out of time... I'll leave others to add more. One final comment. Just because these functions are short does not necessarily mean they are fast. Hidden behind the scenes these functions are iterating over your data many times, you might find in real world cases that traditional loops with more complex bodies are actually faster because they can do the work on one iteration of the data. But for learning purposes that's not an issue. -- 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 rsalawani at hotmail.com Tue May 21 09:30:17 2019 From: rsalawani at hotmail.com (Rahul Alawani) Date: Tue, 21 May 2019 13:30:17 +0000 Subject: [Tutor] A Python Newbie Requesting help with Lambdas, Filters, Maps, and Built-in Functions to solve problems for a List of Songs In-Reply-To: References: Message-ID: Reposting a somewhat shortened version of my earlier ask based on a fellow user's feedback (thanks Alan G). I still think it's important to share/repeat some background. Admin/moderator, please feel free to delete my prior post. Thx. Hello Tutors! I am an ambitious Python newbie without any CS background! I am currently taking an online Python programming course. I have quite some ways to go. But based on what I have learned so far (data types, lists/dictionaries/tuples/sets, conditional statements, for and while loops, functions, lambdas, maps and filters, simple built-in functions like min/max/any/all/abs/round/len/ sorted/reversed), I am trying to solve what I think is an interesting set of problems pertaining to a list of songs (Bollywood artists and songs are listed in the example, but that's NOT important, although it might be too distracting for some people). I would appreciate it if you could simply focus on the nature of the data to be used and the multi-part problem. Those willing to help, it might be easier if you copy and paste the below code into a Python 3 text editor/interpreter (whatever works for you). I have added some comments, too, and hope they will be useful. I am not looking for a spoon-fed solution to every single unsolved problem. Just something that would push me over to the next step would suffice. I know how to solve each of these problems conceptually (anyone can solve them in their mind). What I'm struggling the most with is the syntax and order of certain functions when solving. Perhaps I am not considering some intermediate code steps that are required and/or helpful. Please note that the primary objective is to solve these problems using lambdas, filters, maps, etc. and NOT for or while loops or list comprehensions. Any help will be greatly appreciated with heartfelt thanks and email fist-bumps. Thank you for your time and consideration. Regards, Rahul A. So here it goes......................... # This function accepts a list of several dictionaries that contain song artists' names and their song titles and # returns the following outputs in tuples (remember to return a tuple for each output!): # 4. The longest song title in the entire song list # 6. A tuple containing the name of the artist w/ longest song title and their longest song title # 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title # 9. A list of artists and their longest song titles sorted in descending order of the length of the song title # 9a. Similar list as original but longest title listed first and shortest title listed last for each artist # 9b. BONUS: same as 9a above with artists sorted in default ascending order. Thus the artists will be listed # in regular alphabetical order, but their song titles will listed in the descending order of title length. songmix = [ {"artist":"Rafi","titles":["Pukarta chala hoon main","Aapke haseen rukh pe","Thaheriye hosh main aaloon","Woh jab yaad aaye","Deewana hua badal","Ehsan tera hoga mujhpar"]}, {"artist":"Asha","titles":["Aja aja main hun pyar tera","Dil cheez kya hai","Aaiye meherban","Aao huzur tum ko","In aankhon ki masti mein","Paan khaye saiyan humaro"]}, {"artist":"Suman","titles":["Rahete kabhi jinke dil mein","Na tum hamen jano","Jo hum pe guzarti hai","Rahe na rahe hum","Aajkal tere mere pyar ke","Tujhe dekha tujhe chaha","Parbaton ke pedon par","Tumne pukara aur"]}, {"artist":"Kishor","titles":["Beqarar dil","Nile nile ambar pe","Muqaddar ka sikandar","Mere mehboob kayamat hogi","Mere sapno ki rani kab","Pyar diwana hota hai","O mere dil ke chain","Yeh shaam mastani","Pal pal dil ke paas"]}, {"artist":"Lata","titles":["Lag ja gale","Tera jana dil ke armanon ka","Tera mera pyar amar","Yoon hasraton ke daag","Awaz deke hamen tum bulao","Mujhe kitana pyar hai tumse","Mausam hai aashiqana","Tujhe dekha to jana sanam","Salam-e ishq meri jaan","Yeh dil aur unki"]}, {"artist":"Hemant","titles":["Tumhe yaad hoga","Tum pukar lo","Jane wow kaise log the","Neend na mujhko aye","Beqarar karke humein",]}, {"artist":"Talat","titles":["Jalte hain jisake liye","Jayen to jayen kahan"]}, {"artist":"Manna","titles":["Zindagi kaisi hai paheli haye","Poochho na kaise maine rain bitayee","Laga chunari mein daag"]}, {"artist":"Alka","titles":["Akele hain to kya gam hai","Jane kyun log pyar karte hain","Kuchh kuchh hota hai","Pardesi pardesi jana nahi"]} ] # 4. The longest song title in the entire songs list def longest_title (songmix): temp = max(songmix, key=lambda num: len(max(num['titles']))) return list(map(lambda x: max(songmix, key=lambda num: len(max(num['titles'])))['artist'], filter(lambda title: max(len('title')), temp))) print (longest_title (songmix)) # Error Message in PowerShell: # Traceback (most recent call last): # File ".\Lambdas_Built_in_Functions_Min_Max_Sorted_Adv_short.py", line 33, in # print (longest_title (songmix)) # !!! Returns the entire record {...} for the correct artist ('Manna') (but NOT actual song title) # File ".\Lambdas_Built_in_Functions_Min_Max_Sorted_Adv_short.py", line 31, in longest_title # filter(lambda title: max(len('title')), temp))) # File ".\Lambdas_Built_in_Functions_Min_Max_Sorted_Adv_short.py", line 31, in # filter(lambda title: max(len('title')), temp))) # TypeError: 'int' object is not iterable # Remaining Problem Wishlist: # 6. A tuple containing the name of the artist w/ longest song title and their longest song title # 7. A tuple containing the name of the artist w/ longest song title and the length of their longest song title # 9. A list of artists and their longest song titles sorted in descending order of the length of the song title # 9a. Similar list as original but longest title listed first and shortest title listed last for each artist # 9b. BONUS: same as 9a above with artists sorted in default ascending order. Thus the artists will be listed # in regular alphabetical order, but their song titles will listed in the descending order of title length. From alan.gauld at yahoo.co.uk Tue May 21 20:06:41 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 22 May 2019 01:06:41 +0100 Subject: [Tutor] A Python Newbie Requesting help with Lambdas, Filters, Maps, and Built-in Functions to solve problems for a List of Songs In-Reply-To: References: Message-ID: On 21/05/2019 14:30, Rahul Alawani wrote: > # 4. The longest song title in the entire songs list > def longest_title (songmix): > temp = max(songmix, key=lambda num: len(max(num['titles']))) > return list(map(lambda x: max(songmix, key=lambda num: len(max(num['titles'])))['artist'], > filter(lambda title: max(len('title')), temp))) > print (longest_title (songmix)) There are several problems here. The first and the one that the error is reporting lies in the call to max() max() expects a sequence of values and returns the biggest. But you are passing len('title') which is a single integer value. Hence the error about int not being iterable... But there is another problem with len('title') That will give the result 5 each time since its taking the length of the string 'title'. I suspect you wanted len(title) without the quotes? Finally max() applied to strings returns the highest in lexical value - ie 'b' is higher than 'a'... So >>> max(['a','aa','aaaa','aaa','baa']) 'baa' >>> Which I don't think is what you want? However, there is also a problem with the filter call. filter expects a function that returns a boolean result. It then applies that test to each member of its sequence parameter and returns a list containing only those members for which the test was true. In your case the lambda would return a number - the result of max. All non zero numbers are treated as true by Python so you will get back the same list you started with since all the max calls will (probably) return >0 results. Also I notice you are using key functions to max but that is hardly ever necessary since max will return the largest item regardless of the order. It only needs a key if there are multiple items of the same size and you want to return a specific one of those. Sorting will ensure the right value is picked. But in your case you don't need any sorting. I know you want to practice lambdas but I think initially it would be better for you to practice with map and filter using small named functions. You can then convert them to lambdas later. So for example(untested, so probably buggy): def title_with_size(song): return (len(song), song) def song_lengths(singer): return map(title_with_size, singer['titles']) def max_title(singer): return max(song_lengths(singer)) def longest_title (songmix): titles = map(max_title, songmix) return max(titles)[1] Removing the lambdas makes everything easier to read and therefore easier to understand. Once you understand how the map/filter etc work you can decide where the named functions can be replaced by lambdas. As it stands your code is a fine demonstration of why lambdas are relatively rarely used! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From jessica.ribbit999 at gmail.com Thu May 23 03:38:48 2019 From: jessica.ribbit999 at gmail.com (Jessica Rabbit) Date: Thu, 23 May 2019 07:38:48 +0000 Subject: [Tutor] Learning something new. Message-ID: Hello, my name is Jessica and I hope I found the right e mail to help me learn of to write in code and eventually learn to hack. I'm very new to this sode of the internet and would like to slowly learn. I've barely accomplished anything in my life: so i would love to able to know how to do something that my father can. If this email is read by someone who is willing to help, *teach*, and *yell* at me (figuratively) please let me know. Kind regards, Jessica Rabbit. From alan.gauld at yahoo.co.uk Thu May 23 04:37:12 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 23 May 2019 09:37:12 +0100 Subject: [Tutor] Learning something new. In-Reply-To: References: Message-ID: On 23/05/2019 08:38, Jessica Rabbit wrote: > Hello, my name is Jessica and I hope I found the right e mail to help me > learn of to write in code and eventually learn to hack. Hi Jessica and welcome to the tutor list. > know how to do something that my father can. If this email is read by > someone who is willing to help, *teach*, and *yell* at me (figuratively) > please let me know. The way the list works is that people ask questions of the list membership, who then answer them. The more specific the question the more specific will be the answer. Include any code you have written and any error messages in the message body, do not use attachments, especially not binary ones like screenshots, since the server will drop them. It also sometimes helps to know your Python version and your OS. Have fun, we look forward to your questions and we try not to yell at anyone. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davidthebiglipperlifschitz at gmail.com Thu May 23 08:16:13 2019 From: davidthebiglipperlifschitz at gmail.com (David Lifschitz) Date: Thu, 23 May 2019 15:16:13 +0300 Subject: [Tutor] I'm having a small problem with my code Message-ID: Hi. I am currently working on a project for myself. The code posted here is supposed to ask the user for an amount of numbers, what those numbers are, and places those numbers in a list. The next job of the code is to sort the list of numbers that were inputted in an ascending fashion. There is no error from the code, however, when I run the code the first inputted number stays in its place and doesn't get sorted with the rest of the numbers. Any advice??? emptyList = [] nOFN = int(input("how many numbers do you want to sort: ")) for x in range(nOFN): number1 = int(input("input number: ")) emptyList.append(number1) firstElement = emptyList[0] n = len(emptyList) for j in range(1, n): if emptyList[j-1] > emptyList[j]: (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) print(emptyList) Sent from an email account From cs at cskk.id.au Thu May 23 19:34:33 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 24 May 2019 09:34:33 +1000 Subject: [Tutor] I'm having a small problem with my code In-Reply-To: References: Message-ID: <20190523233433.GA65767@cskk.homeip.net> On 23May2019 15:16, David Lifschitz wrote: >I am currently working on a project for myself. >The code posted here is supposed to ask the user for an amount of numbers, >what those numbers are, and places those numbers in a list. >The next job of the code is to sort the list of numbers that were inputted >in an ascending fashion. >There is no error from the code, Regardless, it clearly isn't doing what you want. In this situation it is helpful (to us, and therefore to you later) to post the output from a run and an example of the output you wanted, and and description of what is deficient in the programme output. You've got the last 2 of these 3. >however, when I run the code the first >inputted number stays in its place and doesn't get sorted with the rest of >the numbers. Comments below the code. >emptyList = [] >nOFN = int(input("how many numbers do you want to sort: ")) > >for x in range(nOFN): > number1 = int(input("input number: ")) > emptyList.append(number1) >firstElement = emptyList[0] >n = len(emptyList) >for j in range(1, n): > if emptyList[j-1] > emptyList[j]: > (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) >print(emptyList) Well, you only make one pass over the list. So if emptyList[0] <= emptyList[1] _on the first (and only) pass_, it will not move. Try comparing: 2 5 3 and: 3 2 5 and see if the behaviour differs. BTW, what you['re implementing looks like a bubble sort to me (look it up). But a since pass over the list isn't enough to sort the whole thing. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Thu May 23 19:58:57 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 May 2019 00:58:57 +0100 Subject: [Tutor] I'm having a small problem with my code In-Reply-To: References: Message-ID: On 23/05/2019 13:16, David Lifschitz wrote: > The next job of the code is to sort the list of numbers that were inputted > in an ascending fashion. You are aware that Python lists have a built in sort method? It will be more efficient than anything you can create yourself in Python. Assuming you know that and decided to write a sort routine for fun.... > There is no error from the code, however, when I run the code the first > inputted number stays in its place and doesn't get sorted with the rest of > the numbers. > Any advice??? Yes, see below: > emptyList = [] This is a terrible name since it becomes misleading the instant you put anything into it! number_list or similar would be more accurate. Or even just 'data'.... > nOFN = int(input("how many numbers do you want to sort: ")) > > for x in range(nOFN): > number1 = int(input("input number: ")) > emptyList.append(number1) You could have used a list comprehension: emptyList = [int(input("input number: ")) for n in range(nOFN)] Now onto the problem sort code > firstElement = emptyList[0] Why did you do this? You never refer to firstElement again... > n = len(emptyList) > for j in range(1, n): > if emptyList[j-1] > emptyList[j]: > (emptyList[j-1], emptyList[j]) = (emptyList[j], emptyList[j-1]) Consider the first case, j=1 If the first element is greater than the second you swap them. Otherwise you leave them in place. The loop now considers elements 2 and 3. If 2 >3 you reverse them, otherwise move on. But if element 3 is less than element 1 you never go back to move it to the top. Consider this example - [3,2,1] 1st iteration -> 2,3,1 2nd iteration -> 2,1,3 Loop ends. But you never swapped 1 and 2 after(or during) the last iteration. Your sort routine is fundamentally flawed. You need a rethink. But not too much because the built in sort will nearly always be preferred! Incidentally, creating working sort algorithms is one of the hardest things to get right in computing. It is one of those things that can seem right then one specific pattern will break it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mhysnm1964 at gmail.com Thu May 23 20:15:45 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Fri, 24 May 2019 10:15:45 +1000 Subject: [Tutor] regular expressions query Message-ID: <033101d511c5$d68900b0$839b0210$@gmail.com> All, Below I am just providing the example of what I want to achieve, not the original strings that I will be using the regular expression against. The original strings could have: "Hello world" "hello World everyone" "hello everyone" "hello world and friends" I have a string which is "hello world" which I want to identify by using regular expression how many times: * "hello" occurs on its own. * "Hello world" occurs in the list of strings regardless of the number of white spaces. Splitting the string into an array ['hello', 'world'] and then re-joining it together and using a loop to move through the strings does not provide the information I want. So I was wondering if this is possible via regular expressions matching? Modifying the original string is one option. But I was wondering if this could be done? From alan.gauld at yahoo.co.uk Fri May 24 05:41:22 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 May 2019 10:41:22 +0100 Subject: [Tutor] regular expressions query In-Reply-To: <033101d511c5$d68900b0$839b0210$@gmail.com> References: <033101d511c5$d68900b0$839b0210$@gmail.com> Message-ID: On 24/05/2019 01:15, mhysnm1964 at gmail.com wrote: > Below I am just providing the example of what I want to achieve, not the > original strings that I will be using the regular expression against. While I'm sure you understand what you want I'm not sure I do. Can you be more precise? > The original strings could have: > "Hello world" > "hello World everyone" > "hello everyone" > "hello world and friends" > I have a string which is "hello world" which I want to identify by using > regular expression how many times: > > * "hello" occurs on its own. Define "on its own" Is the answer for the strings above 4? Or is it 1 (ie once without an accompanying world)? > * "Hello world" occurs in the list of strings regardless of the number > of white spaces. I assume you mean the answer above should be 3? Now for each scenario how do we treat "helloworldeveryone"? "hello to the world" "world, hello" "hello, world" > Splitting the string into an array ['hello', 'world'] and then re-joining it > together and using a loop to move through the strings does not provide the > information I want. So I was wondering if this is possible via regular > expressions matching? It is probably possible by splitting the strings and searching, or even just using multiple standard string searches. But regex is possible too. A lot depends on the complexity of your real problem statement, rather than the hello world example you've given. I suspect the real case will be trickier and therefore more likely to need a regex. > Modifying the original string is one option. But I was wondering if this > could be done? I'm not sure what you have in mind. For searching purposes you shouldn't need to modify the original string. (Of course Python strings are immutable so technically you can never modify a string, but in practice you can achieve the same effect.) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri May 24 05:30:37 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 May 2019 10:30:37 +0100 Subject: [Tutor] I'm having a small problem with my code In-Reply-To: References: Message-ID: <10629935-f246-f2db-8c41-e5c065a733f0@yahoo.co.uk> Forwarding to the list. Always use Reply-All or Reply-List when responding to the list. Otherwise it only goes to the member who posted. Alan G. On 24/05/2019 10:20, David Lifschitz wrote: > Hi. > I'm learning the processes of python so I'm trying to figure out how > to sort it manually. > > On Fri, May 24, 2019 at 3:00 AM Alan Gauld via Tutor > wrote: > > On 23/05/2019 13:16, David Lifschitz wrote: > > > The next job of the code is to sort the list of numbers that > were inputted > > in an ascending fashion. > > You are aware that Python lists have a built in sort method? > It will be more efficient than anything you can create > yourself in Python. Assuming you know that and decided > to write a sort routine for fun.... > > > > There is no error from the code, however, when I run the code > the first > > inputted number stays in its place and doesn't get sorted with > the rest of > > the numbers. > > Any advice??? > > Yes, see below: > > > emptyList = [] > > This is a terrible name since it becomes misleading the > instant you put anything into it! > > number_list or similar would be more accurate. > Or even just 'data'.... > > > nOFN = int(input("how many numbers do you want to sort: ")) > > > > for x in range(nOFN): > >?? ?? ??number1 = int(input("input number: ")) > >?? ?? ??emptyList.append(number1) > > You could have used a list comprehension: > > emptyList = [int(input("input number: ")) for n in range(nOFN)] > > Now onto the problem sort code > > > firstElement = emptyList[0] > > Why did you do this? You never refer to firstElement again... > > > n = len(emptyList) > > for j in range(1, n): > >?? ?? ??if emptyList[j-1] > emptyList[j]: > >?? ?? ?? ?? ??(emptyList[j-1], emptyList[j]) = (emptyList[j], > emptyList[j-1]) > > Consider the first case, j=1 > > If the first element is greater than the second > you swap them. Otherwise you leave them in place. > > The loop now considers elements 2 and 3. > If 2 >3 you reverse them, otherwise move on. > But if element 3 is less than element 1 you never > go back to move it to the top. > > Consider this example - [3,2,1] > > 1st iteration?? ??-> 2,3,1 > 2nd iteration?? ??-> 2,1,3 > > Loop ends. > But you never swapped 1 and 2 after(or during) the last iteration. > > Your sort routine is fundamentally flawed. You need a rethink. > But not too much because the built in sort will nearly always be > preferred! > > Incidentally, creating working sort algorithms is one of the > hardest things to get right in computing. It is one of > those things that can seem right then one specific pattern > will break it. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist?? -?? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- > Sent from an email account -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Fri May 24 06:42:04 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 24 May 2019 11:42:04 +0100 Subject: [Tutor] Fwd: RE: regular expressions query In-Reply-To: <04b501d51218$f6e33c60$e4a9b520$@gmail.com> References: <04b501d51218$f6e33c60$e4a9b520$@gmail.com> Message-ID: <524d960f-4506-032f-fcd5-113c81fe6924@yahoo.co.uk> Forwarding to the list, plase use reply-all or reply-list when responding to list mails. Alan G. -------- Forwarded Message -------- Subject: RE: [Tutor] regular expressions query Date: Fri, 24 May 2019 20:10:48 +1000 From: mhysnm1964 at gmail.com To: 'Alan Gauld' Allan, I have gone back to the drawing board as I found to many problems with the approach I was using. As the original data has multiple spaces between words. I want to find unique phrases in the strings such as "Hello World" regardless of the number of spaces that might be in the string. I have used the following lines of code which finds the number of unique complete strings. transaction = [item for item, count in collections.Counter(narration).items() if count > 1] none-dup-narration = [item for item, count in collections.Counter(narration).items() if count < 2] So I end up with two lists one containing complete unique strings with more than one occurrence and another with only one. As there is common words in the none-dup-narration list of strings. I am trying to find a method of extracting this information. I am still reading collections as this could help. But wanted to understand if you can inject variables into the pattern of regular expression which was the intent of the original question. Each time the regular expression is executed, a different word would be in the pattern. In Python 3.7, I want to understand Unions and Maps. I have read information on this in different places and still don't understand why, how and when you would use them. Something else I have been wondering. Goal here is to grow my knowledge in programming. # end if # end for print (count) # end for input () # end for -----Original Message----- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Friday, 24 May 2019 7:41 PM To: tutor at python.org Subject: Re: [Tutor] regular expressions query On 24/05/2019 01:15, mhysnm1964 at gmail.com wrote: > Below I am just providing the example of what I want to achieve, not > the original strings that I will be using the regular expression against. While I'm sure you understand what you want I'm not sure I do. Can you be more precise? > The original strings could have: > "Hello world" > "hello World everyone" > "hello everyone" > "hello world and friends" > I have a string which is "hello world" which I want to identify by > using regular expression how many times: > > * "hello" occurs on its own. Define "on its own" Is the answer for the strings above 4? Or is it 1 (ie once without an accompanying world)? > * "Hello world" occurs in the list of strings regardless of the number > of white spaces. I assume you mean the answer above should be 3? Now for each scenario how do we treat "helloworldeveryone"? "hello to the world" "world, hello" "hello, world" > Splitting the string into an array ['hello', 'world'] and then > re-joining it together and using a loop to move through the strings > does not provide the information I want. So I was wondering if this is > possible via regular expressions matching? It is probably possible by splitting the strings and searching, or even just using multiple standard string searches. But regex is possible too. A lot depends on the complexity of your real problem statement, rather than the hello world example you've given. I suspect the real case will be trickier and therefore more likely to need a regex. > Modifying the original string is one option. But I was wondering if > this could be done? I'm not sure what you have in mind. For searching purposes you shouldn't need to modify the original string. (Of course Python strings are immutable so technically you can never modify a string, but in practice you can achieve the same effect.) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mats at wichmann.us Fri May 24 11:27:20 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 24 May 2019 09:27:20 -0600 Subject: [Tutor] regular expressions query In-Reply-To: <033101d511c5$d68900b0$839b0210$@gmail.com> References: <033101d511c5$d68900b0$839b0210$@gmail.com> Message-ID: On 5/23/19 6:15 PM, mhysnm1964 at gmail.com wrote: > All, > > > > Below I am just providing the example of what I want to achieve, not the > original strings that I will be using the regular expression against. > > > > The original strings could have: > > > > "Hello world" > > "hello World everyone" > > "hello everyone" > > "hello world and friends" > > > > I have a string which is "hello world" which I want to identify by using > regular expression how many times: > > * "hello" occurs on its own. > * "Hello world" occurs in the list of strings regardless of the number > of white spaces. I don't know if you've moved on from this problem, but here's one way one might tackle finding the hello world's in this relatively simple scenario: 1. join all the strings into a single string, on the assumption that you care about substrings that span a line break. 2. use the findall method to hit all instances 3. specify the ingore case flag to the re method 4. specify one-or-more bits of whitespace between words of the substring in your regular expression pattern. most of that is assumption since as Alan said, you didn't describe the problem precisely enough for a programmer, even if it sounds precise enough in English (e.g. hello occurs on its own - does that mean all instances of hello, or all instances of hello not followed by world?, etc.) strings = [ all your stuff ] hits = re.findall(r'hello\s+world', ' '.join(strings), flags=re.IGNORECASE) Running this on your sample data shows there are three hits (you can do len(hits) for that) === That's the kind of thing regular expressions are good for, but always keep in mind that they're not always that simple to wrestle with, which has led to the infamous quote (credited to Jamie Zawinski, although he repurposed it from an earlier quote on something different): Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. From Richard at Damon-Family.org Sat May 25 21:55:38 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Sat, 25 May 2019 21:55:38 -0400 Subject: [Tutor] Setting Command Line Arguments in IDLE Message-ID: <7065cefe-682d-722a-30eb-506a7f5a3d92@Damon-Family.org> I am working on a python script that will be provided arguments when run from the system command line. Is there any place in IDLE to provide equivalent arguments for testing while developing in IDLE? Is there any way to define the working directory for the program, or will it always be the directory the script is in (it will be typically run using the PATH, so not the same directory as the script)? If not, is there an easy way to detect that I am running in IDLE so I can fake the command line arguments when testing? -- Richard Damon From akleider at sonic.net Sun May 26 10:19:11 2019 From: akleider at sonic.net (Alex Kleider) Date: Sun, 26 May 2019 07:19:11 -0700 Subject: [Tutor] Setting Command Line Arguments in IDLE In-Reply-To: <7065cefe-682d-722a-30eb-506a7f5a3d92@Damon-Family.org> References: <7065cefe-682d-722a-30eb-506a7f5a3d92@Damon-Family.org> Message-ID: <3c5736d6e5ef3b66e3db6cb409e3063e@sonic.net> On 2019-05-25 18:55, Richard Damon wrote: > > Is there any way to define the working directory for the program, or > will it always be the directory the script is in (it will be typically > run using the PATH, so not the same directory as the script)? import os cwd = os.getcwd() os.chdir(path) Sorry, can't help you re Idle. From alan.gauld at yahoo.co.uk Sun May 26 09:26:35 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 26 May 2019 14:26:35 +0100 Subject: [Tutor] Fwd: Re: Setting Command Line Arguments in IDLE In-Reply-To: References: Message-ID: Oops, Forgot to include the list....! -------- Forwarded Message -------- Subject: Re: Setting Command Line Arguments in IDLE Date: Sun, 26 May 2019 09:03:36 +0100 From: Alan Gauld To: Richard Damon On 26/05/2019 02:55, Richard Damon wrote: > I am working on a python script that will be provided arguments when run > from the system command line. Is there any place in IDLE to provide > equivalent arguments for testing while developing in IDLE? > There used to be a dialog for that but in the latest version of IDLE I can't find it. I wonder when it disappeared and why? The best place to ask is probably on the IDLE-dev list. It can be found here: https://mail.python.org/mailman/listinfo/idle-dev > Is there any way to define the working directory for the program, os.getcwd() # read current dir os.chdir() # set current dir > If not, is there an easy way to detect that I am running in IDLE so I > can fake the command line arguments when testing? Not that I'm aware, but the idle-dev gurus may have some ideas. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Sun May 26 11:37:49 2019 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 26 May 2019 09:37:49 -0600 Subject: [Tutor] Fwd: Re: Setting Command Line Arguments in IDLE In-Reply-To: References: Message-ID: <364ab9e9-da9f-60c5-bbad-8473f9a08409@wichmann.us> > On 26/05/2019 02:55, Richard Damon wrote: >> I am working on a python script that will be provided arguments when run >> from the system command line. Is there any place in IDLE to provide >> equivalent arguments for testing while developing in IDLE? >> > > There used to be a dialog for that but in the latest version > of IDLE I can't find it. I wonder when it disappeared and why? > > The best place to ask is probably on the IDLE-dev list. > > It can be found here: > > https://mail.python.org/mailman/listinfo/idle-dev I've seen this question come up on stack overflow, can't recall I've seen a completely satisfactory answer. I would suggest, however, that doing the testing you're considering should be written as unit tests. You can invoke unit tests from inside the program by adding something like this (I'm a pytest fan, but it could be unittest as well of course): import pytest # your code here if __name__ == "__main__": pytest.main(["--capture=sys", "name-of-unittest-script.py"]) You can write your own argument array by fiddling with sys.argv; pytest also provides a mechansim for injecting arguments (I think it's called pytest_addoption). The somewhat hacky way for a script to find out that it's running inside IDLE (note: every time someone asks how to do this, a crowd of people pop up and say "you don't want to be doing that". But enabling a testing scenario might actually be a time you want to?): import sys if "idlelib" in sys.modules: print("We're running in IDLE") These aren't really an answer to what you're asking for, but maybe some tools you might use to think further about the problem? From martin at linux-ip.net Sun May 26 11:09:58 2019 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 26 May 2019 08:09:58 -0700 Subject: [Tutor] Setting Command Line Arguments in IDLE In-Reply-To: <7065cefe-682d-722a-30eb-506a7f5a3d92@Damon-Family.org> References: <7065cefe-682d-722a-30eb-506a7f5a3d92@Damon-Family.org> Message-ID: Hello Richard, In addition to the answers you have received from Alex and Alan, I'll add a bit longer of an answer mostly around how to separate the command-line invocation / argument handling part of your program from the pure-Python logic. While I, also, cannot help terribly with the direct question about your IDLE environment, I hope this explanation is generally helpful and useful. >I am working on a python script that will be provided arguments >when run from the system command line. Is there any place in IDLE >to provide equivalent arguments for testing while developing in >IDLE? I can't really help so much with the IDLE part of the question, but I do have a solution that has worked very well for me for testing programs that are usually invoked by command-line. My answer will focus on that angle rather than the IDLE angle. >Is there any way to define the working directory for the program, >or will it always be the directory the script is in (it will be >typically run using the PATH, so not the same directory as the >script)? Defining working directory. --------------------------- You cannot control the initial working directory when your program has been executed. It was executed by another process (or human, or alien from the Ophiucus Cluster) so your working directory is whatever was in the parent parent process just before forking/execution of your program. However, you are free to chdir() once your program is running: >>> os.getcwd() '/home/mabrown' >>> os.chdir('/home/mabrown/tmp/') >>> os.getcwd() '/home/mabrown/tmp' One bit of care that is warranted when you are calling os.chdir() (besides the usual error-checking, 'Can the user running this program actually chdir() to that directory?)' is to think about whether you want your program to use relative paths or absolute paths. It's worth thinking about early because if you don't, you can end up with a mess of relative and absolute paths. That way madness, bugs and possible data loss lie. What is my install directory? ----------------------------- If you need to know what directory your Python program is in, you can use os.path.dirname(os.path.abspath(__file__)). Sometimes, there are reasons to find your data (or other code?) in the same directory as (or nearby) your Python program. Think carefully about this and consider how that might work with your Python packaging solution, if you are going down that path. In general, I have tried to use command-line options to find data directories or other files needed by a Python program, as that is more intelligible to users (and to me when I come back to the code a year later) than the automatic discovery of files that just happen to be in the same directory as my Python program. Random note on $PATH and full pathnames --------------------------------------- Have you ever been trying to diagnose a problem with a script and you realized about four months two late (because that 20 minute debugging session where you think you are going insane feels like four months by the time you are done), and you realized at the end that the problem was that you thought you were running a copy of script A in directory A, but you were actually running older copy B in directory B. This meant that none of your changes were being reflected in the output and you couldn't figure it out. Well, I'll tell you, I have never, ever had that experience! Nope. Not even once. Yeah. Anyway, I have found that logging os.path.abspath(__file__) to the system log (or to STDERR or elsewhere) what the full path is to the file that is executing. This is useful to avoid the "I think I'm running my dev copy, but am actually running something else." sort of problem. >If not, is there an easy way to detect that I am running in IDLE so >I can fake the command line arguments when testing? I'll give you an example (see below) of how I write the Python to call most of my programs that are invoked directly from the command-line. The basic idea (which I think I picked up here) is to turn anything that you want to be able to test in your program into Python variables almost as soon as the program begins. That way, things like STDIN, STDOUT and the arguments become simply file objects and lists of strings. With file objects and lists of strings you can write testing functions to see how your program behaves when invoked a certain way. So, this is fairly unexciting code: def cli(me, fin, fout, argv): config, args = collectconfiguration(me, argv) rc = process_something(fin, fout, config) if rc == 0: return os.EX_OK return 1 if __name__ == '__main__': me = os.path.basename(sys.argv[0]) sys.exit(cli(me, sys.stdin, sys.stdout, sys.argv[1:])) You will see that I did not add os.environ to the argument list to the function cli(), but if my program(s) did anything with environment variables, I would add that here. You will also see that I don't mess with sys.stderr. In general, I like to stay away from redirecting that unless there's a strong need. Benefits: * you can call cli() in your testing programs with different lists of command-line invocations to see how your program behaves * you can see how -Martin Note: You will see in this example of collectconfiguration() (possibly not the best name for this function) that I make reference to sys.stdin and sys.stdout. They are also arguments to cli(). I think that is OK because I use this sort of calling structure many places and I want to keep the signature for the cli() function the same everywhere. In short, some programs may not actually have the '--input' / '--output' command-line options, in which case cli() has the variables fin and fout to hold those. def collectconfiguration(me, argv): ap = argparse.ArgumentParser(prog=me) ap.add_argument('--input', default=sys.stdin, type=argparse.FileType('r'), help='where to read input data [%(default)s]') ap.add_argument('--output', default=sys.stdout, type=argparse.FileType('w'), help='where to write output data [%(default)s]') ap.add_argument('--apiurl', default='http://localhost:8080', help='specify alternate API URL [%(default)s]') ap.add_argument('--delimiter', default='\t', type=str, help='set delimiter [%(default)s]') ap.add_argument('--loglevel', default=logging.ERROR, type=arg_isloglevel, help='set loglevel, e.g. INFO, ERROR [%(default)s]') ap.add_argument('--version', action='version', version=__version__) # -- and handle the CLI # config, args = ap.parse_known_args(argv) logger.setLevel(config.loglevel) logger.debug("Received the following configuration:") for param, value in sorted(vars(config).items()): logger.debug(" %s = %r", param, value) logger.debug(" args: %r", args) if args: ap.print_help() sys.exit("\nExtra arguments received: %r" % (args,)) return config, args def arg_isloglevel(l, defaultlevel=logging.ERROR): try: level = int(l) return level except ValueError: pass level = getattr(logging, l.upper(), None) if not level: level = defaultlevel return level -- Martin A. Brown http://linux-ip.net/ From nathan-tech at hotmail.com Sun May 26 10:59:40 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Sun, 26 May 2019 14:59:40 +0000 Subject: [Tutor] tweeting from python Message-ID: Hi there, I was wondering if anyone had some tips that might direct me to answers to this problem: I have a program which one downloads to your computer, and you install. Then, I want to add into this program a share button. This share button would send a tweet to your twitter saying, "I am watching episode x of podcast y using program z, check it out at programurl." Now, I think I am looking at tweepy for this, maybe oauth2, but all the tutorials on google are for creating one use apps. This is an app that would role out to users. I am guessing it would need to use some kind of, authorsation to allow it to tweet, but... That's about as far as I can get. Any help would be appreciated greatly. Thanks Nate From ingo at ingoogni.nl Mon May 27 02:06:18 2019 From: ingo at ingoogni.nl (ingo) Date: Mon, 27 May 2019 08:06:18 +0200 Subject: [Tutor] tweeting from python In-Reply-To: References: Message-ID: On 26-5-2019 16:59, nathan tech wrote: > Hi there, > > I was wondering if anyone had some tips that might direct me to answers > to this problem: > > I have a program which one downloads to your computer, and you install. > > Then, I want to add into this program a share button. > > This share button would send a tweet to your twitter saying, "I am > watching episode x of podcast y using program z, check it out at > programurl." > > > Now, I think I am looking at tweepy for this, maybe oauth2, but all the > tutorials on google are for creating one use apps. > Nathan, you may want to give twython a look https://github.com/ryanmcgrath/twython it has Oauth etc. There are tutorials for writing twitter bots with it that you could adapt to your purpose, for example: https://geekswipe.net/technology/computing/code-python-twitter-bot-in-ten-minutes/ Ingo From robertvstepp at gmail.com Mon May 27 13:43:56 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 27 May 2019 12:43:56 -0500 Subject: [Tutor] Query about python recipies for practices In-Reply-To: References: Message-ID: On Sun, May 19, 2019 at 12:55 PM bijaya dalei <2212bijaya at gmail.com> wrote: > > Hii, Good morning. I am a new user of python programming language. I have a > small query on "where to get python recepies for practices".plz > suggest.Thanks. It is not very clear to me what you are asking for, which may be why you have not gotten any responses so far. ActiveState used to have a Python "recipe" section. Apparently it has been moved to a GitHub repository at https://github.com/ActiveState/code I own a book, "Python Cookbook, 3rd ed." by Beazley and Jones. Perhaps that might help? Some cautionary words of warning: If this is an obscure way of getting a pre-packaged homework solution, then you are doing yourself a grave disservice! -- boB From mats at wichmann.us Mon May 27 18:44:47 2019 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 27 May 2019 16:44:47 -0600 Subject: [Tutor] Query about python recipies for practices In-Reply-To: References: Message-ID: On 5/27/19 11:43 AM, boB Stepp wrote: > On Sun, May 19, 2019 at 12:55 PM bijaya dalei <2212bijaya at gmail.com> wrote: >> >> Hii, Good morning. I am a new user of python programming language. I have a >> small query on "where to get python recepies for practices".plz >> suggest.Thanks. > > It is not very clear to me what you are asking for, which may be why > you have not gotten any responses so far. > > ActiveState used to have a Python "recipe" section. Apparently it has > been moved to a GitHub repository at > https://github.com/ActiveState/code and, if you meant problems for practicing... there are a lot of those around. Most courses have problems for you to solve, naturally, and a brief hunt around turned up some sites like these that are not courseware: http://www.practicepython.org/ https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt https://w3resource.com/python-exercises/ No personal experience of the quality of any of these.... From alan.gauld at yahoo.co.uk Mon May 27 19:33:20 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 28 May 2019 00:33:20 +0100 Subject: [Tutor] Query about python recipies for practices In-Reply-To: References: Message-ID: On 27/05/2019 23:44, Mats Wichmann wrote: > and, if you meant problems for practicing... there are a lot of those > around. Most courses have problems for you to solve, naturally, and a > brief hunt around turned up some sites like these that are not courseware: And don't forget the wonderful Python Challenge.... http://www.pythonchallenge.com/ Now up to 33 levels! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sunil.techspk at gmail.com Fri May 31 06:03:08 2019 From: sunil.techspk at gmail.com (Sunil Tech) Date: Fri, 31 May 2019 15:33:08 +0530 Subject: [Tutor] File extension against File content Message-ID: Hi Tutor, Is there any way that I can actually programmatically compare the file extension with its content? This is because we can manually save the file in one extension and later rename the file extension to some other. Thanks - Sunil. G From ingo at ingoogni.nl Fri May 31 06:38:09 2019 From: ingo at ingoogni.nl (ingo) Date: Fri, 31 May 2019 12:38:09 +0200 Subject: [Tutor] File extension against File content In-Reply-To: References: Message-ID: <2c72df54-e8dd-2b22-91c5-5e7cf22d04aa@ingoogni.nl> Many file formats have "magic bytes" that you can use for that purpose. https://en.wikipedia.org/wiki/List_of_file_signatures Ingo On 31-5-2019 12:03, Sunil Tech wrote: > Hi Tutor, > > Is there any way that I can actually programmatically compare the file > extension with its content? > > This is because we can manually save the file in one extension and later > rename the file extension to some other. > > Thanks > - Sunil. G > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From cs at cskk.id.au Fri May 31 07:02:15 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 31 May 2019 21:02:15 +1000 Subject: [Tutor] File extension against File content In-Reply-To: <2c72df54-e8dd-2b22-91c5-5e7cf22d04aa@ingoogni.nl> References: <2c72df54-e8dd-2b22-91c5-5e7cf22d04aa@ingoogni.nl> Message-ID: <20190531110215.GA88514@cskk.homeip.net> On 31May2019 12:38, ingo wrote: >Many file formats have "magic bytes" that you can use for that purpose. >https://en.wikipedia.org/wiki/List_of_file_signatures Also, UNIX systems ship with a command called "file" which inspects a file's leading data for such magic numbers to identify their content. And there's a library called libmagic [1,2] which does this work, and there's a PyPI package called python-magic [3] for using this from Python, not to mention various other PyPI modules [4]. 1: https://sourceforge.net/projects/libmagic/ 2: https://github.com/threatstack/libmagic 3: https://pypi.org/project/python-magic/ 4: https://pypi.org/search/?q=magic Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Fri May 31 09:58:12 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 31 May 2019 14:58:12 +0100 Subject: [Tutor] File extension against File content In-Reply-To: References: Message-ID: On 31/05/2019 11:03, Sunil Tech wrote: > Hi Tutor, > > Is there any way that I can actually programmatically compare the file > extension with its content? For images the standard library offers imghdr I'm not sure how reliable or accurate it is but it claims to identify a dozen or so of the most common formats. For sound there is the similar sndhdr module which tries to do the same thing for audio files. There are also modules for reading exif data from image files and ID tags from audio files. The existence of such tags can indicate that the file is of the appropriate type. Combine with try/except to test a file... > This is because we can manually save the file in one extension and later > rename the file extension to some other. And indeed store the file with no extension at all. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From nathan-tech at hotmail.com Fri May 31 15:41:15 2019 From: nathan-tech at hotmail.com (nathan tech) Date: Fri, 31 May 2019 19:41:15 +0000 Subject: [Tutor] is this doable Message-ID: Hi there, So for a future project of mine, I was wondering something. Is it possible, in python, to store a running task id in the registry? I might be using the complete wrong terms here, because I'm only used to doing this with a specific language, but here's what I want to do: python mytest.py: if(registry.taskid==valid_task): ?print 'already open' ?send to open program to make a ding noise. I understand that the second part, the "send to program" requires the program to handle being sent a "wake up!" event, which is fine, it's the "is it already running" which I am not sure on. Thanks Nate From cs at cskk.id.au Fri May 31 19:08:44 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 1 Jun 2019 09:08:44 +1000 Subject: [Tutor] is this doable In-Reply-To: References: Message-ID: <20190531230844.GA74243@cskk.homeip.net> On 31May2019 19:41, nathan tech wrote: >Is it possible, in python, to store a running task id in the registry? > >I might be using the complete wrong terms here, because I'm only used to >doing this with a specific language, but here's what I want to do: > >python mytest.py: >if(registry.taskid==valid_task): > ?print 'already open' > ?send to open program to make a ding noise. > >I understand that the second part, the "send to program" requires the >program to handle being sent a "wake up!" event, which is fine, it's the >"is it already running" which I am not sure on. Well, you need to have some kind of persistent storage of tasks and a way to check if some described task is running. I don't know what constitutes a task in your mind here, or what you consider "the registry". There is any number of ways to store persistent values. A simple one is a CSV file: keep one around with a line per task including the taskid and whatever other relevant information is needed. Reread the file when needed. To avoid difficulties with updating an arbitrary record in such a file (which is just a text file with lines of variying lengths) you could treat it like a log: just append more lines containing the new task state. On reading the file, just keep the last line per task. Less simple, but more flexible, might be some kind of database. Python ships with SQLite3 support, which lets you have a little SQL database in a local file. It is hard to be any more specific without knowing what you consider a task, and how you'd check if it was active. Cheers, Cameron Simpson From alan.gauld at yahoo.co.uk Fri May 31 19:13:57 2019 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 1 Jun 2019 00:13:57 +0100 Subject: [Tutor] is this doable In-Reply-To: References: Message-ID: On 31/05/2019 20:41, nathan tech wrote: > Is it possible, in python, to store a running task id in the registry? >From mention of the registry I assume you are running Windows? There is no registry on Unixlike systems. The answer in either case is yes since a task ID is just a number. However if the task ends the number will still be stored, so checking whether the ID refers to a live task is the trickier bit. > I might be using the complete wrong terms here, because I'm only used to > doing this with a specific language, Is the language C/C++? If so you may know the OS API calls needed and you could access those directly from Python using ctypes.... That might make your job more familiar and easier. Alternatively, there are OS shell commands that might work that you can call from subprocess. The os module has a bunch of functions that might help but many of them are Unix only or behave differently on Windows/Unix so you will need to study the documentation and probably experiment a bit. Things like waitpid() might work for example, but I haven't tried. Personally I'd use the shell command approach for Unix but no idea what I'd use for Windows. I suspect there may be a dynamic registry entry you can read using the winreg registry module. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From mats at wichmann.us Fri May 31 23:30:16 2019 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 31 May 2019 21:30:16 -0600 Subject: [Tutor] is this doable In-Reply-To: References: Message-ID: <18767fe1-ee08-372e-32c4-73eb41f8ff4c@wichmann.us> On 5/31/19 1:41 PM, nathan tech wrote: > Hi there, > > So for a future project of mine, I was wondering something. > > Is it possible, in python, to store a running task id in the registry? > > I might be using the complete wrong terms here, because I'm only used to > doing this with a specific language, but here's what I want to do: > > > python mytest.py: > > if(registry.taskid==valid_task): > > ?print 'already open' > > ?send to open program to make a ding noise. > > > I understand that the second part, the "send to program" requires the > program to handle being sent a "wake up!" event, which is fine, it's the > "is it already running" which I am not sure on. there's a lot your question leaves unasked... do you want to just code your own apps and have one be able to poke another? that's one problem, you can define the interface yourself. Or do you want to be able to poke arbitrary running tasks? that ends up more complicated. many systems have notification APIs that you can make use of, some of those are more oriented to that model (the mobile systems Android and Tizen), some a little less but still support it (Windows - it's a more prevalent thing in the UWP model). the psutil module can let you find things out about processes, might be useful in your "is the task running" query. if it's okay to start processes together and it's not arbitrary, the multiprocessing module may be of some help. From mhysnm1964 at gmail.com Fri May 31 22:53:00 2019 From: mhysnm1964 at gmail.com (mhysnm1964 at gmail.com) Date: Sat, 1 Jun 2019 12:53:00 +1000 Subject: [Tutor] Interactive editing of variables. Message-ID: <00c001d51825$20fe9730$62fbc590$@gmail.com> Hello all, Python 3.7, windows 10. I have no clue on how to achieve what I want to do and the code I have creates an hash. As shown below: for row in description: text = description_rejex(row) # applies a regular expression test function to remove text. Returns a list. if text[0] not in narration: # Checks a hash to see if the first element is in the dictionary. Result = input(text[0]) # the issue, I want to modify some of the results manually before going into the dictionary. narration[result] = text I have had a look and cannot find an example where I can interactively edit a content of a variable at the command line. I do not want to use GUI at all. As this is a simple program only requiring CLI. I have no problems showing the prompt, but cannot insert text into the edit (input) area. Any ideas?