Re: [Tutor] CSV Read

2018-06-30 Thread Peter Otten
Giulia Marcoux wrote:

> Hello,
> 
> I am a student learning python, and i am running into some difficulties. I
> am tryign to read a csv file that has different delimiters for different
> rows: Example:
> 
> Format:V1.1
> Model: R
> Step Size: 10mm
> Distance: 10cm
> Gain: 1000
> 
> X,Y
> 1,3
> 2,5
> 6,5
> 5,7
> 
> For the X,Y data, I have no problem getting it into the right cells,
> however I cannot separate the values after the colon for the first few
> rows. Its either one or the other. I am using pandas to read my CSV file.
> So far I have:
> 
> d=pd.read_csv('read.csv',delimiter=',',skiprows=0)
> 
> When I replace the delimiter for ':', it no longer works, and if i put
> sep=',|:' it does not work either. In the end I need to plot the x,y
> values and i will need some of the info in the rows above like the gain to
> manipulate the y data. If you have any tips for me I would be very
> grateful!

Rather than forcing both blocks into the same data structure I would read 
the header with the "key: value" lines into a separate object.

If the header and the X,Y pairs are separated by an empty line read until 
that and then feed the file object to pandas.read_csv() to read those pairs:

import itertools
import pandas as pd


def header(f):
"""Pass on lines from f; stop at the first whitespace-only line.
"""
return itertools.takewhile(str.strip, f)


def pairs(lines, sep=":"):
"""Split lines into (key, value) pairs.

Remove leading/trailing whitespace from value.
"""
for line in lines:
key, value = line.split(sep, 1)
yield key, value.strip()


with open("data.txt") as f:
metadata = dict(pairs(header(f)))
df = pd.read_csv(f)

print(metadata)
print(df)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] how to change the command "string" on a tkinter Button?

2018-06-30 Thread Chris Roy-Smith

Hi,

OS is Linux,

Python version is 3.6.5

I am trying to change the command of a tkinter Button in my program. 
Eventually I want to be able to do this to many buttons.


My attempt at code brings up no error messages, but the button appears 
to do nothing.


I really have no idea how to do this, but this is what I wrote.



#!/usr/bin/python3
from tkinter import *

class form(object):
    def __init__(self, x, reply, master, z,bu):
    self.x=x
    self.reply=reply
    self.master=master
    self.z=z
    self.bu=bu

    def change(x, reply, z, b):
    #f contains the alternative command (as a string)
    f=["lambda x=vars, txt=reply,fu=0 bu=b1 :form.change(x, txt, 
fu, bu)", "lambda x=vars, txt=first, fu=1 bu=b1: form.change(x, txt, fu, 
bu)"]

    for i in range(4):
    x[i].set(reply[i])
    #attempt to change command clause
    set.button(f[z])

    def draw(master):
    vars = []
    label = []
    button = StringVar
    for i in range(4):
    var = StringVar()
    vars.append(var)
    label.append("")
    label[i] = Label( master, textvariable=var, relief=RAISED 
).grid(row=i, column=0)

    vars[i].set(first[i])
    b1=Button(master, text="change", command=button).grid(row=i+1, 
column=0)



