Reading a hard encoded file

205 views
Skip to first unread message

seanm...@gmail.com

unread,
Feb 22, 2021, 10:15:22 PM2/22/21
to QATrack+
Hi All,

I am trying to do something very simple. Read in a text file and then extract and display some simple data from it. I have created a text file and uploaded into a test. However when I run the test QATrack cannot see the file location, I have tried many and varied ways of specifying the file location....I must be doing something wrong. Can anyone please point out what I am doing wrong.

import os

os.chdir(path)
f = open("sample_2021-02-23_d961fc.txt", "r")
readanalyse=(f.read(5))

I am getting FileNotFoundError: [Erno 2] No such file or directory.

Appreciate your advice.
Sean

Randle Taylor

unread,
Feb 23, 2021, 9:56:13 AM2/23/21
to seanm...@gmail.com, QATrack+
Hi Sean,

QATrack+ puts both FILE (text mode) and BIN_FILE (binary mode) file objects in the calculation context.  So rather than using the file path, you should use one of those objects like:

# read all lines of data from the uploaded file into the data variable
data = [line.strip() for line in FILE.readlines()]
# now you can do whatever you like with the lines in data

You may also use the built in CSV module or pandas.read_csv:

import csv
dat = csv.reader(FILE)

import pandas
dataframe = pandas.read_csv(FILE)

If that is not what you were after, let me know.

RT


--
You received this message because you are subscribed to the Google Groups "QATrack+" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qatrack+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qatrack/838705a1-8eec-4e8d-9298-7a6b30bcd28en%40googlegroups.com.

seanm...@gmail.com

unread,
May 20, 2021, 12:56:23 AM5/20/21
to QATrack+
Hi Randle,

That works nicely, apologies for the late reply, I got sidetracked implementing other tests ;P

Given that we can upload files to test lists and tests, is there a way to access these directly for reading? I can read the file easily when I use the upload feature in a test, are you saying that QATrack+ cannot directly access the files by specifying a directory.

What I would like to do is:
1. Attach a file eg. test.txt to the test list
2. Then read the file and act on it

If the values in the test.txt file ever changes I will just copy over the original file.

or is using an "upload" test the only method for doing this?

Kind Regards
Sean

Randle Taylor

unread,
May 20, 2021, 6:31:06 AM5/20/21
to seanm...@gmail.com, QATrack+
OK, I see what you are saying.  It should be possible to read it directly from disk, you maybe just need to use the file path which you could do like:

import os
from django.conf import settings

path = os.path.join(setttings.UPLOAD_ROOT, "test", "1206", "sample_2021-02-23_d961fc.txt")
f = open(path, "r")

However, you will have to update the file path every time you upload a new file.  Instead you can do something like:

from qatrack.qa.models import Test
test = Test.objects.get(id=1206)
f = test.attachment_set.filter(name__startswith="sample_").latest("id").attachment
readanalyse = f.read(5)

which should be easier to maintain long term.

RT

seanm...@gmail.com

unread,
May 20, 2021, 10:39:31 PM5/20/21
to QATrack+
Thanks Randle...will definately give that a go. Appreciate the fast reply!

seanm...@gmail.com

unread,
May 24, 2021, 3:07:33 AM5/24/21
to QATrack+

Hi Randle,

If I wanted to access a file attachment that was added to a test list (rather than a test as per above) would I do the same but replace and reference this attachment using "TestList". I get errors if implementing the code below so assuming I have done something wrong. Hopefully I am on the right track..
id=215 is my Test List id found from the attachment shortcut. "linac" is the first 5 letters of an excel I am reading using xlrd.

eg. 
from qatrack.qa.models import TestList
test = TestList.objects.get(id=215)
f = test.attachment_set.filter(attachment__startswith="linac").latest("id").attachment

I am then trying to read f using xlrd.

Cheers
Sean

On Thursday, May 20, 2021 at 8:31:06 PM UTC+10 randle...@gmail.com wrote:

Randle Taylor

unread,
May 25, 2021, 12:45:28 PM5/25/21
to seanm...@gmail.com, QATrack+
Yes that looks ok.  For xlrd you may need to do something like:

f.open("rb")
wb = xlrd.open_workbook(file_contents=f.read())

If that doesn't work you'll have to post what errors you are getting so we can see what the issue is.

Randy


seanm...@gmail.com

unread,
May 28, 2021, 2:15:48 AM5/28/21
to QATrack+
Thanks Randle,

The code works if I remove the filter section:  filter(attachment__startswith="linac")

Any idea why this would be? DO I have to call the filter function somehow?

Cheers
Sean

Randle Taylor

unread,
May 28, 2021, 6:32:38 AM5/28/21
to seanm...@gmail.com, QATrack+
HI Sean,

It's because I forgot the fact that the attachment name starts with the folder it is in (like /uploads/testlistinstance/1234/linac_...)! Sorry about that.  If you plan to add other attachments to the list and want to ensure that only the linac ones are included in the query then you can use .filter(attachment__contains="/linac") which effectively does the same thing as my startwith query. Otherwise you can leave the filter off.

RT

seanm...@gmail.com

unread,
Jun 1, 2021, 1:21:51 AM6/1/21
to QATrack+
Thanks Randle! Working now, very much appreciate your support an dedication to this project.

A small side question unrelated to the above thread. Is it possible to change the size or font of the test name in QA track where it says "Perform Machine Name: Test List Name" immediately after the Admin Options Section.

Also is it possible to display the answer to a composite test in a different colour. Would I need to install colorama or something similar to did this (if possible?).

Cheers
Sean

Randle Taylor

unread,
Jun 2, 2021, 6:32:51 AM6/2/21
to seanm...@gmail.com, QATrack+
You would need to use CSS to make those changes.  You can put css in qatrack/qatrack_core/static/css/site.css

Something like:

#box-perform .box-title {
    font-size: 10px;  /* change font size of title */
}

.qa-composite, .qa-scomposite input {
    color: red; /* change color of composite text to red */
}

should be close to what you need, but you may need to experiment with the browser developer tools to figure out exactly what css selectors and rules you need to use.

After you add site-specific css in that file you need to rerun collectstatic.

RT


seanm...@gmail.com

unread,
Jun 11, 2021, 2:24:35 AM6/11/21
to QATrack+
Thanks again RT...will give that a go for sure. Really appreciate all the great advice.
Reply all
Reply to author
Forward
0 new messages