Does xlwings only work with the first instance of Excel? - python

Is this a known limitation that will be addressed at some point, or is this just something that I need to accept?
If this is not possible with xlwings, I wonder if any of the other alternatives out there supports connecting to other instances.
I'm specifically talking about the scenario where you are calling python from within Excel, so the hope is that the getCaller() function will be able to figure out which instance of the Excel is actually calling it.

Ok, based on your comments I think I can answer your question: Actually, yes, xlwings can handle various instances. But workbooks from untrusted locations (like downloaded from the internet or sometimes on shared network drives) don't play nicely.
So in your case you could try to add the network location to File > Options > Trust Center > Trust Center Settings... > Trusted Locations or, under Trusted Documents, tick the checkbox Allow documents on a network to be trusted.
If you don't have the previlegies to change these options, then I guess you're left with the options of running the tools locally or indeed, open them in the 1st instance...

Related

How to find if a network interface is using dhcp/static ip without relying on sh (e.g. `ip route`) using python?

I currently need to find out if any network interface (on RHEL) is configured to use dynamic IP (DHCP) using python (python2 preferably, but if there's a solution using python3, I'd like to hear it).
I don't want to throw a bash subprocess and just parse the response from ip route or nmcli for instance.
What we tried/found out so far:
There's a python lib (currently archived) netifaces but even with that we were not able to make it give us that answer. Also having second thoughts of depending on a lib that has been archived a while ago.
On /sys/class/net it's possible to see the interfaces and on it there are a lot of properties, but none of it seems to give what we want. Maybe there's another Linux interface that could give us that?!
On /etc/sysconfig/network-scripts/ifcfg-** it's possible to see options of every interface (NetworkManager is the owner I guess) and one could parse the BOOTPROTO= option. However, that does not necessarily show the current connection method because one could change that file or even modify the interface through nmcli and that would only be read after the interface has been restarted (down/up)
Note: Sorry if some specific network terms were not correctly used, I'm not an expert there :)
Any thoughts? Any ideas?

How to share sensitive data among programs while keeping the possibility of comparing them with other local data?

Context
As part of my studies, I am creating a bot capable of detecting scam messages, in Python 3. One of the problems I am facing is the detection of fraudulent websites.
Currently, I have a list of domain names saved in a CSV file, containing both known domains considered safe (discord.com, google.com, etc.), and known fraudulent domains (free-nitro.ru etc.)
To share this list between my personal computer and my server, I regularly "deploy" it in ftp. But since my bot also uses GitHub and a MySQL database, I'm looking for a better system to synchronize this list of domain names without allowing anyone to access it.
I feel like I'm looking for a miracle solution that doesn't exist, but I don't want to overestimate my knowledge so I'm coming to you for advice, thanks in advance!
My considered solutions:
Put the domain names in a MySQL table
Advantages: no public access, live synchronization
Disadvantages: my scam detection script should be able to work offline
Hash the domain names before putting them on git
Advantages: no public access, easy to do, supports equality comparison
Disadvantages: does not support similarity comparison, which is an important part of the program
Hash domain names with locality-sensitive hashing
Advantages: no easy public access, supports equality and similarity comparison
Disadvantages : similarities less precise than in clear, and impossible to hash a new string from the server without knowing at least the seed of the random, so public access problems
My opinion
It seems to me that the last solution, with the LSH, is the one that causes the least problems. But it is far from satisfying me, and I hope to find better.
For the LSH algorithm, I have reproduced it here (from this notebook). I get similarity coefficients between 10% and 40% lower than those obtained with the current plain method.
EDIT: for clarification purpose, maybe my intentions weren’t clear enough (I’m sorry, English is not my native language and I’m bad at explaining things lol). The database or GitHub are just convenient ways to share info between my different bot instances. I could have one locally running on my pc, one on my VPS, one other god know where… and this is why I don’t want a FTP or any kind of synchronisation process involving an IP and/or a fixed destination folder. Ideally I’d like to just take my program at any time, download it wherever I want (by git clone) and just run it.
Please tell me if this isn’t clear enough, thanks :)
At the end I think I'll use yet another solution. I'm thinking of using the MySQL database to store domain names, but only use it in my script to synchronize to it, keeping a local CSV version.
In short, the workflow I'm imagining:
I edit my SQL table when I want to add/remove items to it
When the bot is launched, the script connects to the DB and retrieves all the information from the table
Once the information is retrieved, it saves it in a CSV file and finishes running the rest of the script
If at launch no internet connection is available, the synchronization to the DB is not done and only the CSV file is used.
This way I have the advantages of no public access, an automatic synchronization, an access even offline after the first start, and I keep the support of comparison by similarity since no hash is done.
If you think you can improve my idea, I'm interested!

