Building python dict using multithreading - python

I'm currently building a Tool that generates a PDF report using Python pdfkit and jinja.
This report builds on a lot of static html and around 30 functions that produces data and images (charts) for the report. These functions all access external data through pyodbc or pandas from_sql.
I am now running into performance issues, and the report takes around 5 minutes to build.
I'm hoping to utilize multithreading in order to build a dictionary of data, but have not been able to figure out how to approach the issue.
My current code looks something like this.
def buildReport():
if checkKvaegCVR(SQL = checkKvaegCVRSQL(cvrNummer = cvrNummer), cursor = OEDBCursor):
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("templates/kvaeg/kvaegBase.html")
pdfOptions = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'quiet': '',
'encoding': "UTF-8",
'footer-right': '[page]'
}
css = 'static/css/style.css'
template_vars = {'kvaegForsideBillede': imageBuilder()['kvaegForsideBillede'],
'bagsideBillede': imageBuilder()['bagsideBillede'],
'navn' : bedriftAdresse(cvrNummer = cvrNummer,
cursor = KundeAnalyseDBCursor)[0],
'adresse' : bedriftAdresse(cvrNummer = cvrNummer,
cursor = KundeAnalyseDBCursor)[1],
'postnrBy' : str(int(bedriftAdresse(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor)[2])) + ' ' +
bedriftAdresse(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor)[3],
'fremstillingsprisKorr': imageBuilder()['fremstillingsprisKorr'],
'fremstillingsprisForbedring':imageBuilder()['fremstillingsprisForbedring'],
'graesoptagelse':kgGraesPrKo(),
'indreSaedskifteKort':indreSaedskifteKortPNG(CVRPunkt = CVRPunkt(cvrNummer, KundeAnalyseDBCursor),
CVRBuffer = CVRBuffer(cvrNummer, KundeAnalyseDBCursor),
indreSaedskifteKort = indreSaedskifteKort(indreSaedskifteKortSQL = indreSaedskifteKortSQL(cvrNummer = cvrNummer), cursor = KundeAnalyseDBCursor)),
'naboKort':naboKortPNG(CVRPunkt = CVRPunkt(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor),
CVRBuffer = CVRBuffer(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor),
naboKort = naboKort(naboMarkerSQL = naboMarkerSQL(cvrNummer = cvrNummer),
egneMarkerSQL = egneMarkerSQL(cvrNummer = cvrNummer),
cursor = KundeAnalyseDBCursor))
...
...
30 more functions here
...
...}
pdfkit.from_string(template.render(template_vars), 'KvaegRapport - {}.pdf'.format(cvrNummer), options=pdfOptions, css=css)
print('Rapporten er klar')
else:
print('Kan ikke bygge rapport på dette CVR nummer')
I would like to build the dictionary "Template_vars" using multithreading (probably outside my main function)
Any suggestions?

I could suggest following but with multiprocessing (the following code has not been tested):
from multiprocessing import Process, Queue
def make_smth(func, queue, name, *args, **kwargs):
queue.put((name, func(*args, **kwargs)))
result_queue = Queue()
processes = list()
processes.append(
Process(target=make_smth,
args=(bedriftAdresse, result_queue, "navn"),
kwargs={cvrNummer: cvrNummer, cursor: KundeAnalyseDBCursor[0]}
)
)
processes.append(
Process(target=make_smth,
args=(kgGraesPrKo, result_queue, "graesoptagelse"),
kwargs={}
)
)
#...... You should do it for each of your functions
for p in processes:
p.start()
template_vars = {}
result = result_queue.get()
while result:
template_vars[result[0]] = result[1]
result = result_queue.get()

Related

Maya python API: set Vertex Colors

