what is .random. in this statement? - python

numpy.random.randn(100)
I understand numpy is the name of the imported module and randn is a method defined within the module but not sure what .random. is
Thanks and happy new year!

#Yann's answer is definetly correct but might not make the whole picture clear.
The best analogy for package structure are probably folders. Imagine the whole numpy package as big folder. In said folder are a bunch of files, these are our functions. But you also have subfolder. random is one of these subfolders. It contains more files (functions) who are grouped together because they have to do with the same thing, namely randomness.
numpy
├── arccos
├── vectorize
├── random
│ ├── randn
│ ├── <more functions in the random subfolder>
│ <more functions in the numpy folder>

The .random part is a module within numpy,how you can confirm this is to use the python interpreter
#first import numpy into the interpreter
import numpy
#this is so the interpreter displays the info about the random module
numpy.random
Output should be something like "<module 'numpy.random' from 'path to module'>

Related

understanding hierarchical python modules & packages

I am trying to work with python packages and modules for the first time and come across some import errors I don't understand.
My project has the following structure:
upper
├── __init__.py
├── upper_file.py # contains "from middle.middle_file import *"
└── middle
├── __init__.py
├── middle_file.py # contains "from lower.lower_file import Person, Animal"
└── lower
├── __init__.py
└── lower_file.py # contains the Classes Person and Animal
I can run middle_file.py and can create inside the file a Person() and Animal() without any problems.
If I try to run upper_file.py I get a ModuleNotFoundError: No module named 'lower' error.
However, I have no trouble importing Animal() or Person() in upper_file.py directly with from middle.lower.lower_file import *
If I change the import statement inside middle_file.py from from lower.lower_file import Person, Animal to from middle.lower.lower_file import Person, Animal I can successfully run upper_file.py but not middle_file.py itself (and pycharm underlines the import in middle_file.py red and says it doesn't know middle)
In the end, I need to access inside of upper_file.py a class that is located inside of middle_file.py, but middle_file.py itself depends on the imports of lower_file.py.
I already read through this answer and the docs but just don't get how it works and why it behaves the way it does.
Thanks for any help in advance.
You should use relative import to accomplish this. First link on Google I found some practical example that could help you understand better.
On middle_file try to use from .lower.lower_file import *. It should solve the issue on upper_file.

How to build package like pandas/numpy where pd/np is an object with all the functions

As per title, I am trying to build a python package myself, I am already familiar with writing python packages reading notes from https://packaging.python.org/en/latest/tutorials/packaging-projects/ and https://docs.python.org/3/tutorial/modules.html#packages. These gave me an idea of how to write a bunch of object class/functions where I can import them.
What I want is to write a package like pandas and numpy, where I run import and they work as an "object", that is to say most/all the function is a dotted after the package.
E.g. after importing
import pandas as pd
import numpy as np
The pd and np would have all the functions and can be called with pd.read_csv() or np.arange(), and running dir(pd) and dir(np) would give me all the various functions available from them. I tried looking at the pandas src code to try an replicate their functionality. However, I could not do it. Maybe there is some parts of that I am missing or misunderstanding. Any help or point in the right direction to help me do this would be much appreciated.
In a more general example, I want to write a package and import it to have the functionalities dotted after it. E.g. import pypack and I can call pypack.FUNCTION() instead of having to import that function as such from pypack.module import FUNCTION and call FUNCTION() or instead of importing it as just a submodule.
I hope my question makes sense as I have no formal training in write software.
Let's assume you have a module (package) called my_library.
.
├── main.py
└── my_library/
└── __init__.py
/my_library/__init__.py
def foo(x):
return x
In your main.py you can import my_library
import my_library
print(my_library.foo("Hello World"))
The directory with __init__.py will be your package and can be imported.
Now consider a even deeper example.
.
├── main.py
└── my_library/
├── __init__.py
└── inner_module.py
inner_module.py
def bar(x):
return x
In your /my_library/__init__.py you can add
from .inner_module import bar
def foo(x):
return x
You can use bar() in your main as follows
import my_library
print(my_library.foo("Hello World"))
print(my_library.bar("Hello World"))

Using Resources Module to Import Data Files

Background
I have a function called get_player_call_logic_df which basically reads a csv file from the PLAYER_TEST_INPUT path. I have a module called player_test and inside that i have another folder called player_test_input where i store all the csv files that are used for processing.
Code
PLAYER_TEST_INPUT_DIR = Path("../dev/playerassignment/src/player/player/player_test/player_test_input/")
def get_player_call_logic_df() -> pd.DataFrame:
df = pd.read_csv(
PLAYER_TEST_INPUT_DIR / "player_call_logic.csv"
)
return df
Issue
I created a PR and I got a very good suggestion that I look at the importlib.resources module. You can store the data as a "resource" in the library. Then, instead of referring to the data via filepath, you can import it similar to any other player module.
I am unsure how i would use resources module here. I read up on the doc and here is what i could come up with. I can probably do something like this.
from importlib import resources
def get_player_call_logic_df() -> pd.DataFrame:
with resources.path("player.player_test_input", "player_call_logic.csv") as df:
return df
I feel like i am doing the same thing so i am not sure how to use the resources module correctly. Any help would be appreciated as i am new to python.
Please use
from importlib import resources
import pandas as pd
def get_player_call_logic_df() -> pd.DataFrame::
with resources.path("player.player_test_input", "player_call_logic.csv") as df:
return pd.read_csv(df)
and bear in mind the __init__.py file inside the player_test_input folder:
.
└── player
├── __init__.py
└── player_test_input
├── __init__.py
└── player_call_logic.csv
Very good reference and alternatives can be found here and here

Python import dependencies

I am dynamically creating some python code that I want to run inside a wrapper. Here is an overly simplified example.
[wrapper.py]
import cv2
img = cv2.imread('pic.png',0)
__import__("fragment")
cv2.imshow('pic',img)
[fragment.py]
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
I want the wrapper to set up any imports and variables, then import the fragment which will do stuff (i.e. make the image grayscale) and then do some standardized stuff afterwards (i.e. display image).
The fragments will be changing (genetic algorithm) so I would prefer to keep them separate from the setup which will be constant and will just get make manipulating the fragments more complicated.
When I run the program I get dependency errors on the fragment because cv2 and img are not defined (scope errors). Is there a way to achieve this either with a correction to the method I have used above or with another method?
I expect I might be able to also create the composite of the files in ram and then exec it or write over the fragment with a version of itself that contains all of the needed wrapping, but I wanted to see if there was something cleaner first.
Sincerely, Paul.
The fragments will be changing (genetic algorithm) so I would prefer
to keep them separate from the setup which will be constant and will
just get make manipulating the fragments more complicated.
Whatever the complexity of the genetic algorithms you implemented in fragment.py is, I do not see how importing cv2 (and eventually more modules) will impact it in a way or an other.
However, I agree with the first part of your statement in that you want to respect the principle of separation of concerns and make your code cleaner.
The solution I see for your problem is to set a configuration file config.py in which you set all your imports. But importing config.py into other files is useless unless you succeed to make modules such as cv2 available elsewhere once for all. You can achieve that by dynamically importing them within config.py file:
cv2=__import__('cv2')
in your main program, fragment.py file or whatever module, you can make use of cv2 by simply running this:
import config
config.cv2.imread('pic.png')
import config ↔ you do not need anymore to run: import cv2. This is because this trick renders cv2 as a global variable available across multiple modules.
The same idea is valid for your other variables such as img that you need to declare in your config.py file too.
Given these facts, here is my solution for your problem. Note that I am not using classes and functions: I prefer to address your problem straightforwardly and keep things too simple and clear instead.
Organization of the code:
The config.py file corresponds to your wrapper.py:
solution/
├── application.py
├── cfg
│   ├── config.py
│   └── __init__.pyc
├── gallery
│   └── pic.png
└── genalgos
├── fragment.py
└── __init__.py
config.py:
# This will make cv2 global and thus you won't need to import it in ./genalgos/fragment.py
# You can use the same idea for all your other imports
cv2=__import__('cv2')
imgc=cv2.imread('./gallery/pic.png') # imgc is global
fragment.py:
# The only import you can not avoid is this one
import cfg.config
# imgs is global
# By importing cfg.config you do not need to import cv2 here
imgf=cfg.config.cv2.cvtColor(cfg.config.imgc,cfg.config.cv2.COLOR_BGR2GRAY)
application.py:
import cfg.config
import genalgos.fragment
if __name__=="__main__":
"""
Display the image 'imgc' as it is in 'cfg/config' file
"""
cfg.config.cv2.imshow('Pic in BGR',cfg.config.imgc)
cfg.config.cv2.waitKey(0)
cfg.config.cv2.destroyAllWindows()
"""
Display the grascaled image 'imgf' as it is in 'genalgos/fragment' file which
itself is obtained after transforming imgc of 'cfg/config' file.
"""
cfg.config.cv2.imshow('PIC Grayscaled',genalgos.fragment.imgf)
cfg.config.cv2.waitKey(0) # Press any key to exit
cfg.config.cv2.destroyAllWindows() # Unpaint windows and leave

What is the most pythonic way to import 'sibling' modules into one another?

By 'sibling' modules, I mean two submodules that exist at the same depth within a parent module.
I'm trying to create a flask project using Flask-Restful, and it recommends structuring the project using this schema:
myapi/
__init__.py
app.py # this file contains your app and routes
resources/
__init__.py
foo.py # contains logic for /Foo
bar.py # contains logic for /Bar
common/
__init__.py
util.py # just some common infrastructure
I really like this structure, but I'm not sure how to import something from the 'common' module into the 'resources' module. Can anyone help me out?
In common/__init__.py
from myapi.common.utils import A, B
In resource/foo.py
from myapi.common import A
You can also relative imports in __init__.py like from .utils import A.

Categories

Resources