How can I test to see whether gevent monkey patching is active? - gevent

Is there any "official" way my library can test to see whether gevent monkey patching has been activated?

I don't know about an official way, but here's a way.
Notice os.fork changes when it gets monkey patched:
>>> import os
>>> os.fork
<built-in function fork>
>>> import gevent.monkey; gevent.monkey.patch_all()
>>> os.fork
<function fork at 0x100535140>
And notice what gevent.fork is:
>>> import gevent
>>> gevent.fork
<function fork at 0x100535140>
So, when monkey patching all (or just os):
>>> is_monkeypatched = gevent.fork == os.fork

See the docs.
from gevent import monkey
monkey.is_module_patched('socket')

The short answer is "no" (and, obviously, one would expect to see an API provided in the docs if there were).
The longer answer is "mostly", using monkey.saved. Checking the source you can see that effectively all the usual patching updates this dictionary with references to the original items, so they can be retrieved using monkey.get_original(). This isn't official if you believe officialness requires documentation, but it doesn't seem to be meant as an internal API despite it not being exported in monkey.__all__.
If by your question you mean simply "has any monkey-patching occurred?" then you could just test if monkey.saved: ....
If you mean "has module XXX been monkey-patched?" then you could test for the appropriate name, e.g. if "threading" in monkey.saved: ...
For reference, after doing a monkey.patch_all() here's what you'd see in there. (Note that patch_all() doesn't actually patch everything. It leaves out subprocess, sys, and Event by default, so I included them below.)
>>> import os, gevent
>>> os.name, gevent.__version__
('posix', '1.0.1')
>>> from gevent import monkey
>>> monkey.patch_all(subprocess=True, sys=True, Event=True)
>>> monkey.saved.keys()
['socket', 'thread', 'ssl', 'gevent.socket', 'subprocess', 'sys', 'threading', 'time', 'os', 'select']

Related

How to import TensorMetric in pytorch lighting?

I want to import TensorMetric :
from pytorch_lightning.metrics.metric import TensorMetric
The program throws an exception:
ModuleNotFoundError: No module named 'pytorch_lightning.metrics'
My environment is:
Python 3.8.13
tokenizers==0.9.2
torch==1.5.1
transformers==3.4.0
pytorch-lightning==0.9.0
tensorboard==2.2.0
0.9 is a very old version of Lightning and a lot has changed/improved since then. I would recommend you to use the latest version of PyTorch Lightning (1.6).
Also, we have metrics as a separate library now TorchMetrics.

GnuPG home directory

The following is the code that I have been trying.
import os
import gnupg
import pdb
pdb.set_trace()
gpg = gnupg.GPG(gnupghome='new')
input_data = gpg.gen_key_input(
key_type="RSA",key_length=1024,
passphrase='mounika')
key = gpg.gen_key(input_data)
with open(local.txt,'rb')as f:
status=gpg.encrypt_file(f)
And the following is the error message being generated.
C:\Python27\python.exe C:/SAAS/encrypt.py
Traceback (most recent call last):
File "C:/SAAS/encrypt.py", line 4, in <module>
gpg = gnupg.GPG(gnupghome='new')
File "C:\Python27\lib\site-packages\gnupg.py", line 755, in __init__
raise OSError(msg)
OSError: Unable to run gpg - it may not be available.
Process finished with exit code 1
I am fairly new to GnuPG and after doing a bit of research I tried replacing gnupghome with homedir. But this is raising another error that homedir is an unexpected keyword.Can soneone pls help me with this issue. Any help would be appreciated.
You need to install the gpg program and make sure it is in your PATH. Or provide the full path to the gpg binary in your constructor, like
gpg = gnupg.GPG(gnupghome='new', gpgbinary='C:\\path\\to\\GnuPG\\pub\\gpg.exe')
Check also the Deployment Requirements for the python-gnupg package for more info.
As i encountered, the main reason is your local machine or server doesn't have gnupg package, so you need to install it with apt or whatever your package manager is. That could solve the problem.
in the latest version(2.2.0) of python-gnupg(imported as gnupg),
gnupghome=homedir(where the keyring etc is stored).
A few other things:
Binaries(in windows, that's the exe file) are defined as 'binary'.
best to specify a fingerprint, perhaps your local.txt should be a string('local.txt'),
and I think you're using the saltycrane blog post which is a little outdated at the moment.
So the below should work(OP code refactored):
import os
import gnupg
import pdb
pdb.set_trace()
gpg = gnupg.GPG(homedir='new',
binary="C:/Progra~2/GNU/GnuPG/pub/gpg2.exe")
input_data = gpg.gen_key_input(
key_type="RSA",key_length=1024,
passphrase='mounika')
key = gpg.gen_key(input_data)
with open(local.txt,'rb')as f:
status=gpg.encrypt(f, key.fingerprint)
print status.ok
print status.status
print status.stderr
I reckon your code is just failing silently.

Haskell - System.Process callCommand and other functions are not available

While reading the docs of System.Process and trying to use callCommand I discovered that it is not available:
test.hs:1:24: Module `System.Process' does not export `callCommand'
Why?
callProcess was added to the process library in version 1.2.0.0. I suspect you are using an earlier version.

Severe problems on getting to work jedi vim (windows)

i have already read a lot of threads with similar topics and the tag jedi-vim here on stackoverflow and also other pages.
But this problem still keeps me from doing my actual work :-/.
Up to now I have already tried several steps back and forth, but basically I did the following:
1. Install latest Python3.4 (32bit)
2. Got this precompiled VIM: (https:// bitbucket.org/Haroogan/vim-for-windows/downloads/vim-7.4.417-python-2.7-python-3.4-ruby-2.0.0-lua-5.2-perl-5.18-windows-x86.zip) (32Bit)
3. Installed Pathogen
4. Installed Flake8 successfully
5. Installed Jedi (systemwide?) with pip
6. Got Jedi-vim from github.com/davidhalter/jedi-vim and unpacked the dir jedi-vim-master to:
6.1. $HOME/vimfiles/bundle/ only
6.2. $VIM/vimfiles/bundle/ only
6.3. BOTH
python and python3 both work (before I ONLY had python3).
So
":python3 import sys; print(sys.version)" and
":python2 import sys; print(sys.version)" give an output.
In my vimrc I force jedi-vim to use python3:
"let g:jedi#force_py_version = 3"
And now the actual error is the following:
When I type e.g. "self." in a *.py-file he searches for half a sec and says:
E117: unknown function: pythoncomplete#Complete
-- Omni completion (^O^N^P) Pattern not found
When I want to call a function and want type the parameters, something pops up / gets faded in, but it's some unclear signs and not the actual parameters.
see here
Other info:
:scriptnames :
D:\tools\v\plugin\getscriptPlugin.vim
D:\tools\v\plugin\gzip.vim
D:\tools\v\plugin\matchparen.vim
D:\tools\v\plugin\netrwPlugin.vim
D:\tools\v\plugin\rrhelper.vim
D:\tools\v\plugin\spellfile.vim
D:\tools\v\plugin\tarPlugin.vim
D:\tools\v\plugin\tohtml.vim
D:\tools\v\plugin\vimballPlugin.vim
D:\tools\v\plugin\zipPlugin.vim
D:\tools\v\menu.vim
D:\tools\v\lang\menu_de.latin1.vim
D:\tools\v\lang\menu_de_de.latin1.vim
D:\tools\v\autoload\paste.vim
D:\tools\v_gvimrc
D:\tools\v\syntax\syntax.vim
D:\tools\v\syntax\synload.vim
D:\tools\v\syntax\syncolor.vim
D:\tools\v\filetype.vim
H:\vimfiles\autoload\pathogen.vim
D:\tools\v\ftoff.vim
D:\tools\v\syntax\nosyntax.vim
D:\tools\v\ftplugin.vim
D:\tools\v\indent.vim
D:\tools\v\syntax\python.vim
D:\tools\v\ftplugin\python.vim
D:\tools\v\bundle\jedi-vim-master\ftplugin\python\jedi.vim
D:\tools\v\bundle\jedi-vim-master\autoload\jedi.vim
D:\tools\v\bundle\vim-flake8-master\ftplugin\python_flake8.vim
H:\vimfiles\bundle\vim-flake8-master\ftplugin\python_flake8.vim
D:\tools\v\indent\python.vim
My vimrc:
https://drive.google.com/file/d/0Bwb4EPY-atMdOWhnOGJiNmxYbGM/view?usp=sharing
Your problem is that the jedi-vim completion function is never called.
The error message is about pythoncomplete#Complete, which is VIM's default, but it should be set to :set omnifunc=jedi#completions.
I feel like the last line in scriptnames (D:\tools\v\indent\python.vim) overwrites omnifunc. Don't know why and how it happens, but that's my gut feeling.

Unable to build a simple application sending a HTTP request

I want to perform a HTTP request using the simplest way. I decided to use conduits. Here is my main file:
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit -- the main module
-- The streaming interface uses conduits
import Data.Conduit
import Data.Conduit.Binary (sinkFile)
import qualified Data.ByteString.Lazy as L
import Control.Monad.IO.Class (liftIO)
main :: IO ()
main = do
simpleHttp "http://www.example.com/foo.txt" >>= L.writeFile "foo.txt"
And .cabal:
executable AAA
main-is: Main.hs
hs-source-dirs: src
build-depends: base ==4.6.*, text ==0.11.*, http-conduit, transformers, bytestring
I can't build it, the error is:
$ cabal build
Building AAA-0.1.0.0...
Preprocessing executable 'AAA' for
AAA-0.1.0.0...
src/Main.hs:6:8:
Could not find module `Data.Conduit.Binary'
Perhaps you meant
Data.Conduit.List (needs flag -package conduit-1.1.4)
Data.Conduit.Lift (needs flag -package conduit-1.1.4)
Use -v to see a list of the files searched for.
I've already installed all the libraries shown in the .cabal file by saying cabal install xxx.
What's up with this?
Update:
Couldn't match type `L.ByteString'
with `bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString'
Expected type: bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString
-> IO ()
Actual type: L.ByteString -> IO ()
In the return type of a call of `L.writeFile'
In the second argument of `(>>=)', namely `L.writeFile "foo.txt"'
In a stmt of a 'do' block:
simpleHttp "http://www.example.com/foo.txt"
>>= L.writeFile "foo.txt"
So the problem is that your program imports Data.Conduit.Binary which isn't installed. It lives in the conduit-extra package, so you have to add it to your dependencies and install it if you want to use it.
Your main function doesn't actually use it though, so you can just remove the import and it should fix the current error. You will however get a new error when attempting to build since you also import Data.Conduit which isn't listed in your cabal file either. To fix this error remove the import or add conduit to your build-depends.
Seems like now you have two (at least) versions of bytestring installed, and different packages don't agree on which one has to be used.
I suggest reinstalling http-conduit.

Resources