How to catch Cheetah NameMapper.NotFound exception? - cheetah

I've a piece of code:
#try:
$inv.customer.org.name
#except NameMapper.NotFound
#try:
$inv.customer.person.firstname $inv.customer.person.lastname
#except NotFound
n/a
#end try
#end try
And it raises:
<type 'exceptions.NameError'>:global name 'NameMapper' is not defined
File "/usr/lib64/python2.7/site-packages/modweb/wsgi/main.py", line 139, in application
responseBody = str(pageTemplate)
File "/usr/lib64/python2.7/site-packages/Cheetah/Template.py", line 1005, in __str__
rc = getattr(self, mainMethName)()
File "/srv/www/template.py", line 1
...
Page
https://pythonhosted.org/Cheetah/users_guide/errorHandling.html
uses it similar way, but i've no idea - do I have to import that exception class from somewhere?
There is a lot of talk about C-version of the same _namepper.so lib, but this is not related to that faster version of this same NameMapper -class, I would be a win whatever works finally.

Right, it needs to be defined at the same page that template code is defined, having it in global chainloaded templates won't work.
#from Cheetah import NameMapper
fixes the issue.

Related

Why does Fpdi fail to extend TCPDF when using PHP8.1?

I'm using:
TCPDF version 6.4.4.
FPDI version 2.3.6
PHP Version 8.1.7 (Running on Apache and Windows Server 2012,a dev environment)
And, I don't think it's relevant but I'm also using
tcpdf-extension
These were installed with composer:
{
"require": {
"tecnickcom/tcpdf": "6.4.4",
"setasign/fpdi": "^2.0",
"naitsirch/tcpdf-extension": "dev-master"
}
}
I am also using a Java/PHP bridge. This is relevant because it changes the error message, however, I doubt this is the cause of the issue.
The problem I'm having is that Fpdi fails to extend TCPDF.
The error message I get looks like this:
PHP Fatal error: During class fetch: Uncaught Error: Call to a member function invokeBegin()
on null in http://127.0.0.1:8080/JavaBridge/java/Java.inc:557
Stack trace:
#0 http://127.0.0.1:8080/JavaBridge/java/Java.inc(56): java_Client->invokeMethod(0, 'typeExists', Array)
#1 C:\Program Files (x86)\PHP\v8.1\vendor\setasign\fpdi\src\Tcpdf\Fpdi.php(33):
java_autoload_function('setasign\\Fpdi\\F...')
...
#8 {main} in C:\Program Files (x86)\PHP\v8.1\vendor\setasign\fpdi\src\Tcpdf\Fpdi.php on line 33
It is failing to fetch a class and the php/java bridge, as a last ditch effort to load it, is attempting to fetch the missing class with a java autoloader, which it fails to do because it's not a java class. Unfortunately, it doesn't give me the full name of the class that's it is trying to load. But this error occurs in setasign\fpdi\src\Tcpdf\Fpdi.php on line 33. Which reads:
class Fpdi extends \TCPDF
My code that invokes this looks like this:
define("COMPOSER_AUTOLOADER","C:\\Program Files (x86)\\PHP\\v8.1\\vendor\\autoload.php");
require_once(COMPOSER_AUTOLOADER);
use setasign\Fpdi\Tcpdf\Fpdi;
class PDF extends Fpdi {
...//functions related to my use case
}
My code that calls this extended class:
// initiate PDF
$pdf = new PDF($data['orientation'], 'mm', USLETTER, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetDisplayMode('real');
$pdf->SetAutoPageBreak(true, 1);
//set margins
$pdf->SetMargins(0,0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
...
$pdf->AddPage($data['orientation']);
...
$pdf->Output('newpdf.pdf', 'I');
Also probably relevant, I set up a test that uses only the TCPDF library without Fpdi and it fails to load the constants that are supposed to be set up in TCPDF_static.php. (USLETTER for instance), but if I manually define that constant, it does load the PDF as expected:
require_once("C:\\Program Files (x86)\\PHP\\v8.1\\vendor\\autoload.php");
use setasign\Fpdi\Tcpdf\Fpdi;
define ('USLETTER',array( 612.000, 792.000));
$pdf = new TCPDF('', 'in', USLETTER, true, 'UTF-8', false);
...
$pdf->AddPage();
...
$pdf->Output('newpdf.pdf', 'I');
Another detail: this code was working fine with PHP 5.6 and some older versions of TCPDF (6.2.11) and Fpdi (1.4.2). Upgrading to PHP 8.1.7 required updating these libraries.
Why is TCPDF failing to load its constants and, more importantly, why can't Fpdi extend TCPDF?

basic trouble making the nlohmann json library work in Visual Studio 19

I'm a neophyte. I know I'm doing something bonheaded. I've search this and other fora for the last day and I'm stuck. I'm trying to use nlohmann's json library. I'm getting over 200 error messages, most of which seem to be rooted in the fact that the compiler can't see the header references in the main json.hpp file, but can see the json.hpp file.
I've copied the nlohmann library contents into the source folder and this is the code at the moment:
#include <iostream>
#include "nlohmann\json.hpp"
using json = nlohmann::json;
int main()
{
std::cout << "Hello World!\n";
}
I'm trying to minimize the number of variables I have to chase.
VS is finding the json.hpp file (no squiggly underline once I finally spelled it right, and a bunch of stuff in solution explorer under json.hpp and nlohmann) It also seems to be seeing the subdirectories of nlohmann
the 'json' after nlohmann:: has a squiggly underline; the popup says: namespace 'nlohmann' has no member 'json'
the first 20 or so 211 (!) error messages are code E1696 'cannot open source file' pointing to apparent references from json.hpp an example is: "E1696 cannot open source file "nlohmann/adl_serializer.hpp" json5 C:\Users\Rich\source\repos\json5\json5\nlohmann\json.hpp"
I've added the 'nlohmann' directory to the include directories that the compiler looks for (Project|properties|C/C++|Additional Include Directories) - no change in error message count. Do I need to manually add all of the subdirectories?
I Think you maybe using an older version of c++ (older than c++11).
Try adding this flag to the command -std=c++11.

OpenCV Haar Cascade Creation

I want to try create my own .xml file for my graduation project with this reference.
But I have a problem which stage 6 doesn't work.It gives error such as:
Traceback (most recent call last):
File "./tools/mergevec.py", line 170, in <module>
merge_vec_files(vec_directory, output_filename)
File "./tools/mergevec.py", line 120, in merge_vec_files
val = struct.unpack('<iihh', content[:12])
TypeError: a bytes-like object is required, not 'str'
I have found a solution which says find 0 size vector files and delete them.
But, I don't know which vector files are 0 size and how I can detect them.
Can you help about this please?
I was able to solve my problem when i changed it:
for f in files:
with open(f, 'rb') as vecfile:
content = ''.join(str(line) for line in vecfile.readlines())
data = content[12:]
outputfile.write(data)
except Exception as e:
exception_response(e)
for it:
for f in files:
with open(f, 'rb') as vecfile:
content = b''.join((line) for line in vecfile.readlines())
outputfile.write(bytearray(content[12:]))
except Exception as e:
exception_response(e)
and like before i changed it:
content = ''.join(str(line) for line in vecfile.readlines())
for it:
content = b''.join((line) for line in vecfile.readlines())
because it was waiting for some str, and now it is able to receive the binary archives that we're in need.
:)
Try following this guide. It's more recent.

client.upload_file() for nested modules

I have a project structured as follows;
- topmodule/
- childmodule1/
- my_func1.py
- childmodule2/
- my_func2.py
- common.py
- __init__.py
From my Jupyter notebook on an edge-node of a Dask cluster, I am doing the following
from topmodule.childmodule1.my_func1 import MyFuncClass1
from topmodule.childmodule2.my_func2 import MyFuncClass2
Then I am creating a distributed client & sending work as follows;
client = Client(YarnCluster())
client.submit(MyFuncClass1.execute)
This errors out, because the workers do not have the files of topmodule.
"/mnt1/yarn/usercache/hadoop/appcache/application_1572459480364_0007/container_1572459480364_0007_01_000003/environment/lib/python3.7/site-packages/distributed/protocol/pickle.py", line 59, in loads return pickle.loads(x) ModuleNotFoundError: No module named 'topmodule'
So what I tried to do is - I tried uploading every single file under "topmodule". The files directly under the "topmodule" seems to get uploaded, but the nested ones do not. Below is what I am talking about;
Code:
from pathlib import Path
for filename in Path('topmodule').rglob('*.py'):
print(filename)
client.upload_file(filename)
Console output:
topmodule/common.py # processes fine
topmodule/__init__.py # processes fine
topmodule/childmodule1/my_func1.py # throws error
Traceback:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-13-dbf487d43120> in <module>
3 for filename in Path('nodes').rglob('*.py'):
4 print(filename)
----> 5 client.upload_file(filename)
~/miniconda/lib/python3.7/site-packages/distributed/client.py in upload_file(self, filename, **kwargs)
2929 )
2930 if isinstance(result, Exception):
-> 2931 raise result
2932 else:
2933 return result
ModuleNotFoundError: No module named 'topmodule'
My question is - how can I upload an entire module and its files to workers? Our module is big so I want to avoid restructuring it just for this issue, unless the way we're structuring the module is fundamentally flawed.
Or - is there a better way to have all dask workers understand the modules perhaps from a git repository?
When you call upload_file on every file individually you lose the directory structure of your module.
If you want to upload a more comprehensive module you can package up your module into a zip or egg file and upload that.
https://docs.dask.org/en/latest/futures.html#distributed.Client.upload_file

Can't get Library in Dart to work properly

I have some files and I want them in one library. But it just doesnt work or throws Exceptions.
Let's say I have these files.
Lib.dart, 1.dart, 2.dart, 3.dart and for each of them also an html file, which defines the polymer element.
// Lib.dart
library testLib;
import 'dart:html';
part '1.dart';
part '2.dart';
part '3.dart';
/* ... Code ... */
and
// 1.dart 2.dart and 3.dart
part of testLib;
/* ... Code ... */
It didnt work and i got the message:
'http://localhost:8080/1.dart': error: line 7 pos 6: url expected
part of testLib;
^: http://localhost:8080/1.dart
So I saw this answer: Dart editor: expected url
and I changed my 1.html, 2.html and 3.html to:
// 1.html, 2.html and 3.html
/* ... */
<script type="application/dart" src="Lib.dart"></script>
But now i get this Exception:
Breaking on exception: Already registered (Polymer) prototype for element my-Lib
I dont know why it does not work or what I did wrong there.
I wonder why your part of is on line 7. Do you have a library statement or imports or other code above par of? You can only have comments before part of.
As far as I know currently there is still the limitation that you should have one library for one polymer element.

Resources