strange problems with code generation

E

Eric_Dexter

I am writing out zero byte files with this (using python 2.5). I have
no idea why I am having that problem, I am also looking for an example
of readlines where I can choose a number of lines say lines 12 to 14
and then write them back to disk. any help would be apreaceted.



import sys as sys2
import os as os2

def playscoreinrange(from_file, fromline, toline):
"untested way to play a series of lines from a orc, sco combination
line 14 may not be correct"
print(from_file)
fromfile = os2.path.basename(from_file)
print(fromfile)

orcfilename = fromfile[:-4] + '.orc'
print(orcfilename)
infile2 = open(orcfilename, 'r')
outfile2 = open('temp.orc', 'w')

for line in infile2:
outfile2.write(line)


infile = open(fromfile, 'r')
outfile = open('temp.sco','w')

data = sys2.stdin.readlines()
print(data)
for linenumber in range(fromline, toline):
outfile.writeline(data[linenumber])

https://sourceforge.net/projects/dex-tracker
http://www.dexrow.com


os2.startfile('temp.bat')
 
J

John Machin

I am writing out zero byte files with this (using python 2.5). I have
no idea why I am having that problem

Which output file(s) do you mean, temp.orc or temp.sco or both?
Two possible causes outlined below.
I am also looking for an example
of readlines where I can choose a number of lines say lines 12 to 14
and then write them back to disk.

To the same file? There was a long thread on updating text files very
recently.
any help would be apreaceted.

Have you considered deploying a spelling checker?
import sys as sys2
import os as os2

Why the obfuscation?
def playscoreinrange(from_file, fromline, toline):
"untested way to play a series of lines from a orc, sco combination
line 14 may not be correct"

and which is line 14?? According to my count, it is the blank line
after "outfile2 = open('temp.orc', 'w') "!!
print(from_file)
fromfile = os2.path.basename(from_file)
print(fromfile)

orcfilename = fromfile[:-4] + '.orc'
print(orcfilename)
infile2 = open(orcfilename, 'r')
outfile2 = open('temp.orc', 'w')

for line in infile2:
outfile2.write(line)


infile = open(fromfile, 'r')

infile is not used
outfile = open('temp.sco','w')

data = sys2.stdin.readlines()
print(data)

and how many lines were there in data when you printed it?
for linenumber in range(fromline, toline):

Consider the possibility that fromline >= toline (which may be caused
by either or both not being of a numerical type).

Did you mean range(fromline, toline + 1) ?

do this:
print repr(fromline), type(fromline)
print repr(toline), type(toline)

BTW, print is a *statement*, not a function!
outfile.writeline(data[linenumber])

outfile.writeline() ???

| Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> f = open('fubar.txt', 'w')
| >>> f.writeline('bzzzzt!\n')
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| AttributeError: 'file' object has no attribute 'writeline'

So, the body of your loop wasn't executed (or you overlooked the
exception, or that wasn't the code that was executed) -- so it looks
like the range was empty (or the problem occurred further upstream).

and what does this do?? Read temp.orc and/or temp.sco before you've
closed the file(s)?

Where is the code that was used to call this playscoreinrange function?

HTH,
John
 
E

Eric_Dexter

John said:
I am writing out zero byte files with this (using python 2.5). I have
no idea why I am having that problem

Which output file(s) do you mean, temp.orc or temp.sco or both?
Two possible causes outlined below.
I am also looking for an example
of readlines where I can choose a number of lines say lines 12 to 14
and then write them back to disk.

To the same file? There was a long thread on updating text files very
recently.
any help would be apreaceted.

Have you considered deploying a spelling checker?
import sys as sys2
import os as os2

Why the obfuscation?
def playscoreinrange(from_file, fromline, toline):
"untested way to play a series of lines from a orc, sco combination
line 14 may not be correct"

and which is line 14?? According to my count, it is the blank line
after "outfile2 = open('temp.orc', 'w') "!!
print(from_file)
fromfile = os2.path.basename(from_file)
print(fromfile)

orcfilename = fromfile[:-4] + '.orc'
print(orcfilename)
infile2 = open(orcfilename, 'r')
outfile2 = open('temp.orc', 'w')

for line in infile2:
outfile2.write(line)


infile = open(fromfile, 'r')

infile is not used
outfile = open('temp.sco','w')

data = sys2.stdin.readlines()
print(data)

and how many lines were there in data when you printed it?
for linenumber in range(fromline, toline):

Consider the possibility that fromline >= toline (which may be caused
by either or both not being of a numerical type).

Did you mean range(fromline, toline + 1) ?

do this:
print repr(fromline), type(fromline)
print repr(toline), type(toline)

BTW, print is a *statement*, not a function!
outfile.writeline(data[linenumber])

outfile.writeline() ???

| Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> f = open('fubar.txt', 'w')
| >>> f.writeline('bzzzzt!\n')
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| AttributeError: 'file' object has no attribute 'writeline'

So, the body of your loop wasn't executed (or you overlooked the
exception, or that wasn't the code that was executed) -- so it looks
like the range was empty (or the problem occurred further upstream).

and what does this do?? Read temp.orc and/or temp.sco before you've
closed the file(s)?

Where is the code that was used to call this playscoreinrange function?

HTH,
John

temp bat is

csound temp.orc temp.sco

and it plays the new files I can try closing the other files first. I
am sure the readlines code is crashing it. Because I have moved it
befour the code that is reading a line at a time and it would not even
write the second zero byte file. The comment line is an old line that
I did not update when I made changes. The code that calls it was
auto-generated but boa-constructer .44 (I haven't updated it yet).


#Boa:Dialog:Dialog2

import wx
import csoundroutines2

def create(parent):
return Dialog2(parent)

[wxID_DIALOG2, wxID_DIALOG2BUTTON1, wxID_DIALOG2STATICTEXT1,
wxID_DIALOG2STATICTEXT2, wxID_DIALOG2TEXTCTRL1, wxID_DIALOG2TEXTCTRL2,

] = [wx.NewId() for _init_ctrls in range(6)]

class Dialog2(wx.Dialog):
From_File = ''
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Dialog.__init__(self, id=wxID_DIALOG2, name='', parent=prnt,
pos=wx.Point(759, 203), size=wx.Size(420, 72),
style=wx.DEFAULT_DIALOG_STYLE, title='play csound lines')
self.SetClientSize(wx.Size(412, 38))
self.Bind(wx.EVT_RIGHT_DCLICK, self.OnDialog2RightDclick)

self.textCtrl1 = wx.TextCtrl(id=wxID_DIALOG2TEXTCTRL1,
name='textCtrl1',
parent=self, pos=wx.Point(56, 8), size=wx.Size(100, 21),
style=0,
value='test')

self.textCtrl2 = wx.TextCtrl(id=wxID_DIALOG2TEXTCTRL2,
name='textCtrl2',
parent=self, pos=wx.Point(216, 8), size=wx.Size(100, 21),
style=0,
value='')

self.staticText1 = wx.StaticText(id=wxID_DIALOG2STATICTEXT1,
label='from line', name='staticText1', parent=self,
pos=wx.Point(8, 8), size=wx.Size(41, 13), style=0)
self.staticText1.SetToolTipString('From Line')
self.staticText1.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)
self.staticText1.SetForegroundColour(wx.Colour(255, 0, 0))

self.staticText2 = wx.StaticText(id=wxID_DIALOG2STATICTEXT2,
label='To Line', name='staticText2', parent=self,
pos=wx.Point(168, 8), size=wx.Size(34, 13), style=0)
self.staticText2.SetForegroundColour(wx.Colour(255, 0, 0))

self.button1 = wx.Button(id=wxID_DIALOG2BUTTON1, label='play',
name='button1', parent=self, pos=wx.Point(328, 8),
size=wx.Size(75, 23), style=0)
self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
id=wxID_DIALOG2BUTTON1)

def __init__(self, parent):
self._init_ctrls(parent)

def OnDialog2RightDclick(self, event):
fromline = int(self.textCtrl1.GetValue(self))
toline = int(self.textCtrl2.GetValue(self))
csoundroutines2.playscoreinrange(From_File, fromline,
toline)#needs filename

def OnButton1Button(self, event):
fromline = int(self.textCtrl1.GetValue())
toline = int(self.textCtrl2.GetValue())
csoundroutines2.playscoreinrange(self.From_File, fromline,
toline)#needs filename


https://sourceforge.net/projects/dex-tracker
 
J

John Machin

You say "I am sure the readlines code is crashing it." I can't imagine
how you can be sure of anything, but yes, it is a possibility that
sys.stdin.readlines() might behave strangely when called from a GUI
kit. Why from sys.stdin anyway?

You have two *known* definite problems (not closing your output files,
and no such attribute as "writeline") -- *fix* them, and try again.

Do try to test that function in isolation.
Put print statements in as I suggested. Comment out the .bat file
invocation.
Try calling the function from the interactive interpteter. Check if the
files are being created, with the right size. If it works, add back the
..bat file invocation. If that works, try it from your GUI.

IOW, try to attack your problem methodically.

AND try to answer at least some of the questions that helpers might ask
you, like what does "print(data)" produce -- they're not asked out of
idle curiosity.

HTH,
John
 
E

Eric_Dexter

I never see anything from print(data). The example I tried to adapt
using readlines may be a little old or something. I did close all the
files to prevent problems when I figure out what is wrong with what I
have.
 
E

Eric_Dexter

It is left over from the example I stold it from, I remove it and see
if that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top