Avoid syntax error using hyphens in Python - python

I am writing a python script to automate some simulations with Aspen Plus via its COM functions. But when I want to get access to molecular weights values, I have to write something like this:
import os
import win32com.client as win32
aspen = win32.Dispatch('Apwn.Document')
aspen.InitFromArchive2(os.path.abspath('Aspen\\Flash.bkp'))
MW = aspen.Tree.Data.Properties.Parameters.Pure Components.REVIEW-1.Input.VALUE.MW ACID.Value
But it launchs a syntax error in REVIEW-1, due to hyphens can not be used as identifiers. How can I use them like that?
EDIT:
I replaced dot synax for FindNode function of Aspen COM like that:
MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW-1")
But I still get a None object, however this:
MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components")
works, getting the "COMObject FindNode" so I think that the problem is in the hyphen as well.
Thanks in advance!

Thanks for the tip
For cases of having hyphens, the following should work instead of escaping the "\" character:
MW = aspen.Tree.FindNode(r'\Data\Properties\Parameters\Pure Components\REVIEW-1\Input\VALUE')

Ok, I was breaking my head trying to solve it in Python, but was easier solving it in Aspen renaming the node. I've noticed, also, that spaces sometimes give problems too, so should be renamed as well. In some cases it can't be done or I don't know how, for example:
MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW1\\Input\\VALUE\\MW ACID")
It returns a None object and I don't know hot to rename "MW ACID", but there is a tricky way to get the value:
MW = aspen.Tree.FindNode("\\Data\\Properties\\Parameters\\Pure Components\\REVIEW1\\Input\\VALUE")
for o in MW.Elements:
if o.Name == "MW ACID":
MW_acid = o.Value
For now it works for me, however it will be slower due to iteration. So if someone knows how to solve the problem in Python without renaming names, it will be still helpful. I tried to use unicode and bytes notation for non-breaking hyphen but it didn't works too.
Regards!

Related

How to avoid Python to transform backlash (\) in less-than sign (<) in directory address?

I have a folder, which I'm trying to input in Python as a string, for example folder = r'C:\Users\Desktop' or folder = 'C:\\Users\\Desktop'
I'm then using pyautogui.typewrite(folder), but when I do so the directory name is pasted as C'<Users<Desktop
Do you know what is causing that and how can I solve this issue?
Thank you!
[Solution]
Combining both inputs from "SuperStormer" (thanks a lot!!) along with some additional research and adaptation I manage to solve the issue by:
Change the keyboard to English - looks like pyautogui emulates the typing of the keyboard, and it doesn't support any language other than english - this almost solved my issue, it solved the backslashes, but then the ":" started to become a "?", like 'C?\Users\Desktop'
Use pyperclip to paste string directly from keyboard - I did some simulations and that would have worked, except that I already had something on my clipboard that had to be used in a later step, and as I couldn't find a way to reference more than two clipboards at a time that didn't work right away, but after some workarounds based on that function I finally got to the solution

Python strip unexpected behavior

I was stripping a file name in python for routing purposes and I was getting some unexpected behavior with the python strip function. I've read the docs and searched online but have not been able to find an explanation for the following behavior:
"Getting-Started.md".strip('.md')
Out[29]: 'Getting-Starte'
But if it is any other character aside from 'd' to the left of the period, it works properly:
"Getting-StarteX.md".strip('.md')
Out[30]: 'Getting-StarteX'
It seems like there is something similar to a mirroring going on 'd. md'. I'm doing a double strip to get by this for now, but I was just curious of why this occurs.
Thank you.
strip() would strip all the characters provided in the argument - in your case ., m and d.
Instead, you can use os.path.splitext():
import os
os.path.splitext("Getting-StarteX.md")[0]
If there is only one ".md" appearing at the end of the testing string, you can also use
"Getting-Started.md".split('.md')[0]
Thanks #Carpetsmoker remind me the assumption.

Convert UNC path to local path (and general path handling in Python)

