Delete INI section in Python - python

In python, I'm using win32api to read and write INI settings as and when they are required into specific sections based on IDs.
Once I have finished with the section, I want to be able to delete the entire section from the INI file. How would I go about doing this using the win32api python library?

My proposal is to use native Python module for handling INI files ConfigParser. Have you tried it already? It has remove_section method that probably will work for you.

This is not a Python specific answer, but look into calling WritePrivateProfileString with the lpKeyName parameter set to NULL. With this parameter set to NULL the entire section named in lpAppName, including all entries within the section, is deleted.

Related

Can I specify an absolute location for UDF_Modules within xlwings?

I'm using xlwings to expose python functions as user defined functions within Excel. It works perfectly if the excel file is in the same directory as the .py file which contains my UDF functions.
I would like to save my Excel file anywhere and just update my xlwings.conf file to have the location of the python module which contains the udf definitions.
If I set the conf file to have
"UDF MODULES","C:\src\xlwings_wrapper\xlwings_udfs"
I get the following error ModuleNotFound: No module named 'C:\src\xlwings_wrapper\xlwings_udfs'. How ever I have checked and the xlwings_udfs.py file is in that location.
Does anyone know if setting an absolute path for the UDF Modules is supported by xlwings?
Thanks
David
Have a look at the docs where the settings are explained: The UDF_MODULES only accept the module name. The path to where the modules are is set (and explained) under PYTHONPATH. So in your example you have to set the following:
"UDF MODULES","name_of_module"
"PYTHONPATH","C:\src\xlwings_wrapper\xlwings_udfs"
I made the same mistake. Should have read the manual
For completeness, when using either the xlwings.conf sheet or the UDF module box on the ribbon you need to adopt this setting configuration.

using :ref: in Python docstrings using Sphinx

I'm using Sphinx to document a python project, and I'm trying to create a reusable tip to be used in several locations.
Typically, I'll use the following syntax in a python file:
"""
.. tip::
I want this tip to be used in several locations. Why?
- Save time
- Work less
"""
Now this works whether I put it at the beginning of the file, right under class definition or right under function definition.
I found Sphinx's manual for :ref:, which suggests to use a label:
.. _my_reusable_tip:
.. tip::
...
And then call this tip with :ref:`my_reusable_tip` anywhere I want.
The manual states that 'it works across files, when section headings are changed, and for all builders that support cross-references'
The thing is, it doesn't matter in which .py file in the project I write the label and tip definition, the :ref:`my_reusable_tip` just displays 'my_reusable_tip', and not the tip itself.
What I'm using to build the documentation is
sphinx-apidoc -f -F -o
make html
I'm pretty sure my logic is flawed in some way, but I can't figure out why.
I know that Sphinx searches the project for reStructuredText and renders it if it can, but I think I'm missing something here.
I tried to add this label in a seperate .py file enclosed in """, and in a separate .txt file without enclosed """.
I tried creating an .rst file with the label definition and rebuild the html documentation.
What am I missing here?
Python 3.4.3 BTW.
In sphinx, a :ref: is simply a more robust way of linking (or referencing) another part of the document. Thus, your use of :ref: will simply provide a hyperlink to the label.
It is not a way of substituting or expanding a block.
Inline substitutions are available using using |...|, however an inline substitution cannot be used to substitute a block as you seem to require.
RestructuredText is not a template language, and thus doesn't provide macro like facilities. In the event you need it, an alternative solution is to use a template library such as mako or jinja to deal with this kind of issue.
Just using reStructuredText directive
.. include:: ./my_reusable_tip.txt
in your rst files?

How do I deal with conflicting names when building python docs with doxygen

I'm having a problem with Doxygen for Windows with Python where input files with the same failename cause a conflict wth the output files. This seems to be a bug in doxygen - is there a way to work-around this problem?
Background
We build docs for our API using Doxygen. Our project is overwhelmingly written in python and the only components that our clients care about are python. Due to accidents of history our classes often have unfortunate naming conventions.
For example we have a classes whose fully-qualified name are:
tools.b.foo.Foo
tools.b.bar.Bar
Later this class was re-implemented and put into a new module:
tools.c.foo.Foo_improved
tools.c.bar.Bar_improved
When we want to build our tools API documentation we have a process which checks out tools.* into a directory on the build-server and then we call doxygen with a fairly standard configuration file.
We'd expect that there should be four HTML files in the output, two for foo and two for bar. However what we get is only two files. Both sets of sripts are parsed, however since the module names are the same the documentation for the old version ends up over-writing the documentation which was generated for the new versions. As a result in every case where a python module name is duplicated (but in a different sub-package) we are only getting a single doc file for every file name.
FYI, we are using doxygen 1.7.1 on Windows XP 32bit with Python 2.4.4
Config file is here:
http://pastebin.me/002f3ec3145f4e1896a9cf79e7179493
UPDATE 1: In the generated doc index I can see entries for all four files, however if I follow the links to both Foo and Foo_improved both point to the same file.
You could try explicitly declaring a class w/ full namespace
http://www.doxygen.nl/manual/commands.html#cmdclass

Retrieve header information from exe

I was wondering whether it would be possible to write a python script that retrieves header information from an .exe file. I tried googling but didn't really find any results that were usable.
Thanks.
Sept
There is pefile : multi-platform Python module to read and work with Portable Executable (aka PE) files. Most of the information in the PE Header is accessible, as well as all the sections, section's information and data.
Looks like I'm almost 2 years and a dollar short! If you still need to solve this, MichaƂ Niklas was right on point above. pefile was written for this very purpose. Here is an example from my interactive session:
ipython
import pefile
pe = pefile.PE('file.exe')
pe.print_info()
The output is too verbose to put up here, but the above gives you all header information from a PE.
Download pefile here: pefile
Of course it is possible to write a Python script to retrieve header information from an XYZ file. Three simple steps:
(1) Find docs for the header part of an XYZ file; read them.
(2) Read the docs for the Python struct module or ctypes module or both.
(3) Write and test the script.
Which step are you having trouble with?

reading a configuration information only once in Python

I'm using the ConfigParser to read the configuration information stored in a file. I'm able to read the content and use it across other modules in the project. I'm not sure if the configuration file is read every time I call config.get(parameters). How can I make sure that the configuration information is read only once and rest of the time its read from the cache.
I would try assigning the configuration to a variable.
configVariable = config.get(parameters)
Then you can pass the configuration variable to other modules as necessary.
The default implementation of the ConfigParser class reads its data only once.

Categories

Resources