Python application menu - linux

I'm writing an application menu in Python for a linux Desktop Environment I'm creating, and I'm wondering if there is a module that will allow me to read .desktop files, and create a Dict/Object from the results.
If there isn't, I would like some tips on how to go about writing this myself.

The files you are referring to (.desktop files) are structured similarly to .ini files for many applications, as seen in this example on the Arch Linux Wiki:
[Desktop Entry]
Type=Application # Indicates the type as listed above
Version=1.0 # The version of the desktop entry specification to which this file complies
Name=jMemorize # The name of the application
Comment=Flash card based learning tool # A comment which can/will be used as a tooltip
Exec=jmemorize # The executable of the application.
Icon=jmemorize # The name of the icon that will be used to display this entry
Terminal=false # Describes whether this application needs to be run in a terminal or not
Categories=Education;Languages;Java; # Describes the categories in which this entry should be shown
Sections are headed by [Bracketed Text], and everything else is key-value pairs separated by = (I have also seen :).
Python has the built-in ConfigParser module to handle these. That link is the technical documentation but the PythonWiki has a page with simpler examples. This library does precisely what you asked for: it reads files formatted like .desktop files into objects and dicts.
The first example on the wiki shows this clearly:
>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("c:\\tomorrow.ini")
['c:\\tomorrow.ini']
>>> Config.sections()
['Others', 'SectionThree', 'SectionOne', 'SectionTwo']
We import ConfigParser (renamed to configparser in Python3), instantiate it, and tell it to read the file. Config.sections() is now a list containing all the section headers [Bracketed Text].
There are various flavors of ConfigParser objects depending on your needs. The basic one has methods like getint, getboolean, etc., which take a section and an option and try to return it coerced into the specified Python object. There are also methods like items and defaults to return the items and defaults respectively from a section given as an argument.
I don't know enough personally about .desktop files to know what sorts of things you might encounter or how exactly you need to configure the parser, but this should get you started.

Use the built in open() function with parameters set to read. If you have trouble using the function consider having a look at the python reference Python Library Reference, open() function. There you'll find a comprehensive explanation of how to use the function and it's parameters. Youtube and Google will also give you enough tuts on how to read and write to files in python.
Hope this solves your question.

Related

How to create a logfile when calling CPLEX from python

In the website of https://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.2/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/tutorials/InteractiveOptimizer/solnOptions.html
I knew that when CPLEX solved a problem,it will create a logfile named as "cplex.log", but when I use CPLEX to solve a problem in python, this file wasn't created. And I'm so confused about that if this problem have some matters with the difference of languages. I mean, when using CPLEX to solve a problem in MATLAB, Java or C++, logfile will be created, but not created in python.
I'm expecting for your help.Thanks so much.
The cplex.log file is specific to the CPLEX interactive. It is not created automatically when using the other APIs (e.g., Python, Java, etc.). However, you can create it yourself doing the following (e.g., with the Python API):
cpx = cplex.Cplex()
cplexlog = "cplex.log"
cpx.set_results_stream(cplexlog)
cpx.set_warning_stream(cplexlog)
cpx.set_error_stream(cplexlog)
cpx.set_log_stream(cplexlog)
The argument to the set_*_stream methods can be a path (as above) or a file-like object, so you can do pretty much anything you want (e.g., implement a file-like object to display output on stdout but also write it to a log file, etc.). See the documentation for set_results_stream for more details.
NOTE: There is some output that gets displayed from the interactive that is not available in the other APIs. However, you should be able to recreate it easily as all of the information is available through the programmatic APIs.
EDIT:
With CPLEX 12.10 using a filename with the set_results_stream, set_warning_stream, set_error_stream, and set_log_stream methods has been removed (see announcement here). Instead, a file-like object should be passed in, like so:
with cplex.Cplex() as cpx, \
open("cplex.log") as cplexlog:
cpx.set_results_stream(cplexlog)
cpx.set_warning_stream(cplexlog)
cpx.set_error_stream(cplexlog)
cpx.set_log_stream(cplexlog)
...

How to get SMAC3 working for Python 3x on Windows

This is a great package for Bayesian optimization of hyperparameters (especially mixed integer/continuous/categorical...and has shown to be better than Spearmint in benchmarks). However, clearly it is meant for Linux. What do I do...?
First you need to download swig.exe (the whole package) and unzip it. Then drop it somewhere and add the folder to path so that the installer for SMAC3 can call swig.exe.
Next, the Resource module is going to cause issues because that is only meant for Linux. That is specifically used by Pynisher. You'll need to comment out import pynisher in the execute_func.py module. Then, set use_pynisher:bool=False in the def __init__(self...) in the same module. The default is true.
Then, go down to the middle of the module where an if self.use_pynisher....else statement exists. Obviously our code now enters the else part, but it is not setup correctly. Change result = self.ta(config, **obj_kwargs) to result = self.ta(list(config.get_dictionary().values())). This part may need to be adjusted yet depending on what kind of inputs your function handles, but essentially you can see that this will enable the basic example shown in the included branin_fmin.py module. If doing the random forest example, don't change at all...etc.

How to share a variable between 2 pyRevit scripts?

I am using the latest version of pyRevit, v45.
I'm writing some info in temporary files with
myTempFile = script.get_instance_data_file("id")
This creates a file named pyRevit_2018_xxxx_id.tmp in which I store useful info. If I'm not mistaken, the "xxxx" part is changing every time I reload Revit. Now, I need to get access to this information from another pyRevit script.
How can I retrieve the name of the temp file I need to read? In other words, how do I access "myTempFile" from within the second script, which has no idea of the name of "myTempFile"?
I guess I can share somehow that variable between my script, but what's the proper way to do this? I know this must be a very basic programming question, but I'm indeed not a programmer ;)
Thanks a lot,
Arnaud.
Ok, I realise now that my variables in the 1st script cease to exist after its execution.
So for now I wrote the file name in another file, of which I know the name.. That works.
But if there's a cleaner way to do this, I'd be glad to learn ;)
Arnaud
pyrevit.script module provides 4 different methods for creating temporary files based on their use case:
get_instance_data_file:
for data files marked with Revit instance pid. This means that scripts running on another instance will not see this temp file.
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_instance_data_file
get_universal_data_file:
for temp files accessible to all Revit instances and versions
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_universal_data_file
get_data_file:
Base method to get a standard temp file for current revit version
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_data_file
get_document_data_file:
temp file marked with active document (so scripts working on another document will not see this)
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_document_data_file
Each method uses a pattern to create the temp file name. So as long as the call to the method is the same of different scripts, the method generates the same file name.
Example:
Script 1:
from pyrevit import script
tfile = script.get_data_file('mydata')
Script 2:
from pyrevit import script
tempfile = script.get_data_file('mydata')
In this example tempfile = tfile since the file id is the same.
There is documentation on each so make sure you take a look at those and pick the flavor that serves your purpose.

Import data to OMNET++

I am trying to model a network in OMNET++. What I have is a text file (can be in an Excel file format) with nodes' names, list of interfaces, and interface connections. What I like to do is to write a program (perhaps a plug-in) to feed this file to OMNET++ and (automatically) create .ned and .cc based on this file. The rationale is that there is a long list of nodes/interfaces, that makes it difficult to do it manually, and possibly a change in the connections makes it difficult to recreate it, undelss it is done automatically. Could you point to some links/websites/documents, so that I learn how to write a plugin to read the information and create the nodes and their connections automatically? Obviously, the node types and characteristics could be modified in the plugin as necessary later.
An example is like:
(some other information there)...
cr1.atl-cr1.hst cr1.atl cr1.hst 2488
cr1.kcy-cr1.wdc cr1.kcy cr1.wdc 2488
cr1.atl-cr2.atl cr1.atl cr2.atl 10000
cr2.atl-cr1.wdc cr1.wdc cr2.atl 2488
...
where the second column is the source node, the third column is the destination node, and the first column is the link (firstNode-secondNode). The 4th column is the capacity/delay or other information of the link.
If you want this to be as flexible as possible, I would recommend writing a small Python script that reads a .csv file and renders .ned files as needed.
You might even consider using a templating engine like Mako. Quoting from its website, Mako is pretty straightforward to use:
from mako.template import Template
print(Template("hello ${data}!").render(data="world"))

setupcon use a variant as default

Context
I'm building my complete debian system configuration,
so I'm modifying the keyboard and console setups.
I prefer not to modify the base files to keep a maximum
commpatibility and modularity. So I want to use VARIANT
(see setupcon (5)) and load them at init.
But not sure I'm doing it right.
Desired Architecture
I will only use keyboard file for the following example.
There is the base file /etc/default/keyboard
And two possible custom files (according to setupcon (5))
~/.keyboard
/etc/default/keyboard.variant
~/.keyboard
It provides a custom behaviour per $HOME (user)
/etc/default/keyboard.variant
A global and default keyboard setup
I would like to use the three at a time.
Problem
The daemon calling setupcon are console-setup and console-setup-mini
(according to the coments in their initd scripts). They are started
before login shell, so won't know ~/.keyboard.
setupcon needs to be called
setupcon variant
or, looking at the sources, with a variable $VARIANT
VARIANT=variant
What is the best solution to adopt, saving a maximum modularity.
Thank you,

Resources