I'm using Python 2.7.6 and Django 1.5.5.
How I can write a line in SimpleDocTemplate?
I'm tryng this:
#login_required
def report(request):
rep = Report(request.user.username + "_cities.pdf")
# Title
rep.add_header("Cities")
line = Line(0, 100, 500, 100)
rep.add(line)
# Body
columns = ("City")
cities = [(p.name) for p in City.objects.all()]
table = Table([columns] + cities, style=GRID_STYLE)
table.hAlign = "LEFT"
table.setStyle([('BACKGROUND', (1, 1), (-2, -2), colors.lightgrey)])
rep.add(table)
rep.build()
return rep.response
The Line() is from reportlab.graphics.shapes import Line.
The class Report is only a wrapper class to SimpleDocTemplate:
class Report:
styles = None
response = None
document = None
elements = []
def __init__(self, report_file_name):
self.styles = styles.getSampleStyleSheet()
self.styles.add(ParagraphStyle(name='Title2',
fontName="Helvetica",
fontSize=12,
leading=14,
spaceBefore=12,
spaceAfter=6,
alignment=TA_CENTER),
alias='title2')
self.response = HttpResponse(mimetype="application/pdf")
self.response["Content-Disposition"] = "attachment; filename=" + report_file_name
self.document = SimpleDocTemplate(self.response, topMargin=5, leftMargin=2, rightMargin=1, bottomMargin=1)
self.document.pagesize = portrait(A4)
return
def add_header(self, header_text):
p = Paragraph(header_text, self.styles['Title2'])
self.elements.append(p)
def add(self, paragraph):
self.elements.append(paragraph)
def build(self):
self.document.build(self.elements)
When I call report function, I receive the error message:
Line instance has no attribute 'getKeepWithNext'
hen I remove/comment the lines with Line(), the error don't ocurre.
You can help me?
How to write that line?
Just adding Line to the list of elements doesn't work: you can only pass Flowables to SimpleDocTemplate.build(). But you can wrap it in a Drawing, which is a Flowable:
d = Drawing(100, 1)
d.add(Line(0, 0, 100, 0))
rep.add(d)
Related
I am using xlsxwriter and I have created a class where I have created a workbook. Then I have added 2 worksheets to it.
Now I have written a method that's writing data to one of the worksheets, but now I would like to use it on both worksheets. This is a simple example to explain the situation:
import xlsxwriter
class ExcelWorkbook():
def __init__(self, filename):
self.wb = xlsxwriter.Workbook(filename)
self.ws1 = self.wb.add_worksheet('Num')
self.ws1LineCount = 0
self.ws2 = self.wb.add_worksheet('Result')
self.ws2LineCount = 0
def write_data(self, data, title):
self.ws1.write_row(self.ws1LineCount, 0, title)
self.ws1LineCount += 1
self.ws1.write_row(self.ws1LineCount, 0, data)
self.ws1LineCount += 1
xlsxWorkbook = ExcelWorkbook('Test2.xlsx')
numArr = (0.000000520593523979187, 13.123456789, 1.789456, 0.002345, 0.00123, 1)
titleBar = ('Date', 'quantity', 'Average [m]', 'Standard Dev [m]', 'Test', 'Success')
xlsxWorkbook.write_data(numArr, titleBar)
Now I'd like to use the write_data method for both worksheets, so I thought I'd pass the worksheet as a parameter, but unfortunately it's not that simple, as I cannot pass the instance variable self.ws1 or self.ws2.
So the question is: how can I do that?
I came up with a very nasty solution, like this:
def write_data(self, data, title, instance = 'ws1'):
if instance == 'ws1':
instance = self.ws1
lineCounter = self.ws1LineCount
elif instance == 'ws2':
instance = self.ws2
lineCounter = self.ws2LineCount
instance.write_row(self.ws1LineCount, 0, title)
lineCounter += 1
instance.write_row(self.ws1LineCount, 0, data)
lineCounter += 1
but honestly I don't like it. Is there a proper way to do it, or is it like a completely unreasonable thing?
Instead of the if block, better use workbook.get_worksheet_by_name() method
def write_data(self, data, title, ws = 'Num'):
wsheet = self.wb.get_worksheet_by_name(ws)
wsheet.write_row(self.ws1LineCount, 0, title)
lineCounter += 1
wsheet.write_row(self.ws1LineCount, 0, data)
lineCounter += 1
EDIT: or you can use getattr() function, e.g.
def write_data(self, data, title, ws = 'ws1'):
wsheet = getattr(self, ws, self.ws1))
wsheet.write_row(self.ws1LineCount, 0, title)
lineCounter += 1
wsheet.write_row(self.ws1LineCount, 0, data)
lineCounter += 1
I'd do something like this:
from collections import defaultdict
import xlsxwriter
class ExcelWorkbook():
def __init__(self, filename):
self.wb = xlsxwriter.Workbook(filename)
self.ws1 = self.wb.add_worksheet('Num')
self.ws2 = self.wb.add_worksheet('Result')
self._line_counts = defaultdict(int)
def write_data(self, ws, data, title):
self._write_row(ws, title)
self._write_row(ws, data)
def _write_row(self, ws, content):
ws.write_row(self._line_counts[ws], 0, content)
self._line_counts[ws] += 1
xlsxWorkbook = ExcelWorkbook('Test2.xlsx')
numArr = (0.000000520593523979187, 13.123456789, 1.789456, 0.002345, 0.00123, 1)
titleBar = ('Date', 'quantity', 'Average [m]', 'Standard Dev [m]', 'Test', 'Success')
xlsxWorkbook.write_data(xlsxWorkbook.ws1, numArr, titleBar)
xlsxWorkbook.write_data(xlsxWorkbook.ws2, numArr, titleBar)
By using defaultdict, you don't need to explicitly keep a line count variable for each worksheet. The write_data function accepts a new parameter ws so that you can set the worksheet that should be written to. Writing a row can be factored into a separate method to reduce code duplication.
Does this answer your question?
Edit:
a default value cannot access self, but you can work around this quite easily:
def write_data(self, data, title, ws=None):
if ws is None:
ws = self.ws1
self._write_row(ws, title)
self._write_row(ws, data)
Maybe you should consider to have an extra class ExcelWorksheet, in order to put all logic related to them inside:
class ExcelWorksheet(object):
def __init__(self, workbook, name):
self.wb = workbook
self.ws = self.wb.add_worksheet(name)
self.wsLineCount = 0
def write_data(self, data, title):
self.ws.write_row(self.wsLineCount, 0, title)
self.wsLineCount += 1
self.ws.write_row(self.wsLineCount, 0, data)
self.wsLineCount += 1
This way, you can refactor your code to this:
class ExcelWorkbook(object):
def __init__(self, filename):
self.wb = xlsxwriter.Workbook(filename)
self.ws1 = ExcelWorksheet(self.wb, 'Num')
self.ws2 = ExcelWorksheet(self.wb, 'Result')
xlsxWorkbook = ExcelWorkbook('Test2.xlsx')
numArr = (0.000000520593523979187, 13.123456789, 1.789456, 0.002345, 0.00123, 1)
titleBar = ('Date', 'quantity', 'Average [m]', 'Standard Dev [m]', 'Test', 'Success')
xlsxWorkbook.ws1.write_data(numArr, titleBar)
xlsxWorkbook.ws2.write_data(numArr, titleBar)
With vars(self), you can access any element of the object as a dictionnary.
Then vars(self)["ws1"] is like self.ws1 and vars(self)["ws2"] is like self.ws2
Then you just have to pass a key argument "ws1" or "ws2" in write_data.
def write_data(self, data, title, key='ws1'):
instance = vars(self)[key]
lineCounter = vars(self)[key+"LineCount"]
instance.write_row(lineCounter , 0, title)
lineCounter += 1
instance.write_row(lineCounter , 0, data)
lineCounter += 1
I am trying to add UserControl to a WinForm using PythonNet but not having any luck. For testing, I added a button and that shows up but not the UserControl and I am not sure what I a doing wrong.
All the code can be placed into one py file. I broke into a few sections hoping it will be easier to read.
USER CONTROL
class Field(WinForms.UserControl):
def __init__(self):
self.InitializeComponents()
pass
def InitializeComponents(self):
self.components = System.ComponentModel.Container()
self.label = WinForms.Label()
self.textBox = WinForms.Label()
## Label
# self.label.Anchor = ((WinForms.AnchorStyles)(
# ((WinForms.AnchorStyles.Top | WinForms.AnchorStyles.Bottom)
# | WinForms.AnchorStyles.Left)))
self.label.AutoSize = True
self.label.Location = System.Drawing.Point(3, 7)
self.label.Name = "label"
self.label.Size = System.Drawing.Size(29, 13)
self.label.TabIndex = 0
self.label.Text = "label"
## TextBox
# self.textBox.Anchor = ((WinForms.AnchorStyles)(
# (((WinForms.AnchorStyles.Top | WinForms.AnchorStyles.Bottom)
# | WinForms.AnchorStyles.Left)
# | WinForms.AnchorStyles.Right)))
# self.textBox.Location = System.Drawing.Point(115, 3)
self.textBox.Name = "textBox"
self.textBox.Size = System.Drawing.Size(260, 20)
self.textBox.TabIndex = 1
## Control
self.AutoScaleMode = WinForms.AutoScaleMode.Font
self.Controls.Add(self.textBox)
self.Controls.Add(self.label)
self.Name = "Field"
self.Size = System.Drawing.Size(378, 26)
# self.PerformLayout()
PARENT FORM
class ParentForm(WinForms.Form):
def __init__(self):
self.InitializeComponents()
# region Form Design
def InitializeComponents(self):
self.components = System.ComponentModel.Container()
self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
self.ClientSize = System.Drawing.Size(385, 180)
self.Name = "ParentForm"
self.Text = "Parent Form"
self.field1 = Field()
self.field1.Location = Point(13, 13)
self.field1.Name = "field1"
self.field1.Size = Size(378, 26)
self.field1.TabIndex = 1
self.Controls.Add(self.field1)
self.button1 = WinForms.Button()
self.button1.Location = Point(13, 50)
self.button1.Size = Size(50, 20)
self.button1.Text = "Button1"
self.Controls.Add(self.button1)
pass
def Dispose(self):
self.components.Dispose()
WinForms.Form.Dispose(self)
ENTRY POINT AND IMPORTS
import clr
import System
import System.Windows.Forms as WinForms
from System.IO import File
from System.Text import Encoding
from System.Drawing import Color, Point, Size
from System.Threading import ApartmentState, Thread, ThreadStart
def appThread():
app = ParentForm()
WinForms.Application.Run(app)
app.Dispose()
def appEntry():
thread = Thread(ThreadStart(appThread))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
if __name__ == '__main__':
appEntry()
The following code runs and does what it's supposed to do but I'm having trouble using the XlxsWriter module in Python in order to get some of the results into a .xlxs file. The goal is for the output to contain information from the function block_trial where it tracks each block and gives me the all_powers variable the corresponds with that trial. Installing the module into my user directory goes smoothly but it won't give me a file that gives me both sets of information.
At the moment, I'm using:
import xlsxwriter
workbook = xlsxwriter.Workbook('SRT_data.xlsx')
worksheet = workbook.add_worksheet()
Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
Write some simple text.
worksheet.write('A1', 'RT')
workbook.close()
But can't get any of my data to show up.
import random, math
num_features = 20
stim_to_vect = {}
all_stim = [1,2,3,4,5]
all_features = range(num_features)
zeros=[0 for i in all_stim]
memory=[]
def snoc(xs,x):
new_xs=xs.copy()
new_xs.append(x)
return new_xs
def concat(xss):
new_xs = []
for xs in xss:
new_xs.extend(xs)
return new_xs
def point_wise_mul(xs,ys):
return [x*y for x,y in zip(xs,ys)]
for s in snoc(all_stim, 0):
stim_to_vect[s]= []
for i in all_features:
stim_to_vect[s].append(random.choice([-1, 1]))
def similarity(x,y):
return(math.fsum(point_wise_mul(x,y))/math.sqrt(math.fsum(point_wise_mul(x,x))*math.fsum(point_wise_mul(y,y))))
def echo(probe,power):
echo_vect=[]
for j in all_features:
total=0
for i in range(len(memory)):
total+=math.pow(similarity(probe, memory[i]),power)*memory[i][j]
echo_vect.append(total)
return echo_vect
fixed_seq=[1,5,3,4,2,1,3,5,4,2,5,1]
prev_states={}
prev_states[0]=[]
prev=0
for curr in fixed_seq:
if curr not in prev_states.keys():
prev_states[curr]=[]
prev_states[curr].append(prev)
prev=curr
def update_memory(learning_parameter,event):
memory.append([i if random.random() <= learning_parameter else 0 for i in event])
for i in snoc(all_stim,0):
for j in prev_states[i]:
curr_stim = stim_to_vect[i]
prev_resp = stim_to_vect[j]
curr_resp = stim_to_vect[i]
update_memory(1.0, concat([curr_stim, prev_resp, curr_resp]))
def first_part(x):
return x[:2*num_features-1]
def second_part(x):
return x[2*num_features:]
def compare(curr_stim, prev_resp):
for power in range(1,10):
probe=concat([curr_stim,prev_resp,zeros])
theEcho=echo(probe,power)
if similarity(first_part(probe),first_part(theEcho))>0.97:
curr_resp=second_part(theEcho)
return power,curr_resp
return 10,zeros
def block_trial(sequence):
all_powers=[]
prev_resp = stim_to_vect[0]
for i in sequence:
curr_stim = stim_to_vect[i]
power,curr_resp=compare(curr_stim,prev_resp)
update_memory(0.7,concat([curr_stim,prev_resp,curr_resp]))
all_powers.append(power)
prev_resp=curr_resp
return all_powers
I've only just started coding, so I thought I would try and make something simple, however, I can't select the objects from my ls, I know the error is in my def attrLockT and was wondering if anyone could help me to fix this issue and understand what I am doing wrong?
import maya.cmds as cmds
#Selects the attributes
sat = ['.tx', '.ty', '.tz']
sar = ['.rx', '.ry', '.rz']
sas = ['.sx', '.sy', '.sz']
#Creates the list of currently selected objects
myList = cmds.ls(sl = True)
#Lock the translate attributes of the selected objects
def attrLockT(*args):
checkAttr=cmds.getAttr (myList[0] + sat)
if (checkAttr == 0):
cmds.setAttr(myList[0] + sat, lock = 1)
#Delete window if it is already open
if cmds.window('animationCtrl', exists=True):
cmds.deleteUI('animationCtrl', window=True)
#Setup the window
cmds.window(
'animationCtrl',
title = "Animation Controls",
widthHeight = (300, 500),
s = False)
form = cmds.formLayout()
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout(
form,
edit=True,
attachForm=(
(tabs, 'top', 0),
(tabs, 'left', 0),
(tabs, 'bottom', 0),
(tabs, 'right', 0)))
#Layout for the first tab
child1 = cmds.gridLayout( numberOfRowsColumns=(4, 3) , cwh = (100, 50))
cmds.text(label = "")
cmds.text(label = "Lock", align = "center", h = 20, w = 250)
cmds.text(label = "")
cmds.button(label = "Translate", h = 300, w = 250, c = attrLockT)
cmds.button(label = "Rotate", h = 50, w = 250)
cmds.button(label = "Scale", h = 50, w = 250)
cmds.text(label = "")
cmds.text(label = "Unlock", align = "center", h = 20, w = 250)
cmds.text(label = "")
cmds.button(label = "Translate", h = 50, w = 250)
cmds.button(label = "Rotate", h = 50, w = 250)
cmds.button(label = "Scale", h = 50, w = 250)
cmds.setParent( '..' )
#Layout for the second tab
child2 = cmds.rowColumnLayout(numberOfColumns=3)
cmds.button()
cmds.button()
cmds.button()
cmds.setParent( '..' )
cmds.tabLayout(
tabs,
edit=True,
tabLabel=((child1, 'Lock/Unlock'), (child2, 'Keyable/Unkeyable')))
cmds.showWindow('animationCtrl')
The error that is thrown is
# Error: coercing to Unicode: need string or buffer, list found
# Traceback (most recent call last):
# File "<maya console>", line 16, in attrLockT
# TypeError: coercing to Unicode: need string or buffer, list found
Does this work?
myList[0] + sat
Is myList[0] type of list ? Because the sat variable is certainly list.
If myList is just a list of string then myList[0] will be just one element of type string and it will produce an error.
Simplify your program, just leave only locking routine and window with button to see what will happen, be sure that the right name of an object + attribute is passed to getAttr - just string like 'obj.attrib'.
Some python specific clues for your function
If you need to sum two lists:
[ objName + attName for objName, attName in zip(myList, sat) ]
that will result, for example, in ['obj1.tx', 'obj2.ty', 'obj3.tz']
If you need apply a list of attributes to an object:
[ myList[0] + a for a in sat ]
that will result in ['obj1.tx', 'obj1.ty', 'obj1.tz']
If you need apply the same list of attributes to all objects:
[ objName + attName for objName in myList for attName in sat ]
will result in ['obj1.tx', 'obj1.ty', 'obj1.tz', 'obj2.tx', ..., 'obj3.tz']
Then you can call your locking function over that result list:
def locker(a):
checkAttr = cmds.getAttr(a)
if (checkAttr == 0):
cmds.setAttr(a, lock = 1)
and finally it should look:
def attrLockT(*args):
atrlist = [ ..... ]
for a in atrlist:
locker(a)
Two issues:
first, you want to loop through the individual attributes and concatenate them with object names:
def lockTranslate(*args):
for obj in mylist:
for attr in ['.tx', '.ty', '.tz']:
cmds.setAttr(obj + "." + attr, l=True)
second, and maybe more important, you will likely have problems the scope of your functions. In the form you have it typed in, variables like myList and sat, etc are accessible to the function through closure - it will work if you execute all of this in the listener, but if you break the closure (for example, if this goes into a function that gets called by another function) things wont work -- myList will be stuck pointing at whatever was selected when the function was defined.
In this particular case you probably want to just operate on selection instead of inheriting myList:
def lockTranslate(*args):
for obj in cmds.ls(sl=True, type = 'transform'):
for attr in ['.tx', '.ty', '.tz']:
cmds.setAttr(obj + "." + attr, l=True)
I have a pyramid/python application with the following view callable:
#view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
if 'form.submitted' in request.params:
name= request.params['name']
input_file=request.POST['stl'].file
vertices, normals = [],[]
if input_file.read(5) == b'solid':
for line in input_file:
parts = line.split()
if parts[0] == 'vertex':
vertices.append(map(float, parts[1:4]))
elif parts[0] == 'facet':
normals.append(map(float, parts[2:5]))
ordering=[]
N=len(normals)
for i in range(0,N):
ordering.append([3*i,3*i+1,3*i+2])
data=[vertices,ordering]
else:
f=input_file
points=[]
triangles=[]
normals=[]
def unpack (f, sig, l):
s = f.read (l)
fb.append(s)
return struct.unpack(sig, s)
def read_triangle(f):
n = unpack(f,"<3f", 12)
p1 = unpack(f,"<3f", 12)
p2 = unpack(f,"<3f", 12)
p3 = unpack(f,"<3f", 12)
b = unpack(f,"<h", 2)
normals.append(n)
l = len(points)
points.append(p1)
points.append(p2)
points.append(p3)
triangles.append((l, l+1, l+2))
#bytecount.append(b[0])
def read_length(f):
length = struct.unpack("#i", f.read(4))
return length[0]
def read_header(f):
f.seek(f.tell()+80)
read_header(f)
l = read_length(f)
try:
while True:
read_triangle(f)
#except Exception, e:
#print "Exception",e[0]
#write_as_ascii(outfilename)
data=[points,triangles]
jsdata=json.dumps(data)
renderer_dict = dict(name=name,data=jsdata)
path=shortuuid.uuid()
html_string = render('tutorial:templates/view.pt', renderer_dict, request=request)
s3=boto.connect_s3(aws_access_key_id = 'AKIAIJB6L7Q', aws_secret_access_key = 'sId01dYCMhl0wX' )
bucket=s3.get_bucket('cubes.supercuber.com')
k=Key(bucket)
k.key='%(path)s' % {'path':path}
k.set_contents_from_string(html_string, headers={'Content-Type': 'text/html'})
k.set_acl('public-read')
return HTTPFound(location="http://cubes.supercuber.com/%(path)s" % {'path':path})
return {}
as you can see, the code checks whether an uploaded stl file is ascii (if is starts with the word "solid") or binary. If its ascii, everything works fine and the data variable gets filled with the vertices and ordering. However, if it doesn't start with solid and is a binary stl file the data variable never gets filled. Why is this?
You read the first 5 bytes to check for the filetype, but never reset the file position to the beginning.
Add a call to .seek(0):
f=input_file
f.seek(0)
You can always seek relatively without a call to f.tell():
f.seek(80, 1) # relative seek, you can use `os.SEEK_CUR` as well.