System: Python 2.6 on Windows 7 64 bit
Recently I did a lot of path formatting in Python. Since the string modifications are always dangerous I started to do it the proper way by using the 'os.path' module.
The first question is if this is the right way to to handle incoming paths? Or can I somehow optimize this?
sCleanPath = sRawPath.replace('\n', '')
sCleanPath = sCleanPath.replace('\\', '/')
sCleanPythonPath = os.path.normpath(sCleanPath)
For formatting the 'sCleanPythonPath' now I use only functions from the 'os.path' module. This works very nice and I didn't had any problems so far.
There is only one exception. I have to change paths so they point not longer on a network storage but on a local drive. Is started to use 'os.path.splitunc()' in conjunction with 'os.path.join()'.
aUncSplit = os.path.splitunc(sCleanPythonUncPath)
sLocalDrive = os.path.normpath('X:/mount')
sCleanPythonLocalPath = (os.path.join(sLocalDrive, aUncSplit[1]))
Unfortunately this doesn't work due to the nature of how absolute paths are handled using 'os.path.join()'. All the solutions I found in the web are using string replacements again which I want to avoid by using the 'os.path' module. Have I overseen anything? Is there another, maybe a better way to do this?
All comments on this are very welcome!
You could optimize the first part slightly by removing the replace() call because on Windows normpath() converts forward slashes to backward slashes:
sCleanPath = sRawPath.replace('\n', '')
sCleanPythonPath = os.path.normpath(sCleanPath)
Here's something that would make the second part of your question work without doing a string replacement:
sSharedFolder = os.path.relpath(os.path.splitunc(sCleanPythonUncPath)[1], os.sep)
sLocalDrive = os.path.normpath('X:/mount') # why not hardcode the result?
sCleanPythonLocalPath = os.path.join(sLocalDrive, sSharedFolder)

How do I handle Python unicode strings with null-bytes the 'right' way?