pyCharm: safe refactoring information for application on depending code

If I do refactoring in a library pyCharm does handle all depending applications which are known to the current running pyCharm instance.
But code which is not known to the current pyCharm does not get updated.
Is there a way to store the refactoring information in version control, so that depending applications can be updated if they get the update to the new version of the library?
Use Case:
class Server:
pass
gets renamed to
class ServerConnection:
pass
If a team mate updates the code of my library, his usage of Server needs to be changed to ServerConnection.
It would be very nice if pyCharm (or an other tool) could help my team mate to update his code automatically.
As far as I can tell this is not possible neither with a vanilla PyCharm nor with a plugin nor with a 3rd party tool.
It is not mentioned in the official documentation
There is no such plugin in the JetBrains Plugin Repositories
If PyCharm writes refactoring information to it's internal logs, you could build this yourself (but would you really want to?)
I am also not aware of any python specific refactorig tool that does that. You can check for yourself: there is another SO question for the most popular refactoring tools
But ...
I am sure there are reasons why your situation is like it is - there always are good reasons (and most of the time the terms 'historic and 'grown' turn up in explanations of these reasons) but I still feel obligated to point out what qarma already mentioned in his comment: the fact that you want to do something like replaying a refactoring on a different code base points towards a problem that should be solved in a different way.
Alternative 1: introduce an API
If you have different pieces of software that depend on each other on such a deep level, it might be a good idea to define an API that decouples the code bases from each others internals. With an API it is clear which parts have to be stable. If changes have to be done on the API level they must be communicated and coordinated with the involved teams.
Alternative 2: Make it what it actually is: one code base
If A1 for whatever reason is not possible I would conclude that you actually have one system distributed over different code bases and then those should be merged into one code base. Different teams can still work on the same code base (hopefully using a DVCS) but global refactorings can be done with tooling help and they reach all parts of the system.
Alternative 3: Make these refactorings in PyCharm over all involved code bases
Even if you can't merge them into one code base you could combine them easily in PyCharm by loading different projects into the same Window. I do this without problems with two git projects that have to be in different repositories but still share certain aspects. PyCharm handles commits to these repositories transparently: if you make changes in several repositories and commit them you write one commit message and the commits will be done to all repositories.

Virtual File System with FTP

I've been reading through the FileSystem documentation in Python from here:
http://packages.python.org/fs/filesystems.html
After taking the time to read through it and a bit of Google-ing I didn't quite find the answer I was looking for. I was wondering if it was possible with Python, and avoiding any UAC issues on Windows, if you could 'mount' or display a file free from an FTP server inside Windows Explorer for any other applications to -read- from.
Is something this possible? What's the best approach to achieve it? Thanks in advance!
Windows explorer has built-in support for FTP, instructions with screenshots here.
Open Windows Explorer (My computer)
Right-click anywhere in the folder, and then click Add a Network Location.
This displays the Add Network Connection wizard. Click Next.
In the wizard, select Choose a custom network location, and then click Next.
Enter the name of the FTP site, with the full FTP:// in front of it, and then click Next (something like - for example if you domain name is DOMAIN.NET the full name should look like this: ).
To use a name and password, clear the Log on anonymously check box. Type your FTP Account Username, and then click Next.
By default, the name of the shortcut is the same as the FTP address. If you want to give the shortcut a different name, type it in the Type a name for this network location box. Click Next.
You can even use it from the standard "file" dialog box. Is it enough?
[update]
Out of curiosity, does this have support for FTPS and/or SFTP? – André Caron
#AndréCaron: I think WebDAV with SSL is supported, but no native support for SFTP or FTPS. There are extensions like Swish (opensource) or Expandrive (payware).
This is a non-trivial matter and afaik there exist not such a thing as provide os-level functionality through some lines of magical python code.
A possible solution to emulate your intention could be to create a local webdav proxy, which maps to the ftp-server. I know there exists a feature to attach a webdav-network device to a local drive-letter, but because the last time i touched a windows system was ... - i can't even remember - i can't tell you if you might also attach a ftp-resource this way directly.
I guess looking for a solution on a python level might not be productive in a short-term perspective and even on mid/long-term's not possible without heavy tinkering on your side.
While not a "filesystem" per-se, you can provide a Windows Explorer Namespace Extension which will allow the Windows explorer to browse any virtual file-system. This is used to implement browsing of ZIP archives, for example. Note that this does not provide a real file system drive, so it will not allow you to open the contents of this namespace extension using regular file I/O functions.
The namespace extensions are written in COM, and it is possible to implement COM interfaces in Python.
AFAIK, the only permissions you need are for installing of the namespace extension (the COM DLL, plus the namespace registration). Everything else runs in the logged-in user's context and requires no special permissions.

Is there a better way (besides COM) to remote-control Excel?

I'm working on a regression-testing tool that will validate a very large number of Excel spreadsheets. At the moment I control them via COM from a Python script using the latest version of the pywin32 product. Unfortunately COM seems to have a number of annoying drawbacks:
For example, the slightest upset seems to be able to break the connection to the COM-Server, once severed there seems to be no safe way to re-connect to the Excel application. There's absolutely no safety built into the COM Application object.
The Excel COM interface will not allow me to safely remote-control two seperate instances of the Excel application operating on the same workbook file, even if they are read-only.
Also when something does go wrong I seldom get any useful error-messages... at best I can except a numerical error-code or a barely useful message such as "An Exception has occurred". It's almost impossible to know why something went wrong.
Finally, COM lacks the ability to control some of the most fundamental aspects of Excel? For example there's no way to do a guaranteed close of just the Excel process that a COM client is connected to. You cannot even use COM to find Excel's PID.
So what if I were to completely abandon COM? Is there an alternative way to control Excel?
All I want to do is run macros, open and close workbooks and read and write cell-ranges? Perhaps some .NET experts know a trick or two which have not yet bubbled into the Python community? What about you office-hackers? Could there be a better way to get at Excel's innards than COM?
There is no way that completely bypasses COM. You can use VSTO (Visual Studio Tools for Office), which has nice .NET wrappers on the COM objects, but it is still COM underneath.
The Excel COM interface will not allow me to safely remote-control two seperate instances of the Excel application operating on the same workbook file, even if they are read-only.
This is not a limitation of COM, this is a limitation of Excel. Excel will not even let you open two files with the same name at the same time if they exist in different directories. It is a fundamental limitation of the Excel program.
To answer your other questions
If you check your python documentation, there should be a way to connect to an existing server if the connection is lost.
The lack of useful error messages again may be to do with Python.
You cannot even use COM to find Excel's PID.
COM is an internal object model and exposed what it wishes. PID are available to outside processes as much as they are to internal, there is no real reason to expose as a COM interface.
It is also possible to run Excel as a server application and use it as a calculation engine. This allows non IT users to specify business rules within Excel and call them through webservices. I have not worked with this myself, but I know a coworker of mine used this once. Walkthrough: Developing a Custom Application Using Excel Web Services could be a good starting point. A first glance at that page looks like it requires Sharepoint. This might not be suiteable for every environment.
Have you looked at the xlrd and xlwt packages? I'm not in need of them any more, but I had good success with xlrd on my last project. Last I knew, they couldn't process macros, but could do basic reading and writing of spreadsheets. Also, they're platform independent (the program I wrote was targetted to run on Linux)!
You could use Jython with the JExcelApi (http://jexcelapi.sourceforge.net/) to control your Excel application. I've been considering implementing this solution with one of my PyQt projects, but haven't gotten around to trying it yet. I have effectively used the JExcelApi in Java applications before, but have not used Jython (though I know you can import Java classes).
NOTE: the JExcelApi may be COM under the hood (I'm not sure).

Categories