How can I set vertex colors with the Maya python api, given a number of uv shells? So for each shell within a single mesh assign a random vertex color. At the bottom I have the previous code which is too slow in more dense meshes.
def setColors():
shells = getUvShelList( cmds.ls(sl=1)[0] )
print ( shells )
"""
#3 similar objects combined into one
{0: ['pCube4.map[0]', 'pCube4.map[1]', 'pCube4.map[2]', 'pCube4.map[3]', 'pCube4.map[4]', 'pCube4.map[5]', 'pCube4.map[6]', 'pCube4.map[7]', 'pCube4.map[8]', 'pCube4.map[9]', 'pCube4.map[10]', 'pCube4.map[11]', 'pCube4.map[12]', 'pCube4.map[13]'],
1: ['pCube4.map[14]', 'pCube4.map[15]', 'pCube4.map[16]', 'pCube4.map[17]', 'pCube4.map[18]', 'pCube4.map[19]', 'pCube4.map[20]', 'pCube4.map[21]', 'pCube4.map[22]', 'pCube4.map[23]', 'pCube4.map[24]', 'pCube4.map[25]', 'pCube4.map[26]', 'pCube4.map[27]'],
2: ['pCube4.map[28]', 'pCube4.map[29]', 'pCube4.map[30]', 'pCube4.map[31]', 'pCube4.map[32]', 'pCube4.map[33]', 'pCube4.map[34]', 'pCube4.map[35]', 'pCube4.map[36]', 'pCube4.map[37]', 'pCube4.map[38]', 'pCube4.map[39]', 'pCube4.map[40]', 'pCube4.map[41]']}
"""
selList2 = om2.MGlobal.getActiveSelectionList()
dagPath = selList2.getDagPath(0)
selMesh = om2.MFnMesh(dagPath)
vertList = list(set(selMesh.getVertices()[1]))
lenVertList = len(vertList)
for shell in shells:
selection_shell = shells.get(shell)
r = [random.random() for i in range(3)]
tempColor = om2.MColor([r[0],r[1],r[2]])
vertexColorList = om2.MColorArray(lenVertList, tempColor)
selMesh.setVertexColors(vertexColorList, vertList)
setColors()
My previous code(too slow):
for shell in chunk:
selection_shell = shells.get(shell)
cmds.select(selection_shell)
facesSel = cmds.polyListComponentConversion(fromUV=True, toFace=True)
cmds.select(facesSel)
r = [random.random() for i in range(3)]
cmds.polyColorPerVertex(facesSel,rgb=(r[0], r[1], r[2]), cdo=1 )
cmds.select(deselect=1)
Here's what I ended up doing, creating a random color per element within a mesh. Similar to element ID in 3ds max.
To answer my own question, I had to convert the UV indices to Vertex indices per shell.
import maya.cmds as cmds
import maya.api.OpenMaya as om
from itertools import zip_longest
from itertools import chain
import random
import colorsys
import time
import maya.mel as mel
class colorVariationShell():
def __init__(self):
self.setSelection()
self.setColors()
def setSelection(self):
# Get the currently selected mesh.
self.mSelList = om.MGlobal.getActiveSelectionList()
self.path = self.mSelList.getDagPath(0)
self.fnMesh = om.MFnMesh(self.path)
def get_progress_bar(self,status_text, max_value):
try:
progress_bar = mel.eval("$tmp = $gMainProgressBar")
except RuntimeError:
progress_bar = None
if progress_bar:
cmds.progressBar(
progress_bar,
edit=True,
beginProgress=True,
isInterruptable=False,
status=status_text,
maxValue=max_value
)
return progress_bar
def getUvShellsOnObj(self):
#Get UV Sets
uvSets = self.fnMesh.getUVSetNames()
for uvset in uvSets:
uvShellIds = self.fnMesh.getUvShellsIds(uvset)[1]
polyVertices = self.fnMesh.getVertices()
polyUvs = self.fnMesh.getAssignedUVs(uvset)
shells = {}
vertexGroup = []
list_zip = zip(polyVertices[1], polyUvs[1])
zipped_list = list(list_zip)
main_progress_bar_3 = self.get_progress_bar("Getting UV Shells IDS", len(zipped_list))
for i, n in enumerate(uvShellIds):
if n in shells:
shells[n].append( i )
else:
shells[n] = [ ( i ) ]
cmds.progressBar(main_progress_bar_3, edit=True, step=1)
cmds.progressBar(main_progress_bar_3, edit=True, endProgress=True)
vertsID = []
uvsID = []
main_progress_bar = self.get_progress_bar("Gathering UV and Vertex Info", len(zipped_list))
for zipItem in zipped_list:
vertsID.append(zipItem[0])
uvsID.append(zipItem[1])
cmds.progressBar(main_progress_bar, edit=True, step=1)
cmds.progressBar(main_progress_bar, edit=True, endProgress=True)
vertex_id_from_shell = {}
main_progress_bar_2 = self.get_progress_bar("Passing UV and Vertex data", len(shells))
for shell in shells:
selection_shell = shells.get(shell)
for idx, item in enumerate(selection_shell):
if item in uvsID:
uv_index = uvsID.index(item)
vertex_ids = vertsID[uv_index]
# if the list does not exist, create it
if shell not in vertex_id_from_shell:
vertex_id_from_shell[shell] = []
# append to list
vertex_id_from_shell[shell].append(vertex_ids)
cmds.progressBar(main_progress_bar_2, edit=True, step=1)
cmds.progressBar(main_progress_bar_2, edit=True, endProgress=True)
return shells, vertex_id_from_shell
def setColors(self):
shells, target_vertex_id = self.getUvShellsOnObj()
#ammount of different colors
var = 3
vertexData = {key : [] for key in range(var)}
pool = []
for val in target_vertex_id.values():
if not pool:
pool = list(range(var))
random.shuffle(pool)
vertexData[pool.pop()].extend(val)
main_progress_bar = self.get_progress_bar("Applying Vertex Colors", len(vertexData))
for vertex_idx, selection_vertex in vertexData.items():
vertexIds = selection_vertex
lenVertList = len(vertexIds)
sat_random = random.uniform(0.5, 1)
increment = 1/len(vertexData)
color_hsv = (vertex_idx*increment, sat_random, vertex_idx*increment+1/len(vertexData))
rgb_color = colorsys.hsv_to_rgb(color_hsv[0],color_hsv[1],color_hsv[2])
final_color = om.MColor(rgb_color)
vertexColorList = om.MColorArray(lenVertList, final_color)
#apply the color
self.fnMesh.setVertexColors(vertexColorList, vertexIds)
cmds.progressBar(main_progress_bar, edit=True, step=1)
cmds.polyOptions(colorShadedDisplay=1)
cmds.progressBar(main_progress_bar, edit=True, endProgress=True)
start_time = time.time()
colorVariationShell()