reply=["now I don't know", "Happy birthday", "Many happy returns", "Next 
it's my turn",1]
first=["What's your name?", "My name is Fred", "I have just had my 
birthday", "your's is next!",2]

master=Tk()

form.draw(master)
master.mainloop()

=

How should I do this, I had worked around the problem by destroying  the 
window and building it again, but it was pointed out that I have an 
unusual coding style doing this.


All hints appreciated!

Regards, Chris Roy-Smith

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RAD GUI Development (like Visual Studio/VBA)

2018-06-30 Thread Shall, Sydney

On 30/06/2018 00:34, Alan Gauld via Tutor wrote:

On 29/06/18 16:05, Glen wrote:


Can someone advise on a RAD, drag and drop style graphical form/dialog
creator? Akin to VBA or Visual Studio that will work with Python?


There is nothing even close to the VB GUI builder for Python.

That's because Python has to cover many GUI frameworks,
VB only has to deal with one. Which is why VB is pretty
much useless for anything other than Windows...

But there are some GUI tools available:
- Glade  - PyQt/Side?
- SpecTcl - Tkinter
- Dabo - WxPython (customised for simple DB apps)

I've tried all of them and only Dabo worked for me,
but Davo wasn't really suitable for my particular
project at the time. But YMMV

There are some others I haven't tried too
- Kivy, Blackadder(??)

And if you want to try using Jython or MacPython(?)
you can use the native GUI builders for those:
- Eclipse/Netbeans (Java)
- XDeveloper (MacOS) - I tried this once and it kind of works...


Alan,

Could you expand a bit on the use of XDeveloper.
I have need of some such utility for my project and I use a MAC OS X.

Thanks.

--

_

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-06-30 Thread Alan Gauld via Tutor
On 30/06/18 03:55, Chris Roy-Smith wrote:

> I am trying to change the command of a tkinter Button in my program. 
> Eventually I want to be able to do this to many buttons.

Since I'm not 100% sure if you mean the command or the label or both
here is a simple example that does both...


import tkinter as tk

def cmd1(): print('This is command 1')

def cmd2(): print('This is number 2')

def swapCmd():
c = b1['command']
# kluge to get the function name from Tcl id
if str(c).endswith("cmd1"):
b1['command'] = cmd2
else:
b1['command'] = cmd1

def swapText():
t = b1['text']
if t == "Cool":
b1['text'] = "Hot"
else:
b1['text'] = "Cool"

# make GUI
top = tk.Tk()
win = tk.Frame(top)
win.pack()
b1 = tk.Button(win,text="Cool", command=cmd1)
b1.pack()
b2 = tk.Button(win, text="Swap text", command=swapText)
b2.pack()
b3 = tk.Button(win, text="Swap cmd", command=swapCmd)
b3.pack()

top.mainloop()
###

> I really have no idea how to do this, but this is what I wrote.

The class is a bit odd. I think you may need to review
how you define methods.

> 
> 
> #!/usr/bin/python3
> from tkinter import *
> 
> class form(object):

In Python 3 you don't need to explicitly inherit object,
its done automatically.


>      def __init__(self, x, reply, master, z,bu):
>      self.x=x
>      self.reply=reply
>      self.master=master
>      self.z=z
>      self.bu=bu

This is fine but your later code never seems to create
an instance of form so this code never runs.

>      def change(x, reply, z, b):

Note that this has no self parameter so is not really
a method (or more specifically if you call it as a
method x will be treated as self...)

>      #f contains the alternative command (as a string)
>      f=["lambda x=vars, txt=reply,fu=0 bu=b1 :form.change(x, txt, 
> fu, bu)", "lambda x=vars, txt=first, fu=1 bu=b1: form.change(x, txt, fu, 
> bu)"]

Why store the commands as strings? Just store the actual command
objects. Functions(including lambdas) are objects in Python just
like any other object.

>      for i in range(4):
>      x[i].set(reply[i])

Where does the 4 come from?
Its not at all clear what x is supposed to be?
And you never call it.

>      #attempt to change command clause
>      set.button(f[z])

And where is z defined?
And set.button should refer to the button attribute of set.
But set has no such attribute. I don;t know what you are
trying to do here?

>      def draw(master):

Again this is not a method of form, it has no self.
Its just a function inside a class.

>      vars = []
>      label = []
>      button = StringVar

You just set button to be the StringVar class.
I suspect you wanted an instance so should have parens?

>      for i in range(4):
>      var = StringVar()
>      vars.append(var)
>      label.append("")
>      label[i] = Label( master, textvariable=var, relief=RAISED 
> ).grid(row=i, column=0)

grid always returns None so you are just adding Nones to your list.

>      vars[i].set(first[i])

Since you are still inside the loop you could just use var directly:

var.set(first[i])

>      b1=Button(master, text="change", command=button).grid(row=i+1, 
> column=0)

You have set command to be the StringVar class. When you execute the
button it will create an instance of StringVar which will then be deleted.

> reply=["now I don't know", "Happy birthday", "Many happy returns", "Next 
> it's my turn",1]
> first=["What's your name?", "My name is Fred", "I have just had my 
> birthday", "your's is next!",2]
> master=Tk()
> 
> form.draw(master)
> master.mainloop()
> 
> =
> 
> How should I do this,

Hopefully my code will give you a clue.
I'm not totally clear what the code above is supposed to be
doing since it never changes the GUI, rather the draw function
creates some buttons and the rest of your code is never used.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RAD GUI Development (like Visual Studio/VBA)

2018-06-30 Thread Alan Gauld via Tutor
On 30/06/18 11:50, Shall, Sydney wrote:

>> And if you want to try using Jython or MacPython(?)
>> you can use the native GUI builders for those:
>> - Eclipse/Netbeans (Java)
>> - XDeveloper (MacOS) - I tried this once and it kind of works...
>>
> Alan,
> 
> Could you expand a bit on the use of XDeveloper.
> I have need of some such utility for my project and I use a MAC OS X.
I've only gone through the tutorial and made some tweaks
to the resulting code but it seemed to work pretty much
as usual for Cocoa projects using Objective C.

You layout the UI then connect the widgets to your code
(which is in Python of course).

The PyObjC project is here:

https://pythonhosted.org/pyobjc/

The tutorial I used is here:

https://pythonhosted.org/pyobjc/tutorials/firstapp.html

Note that it is old, but I only have an iBook from 2001
running MacOS Lion, so age wasn't an issue for me!

-- 
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] CSV Read

2018-06-30 Thread David Rock

> On Jun 30, 2018, at 02:01, Peter Otten <__pete...@web.de> wrote:
> 
> 
> Rather than forcing both blocks into the same data structure I would read 
> the header with the "key: value" lines into a separate object.
> 
> If the header and the X,Y pairs are separated by an empty line read until 
> that and then feed the file object to pandas.read_csv() to read those pairs:

Ultimately, everything presented so far is conjecture without getting 
clarification from the original poster on what they actually need and what the 
dataset looks like.  Does every file look the same? is it always a header and 
data separated by empty lines?

Hopefully we will get some feedback.


— 
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor