Re: [Tutor] Request for help with code
When I post code with questions, I just copy and paste from Python IDLE 3.6. Colors are removed, but indentation is preserved. On Tue, Nov 6, 2018 at 6:59 PM Mats Wichmann wrote: > On 11/6/18 4:36 PM, Joseph Gulizia wrote: > > Funny using a text editorand showed indented in my browser. > Won't > > bother the list again. > > We don't want you to "not bother" us, just hoping to get things in a > state where we can actually help... > > here's what we end up seeing: > > https://www.mail-archive.com/tutor@python.org/msg79222.html > https://www.mail-archive.com/tutor@python.org/msg79225.html > > since unlike most other languages, indentation is a crucial part of the > syntax, we tend to grumble. > > there must be some solution... guys, do you know of any alternate way to > send a message to the list if a mail client isn't cooperating? or do we > have instructions for beating gmail into submission? Obviously gmail is > a key part of the modern infrastructure despite its (ahem) misfeatures. > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
I may be missing something but it looks like the embedded double quotes may be a problem in this: cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ... if you use single quotes as in: cut -d"=" becomes cut -d'=' or escape the double quotes with \" and so on ... The above seems to be seen as: cmd=ALPHA=BETA % ... where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d" and BETA=" -f3" Other issues may also be there. -Original Message- From: Tutor On Behalf Of Alan Gauld via Tutor Sent: Tuesday, November 6, 2018 7:37 PM To: tutor@python.org Cc: python-...@python.org Subject: Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python On 06/11/2018 18:07, srinivasan wrote: > bash command in python using subprocess module, I ma seeing the below > *cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % > (fs)* In general you should try to do as little as possible using bash and subprocess. Especially try to avoid long pipelines since you are starting a new OS process for every element in the pipeline. That means, in your case, you are running 4 processes to get your result - Python, blkid, grep and cut Python is designed to do much of what the shell command can do almost as easily and much more efficiently (no new processes being started). In this case just execute the blkid bit in bash because its too difficult to replicate simply in Python. Then use Python to search for the TYPE lines and slice them to size. That will, in turn, simplify your command string and remove the issue of multiple quotes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] best way to dynamically set class variables?
Hi, Background: In my code I use sqlalchemy to read SQL server data. I want to write that data to HDF5 using pytables (see 'declaring a column descriptor': https://www.pytables.org/usersguide/tutorials.html). My question is not about pytables or sqlalchemy per se, but I thought it would be informative to mention this. What is the best way to dynamically set class variables? I am looking for a generalization of something like this: class Parent: pass class Child(Parent): col1 = 'str' col2 = 'int' Several (im)possible solutions: # --- class Parent: pass class_vars = dict(col1='str', col2='int') # approach 1 Child = type('Child', (Parent,), class_vars) # approach 2 class Child(Parent): pass Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' object has no attribute 'update' # approach 3 class Child(Parent): pass for k, v in class_vars.items(): setattr(Child, k, v) I initially chose approach #1, but I find this way of defining a class quite cryptic (but then, it's part of the language definition!). What's the best way to do this? I am using Python 3.5 (Windows). Thanks in advance! Best wishes, Albert-Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On 07/11/2018 14:48, Albert-Jan Roskam wrote: > What is the best way to dynamically set class variables? Remember the golden rule of OOP is that objects(and classes) should do it to themselves. Ideally the class variables should be there to support some kind of class behaviour and that behaviour should set the variables. (Reading is arguably different, it's usually OK for external objects to "grab a value" from a class. But if you are modifying it from outside then who is doing what and should that "what" not be done by the class (maybe in a class method)? Having said that, if you can find a valid scenario where objects/functions outside the class need to modify the internals of the class directly then directly is how they should do it. class C: classvar = 'foo' x = C.classvar # read directly C.classvar = 'bar' # assign directly I am looking for a generalization of something like this: > > class Parent: pass > class Child(Parent): > col1 = 'str' > col2 = 'int' What's not general about that? I think I'm maybe missing the point of your question? > # --- > class Parent: pass > class_vars = dict(col1='str', col2='int') > > # approach 1 > Child = type('Child', (Parent,), class_vars) > > # approach 2 > class Child(Parent): pass > Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' object > has no attribute 'update' > > # approach 3 > class Child(Parent): pass > for k, v in class_vars.items(): > setattr(Child, k, v) That all seems incredibly complicated and I'm not sure what it would buy you over direct assignment? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
After changing the line to *"cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs"*, Now I dont see the error "SyntaxError: can't assign to literal" This is not returning exactly "*vfat*" instead of this, it is returning as "* /dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* " Could you please help me as it seems to be like grep and cut commands are not working in the above line ie., on *cmd = "blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3" % fs*? On Wed, Nov 7, 2018 at 9:41 AM Avi Gross wrote: > I may be missing something but it looks like the embedded double quotes > may be a problem in this: > > cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % ... > > if you use single quotes as in: > > cut -d"=" > > becomes > > cut -d'=' > > or escape the double quotes with \" and so on ... > > The above seems to be seen as: > > cmd=ALPHA=BETA % ... > > where ALPHA="blkid -o export %s | grep 'TYPE' | cut -d" > and BETA=" -f3" > > Other issues may also be there. > -Original Message- > From: Tutor On Behalf Of > Alan Gauld via Tutor > Sent: Tuesday, November 6, 2018 7:37 PM > To: tutor@python.org > Cc: python-...@python.org > Subject: Re: [Tutor] SyntaxError: can't assign to literal while using > ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using > subprocess module in Python > > On 06/11/2018 18:07, srinivasan wrote: > > > bash command in python using subprocess module, I ma seeing the below > > *cmd = "blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % > > (fs)* > > In general you should try to do as little as possible using bash and > subprocess. Especially try to avoid long pipelines since you are starting a > new OS process for every element in the pipeline. That means, in your case, > you are running 4 processes to get your result - Python, blkid, grep and cut > > Python is designed to do much of what the shell command can do almost as > easily and much more efficiently (no new processes being started). > > In this case just execute the blkid bit in bash because its too difficult > to replicate simply in Python. Then use Python to search for the TYPE lines > and slice them to size. > > That will, in turn, simplify your command string and remove the issue of > multiple quotes. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
Even after changing as per the below "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" or: 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' or: "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" Still my output is: */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* My expected output should be only: *vfat* Could you guys please do the needful? On Wed, Nov 7, 2018 at 11:10 AM Brian J. Oney wrote: > On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote: > > blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3 > > You don't need to escape the single quotes. > Try either: > > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > HTH > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
On Wed, 2018-11-07 at 10:22 +0100, srinivasan wrote: > blkid -o export %s | grep \'TYPE\' | cut -d\"=\" -f3 You don't need to escape the single quotes. Try either: "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" or: 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' or: "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" HTH ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Displaying Status on the Command Line
Hello, I'm sure that this is simple and my searches have just not used the correct words. What I would like to do is display, on a single line, in the terminal / command line a progress percentage, or, simply a sequence of - / - \, etc.. or even, accumulating period characters. What would the escape codes be, or is there a better way to handle this? Note that I need this to be platform agnostic. Thank you in advance for your insight. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for help with code
Gmail: Click Compose (Upper Left corner) Window opens. In lower right corner within the compose window, click on the 3 stacked dots. Select text only. On Wed, Nov 7, 2018 at 1:39 AM Adam Eyring wrote: > > When I post code with questions, I just copy and paste from Python IDLE > 3.6. Colors are removed, but indentation is preserved. > > On Tue, Nov 6, 2018 at 6:59 PM Mats Wichmann wrote: > > > On 11/6/18 4:36 PM, Joseph Gulizia wrote: > > > Funny using a text editorand showed indented in my browser. > > Won't > > > bother the list again. > > > > We don't want you to "not bother" us, just hoping to get things in a > > state where we can actually help... > > > > here's what we end up seeing: > > > > https://www.mail-archive.com/tutor@python.org/msg79222.html > > https://www.mail-archive.com/tutor@python.org/msg79225.html > > > > since unlike most other languages, indentation is a crucial part of the > > syntax, we tend to grumble. > > > > there must be some solution... guys, do you know of any alternate way to > > send a message to the list if a mail client isn't cooperating? or do we > > have instructions for beating gmail into submission? Obviously gmail is > > a key part of the modern infrastructure despite its (ahem) misfeatures. > > > > > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex for Filesystem path (Asad)
Hi All, I tired seems its not working as required : from os.path import dirname, join testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") dirpath = join(testdir, '123456/789') print dirpath /a/b/c/d/test/test_2814__2018_10_05_12_12_45\123456/789 Instead i need the script to go to the location : /a/b/c/d/test/123456/789 Please advice . Thanks, > -- Forwarded message -- > From: Cameron Simpson > To: tutor@python.org > Cc: > Bcc: > Date: Wed, 7 Nov 2018 06:47:33 +1100 > Subject: Re: [Tutor] Regex for Filesystem path > On 06Nov2018 18:10, Alan Gauld wrote: > >On 06/11/2018 13:13, Asad wrote: > > > >> Can you provide some advice and code for the following problem : > > > >The first thing is to go read the documentation for the os.path module. > >It is designed for reliable path manipulation. > > > >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > >> > >> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", > 'r' ) > >> st1 = f3.readlines () > > > >You hardly ever need readlines() any more, just iterate > >over the file, its much easier. > > > >> for j in range ( len ( st1 ) ): > > > >for line in f3: > > Not to mention cheaper in memory usage. > > [...snip...] > >>a = mo.group() ## 123456/789 > >>=== > >>How to do I traverse to the required directory which is > >> /a/b/c/d/test/123456/789 ? > > > >You can use relative paths in os.chdir. > >So a payth of '..' will be one level up from the current > >directory. Of course you need to chdir to that directory first > >but os.path will tell you the dir you need. > > It is better to just construct the required path. Chdir there requires a > chdir back, and chdir affects all the relative paths your programme may > be using. > > I'd use os.path.dirname to get '/a/b/c/d/test' and then just append to > it with os.path.join to contruct each directory path. > > [...] > >But I'm guessing that's too obvious so the path may vary? > >>1) First I need to extract /a/b/c/d/test/ from > >> /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log ? > > Use os.path.dirname: > ># up the top >from os.path import dirname, join > ># later >testdir = dirname(logfile_path) > > >get the dir then chdir to .. from there. > > > >>2) Then add 123456/789 and create directory location as > >> /a/b/c/d/test/123456/789 > > > >Simple string manipulation or use the os.path functions. > > Eg dirpath = join(testdir, '123456/789') > > >>3) cd /a/b/c/d/test/123456/789 > > > >os.chdir() > > I still recommend avoiding this. Just construct the full path to what > you need. > > >>4) look for the latest file in the directory > /a/b/c/d/test/123456/789 > > > >Slightly more complex, you need the creation timestamp. > >You can find that with os.path.getctime() (or several > >other options, eg os.stat) > > Do not use ctime, it is _not_ "creation" time. It is "last change to > inode" time. It _starts_ as creation time, but a chmod or even a > link/unlink can change it: anything that changes the metadata. > > Generally people want mtime (last nmodified time), which is the last > time the file data got changed. It is more meaningful. > > Cheers, > Cameron Simpson > > > > > -- Asad Hasan +91 9582111698 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Is there a better way
I have an sqlite database as a source {Asset and Test data from a Megger PAT Tester}, which contains a number of tables, and I end up with an OpenOffice spreadsheet, with numerous sheets The data is proceesed in three parts, each using a separate Class. I extract the useful tables, and store them as '.csv' versions. {Class#1} I then need to correlate data from various tables, and sort into locations, again storing them as '.csv' versions. {Class#2} Finally, I write all of the locations as separate sheets in an OpenOffice spreadsheet. {Class#3} At this final stage I am converting table index numbers to the relevant string. For example, from the '.csv' file the : TestGroupIndex [1] => Test Group ['SC2'] '.csv' file for a location AssetIndex AssetId Description TestGroupIndex RetestFrequency 106 43 Router 1 24 164 25 500 Drill 8 24 167 26 110v Ext Lead 11 24 173 37 Router DW625E 1 24 180 47 Vacuum 1 24 181 48 110v Ext11 24 sheet from '.ods' file Asset Asset TestTestNext Index Id Description Group FreqTest 106 43 Router ['SC2'] 24 24/11/19 164 25 500 Drill ['TO2'] 24 17/12/19 167 26 110v Ext Lead ['EL3'] 24 24/11/19 173 37 Router DW625E ['SC2'] 24 12/10/20 180 47 Vacuum ['SC2'] 24 27/12/19 181 48 110v Ext['EL3'] 24 17/12/19 Test Group '.csv' TestGroupIndex Class Description TestGroupId VoltageOutput 1 2 Class 2 Double insulatedSC2 230 2 1 Standard class 1 earthedSC1 230 The test group table has 30 entries but the index range is 1:31, i.e. one test group has been deleted at some point in the past, and the numbering is out of my control. To access the test group I read the testgroup.csv file to a numpy array, inside {Class#3}, with two blanks, one for [0], and the other deleted group. class OdsProcesses: def __init__(self, path, filename, noGroups): . . self.group = np.zeros(((self.noGroups + 1), 1), dtype = np.dtype('U10')) # 10-character string def extract_TestGroup(self): # Open Test Group file for reading groupFilename = self.path groupFilename += self.filename groupFilename += self.TG_suffix testGroup = csv.DictReader(open(groupFilename, 'r')) # Loop through all Data for row in testGroup: index = int(row["TestGroupIndex"]) self.group[index] = row["TestGroupId"] def get_TestGroup(self, index): groupStr = self.group[index] def writeODSData(self, sheet, roomNum): . . groupID = self.get_TestGroup(group) This works but feels 'clumpy', and I have a feeling that there is a better way, but I have run out of ideas. I am running Python 3.7.0, on a Windows 10 PC Thank you for any assistance Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 07/11/2018 16:22, Chip Wachob wrote: > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? It depends on your Python version. In Python v2 you simply put a comma after your output character to turn off the auto newline while someProcess():# should probably be in a separate thread... time.sleep(1) # 1 second pause print '.', # comma suppresses newline If you want to suppress the spaces tyoo things get a tad more complex and you are probably best writing direct to sys.stdout In Python 3 there are parameters to print() while someProcess(): time.sleep(1) print('.', end='', sep='') # no newline and no spaces You shouldn't need any special escape codes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex for Filesystem path (Asad)
On 07/11/2018 15:56, Asad wrote: > Hi All, > > I tired seems its not working as required : > > from os.path import dirname, join > > testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") Note that this will set testdir to /a/b/c/d/test/test_2814__2018_10_05_12_12_45 But you want the dir above that. The easiest way (if this is always the case) is to just use dirname() again: testdir = dirname(testdir) Which should result in: /a/b/c/d/test You could of course do it in one line as myPath = "/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log" testdir = dirname( dirname(myPath) ) Which is nicer than my original suggestion of using chdir and relative paths :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Wed, Nov 7, 2018 at 1:17 PM Alan Gauld via Tutor wrote: > In Python 3 there are parameters to print() > > while someProcess(): >time.sleep(1) >print('.', end='', sep='') # no newline and no spaces You'll also want `flush=True` here to avoid having your dots buffered until end-of-line. -- Zach ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
On 07/11/2018 11:31, srinivasan wrote: > Even after changing as per the below > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > Still my output is: > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* > > My expected output should be only: > *vfat* > > Could you guys please do the needful? I would strongly suggest that you stop trying to use grep and cut from within the subprocess. Instead just process the lines with Python. Its clearer and more efficient. In this case it seems to be almost trivial: output = subprocess # run blkid shell command and read output for line in output: if line.endswith("vfat"): # do whatever you want to do with line. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
Not sure what you're after, but what's wrong with just setting them? Parent.avar = "a class var" On November 7, 2018 7:48:40 AM MST, Albert-Jan Roskam wrote: >Hi, > >Background: In my code I use sqlalchemy to read SQL server data. I want >to write that data to HDF5 using pytables (see 'declaring a column >descriptor': https://www.pytables.org/usersguide/tutorials.html). My >question is not about pytables or sqlalchemy per se, but I thought it >would be informative to mention this. > >What is the best way to dynamically set class variables? I am looking >for a generalization of something like this: > >class Parent: pass >class Child(Parent): >col1 = 'str' >col2 = 'int' > >Several (im)possible solutions: > ># --- >class Parent: pass >class_vars = dict(col1='str', col2='int') > ># approach 1 >Child = type('Child', (Parent,), class_vars) > ># approach 2 >class Child(Parent): pass >Child.__dict__.update( class_vars ) # AttributeError: 'mappingproxy' >object has no attribute 'update' > ># approach 3 >class Child(Parent): pass >for k, v in class_vars.items(): >setattr(Child, k, v) > >I initially chose approach #1, but I find this way of defining a class >quite cryptic (but then, it's part of the language definition!). What's >the best way to do this? I am using Python 3.5 (Windows). Thanks in >advance! > >Best wishes, >Albert-Jan >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Sent from my Android device with K-9 Mail. Please excuse my brevity. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On 7 Nov 2018 20:36, Mats Wichmann wrote: Not sure what you're after, but what's wrong with just setting them? Parent.avar = "a class var" Hi Alan, Mats, I should have mentioned that the code is part of a function sql_to_hdf5. So it should be able to convert an *arbitrary* Sql server table into hdf5 format*). But even if it was just one specific table: it's quite cumbersome to write the column names (class variable names) and the target hdf5 data types (class variable values) for, say, a hundred columns. This is an example from the pytables website: >>> from tables import * >>> class Particle(IsDescription): ... name = StringCol(16) # 16-character String ... idnumber = Int64Col() # Signed 64-bit integer ... ADCcount = UInt16Col() # Unsigned short integer ... TDCcount = UInt8Col() # unsigned byte ... grid_i= Int32Col() # 32-bit integer ... grid_j= Int32Col() # 32-bit integer ... pressure = Float32Col()# float (single-precision) ... energy= Float64Col()# double (double-precision) Imagine having to write this for 100 columns, brrr. So the code grabs the sql column names and the datatypes (easy with sqlalchemy) and translates that into the hdf5 equivalent. And pytables uses a class, with class variables, to define this. A classmethod might be nice. I've never used this before, but I'll try this (I did try __new__ this afternoon, but it looked even more complicated). And it just occurred to me that exec() might even be a way (namedtuple does it, too). Or should I now grab my coat for even mentioning exec? :-))) Best wishes, Albert-Jan *) I tried using pandas to_hdf for this, but this does not work wel with mixed dtypes, NULL values, and chunkwise reading. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 07Nov2018 11:22, Chip Wachob wrote: I'm sure that this is simple and my searches have just not used the correct words. What I would like to do is display, on a single line, in the terminal / command line a progress percentage, or, simply a sequence of - / - \, etc.. or even, accumulating period characters. What would the escape codes be, or is there a better way to handle this? Note that I need this to be platform agnostic. I'e got a module 'cs.upd' on PyPI which does this. "pip install cs.upd" to obtain it. Typical usage: import sys import time from cs.upd import Upd upd = Upd(sys.stdout) for percentage in range(1,100): upd.out("Progress: %d%%", percentage) if percentage % 10 == 0: upd.nl("completed %d%%", percentage) time.sleep(0.1) upd.out("Complete!") time.sleep(1.0) That example is obviously contrived, and the sleeps are so you can see it all happen. But you can slot this into simple terminal based programmes to present dynamic progress. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On Wed, Nov 07, 2018 at 02:48:40PM +, Albert-Jan Roskam wrote: > What is the best way to dynamically set class variables? I am looking > for a generalization of something like this: > > class Parent: pass > class Child(Parent): > col1 = 'str' > col2 = 'int' The obvious solution is to do exactly that: just set the class attribute in the subclass. If the value is dynamically generated, so be it: class Child(Parent): col1 = calculate_some_value() col2 = col1 + calculate_something_else() If the names of the class attributes themselves have to be generated, that's what locals() is for: class Child(Parent): for i in range(10): name = "column" + str(i) locals()[name] = i del i will give you class attributes column0 = 0, column1 = 1, etc. Of course you can generate the names any way you like, e.g. read them from a text file. A cleaner solution might be to move the code into a function and pass the class namespace to it: def make_attrs(ns): for i in range(10): name = "column" + str(i) ns[name] = i class Child(Parent): make_attrs(locals()) assert column0 == 0 assert Child.column1 == 1 Does this help? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On Wed, 7 Nov 2018 at 18:35, Alan Gauld via Tutor wrote: > > On 07/11/2018 14:48, Albert-Jan Roskam wrote: > > > What is the best way to dynamically set class variables? > > I think I'm maybe missing the point of your question? I think you are as well :) IIUC then the question is: how can I programatically/dynamically create a class that has some attributes derived from data that is known at runtime? Am I understanding this correctly Albert? > > # --- > > class Parent: pass > > class_vars = dict(col1='str', col2='int') > > > > # approach 1 > > Child = type('Child', (Parent,), class_vars) This seems fine to me. It may seem cryptic but that's only because it's unusual to do this. You are creating a "type" and that is the constructor for type objects. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On Wed, Nov 07, 2018 at 11:22:22AM -0500, Chip Wachob wrote: > Hello, > > I'm sure that this is simple and my searches have just not used the > correct words. > > What I would like to do is display, on a single line, in the terminal > / command line a progress percentage, or, simply a sequence of - / - > \, etc.. or even, accumulating period characters. > > What would the escape codes be, or is there a better way to handle this? Here's a quick and easy(?) way to do this which works on typical Linux systems. I've written it for Python 3, let us know if you need help getting it to work on Python 2. import time def spinner_demo(): for i in range(1, 101): c = r'-\|/-\|/'[i % 8] print("Spinner: %c %d%%\r" % (c, i), flush=True, end='') time.sleep(0.15) print('\n') To display a throbber instead, change the first line inside the loop to c = '.oOo'[i % 4] Here's a progress bar: def progress_demo(): N = 250 template = "Working hard... %- 40s %3.0f%% complete\r" for i in range(N): perc = i*100/N bar = '#'*(i*40//N + 1) print(template % (bar, perc), flush=True, end='') time.sleep(0.1) print('\n') > Note that I need this to be platform agnostic. That's hard, even on a single platform like Linux. Most xterminal windows use either the xterm or vt1000 set of commands, which are broadly similar, but that's not guaranteed. If somebody happens to be running a different terminal type, they'll see something weird. And I have no idea what happens on Windows. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On 07/11/2018 20:07, Albert-Jan Roskam wrote: > I should have mentioned that the code ... should be able to convert > an *arbitrary* Sql server table into hdf5 format*). Umm, yes that would have helped! from tables import * class Particle(IsDescription): > ... name = StringCol(16) # 16-character String > ... idnumber = Int64Col() # Signed 64-bit integer > ... ADCcount = UInt16Col() # Unsigned short integer > ... TDCcount = UInt8Col() # unsigned byte > ... grid_i= Int32Col() # 32-bit integer > ... grid_j= Int32Col() # 32-bit integer > ... pressure = Float32Col()# float (single-precision) > ... energy= Float64Col()# double (double-precision) > > > Imagine having to write this for 100 columns, brrr. No problem, I've done that dozens of time for production C++ code, it's business as usual in commercial programming. Of course I'd get the data from a meta SQL query and feed it into an emacs macro to generate the code but I'd still have to churn out the class definitions. (Or I might even write a Python program to generate the C++ code for me) But it's eminently doable and keeps the rest of the code simple. Of course Python allows for dynamic creation of classes so you can be more subtle than that. :-) > So the code grabs the sql column names and the datatypes > and translates that into the hdf5 equivalent. Should be relatively easy to write a function to do that. I'm not clear if the class definition already exists or if you are trying to create the class (Based on the table name maybe?)as well as its attributes? Also column names are usually instance attributes not class attributes. Each instance of the class being a row in the table... > And pytables uses a class, with class variables, to define this. No idea what pytables is or what it does so can't comment. > A classmethod might be nice. I've never used this before, If there is some kind of abstract superclass (TableModel or somesuch?) then a class method there could spin off the new subclasses. > exec() might even be a way It's a way but it's a bad way! :-) Another option would be to explore metaclasses and modify the class creation mechanism -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
On Wed, Nov 07, 2018 at 10:22:00AM +0100, srinivasan wrote: > Could you please help me as it seems to be like grep and cut commands are > not working in the above line ie., on *cmd = "blkid -o export %s | grep > \'TYPE\' | cut -d\"=\" -f3" % fs*? Then get them working first. What happens when you execute that line directly on the command line? But I have to wonder, are you being paid by the hour? Because it seems like a waste of time and effort to be using a pipeline of commands for this. You seem to be deliberately turning a small, easy job into a huge, difficult job. The *easiest* way to do this is to do the string processing in Python, not using the hard-to-use shell commands. Python has its own grep module: import grep but since all you are doing is looking for a plain string with no wildcards, you don't even need that. Collect the output of blkid into a string, then do your processing in Python: # Untested for line in output.splitlines(): # each line looks something like this # "/dev/md0: UUID="3cb91a07-14c9-4ae4-81e1-6eb662eabeee" TYPE="ext3" p = line.find('TYPE="') if p == -1: continue # skip the line result = line[p:] if line.endswith('"'): result = result[:-1] print("Found file type", result) Much easier to read and write and debug than a pipeline of Unix command line tools, and likely to be faster too. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SyntaxError: can't assign to literal while using ""blkid -o export %s | grep 'TYPE' | cut -d"=" -f3" % (fs)" using subprocess module in Python
On 11/7/18 4:31 AM, srinivasan wrote: > Even after changing as per the below > "blkid -o export %s | grep 'TYPE' | cut -d'=' -f3" > or: > 'blkid -o export %s | grep "TYPE" | cut -d"=" -f3' > or: > "blkid -o export %s | grep \"TYPE\" | cut -d\"=\" -f3" > > Still my output is: > */dev/mmcblk1p1: LABEL="efi" UUID="1084-AA42" TYPE="vfat"* > > My expected output should be only: > *vfat* then you don't want -f3... vfat is the 4th field if you split (cut) that string on equal signs. usually if a shell pipeline doesn't work from Python, it means you're not invoking the shell. I notice in your original code you default the "shell=" value to false. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Displaying Status on the Command Line
On 08Nov2018 10:00, Steven D'Aprano wrote: Note that I need this to be platform agnostic. That's hard, even on a single platform like Linux. Most, nearly all, terminal honour carriage return and backspace. That is technically enough. Even terminals with a destructive backspace (rare - it is normally just a cursor motion) can get by (backspace, overwrite the new text). Most xterminal windows use either the xterm or vt1000 set of commands, which are broadly similar, but that's not guaranteed. If somebody happens to be running a different terminal type, they'll see something weird. And I have no idea what happens on Windows. I'd sort of expect Windows terminals, even cmd.exe, to accept the ANSI sequences, which is what vt100 and xterms use. But that is expectation, not knowledge. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On 07/11/2018 23:06, Alan Gauld via Tutor wrote: > Another option would be to explore metaclasses > and modify the class creation mechanism Which, of course, is what you were doing with the type(...) call in your post... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
On 07/11/2018 23:06, Alan Gauld via Tutor wrote: >> Imagine having to write this for 100 columns, brrr. > > No problem, I've done that dozens of time for production C++ code, > it's business as usual in commercial programming. > > Of course I'd get the data from a meta SQL query and feed it into an > emacs macro to generate the code but I'd still have to churn out the > class definitions. (Or I might even write a Python program to generate > the C++ code for me) But it's eminently doable and keeps the rest of the > code simple. It just occurred to me that this bit of C++ nostalgia might not be as irrelevant as I first thought. A simple approach you could use would be to get Python to generate a new python file(module) containing the required class definition (simple string processing) and then dynamically import the new file. Very similar to my emacs macro approach except with dynamic loading. You'd probably need a batch/cron job to version control all these new files too... And I've no idea how efficient that would be if there were a lot of tables to be processed - but in that scenario I suspect the whole class per table approach is flawed! Just a thought... or 3... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to dynamically set class variables?
Alan Gauld via Tutor wrote: >>exec() might even be a way > > It's a way but it's a bad way! :-) And then > A simple approach you could use would be to get Python to > generate a new python file(module) containing the required class > definition (simple string processing) and then dynamically > import the new file. That's basically exec(), with better tracebacks and a higher chance to run outdated code ;) By the way I don't think exec() is bad as long as you control its input and as long as this input is fairly simple. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor