Mac System name - python

I'm trying to write a program in python that uses commands and files that are os dependent and I don't have access to a mac. What is the exact system name for the os.name() command?
Better yet what is the platform.system() answer. I have windows which of course is 'windows' and I have Linux which in my case is 'Linux' but what is mac? So right now I need the output for os.name() and platform.system(). Preferably a more recent version of OS X like either mountain lion or Maverick.

The platform name for all OS X releases is Darwin:
Python 2.7.6 (default, Apr 28 2014, 17:17:35)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'
platform.system() uses os.uname(), and you can look up responses on the uname Wikipedia page.

Related

Accessing Python Modules Installed with Brew

I am trying to access "rdkit" module in python, but my python install doesn't seem to find the module. I installed it using brew as suggested here. I included the commands I use to reproduce the problem.
Is a simple fix just adding the "/usr/local/Cellar" to the python path? Symlinc the site-packages to the cellar? Candidly I did not understand the difference between pip and brew when updating my python, so my first thought is that the python path is not correct. Any suggestions greatly appreciated.
X:~\ python3 Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import rdkit Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'rdkit'
X:~\ which python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
X:~\ python3
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
**#:/usr/local/Cellar\** ls
boost/ gettext/ libffi/ numpy/ sip/
boost-python3/ glew/ libidn2/ openblas/ sqlite/
cmake/ glib/ libmpc/ openssl#1.1/ tcl-tk/
ddclient/ glm/ libpng/ pandoc/ tree/
eigen/ gmp/ libtiff/ pcre/ webp/
fontconfig/ graphviz/ libtool/ pkg-config/ wget/
freeglut/ gts/ libunistring/ pyqt/ xz/
freetype/ icu4c/ mmtf-cpp/ python/
gcc/ isl/ mpfr/ qt/
gd/ jasper/ msgpack/ **rdkit/**
gdbm/ jpeg/ netpbm/ readline/
Details:
OS Catalina: 10.15.3

Using Appkit and Python to hide mouse cursor on OSX

I'm trying to script hiding the mouse cursor on OSX 10.9. I have Chrome starting and going full screen for a kiosk and I'd like to periodically run a script to hide the cursor.
Applescript no longer directly supports "call method" to call the objective C method, so I thought the simplest method would be to use AppKit from the provided python.
It crashes:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AppKit
>>> AppKit.NSCursor.hide()
Assertion failed: (CGAtomicGet(&is_initialized)), function CGSConnectionByID, file Services/Connection/CGSConnection.c, line 123.
Abort trap: 6
I suspect there is a pre-requisite call I need to make to initialize something, but I haven't found anything yet while digging through docs/google.
What am I missing?
I've had luck with the Quartz included with PyObjC:
import Quartz
Quartz.CGDisplayHideCursor(Quartz.CGMainDisplayID())

Python treat files with uppercase and lowercase names the same

I just found out this today:
If I have an existing file named a111, and I want to create a new file named A111 in the same directory with Python:
f = file('A111', 'w')
f.write('test')
f.close()
It will overwrite my file a111 and there's no A111!!
How do I prevent this from happening?
It is not due to python. It is due to the case-insensitivity of your underlying file system (I'm guessing HFS+ in your case?). From wikipedia:
Not all file systems in Unix-like systems are case-sensitive; by default, HFS+ in Mac OS X is case-insensitive
The solution is to use a case-sensitive file system, if you want one, or to use a different filename!
This does reproduce for me, actually.
nixon:~ matt$ touch a111
nixon:~ matt$ python
Python 2.7.2 (default, Nov 14 2011, 19:37:59)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f = file('A111', 'w')
>>> f.write('test')
>>> f.close()
>>>
nixon:~ matt$ cat a111
test
Also on a mac.
nixon:~ matt$ uname -a
Darwin nixon.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
nixon:~ matt$ python --version
Python 2.7.2
I suspect you'll find that what's going on is that we're both using HFS, which is a case-insensitive filesystem.
A Mac's HFS+ filesystem is case-insensitive by default, unless you perform an installation from scratch - one of the installation options is turning on the case sensitivity.

Make Emacs use UTF-8 with Python Interactive Mode

When I start Python from Mac OS' Terminal.app, python recognises the encoding as UTF-8:
$ python3.0
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
This works the same for python2.5.
But inside Emacs, the encoding is US-ASCII.
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'US-ASCII'
How do I make Emacs communicate with Python so that sys.stdout knows to use UTF-8?
Edit: Since I don't have the rep to edit the accepted answer, here is precisely what worked for me on Aquaemacs 1.6, Mac OS 10.5.6.
In the python-mode-hook, I added the line
(setenv "LANG" "en_GB.UTF-8")
Apparently, Mac OS requires "UTF-8", while dfa says that Ubuntu requires "UTF8".
Additionally, I had to set the input/output encoding by doing C-x RET p and then typing "utf-8" twice. I should probably find out how to set this permanently.
Thanks to dfa and Jouni for collectively helping me find the answer.
Here is my final python-mode-hook:
(add-hook 'python-mode-hook
(lambda ()
(set (make-variable-buffer-local 'beginning-of-defun-function)
'py-beginning-of-def-or-class)
(define-key py-mode-map "\C-c\C-z" 'py-shell)
(setq outline-regexp "def\\|class ")
(setenv "LANG" "en_GB.UTF-8"))) ; <-- *this* line is new
check your environment variables:
$ LANG="en_US.UTF8" python -c "import sys; print sys.stdout.encoding"
UTF-8
$ LANG="en_US" python -c "import sys; print sys.stdout.encoding"
ANSI_X3.4-1968
in your python hook, try:
(setenv "LANG" "en_US.UTF8")

Why do I get wrong results for hmac in Python but not Perl?

I'm trying to compute hmac using sha-512.
The Perl code:
use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
and gives the correct hash of
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
Python version:
import hashlib, hmac
print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
which gives the incorrect hash of
9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182
477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a
any ideas why the Python version is giving me the wrong hash?
Edit:
version is
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
yes indeed -- it seems the Leopard version of python2.5 is the one that is broken.
below run on a Penryn-based MBP...
$ **uname -a**
Darwin lizard-wifi 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386
dpc#lizard-wifi:~$ **which python**
/usr/bin/python
Running this version installed in Leopard OS
dpc#lizard-wifi:~$ python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a
>>>
And then the MacPorts version of python2.5
$ /opt/local/bin/python2.5
Python 2.5.4 (r254:67916, Feb 3 2009, 21:40:31)
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
>>>
I am unable to replicate your results here. In IDLE using Python 2.5:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
...
IDLE 1.2.2
>>> import hashlib, hmac
>>> print hmac.new("\x0b"*20, "Hi There", hashlib.sha512).hexdigest()
87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854
Which version of Python? Strings are Unicode in Python 3. Is this a Unicode issue?
Under python 2.5.2 I get the correct hash
I guess the old version was the problem

Categories

Resources