Python FedEx Image Stream to Image

I am trying to automate my shipping process using the Python fedex module. I am using developer test credentials. The code to create my shipment can be seen here:
import fedex
from fedex.services.ship_service import FedexProcessShipmentRequest, FedexDeleteShipmentRequest
from fedex.config import FedexConfig
CONFIG_OBJ = FedexConfig(key='******',
password= '*****',
account_number='***',
meter_number='***',
use_test_server=True)
shipment = FedexProcessShipmentRequest(CONFIG_OBJ)
shipment = FedexProcessShipmentRequest(CONFIG_OBJ)
shipment.RequestedShipment.DropoffType = 'REGULAR_PICKUP'
shipment.RequestedShipment.ServiceType = 'FEDEX_GROUND'
shipment.RequestedShipment.PackagingType = 'YOUR_PACKAGING'
shipment.RequestedShipment.Shipper.Contact.PersonName = 'Sender Name'
shipment.RequestedShipment.Shipper.Contact.PhoneNumber = '9012638716'
shipment.RequestedShipment.Shipper.Address.StreetLines = ['Address Line 1']
shipment.RequestedShipment.Shipper.Address.City = 'Detroit'
shipment.RequestedShipment.Shipper.Address.StateOrProvinceCode = 'Mi'
shipment.RequestedShipment.Shipper.Address.PostalCode = '48127'
shipment.RequestedShipment.Shipper.Address.CountryCode = 'US'
shipment.RequestedShipment.Recipient.Contact.PersonName = 'Recipient Name'
shipment.RequestedShipment.Recipient.Contact.PhoneNumber = '9012637906'
shipment.RequestedShipment.Recipient.Address.StreetLines = ['Address Line 1']
shipment.RequestedShipment.Recipient.Address.City = 'Detroit'
shipment.RequestedShipment.Recipient.Address.StateOrProvinceCode = 'Mi'
shipment.RequestedShipment.Recipient.Address.PostalCode = '48127'
shipment.RequestedShipment.Recipient.Address.CountryCode = 'US'
shipment.RequestedShipment.EdtRequestType = 'NONE'
shipment.RequestedShipment.ShippingChargesPayment.Payor.ResponsibleParty.AccountNumber \
= CONFIG_OBJ.account_number
shipment.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER'
shipment.RequestedShipment.LabelSpecification.LabelFormatType = 'COMMON2D'
shipment.RequestedShipment.LabelSpecification.ImageType = 'PNG'
shipment.RequestedShipment.LabelSpecification.LabelStockType = 'PAPER_7X4.75'
shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
The code executes without problem and outputs the following JSON:
(reply){
HighestSeverity = "SUCCESS"
Notifications[] =
(Notification){
Severity = "SUCCESS"
Source = "ship"
Code = "0000"
Message = "Success"
LocalizedMessage = "Success"
},
Version =
(VersionId){
ServiceId = "ship"
Major = 23
Intermediate = 0
Minor = 0
}
JobId = "aac513eb0455f072033104594"
CompletedShipmentDetail =
(CompletedShipmentDetail){
UsDomestic = True
CarrierCode = "FDXG"
MasterTrackingId =
(TrackingId){
TrackingIdType = "FEDEX"
TrackingNumber = "794608334864"
}
ServiceTypeDescription = "FXG"
ServiceDescription =
(ServiceDescription){
ServiceType = "FEDEX_GROUND"
Code = "92"
Names[] =
....
I would like to show the physical shipping label that the following subsection of the output points to:
Label =
(ShippingDocument){
Type = "OUTBOUND_LABEL"
ShippingDocumentDisposition = "RETURNED"
ImageType = "PNG"
Resolution = 200
CopiesToPrint = 1
Parts[] =
(ShippingDocumentPart){
DocumentPartSequenceNumber = 1
Image = "iVBORw0KGgoAAAANSUhEUgAABXgAAAO2AQAAAAB6QsJkAAAnSklEQVR42u2dzY8jyXXgg6ahlIDZSR11aHdqIWCva2Eus1B70oAPOvpP0Oiko2agg3o8xYpstLB1MVQ2fBJgqP4E722x25op9taueFrX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LyHcvYEwthefvtn9jy+aPu34tz+fxIbiPbg+fNkr73I02s1Go9ge38x2/2fd+ANx7LE//c83YB3+YO7bXyLYE6/cx/AaXsNreA2v4TW8htfwGl7Da3gNr+E1vIbX8Bpew2t4Da/hNbyG1/AaXsNreA2v4TW8htfwGl7Da3gNr+E1vIbX8Bpew2t4Da/hNbyG1/AaXsNreA2v4TW8htfwGl7Da3gNr+E1vIbX8Bpew2t4Da/hNbyG1/AaXsNreA2v4TW8htfwGl7Da3gNr+E1vIbX8Bpew2t4Da/hNbyG1/AaXsNreA2v4TW8hlf18/8BREqCrGpwnF0AAAAASUVORK5CYII="
},
}
However, I am unsure how to do this. Can anybody offer me any guidance?
Thank you!

How can I change command function using checkbox in pymel

I want to use a checkbox to control the ratio multiplied in slider function.
First I try to change the variable directly, but failed.
Then I try to change the dragCommand, but also failed.
How can I control it?
import pymel.core as pm
import math
myRatio = math.sqrt(2) * 0.5
def myPolyBevel(*args):
# called by convert
def convertOn(*args):
#myRatio = math.sqrt(2) * 0.5
myOffsetSlider.dragCommand = myOffsetOn
def convertOff(*args):
#myRatio = 1.0
myOffsetSlider.dragCommand = myOffsetOff
# called by offset
def myOffsetOn(*args):
temp = pm.floatSliderGrp( myOffsetSlider, query = True, value = True ) * myRatio
pm.setAttr( myBevel.name()+".offset", temp )
def myOffsetOff(*args):
temp = pm.floatSliderGrp( myOffsetSlider, query = True, value = True )
pm.setAttr( myBevel.name()+".offset", temp )
# main
if pm.selected():
# add Bevel
newBevel = pm.polyBevel3( pm.selected(), offset = 0.1 * myRatio, segments = 2, smoothingAngle = 30, chamfer = 1 )
myBevel = newBevel[0]
# UI
bevelWindow = pm.window(title = 'Bevel')
bevelLayout = pm.columnLayout()
myConvert = pm.checkBoxGrp( numberOfCheckBoxes = 1, label = '', label1 = 'Convert', value1 = 1, onCommand = convertOn, offCommand = convertOff )
myOffsetSlider = pm.floatSliderGrp( label='Offset', field = True,
minValue = 0.0, maxValue = 5.0,
fieldMinValue = 0.0, fieldMaxValue = 100.0,
value = 0.1, step = 0.001,
dragCommand = myOffsetOn, changeCommand = myOffsetOn )
pm.showWindow()
# Main
mainWindow = pm.window(title = 'Star')
mainLayout = pm.gridLayout( cellWidthHeight = (40,40), numberOfColumns = 5 )
bevelButton = pm.button( label = 'Bevel', command = myPolyBevel)
pm.showWindow()
You may have to not nest all this command, it makes it harder (or use some Class method)
use partial to pass args :
Maya Python - Using data from UI
here a useful post you can refer, check theodor and my answers on the topic (we often answer this question )

TraitsUI - Joining views

Reading the documentation about applying the MVC pattern with TraitsUI, I read the example MVC_demo.py. Now, I'm wondering how to manage multiple "MVC".
I want to write some "includeallMVCs.py" and to have something like:
import MyViewController1, MyViewController2, MyViewController2
class IncludeallMVCs(HasTraits):
view = Include(MyViewController1, MyViewController2, MyViewController3)
Where MyViewController, MyViewController, MyViewController are classes like the MVC_demo sample.
So, the idea is to separate differents views with their controllers, and then "join" all of them in only one "generic" view.
Searching in the examples I found this one: Dynamic Forms Using Instances
Then I modified it to separate an AdultHandler. In this example I use the salary variable to be controlled by AdultHandler.
File: adult.py
from traitsui.api import Handler
class AdultHandler(Handler):
def object_salary_changed (self, info):
if (info.object.salary >= 20000):
print "Good Salary!"
else:
print "Bad Salary"
File: adult_model.py
from adult import AdultHandler
class AdultSpec ( HasTraits ):
""" Trait list for adults (assigned to 'misc' for a Person when age >= 18).
"""
marital_status = Enum( 'single', 'married', 'divorced', 'widowed' )
registered_voter = Bool
military_service = Bool
salary = Int
traits_view = View(
'marital_status',
'registered_voter',
'military_service',
'salary',
handler = AdultHandler()
)
if __name__ == '__main__':
a = AdultSpec()
a.configure_traits()
File: main.py
from adult_model import AdultSpec
class Person ( HasTraits ):
""" Demo class for demonstrating dynamic interface restructuring.
"""
datainput = Instance( AdultSpec )
# Interface for attributes that depend on the value of 'age':
spec_group = Group(
Group(
Item( name = 'datainput', style = 'custom' ),
show_labels = False
),
label = 'Additional Info',
show_border = True
)
# A simple View is enough as long as the right handler is specified:
view = View(
Group(
spec_group
),
title = 'Using MVC',
buttons = [ 'OK' ],
resizable = True,
width = 300
)
# Create the demo:
demo = Person( datainput = AdultSpec() )
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()

Pyramid, setting i18n by page

i have pages with multiple languages... something like this:
/ - > en,us,pt,es and the default is US
/foo - > pt,en and default is PT
/bar -> pt and default is PT
on the database i have:
|--URL--|-Languages-|-defLang-
| foo | pt,us | pt
| bar | pt | pt
on the __init__.py i've set it like this:
tsf = TranslationStringFactory('myproject')
def add_renderer_globals(event):
request = event['request']
event['_'] = request.translate
event['localizer'] = request.localizer
def add_localizer(event):
request = event.request
languages = ('en','us','es')
deflang = 'pt'
# CHECK current page
if 'page' in request.matchdict:
currentpage = DBSession.query(Pages).filter_by(url=request.matchdict['page']).first()
#SET pages languages
languages = currentpage.Languages.split(',')
deflang = currentpage.defLang
if not default_locale_negotiator(request):
#set language
request._LOCALE_ = request.accept_language.best_match(languages , deflang)
localizer = get_localizer(request)
request.lang = localizer.locale_name
def auto_translate(string):
return localizer.translate(self.tsf(string))
request.localizer = localizer
request.translate = auto_translate
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
session_factory = session_factory_from_settings(settings)
config = Configurator(settings=settings)
config.include('pyramid_mailer')
config.set_session_factory(session_factory)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_subscriber(add_renderer_globals, BeforeRender)
config.add_subscriber(add_localizer, ContextFound)
config.add_translation_dirs('myproject:locale/')
config.add_route('home', '/')
config.add_route('page', '/{page}')
config.scan()
return config.make_wsgi_app()
but that's not working, only the home it's working fine
if i'm missing any info please warn me.
thanks
It looks to me like you are searching for 'entity' in request.matchdict when in reality you should be searching for 'page', given your route '/{page}'.
def add_localizer(event):
request = event.request
languages = ('en','us','es')
deflang = 'pt'
# CHECK current page
if 'page' in request.matchdict:
currentpage = DBSession.query(Pages).filter_by(url=request.matchdict['page']).first()
#SET pages languages
languages = currentpage.Languages.split(',')
deflang = currentpage.defLang
#----- CHANGED ----------------------
dln = default_locale_negotiator(request)
if not dln or dln not in languages:
#set the default language
request._LOCALE_ = deflang
localizer = get_localizer(request)
request.lang = localizer.locale_name
def auto_translate(string):
return localizer.translate(self.tsf(string))
request.localizer = localizer
request.translate = auto_translate
thanks

Categories

Resources