Question
It seems that PyWin32 is comfortable with giving null-terminated unicode strings as return values. I would like to deal with these strings the 'right' way.
Let's say I'm getting a string like: u'C:\\Users\\Guest\\MyFile.asy\x00\x00sy'. This appears to be a C-style null-terminated string hanging out in a Python unicode object. I want to trim this bad boy down to a regular ol' string of characters that I could, for example, display in a window title bar.
Is trimming the string off at the first null byte the right way to deal with it?
I didn't expect to get a return value like this, so I wonder if I'm missing something important about how Python, Win32, and unicode play together... or if this is just a PyWin32 bug.
Background
I'm using the Win32 file chooser function GetOpenFileNameW from the PyWin32 package. According to the documentation, this function returns a tuple containing the full filename path as a Python unicode object.
When I open the dialog with an existing path and filename set, I get a strange return value.
For example I had the default set to: C:\\Users\\Guest\\MyFileIsReallyReallyReallyAwesome.asy
In the dialog I changed the name to MyFile.asy and clicked save.
The full path part of the return value was: u'C:\Users\Guest\MyFile.asy\x00wesome.asy'`
I expected it to be: u'C:\\Users\\Guest\\MyFile.asy'
The function is returning a recycled buffer without trimming off the terminating bytes. Needless to say, the rest of my code wasn't set up for handling a C-style null-terminated string.
Demo Code
The following code demonstrates null-terminated string in return value from GetSaveFileNameW.
Directions: In the dialog change the filename to 'MyFile.asy' then click Save. Observe what is printed to the console. The output I get is u'C:\\Users\\Guest\\MyFile.asy\x00wesome.asy'.
import win32gui, win32con
if __name__ == "__main__":
initial_dir = 'C:\\Users\\Guest'
initial_file = 'MyFileIsReallyReallyReallyAwesome.asy'
filter_string = 'All Files\0*.*\0'
(filename, customfilter, flags) = \
win32gui.GetSaveFileNameW(InitialDir=initial_dir,
Flags=win32con.OFN_EXPLORER, File=initial_file,
DefExt='txt', Title="Save As", Filter=filter_string,
FilterIndex=0)
print repr(filename)
Note: If you don't shorten the filename enough (for example, if you try MyFileIsReally.asy) the string will be complete without a null byte.
Environment
Windows 7 Professional 64-bit (no service pack), Python 2.7.1, PyWin32 Build 216
UPDATE: PyWin32 Tracker Artifact
Based on the comments and answers I have received so far, this is likely a pywin32 bug so I filed a tracker artifact.
UPDATE 2: Fixed!
Mark Hammond reported in the tracker artifact that this is indeed a bug. A fix was checked in to rev f3fdaae5e93d, so hopefully that will make the next release.
I think Aleksi Torhamo's answer below is the best solution for versions of PyWin32 before the fix.
I'd say it's a bug. The right way to deal with it would probably be fixing pywin32, but in case you aren't feeling adventurous enough, just trim it.
You can get everything before the first '\x00' with filename.split('\x00', 1)[0].
This doesn't happen on the version of PyWin32/Windows/Python I tested; I don't get any nulls in the returned string even if it's very short. You might investigate if a newer version of one of the above fixes the bug.
ISTR that I had this issue some years ago, then I discovered that such Win32 filename-dialog-related functions return a sequence of 'filename1\0filename2\0...filenameN\0\0', while including possible garbage characters depending on the buffer that Windows allocated.
Now, you might prefer a list instead of the raw return value, but that would be a RFE, not a bug.
PS When I had this issue, I quite understood why one would expect GetOpenFileName to possibly return a list of filenames, while I couldn't imagine why GetSaveFileName would. Perhaps this is considered as API uniformity. Who am I to know, anyway?

In Python what's the best way to emulate Perl's __END__?

Am I correct in thinking that that Python doesn't have a direct equivalent for Perl's __END__?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
One thought that occurred to me was to use a triple-quoted string. Is there a better way to achieve this in Python?
print "Python..."
"""
End of code. I can put anything I want here.
"""
The __END__ block in perl dates from a time when programmers had to work with data from the outside world and liked to keep examples of it in the program itself.
Hard to imagine I know.
It was useful for example if you had a moving target like a hardware log file with mutating messages due to firmware updates where you wanted to compare old and new versions of the line or keep notes not strictly related to the programs operations ("Code seems slow on day x of month every month") or as mentioned above a reference set of data to run the program against. Telcos are an example of an industry where this was a frequent requirement.
Lastly Python's cult like restrictiveness seems to have a real and tiresome effect on the mindset of its advocates, if your only response to a question is "Why would you want to that when you could do X?" when X is not as useful please keep quiet++.
The triple-quote form you suggested will still create a python string, whereas Perl's parser simply ignores anything after __END__. You can't write:
"""
I can put anything in here...
Anything!
"""
import os
os.system("rm -rf /")
Comments are more suitable in my opinion.
#__END__
#Whatever I write here will be ignored
#Woohoo !
What you're asking for does not exist.
Proof: http://www.mail-archive.com/python-list#python.org/msg156396.html
A simple solution is to escape any " as \" and do a normal multi line string -- see official docs: http://docs.python.org/tutorial/introduction.html#strings
( Also, atexit doesn't work: http://www.mail-archive.com/python-list#python.org/msg156364.html )
Hm, what about sys.exit(0) ? (assuming you do import sys above it, of course)
As to why it would useful, sometimes I sit down to do a substantial rewrite of something and want to mark my "good up to this point" place.
By using sys.exit(0) in a temporary manner, I know nothing below that point will get executed, therefore if there's a problem (e.g., server error) I know it had to be above that point.
I like it slightly better than commenting out the rest of the file, just because there are more chances to make a mistake and uncomment something (stray key press at beginning of line), and also because it seems better to insert 1 line (which will later be removed), than to modify X-many lines which will then have to be un-modified later.
But yeah, this is splitting hairs; commenting works great too... assuming your editor supports easily commenting out a region, of course; if not, sys.exit(0) all the way!
I use __END__ all the time for multiples of the reasons given. I've been doing it for so long now that I put it (usually preceded by an exit('0');), along with BEGIN {} / END{} routines, in by force-of-habit. It is a shame that Python doesn't have an equivalent, but I just comment-out the lines at the bottom: extraneous, but that's about what you get with one way to rule them all languages.
Python does not have a direct equivalent to this.
Why do you want it? It doesn't sound like a really great thing to have when there are more consistent ways like putting the text at the end as comments (that's how we include arbitrary text in Python source files. Triple quoted strings are for making multi-line strings, not for non-code-related text.)
Your editor should be able to make using many lines of comments easy for you.

Categories

Resources