Can I make a dictionary available to multiple python scripts? - python

I have a dictionary of addresses with their usernames and passwords listed that looks something like this:
address_dict = {'address1':{'username':'abc', 'password':'123'}, 'address2':{'username':'xyz', 'password':'456'}}
Is there a way to make this dictionary accessible for multiple scripts to read from and possibly write to? Like save it as seperate python file and import it or something?

Yes, you can do just that:
# module.py
address_dict = {'address1':{'username':'abc', 'password':'123'}, 'address2':{'username':'xyz', 'password':'456'}}
# main.py
import module
print(module.address_dict)
If you don't like the module. prefix, you could import the dictionary like so:
from module import address_dict
print(address_dict)

To access it and modify it at runtime, you can just define it in a module and then import it. But if you want your changes to be persistent (i.e. see the changed version next time you run the script) you need something else, like a database.
The simplest to use in this case would probably be the shelve module, which is based on pickle. You can also use pickle itself if you wish.

take a look at pickle :)
http://docs.python.org/2/library/pickle.html
You can use it to dump objects to files and also to read them back in with any other python script.

Related

bazel logging inside of a BUILD file

Hi I want to see what is being passed to a rule that is defined in a rules.bzl file. So I would like to add just a logging statement inside of either the BUILD file or the rules.bzl file just to see what parameter was passed. I realize there is a whole --execution_log_binary_file and there is a good deal of stuff to read up on, but I am definitely looking to do something just once and quick and dirty. Do I import logging module? Is it something else? Thanks
Use the builtin Starlark print() function in BUILD or .bzl files to dump values Bazel's console.
bazel query --output=build //my/package:all will print out the definitions of all the targets created by macros called from my/package/BUILD by any .bzl file. This includes all the attributes passed to to various rules to create those targets.

How to extend or monkey-patch a built-in module

I want to write a custom json module for my team,
First I created a 'json.py' file under the folder(namespace) 'dqa_fileio/config'
so when others want to use my module, it should be called by import dqa_fileio.config.json
Then, I want to creates a read_file method to load a json file
But I got AttributeError: 'module' object has no attribute 'load'
I think that because the file_name is called json.py,
But I want to keep the filename, is there anyway like Ruby that I can open a class to extend it's ability ? But it seems the json is a folder(namespace) not a class ? Please correct me if any wrong concept. Thanks
json.py (under the namespace dqa_fileio/config)
import json
class Json(object):
def __init__(self):
pass
def read_file(self, file_name):
return json.load(open(file_name, 'r'))
if __name__ == '__main__':
Json().read_file(sys.argv[1])
That won't work because how do you think python is supposed to know which one to choose?
You have to pick a different name, as long as you want import json to import the correct module.
Look here for more information on the same type of question.
The only other thing I would suggest is to use a package.
Put your json.py module in some kind of package.
e.g:
common
|-__init__.py
|-json.py
Put common in your PYTHONPATH and/or package it up nicely with an appropriate setup.py which your team can install and use in their projects.
Then import it like:
from common import json
See: https://docs.python.org/2/tutorial/modules.html
The problem is that python thinks import json is just importing itself, and your class has no load method. The only way to get around this is to change your file name, or use some very unpythonic circular imports.

Importing XML to a dictionary, then accessing that dictionary from another module - Python

I am writing code that reads XML and creates a dictionary. I want to use that dictionary between modules, can I import that generated dictionary to another module?
I thought importing the module would be fine, however since the dictionary is only generated after running the module it is created in, this does not work. Is there any simple way to do this or do I need to write the dictionary to a file and read it again?
One method you could use is to include a return statement in the module that creates a dict. For instance,
def read_xml():
dict1 = create_dict_from_xml()
return dict1
you then could access that dictionary by writing in the other module dict1 = read_xml(). This will only work while the program is running. If you want to save the dict I would recommend using the pickle module. The documentation for that can be fount here. If I didn't answer your question let me know and possibly post some of your source code.
Hope this helped.

python, saving changes made to an imported module

