Dump Contents of Python Module loaded in memory - python

I ran the Python REPL tool and imported a Python Module. Can I can dump the contents of that Module into a file? Is this possible? Thanks in advance.

In what format do you want to write the file? If you want exactly the same format that got imported, that's not hard -- but basically it's done with a file-to-file copy. For example, if the module in question is called blah, you can do:
>>> import shutil
>>> shutil.copy(blah.__file__, '/tmp/blahblah.pyc')

Do you mean something like this?
http://www.datamech.com/devan/trypython/trypython.py
I don't think it is possible, as this is a very restricted environment.
The __file__ attribute is faked, so doesn't map to a real file

You might get a start by getting a reference to your module object:
modobject = __import__("modulename")
Unfortunately those aren't pickleable. You might be able to iterate over dir(modobject) and get some good info out catching errors along the way... or is a string representation of dir(modobject) itself what you want?

Related

Python - How to write a windows path to a json file?

I am working together with a colleague and he has Ubuntu while I have windows. We have a dataset of json files which have in them a "path" written. His paths look like this:
'C:/Users/krock/Desktop/FIIT/BP/Ubuntu/luadb/etc/luarocks_test/modules/30log/share/lua/5.3/30log.lua'
But this doesn't work on Windows, I was trying to do
some_string.replace('/', '\\')
But this results in strings written in json that look like this:
'C:\\Users\\krock\\Desktop\\FIIT\\BP\\Ubuntu\\luadb\\etc\\luarocks_test\\data_all'
On my windows machine, I can't read (the program) these paths as it give an error:
No such file or directory
Is there a solution to this?
EDIT: I tried using Path from pathlib, but I got another error saying:
TypeError: Object of type WindowsPath is not JSON serializable
I found the solution to this is to do str(Path(path_string)), but the result is again the path in double quotes.
Yes, the solution is to use Python's built in pathlib. Also, using string literals might help the clarity of your program.
https://docs.python.org/3/library/pathlib.html
This question is missing code samples, so can't be more specific, but generally speaking, doing this manually is error-prone. Consider using a library, such as pathlib. E.G:
>>> from pathlib import Path
>>> Path('luarocks_test/modules/30log/share/lua/5.3/30log.lua')
PosixPath('luarocks_test/modules/30log/share/lua/5.3/30log.lua')
On Windows, instantiating a Path would give you a WindowsPath. You'll also want to use relative, rather than absolute references, as the paths will be different on your workstations.

module 'pickle' has no attribute 'dump'

import pickle
imelda = ('More Mayhem',
'IMelda May',
'2011',
((1, 'Pulling the Rug'),
(2, 'Psycho'),
(3, 'Mayhem'),
(4, 'Kentish Town Waltz')))
with open("imelda.pickle", "wb") as pickle_file:
pickle.dump(imelda, pickle_file)
I am trying to execute this code, but the console keeps telling me:
module 'pickle' has no attribute 'dump'
Do I have to install pickle via pip? I am not sure what is happening here.
Happened to me too. I had a file called pickle.py in my current directory. Just rename it and it's fixed :)
You might have used your Python file name as pickle.py. Python interpreter is confused and looking for dump function in you file pickle.py instead of the package you have imported. Change the name of your file to something else, it will work.
just check your file name.
if it is pickle.py, then I recommend you to change your file name. instead rename as pickle1.py
it will work.
I don't think its a file name problem. You wrote dumps as dump. Try it with dumps.
If you have file name is pickle.py and any files also saved for this name like pickle.py then remove it first. Then after,save file with other name like pikkle.py instead of pickle.py. your code will be execute.

How to separate filename from path in python (PyQt4.QtCore.QString)

How to separate filename from path using Python?
I'm using PyQt4 and my String is not Python String but, PyQt4.QtCore.QString
I can do it like:
filename=my_path.split("/")[-1]
But I think separator is OS specific, also I can't use something like os.path.basename because it only work for original python string, so what will be the best option to do it?
You can convert the QString to a Python str before use. For example:
filename_str = unicode(my_path)
...and then use standard Python os functions to get the filename:
os.path.basename(filename_str)
Or, in a single step:
os.path.basename(unicode(my_path))
Note you can avoid this problem altogether by using the newer PyQt4 API v2, or alternatively using PyQt5. With these updates PyQt functions return native Python strings (and other variables) where possible so you can work with them without converting. It makes things a lot simpler.

Check if GZIP file exists in Python

I would like to check for the existence of a .gz file on my linux machine while running Python. If I do this for a text file, the following code works:
import os.path
os.path.isfile('bob.asc')
However, if bob.asc is in gzip format (bob.asc.gz), Python does not recognize it. Ideally, I would like to use os.path.isfile or something very concise (without writing new functions). Is it possible to make the file recognizable either in Python or by changing something in my system configuration?
Unfortunately I can't change the data format or the file names as they are being given to me in batch by a corporation.
After fooling around for a bit, the most concise way I could get the job done was
subprocess.call(['ls','bob.asc.gz']) == 0
which returns True if the file exists in the directory. This is the behavior I would expect from
os.path.isfile('bob.asc.gz')
but for some reason Python won't accept files with extension .gz as files when passed to os.path.isfile.
I don't feel like my solution is very elegant, but it is concise. If someone has a more elegant solution, I'd love to see it. Thanks.
Of course it doesn't; they are completely different files. You need to test it separately:
os.path.isfile('bob.asc.gz')
This would return True if that exact file was present in the current working directory.
Although a workaround could be:
from os import listdir, getcwd
from os.path import splitext, basename
any(splitext(basename(f))[0] == 'bob.asc' for f in listdir(getcwd()))
You need to test each file. For example :
if any(map(os.path.isfile, ['bob.asc', 'bob.asc.gz'])):
print 'yay'

How to read/write a Fortran namelist with Python?

I would like to know how to easily read and write values from a Fortran namelist file in Python.
There is a module called f90nml which reads/writes Fortran namelists. With this module you can read a namelist into a nested Python dictionary:
import f90nml
nml = f90nml.read('sample.nml')
The values can be edited and written back to disk.
nml['config_nml']['steps'] = 432
nml.write('new_sample.nml')
The package can be installed with pip:
pip install f90nml
Source code is at
https://github.com/marshallward/f90nml
I wrote a python module to read/write Fortran namelist files because I couldn't find anything that quite worked for me: https://github.com/leifdenby/namelist_python
It:
Parses ints, floats, booleans, escaped strings and complex numbers.
Parses arrays in both index notation and inlined.
Can write namelist format files.
Has tab-completion and variable assignment in interactive console
I've written quite a few tests too, if there are any namelist files that don't parse correctly let me know and I'll have a look.

Categories

Resources