Is there a way to implement with statement rename as none?
For instance,
class MyClass(object):
def test(self,s):
print s
then I want to execute this string code in the users's context, such as
with MyClass(a):
test("hello")
then it will print out "hello"
What would be the design pattern and how do I convert user's code this way?
Related
I need to log messages that will possibly exceed the 32k character limit. How can I extend the LogWriter with a debug(character) or what would be the best way to use the Logging Framework to log messages that are greater than 32k characters
There is a way, but it takes a bit of code.
The first step is to use the debug(LogMessage, ...) method, rather than debug(character, ...) method.
You will need to build the LogMessage but can't use its public properties (since they are also character. So you will need to use the LogMessage's AddContext method. This takes a character for the key and a Progress.Lang.Object as the value. You can use an instance of OpenEdge.Core.String, which holds longchar values.
using OpenEdge.Logging.*.
using OpenEdge.Core.*.
define variable logger as ILogWriter no-undo.
define variable logMsg as LogMessage no-undo.
logger = LoggerBuilder:GetLogger('something').
// do stuff
define variable longcharWithLotsOfData as longchar no-undo.
logMsg = new LogMessage(logger:Name, 'short message').
logMsg:AddContext('long-message', new String(longcharWithLotsOfData)).
logger:debug(logMsg).
You will probably also need to add your own filter to read this message from that context, and write it into the logfile. You can see an example of how to create one here .
You'll need to write to a 'named file' rather than the LOG-MANAGER since the WRITE-MESSAGE() method only takes a character for the message.
The writer will need to output the String object's Value property; ToString() returns a character. The filter will need to check the type of the object returned from the GetContext() method, and cast it to get the value.
You will need to use the COPY-LOB... APPEND statement to write the longchar value to the output file.
There are basically 3 steps
Create a class that implements ILoggerFilter
Add the filter definition to the filter property in logging.config
Add the filter to your logger(s) in logging.config
I would like to ask If there is a possibility to log python keywords to log.html like robot keywords. If we executin Robot/Python keyword from robot file it's ok - there is a "node" for keyword. When we run python keyword and if that keyword contain other keywords inside, these keywords are not logged properly - doesn't have own node. In log.html that keywords are under one "node" (parent), executed from robot file.
Is there any decorator for keywords or other method to "tell" for robot that methods inside that keyword are too keywords?
Robot:
Run Something
+Run other keyword
+Execute command
Python:
- Run something
....
....
Thank you in advance for your help.
Br, Tomasz
Your image in comments helped me to realize your problem.
Is there any decorator for keywords or other method to "tell" for robot that methods inside that keyword are too keywords?
As I know, there isn't such decorator. Robot has no knowledge about internal call-graph of a Python keyword and is unable to produce tree-like structure for logs of your internal function calls.
I think, there is a workaround for that. Logging and return value are the only ways that you can communicate to Robot Framework from within your Python code and tell it something. I suggest you to use logging API to make your Robot Framework log more structured and this decorator is one of several things you can do:
from robot.api import logger
def logged_keyword(some_function):
def wrapper():
logger.info('start of {}'.format(some_function.__name__))
some_function()
logger.info('end of {}'.format(some_function.__name__))
return wrapper
def show_my_name():
logger.info('Tommy')
print_colour()
#logged_keyword
def print_colour():
logger.info('blue')
print_other_colour()
#logged_keyword
def print_other_colour():
logger.info('white')
There is a way to tell to Robot Framework that you are logging HTML rather than plain text; so, you can use a decorator to log HTML mark-ups to help Robot Framework structure your log messages. Maybe the HTML source of Robot Framework log is a good place to start (inspect element in browser).
def markuped_keyword(some_function):
def wrapper():
logger.info('<div>', html=True)
some_function()
logger.info('</div>', html=True)
return wrapper
You should refine the mark-up to have a tree-like structure for your logs.
Good luck!
How do I define a single context in a QtLinguist .ts file, rather than having one context per file?
I need this because I have identical strings showing up in different files for which I want the same translation (and no duplicates)
From C++, you can explicitly specify the translation context on a string-by-string basis by using the static function QCoreApplication::translate(const char* context, const char* text) instead of the traditional QObject::tr(const char* text) (see this doc for more details: http://doc.qt.io/qt-5/qcoreapplication.html#translate)
e.g.
menuItem->setText(QCoreApplication::translate("UniversalContext", "Exit"));
Similarly, you can specify the context in QML using the qsTranslate(context, text) function, a la
Text { text: qsTranslate("UniversalContext", "hello") }
These will all get picked up by lupdate as belonging to the same context (duplicate strings will only show up once in your .ts file)
If you have a lot of strings and it gets painful to specify the context on every call to translate/qsTranslate, you should be fairly simple to create a 1-arg macro (c++) or a js function (qml) as a wrapper.
I need to continue writing to the same file using a file output stream, However I'm implementing the program using a javafx GUI from different windows. but:
Since you can not access a global variable without intializing the stream as final I have to make it final however,
Since I have to wrap the the Stream in a try catch, the variable is not recognised as initialised.
I was previously intialising in method like this
eW = outputFactory .createXMLEventWriter(new FileOutputStream("File.txt"));
However, that obivously overwrites the same file when you write the same statement again.
So essentially my question is, how can you set a final variable that needs to be surrounded in a try catch?
final File file = new File("File.txt");
final FileOutputStream fileOS = new FileOutputStream(file);
You don't need to initialize a variable as final to access it from different threads or objects. There are other ways to do it.
You can use a helper class, or even a helper thread. The second option is better in (the most common) case where you have more than 1 thread in your project. You can then use a normal try-catch block that has a (almost infinite) while loop inside, that checks a write queue and writes something when needed.
If you only use 1 thread and only append to a file, you may want to do just that - open the file to append, rather than overwrite. There's a boolean flag in the constructor.
You can take a look at the FileOutputStream(File file, boolean append) constructor, which would allow you to append to the end of the file rather than overwrite each time.
I do not know how you implemented it, but if you are writing to the same file from multiple windows, it might be easier to have a helper class which exclusively handles writing to file. This would allow you to centralize your File output code to one place making it easier to debug and maintain.
I am writing a DSL for expressing flow (original I know) in groovy. I would like to provide the user the ability to write functions that are stored and evaluated at certain points in the flow. Something like:
states {
"checkedState" {
onEnter {state->
//do some groovy things with state object
}
}
}
Now, I am pretty sure I could surround the closure in quotes and store that. But I would like to keep syntax highlighting and content assist if possible when editing these DSLs. I realize that the closure COULD reference artifacts from the surrounding flow definition which would no longer be valid when executing the closure in a different context, and I am fine with this. In reality I would like to use the closure syntax for a non-closure function definition.
tl;dr; I need to get the closure's code while evaluating the DSL so that it can be stored in the database and executed by a script host later.
I don't think there is a way to get a closure's source code, as this information is discarded during compilation. Perhaps you could try writing an AST transformation that would make closure's syntax tree available at runtime.
If all you care about is storing the closure in the database, and you don't need later access to the source code, you can try serializing it and storing the serialized form.
Closure implements Serializable, and after nulling its owner, thisObject and delegate attributes I was able to serialize it, but I'm getting ClassNotFoundException on deserialization.
def myClosure = {a, b -> a + b}
Closure.metaClass.setAttribute(myClosure, "owner", null)
Closure.metaClass.setAttribute(myClosure, "thisObject", null)
myClosure.delegate = null
def byteOS = new ByteArrayOutputStream()
new ObjectOutputStream(byteOS).writeObject(myClosure)
def serializedClosure = byteOS.toByteArray()
def input = new ObjectInputStream(new ByteArrayInputStream(serializedClosure))
def deserializedClosure = input.readObject() // throws CNFE
After some searching, I found Groovy Remote Control, a library created specifically to enable serializing closures and executing them later, possibly on a remote machine. Give it a try, maybe that's what you need.