I have a situation where users can pick another method from another clases and use it in thier own class using the .im_func. i give an example below
import foo1
import foo2
foo1.ClassX.methodX = foo2.ClassX.methodX.im_func
Where methodX could be implemented differently in both modules.
When i instantiate the object say foo1.Class(), methodX from modulefoo2` is used.
My problem is how to save the changes made maybe as foo3.py to a new source code file.
saving it as new py could be a problem but you can easily use serialization for it (pickle module)
see: http://docs.python.org/library/pickle.html
The source code can be retrieved with inspect module. However, the problem with that is, that it's the original source code, not source code of dynamically modified object.
Have you considered using parser combined with the aforementioned inspect to do this? In this situation it might be better to simply go with text processing rather than attempting to use imported modules.
EDIT: An example of using the parser to print the file:
with open('foo1.py','r') as fh:
st = parser.suite(fh.read())
src1 = parser.st2list(st)
with open('foo2.py','r') as fh:
st = parser.suite(fh.read())
src2 = parser.st2list(st)
You'd have to then do some tricky programming to merge the methods from the source code and write it to a file. But then again I have the strange feeling I'm not quite understanding the question...

load python code at runtime

I would like to load a .py file at runtime. This .py file is basically a config file with the following format:
var1=value
var2=value
predicate_function=func line : <return true or false>
Once this file is loaded, I would like to be able to access var1, var2 and predicate_function. For each line, I'll pass it to the predicate function, and if it returns false, I'll ignore it.
In any case, I'm not sure how to load a python file at runtime and access its variables.
Clarification: there may be any number of these config files that I need to pass to the main program and I won't know their names until runtime. Google tells me I should use __import__. I'm not sure how to correctly use that method and then access the variables of the imported file.
As written in the python official documentation, if you just want to import a module by name, you can look it up in the sys.modules dictionary after using __import__.
Supposing your configuration is in myproject.mymodule, you would do like that :
module_name = 'myproject.mymodule'
import sys
__import__(module_name)
mymodule = sys.modules[module_name]
# Then you can just access your variables and functions
print mymodule.var1
print mymodule.var2
# etc...
You can also use the return value of __import__ statement but you will have to understand fully how python works with namespaces and scopes.
You just need to be able to dynamically specify the imports and then dynamically get at the variables.
Let's say your config file is bar.py and looks like this:
x = 3
y = 4
def f(x): return (x<4)
Then your code should look like this:
import sys
# somehow modnames should be a list of strings that are the names of config files
#
# you can do this more dynamically depending on what you're doing
modnames = ['bar']
for modname in modnames:
exec('import %s' % modname)
for modname in modnames:
mod = sys.modules[modname]
for k in mod.__dict__:
if k[:2] != '__':
print modname, k, mod.__dict__[k]
I get this output:
bar f <function f at 0x7f2354eb4cf8>
bar x 3
bar y 4
Then you at least have all the variables and functions. I didn't quite get what you wanted from the predicate functions, but maybe you can get that on your own now.
To access another Python module, you import it. execfile has been mentioned by a couple people, but it is messy and dangerous. execfile clutters your namespace, possibly even messing up the code you are running. When you want to access another Python source file, use the import statement.
Even better would be not to use a Python file for configuration at all, but rather to use the builtin module ConfigParser or a serialization format like JSON. This way your configuration files don't allow execution of arbitrary (possibly malicious) code, doesn't require people to know Python to configure your program, and can easily be altered programatically.
If the imported module is on the regular search path, you can use __import__.
If you need to load the module from an arbitrary path in the filesystem, use imp.load_module.
Be sure to consider the security implications of loading arbitrary user-specified code.
In Python 2.*, execfile works (I recommend passing a specific dictionary and accessing the variables from there -- as the note in the docs says, execfile can't affect the calling function's locals() dictionary).
In Python 3.*, execfile has been removed, so do, instead:
with open('thefile.py') as f:
exec(f.read(), somedict)
Since the Python version hasn't been clearly mentioned, it is worth pointing out that the imp module has been deprecated in newer Python versions in favor of the importlib module. Example here.
I'm kinda late to the party, but I want to present an alternative answer nonetheless.
If you want to import code without affecting the global module namespace, you can create an anonymous module (using types.ModuleType) and load arbitrary code in it (using compile and exec). For instance, like this:
import types
filename = "/path/to/your/file.py"
with open(filename) as fp:
code = compile(fp.read(), filename, "exec")
config_module = types.ModuleType("<config>")
exec code in config_module.__dict__
You can then access the variables as config_module.var1, &c.
If you want to have a configuration file that will only be edited by the user when the program isn't running, just import it as a normal python file
ie.
main.py:
import config
print config.var1
config.py:
var="var12"
var2 = 100.5
try the imp module : http://docs.python.org/library/imp.html

Categories